actix_web/test/
test_services.rs

1use actix_utils::future::ok;
2
3use crate::{
4    body::BoxBody,
5    dev::{fn_service, Service, ServiceRequest, ServiceResponse},
6    http::StatusCode,
7    Error, HttpResponseBuilder,
8};
9
10/// Creates service that always responds with `200 OK` and no body.
11pub fn ok_service(
12) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
13    status_service(StatusCode::OK)
14}
15
16/// Creates service that always responds with given status code and no body.
17pub fn status_service(
18    status_code: StatusCode,
19) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
20    fn_service(move |req: ServiceRequest| {
21        ok(req.into_response(HttpResponseBuilder::new(status_code).finish()))
22    })
23}
24
25#[doc(hidden)]
26#[deprecated(since = "4.0.0", note = "Renamed to `status_service`.")]
27pub fn simple_service(
28    status_code: StatusCode,
29) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
30    status_service(status_code)
31}
32
33#[doc(hidden)]
34#[deprecated(since = "4.0.0", note = "Renamed to `status_service`.")]
35pub fn default_service(
36    status_code: StatusCode,
37) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
38    status_service(status_code)
39}