Skip to content

Commit ace79e1

Browse files
committed
Add tests for status range in logger, and relative changes entry
1 parent 07936b5 commit ace79e1

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

actix-web/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
### Added
6+
- Add `Logger::statuses` to filter the range of statuses logged.
7+
58
## 4.4.0
69

710
### Added

actix-web/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ tls-openssl = { package = "openssl", version = "0.10.55" }
123123
tls-rustls = { package = "rustls", version = "0.21" }
124124
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
125125
zstd = "0.12"
126+
capture-logger = "0.1"
126127

127128
[[test]]
128129
name = "test_server"

actix-web/src/middleware/logger.rs

+72-17
Original file line numberDiff line numberDiff line change
@@ -393,32 +393,33 @@ where
393393
debug!("Error in response: {:?}", error);
394394
}
395395

396-
let res = if this.status_range.contains(&res.status()) {
397-
if let Some(ref mut format) = this.format {
398-
// to avoid polluting all the Logger types with the body parameter we swap the body
399-
// out temporarily since it's not usable in custom response functions anyway
396+
let mut format = if this.status_range.contains(&res.status()) {
397+
this.format.take()
398+
} else {
399+
None
400+
};
400401

401-
let (req, res) = res.into_parts();
402-
let (res, body) = res.into_parts();
402+
let res = if let Some(ref mut format) = format {
403+
// to avoid polluting all the Logger types with the body parameter we swap the body
404+
// out temporarily since it's not usable in custom response functions anyway
403405

404-
let temp_res = ServiceResponse::new(req, res.map_into_boxed_body());
406+
let (req, res) = res.into_parts();
407+
let (res, body) = res.into_parts();
405408

406-
for unit in &mut format.0 {
407-
unit.render_response(&temp_res);
408-
}
409+
let temp_res = ServiceResponse::new(req, res.map_into_boxed_body());
409410

410-
// re-construct original service response
411-
let (req, res) = temp_res.into_parts();
412-
ServiceResponse::new(req, res.set_body(body))
413-
} else {
414-
res
411+
for unit in &mut format.0 {
412+
unit.render_response(&temp_res);
415413
}
414+
415+
// re-construct original service response
416+
let (req, res) = temp_res.into_parts();
417+
ServiceResponse::new(req, res.set_body(body))
416418
} else {
417419
res
418420
};
419421

420422
let time = *this.time;
421-
let format = this.format.take();
422423
let log_target = this.log_target.clone();
423424

424425
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
@@ -779,7 +780,13 @@ mod tests {
779780
header::HeaderValue::from_static("ACTIX-WEB"),
780781
))
781782
.to_srv_request();
782-
let _res = srv.call(req).await;
783+
capture_logger::begin_capture();
784+
// The log is executed on drop, so the result need to be dropped
785+
let _ = srv.call(req).await;
786+
let log = capture_logger::pop_captured().unwrap();
787+
assert!(log.message().contains("ttt"));
788+
assert!(log.message().contains("ACTIX-WEB"));
789+
capture_logger::end_capture();
783790
}
784791

785792
#[actix_rt::test]
@@ -805,6 +812,54 @@ mod tests {
805812
let _res = srv.call(req).await.unwrap();
806813
}
807814

815+
#[actix_rt::test]
816+
async fn test_logger_status_range_include() {
817+
let srv = |req: ServiceRequest| {
818+
ok(req.into_response(HttpResponse::build(StatusCode::OK).finish()))
819+
};
820+
let logger = Logger::new("%{User-Agent}i test_included %s").statuses(StatusCode::OK..);
821+
822+
let srv = logger.new_transform(srv.into_service()).await.unwrap();
823+
824+
let req = TestRequest::default()
825+
.insert_header((
826+
header::USER_AGENT,
827+
header::HeaderValue::from_static("ACTIX-WEB"),
828+
))
829+
.to_srv_request();
830+
capture_logger::begin_capture();
831+
// The log is executed on drop, so the result need to be dropped
832+
let _ = srv.call(req).await;
833+
let log = capture_logger::pop_captured().unwrap();
834+
assert!(log.message().contains("200"));
835+
assert!(log.message().contains("ACTIX-WEB"));
836+
capture_logger::end_capture();
837+
}
838+
839+
#[actix_rt::test]
840+
async fn test_logger_status_range_exclude() {
841+
let srv = |req: ServiceRequest| {
842+
ok(req.into_response(HttpResponse::build(StatusCode::OK).finish()))
843+
};
844+
let logger =
845+
Logger::new("%{User-Agent}i test_excluded %s").statuses(StatusCode::BAD_REQUEST..);
846+
847+
let srv = logger.new_transform(srv.into_service()).await.unwrap();
848+
849+
let req = TestRequest::default()
850+
.insert_header((
851+
header::USER_AGENT,
852+
header::HeaderValue::from_static("ACTIX-WEB"),
853+
))
854+
.to_srv_request();
855+
capture_logger::begin_capture();
856+
// The log is executed on drop, so the result need to be dropped
857+
let _ = srv.call(req).await;
858+
let log = capture_logger::pop_captured();
859+
assert!(log.is_none());
860+
capture_logger::end_capture();
861+
}
862+
808863
#[actix_rt::test]
809864
async fn test_escape_percent() {
810865
let mut format = Format::new("%%{r}a");

0 commit comments

Comments
 (0)