mirror of
https://github.com/fafhrd91/actix-web
synced 2025-02-21 11:54:47 +01:00
Add tests for status range in logger, and relative changes entry
This commit is contained in:
parent
5d9e498cdf
commit
278cac34ae
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Add `Logger::statuses` to filter the range of statuses logged.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Updated `zstd` dependency to `0.13`.
|
- Updated `zstd` dependency to `0.13`.
|
||||||
|
@ -123,6 +123,7 @@ tls-openssl = { package = "openssl", version = "0.10.55" }
|
|||||||
tls-rustls = { package = "rustls", version = "0.21" }
|
tls-rustls = { package = "rustls", version = "0.21" }
|
||||||
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
|
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
|
||||||
zstd = "0.13"
|
zstd = "0.13"
|
||||||
|
capture-logger = "0.1"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "test_server"
|
name = "test_server"
|
||||||
|
@ -393,8 +393,13 @@ where
|
|||||||
debug!("Error in response: {:?}", error);
|
debug!("Error in response: {:?}", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = if this.status_range.contains(&res.status()) {
|
let mut format = if this.status_range.contains(&res.status()) {
|
||||||
if let Some(ref mut format) = this.format {
|
this.format.take()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = if let Some(ref mut format) = format {
|
||||||
// to avoid polluting all the Logger types with the body parameter we swap the body
|
// to avoid polluting all the Logger types with the body parameter we swap the body
|
||||||
// out temporarily since it's not usable in custom response functions anyway
|
// out temporarily since it's not usable in custom response functions anyway
|
||||||
|
|
||||||
@ -412,13 +417,9 @@ where
|
|||||||
ServiceResponse::new(req, res.set_body(body))
|
ServiceResponse::new(req, res.set_body(body))
|
||||||
} else {
|
} else {
|
||||||
res
|
res
|
||||||
}
|
|
||||||
} else {
|
|
||||||
res
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let time = *this.time;
|
let time = *this.time;
|
||||||
let format = this.format.take();
|
|
||||||
let log_target = this.log_target.clone();
|
let log_target = this.log_target.clone();
|
||||||
|
|
||||||
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
|
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
|
||||||
@ -779,7 +780,13 @@ mod tests {
|
|||||||
header::HeaderValue::from_static("ACTIX-WEB"),
|
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||||
))
|
))
|
||||||
.to_srv_request();
|
.to_srv_request();
|
||||||
let _res = srv.call(req).await;
|
capture_logger::begin_capture();
|
||||||
|
// The log is executed on drop, so the result need to be dropped
|
||||||
|
let _ = srv.call(req).await;
|
||||||
|
let log = capture_logger::pop_captured().unwrap();
|
||||||
|
assert!(log.message().contains("ttt"));
|
||||||
|
assert!(log.message().contains("ACTIX-WEB"));
|
||||||
|
capture_logger::end_capture();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
@ -805,6 +812,54 @@ mod tests {
|
|||||||
let _res = srv.call(req).await.unwrap();
|
let _res = srv.call(req).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_logger_status_range_include() {
|
||||||
|
let srv = |req: ServiceRequest| {
|
||||||
|
ok(req.into_response(HttpResponse::build(StatusCode::OK).finish()))
|
||||||
|
};
|
||||||
|
let logger = Logger::new("%{User-Agent}i test_included %s").statuses(StatusCode::OK..);
|
||||||
|
|
||||||
|
let srv = logger.new_transform(srv.into_service()).await.unwrap();
|
||||||
|
|
||||||
|
let req = TestRequest::default()
|
||||||
|
.insert_header((
|
||||||
|
header::USER_AGENT,
|
||||||
|
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||||
|
))
|
||||||
|
.to_srv_request();
|
||||||
|
capture_logger::begin_capture();
|
||||||
|
// The log is executed on drop, so the result need to be dropped
|
||||||
|
let _ = srv.call(req).await;
|
||||||
|
let log = capture_logger::pop_captured().unwrap();
|
||||||
|
assert!(log.message().contains("200"));
|
||||||
|
assert!(log.message().contains("ACTIX-WEB"));
|
||||||
|
capture_logger::end_capture();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_logger_status_range_exclude() {
|
||||||
|
let srv = |req: ServiceRequest| {
|
||||||
|
ok(req.into_response(HttpResponse::build(StatusCode::OK).finish()))
|
||||||
|
};
|
||||||
|
let logger =
|
||||||
|
Logger::new("%{User-Agent}i test_excluded %s").statuses(StatusCode::BAD_REQUEST..);
|
||||||
|
|
||||||
|
let srv = logger.new_transform(srv.into_service()).await.unwrap();
|
||||||
|
|
||||||
|
let req = TestRequest::default()
|
||||||
|
.insert_header((
|
||||||
|
header::USER_AGENT,
|
||||||
|
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||||
|
))
|
||||||
|
.to_srv_request();
|
||||||
|
capture_logger::begin_capture();
|
||||||
|
// The log is executed on drop, so the result need to be dropped
|
||||||
|
let _ = srv.call(req).await;
|
||||||
|
let log = capture_logger::pop_captured();
|
||||||
|
assert!(log.is_none());
|
||||||
|
capture_logger::end_capture();
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_escape_percent() {
|
async fn test_escape_percent() {
|
||||||
let mut format = Format::new("%%{r}a");
|
let mut format = Format::new("%%{r}a");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user