Function actix_test::read_response[][src]

pub async fn read_response<S, B>(
    app: &'_ S,
    req: Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> Bytes where
    S: Service<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>, Response = ServiceResponse<B>, Error = Error>,
    B: MessageBody + Unpin,
    <B as MessageBody>::Error: Into<Error>, 
Expand description

Helper function that returns a response body of a TestRequest

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::read_response(&app, req).await;
    assert_eq!(result, Bytes::from_static(b"welcome!"));
}