actix_web_lab/
test_services.rs

1use actix_utils::future::ok;
2use actix_web::{
3    Error, HttpResponseBuilder,
4    body::BoxBody,
5    dev::{Service, ServiceRequest, ServiceResponse, fn_service},
6    http::StatusCode,
7};
8
9/// Creates service that always responds with given status code and echoes request path as response
10/// body.
11pub fn echo_path_service(
12    status_code: StatusCode,
13) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
14    fn_service(move |req: ServiceRequest| {
15        let path = req.path().to_owned();
16        ok(req.into_response(HttpResponseBuilder::new(status_code).body(path)))
17    })
18}