Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the body of a Response in actix_web unit test?

I'm building a web API service with Rust and actix_web.

I want to test a route and check if the received response body is what I expect. But I'm struggling with converting the received body ResponseBody<Body> into JSON or BSON. The called route actually returns application/json.

let mut app = test::init_service(App::new()
        .data(AppState { database: db.clone() })
        .route("/recipes/{id}", web::post().to(add_one_recipe))
    ).await;

let payload = create_one_recipe().as_document().unwrap().clone();

let req = test::TestRequest::post()
    .set_json(&payload).uri("/recipes/new").to_request();

let mut resp = test::call_service(&mut app, req).await;
let body: ResponseBody<Body> = resp.take_body(); // Here I want the body as Binary, String, JSON, or BSON. The response is actually application/json.
like image 966
zel873ju Avatar asked Sep 15 '20 22:09

zel873ju


1 Answers

The actix/examples repository achieves this by defining a new trait called BodyTest...

Actix Web 3

trait BodyTest {
    fn as_str(&self) -> &str;
}

impl BodyTest for ResponseBody<Body> {
    fn as_str(&self) -> &str {
        match self {
            ResponseBody::Body(ref b) => match b {
                Body::Bytes(ref by) => std::str::from_utf8(&by).unwrap(),
                _ => panic!(),
            },
            ResponseBody::Other(ref b) => match b {
                Body::Bytes(ref by) => std::str::from_utf8(&by).unwrap(),
                _ => panic!(),
            },
        }
    }
}

After which you may simply do:

assert_eq!(resp.response().body().as_str(), "Your name is John");

Actix Web 4

This trait is now much simpler (you could skip entirely):

trait BodyTest {
    fn as_str(&self) -> &str;
}

impl BodyTest for Bytes {
    fn as_str(&self) -> &str {
        std::str::from_utf8(self).unwrap()
    }
}

And to use it:

let body = to_bytes(resp.into_body()).await.unwrap();
assert_eq!(body.as_str(), "Your name is John");

Reference to full code these excerpts were taken from: https://github.com/actix/examples/blob/master/forms/form/src/main.rs

like image 113
SchnellAdvice Avatar answered Sep 20 '22 23:09

SchnellAdvice