pub async fn call_and_read_body<S, B>(app: &S, req: Request) -> Byteswhere
    S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
    B: MessageBody,
Expand description

Helper function that returns a response body of a TestRequest

Examples

use actix_web::{test, web, App, HttpResponse, http::header};
use bytes::Bytes;

#[actix_web::test]
async fn test_index() {
    let app = test::init_service(
        App::new().service(
            web::resource("/index.html")
                .route(web::post().to(|| async {
                    HttpResponse::Ok().body("welcome!")
                })))
    ).await;

    let req = test::TestRequest::post()
        .uri("/index.html")
        .header(header::CONTENT_TYPE, "application/json")
        .to_request();

    let result = test::call_and_read_body(&app, req).await;
    assert_eq!(result, Bytes::from_static(b"welcome!"));
}

Panics

Panics if:

  • service call returns error;
  • body yields an error while it is being read.