actix_web_lab/
test_services.rs

1use actix_utils::future::ok;
2use actix_web::{
3    body::BoxBody,
4    dev::{fn_service, Service, ServiceRequest, ServiceResponse},
5    http::StatusCode,
6    Error, HttpResponseBuilder,
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}