Repository: actix/actix-web
File: src/test.rs (Rust)

Code before:
    ///
    /// ```rust
    /// use actix_web::{App, test};
    /// use serde::{Serialize, Deserialize};
    /// 
    /// #[derive(Serialize, Deserialize)]
    /// pub struct Person { id: String, name: String }
    /// 
    /// #[test]
    /// fn test_add_person() {
    ///     let mut app = test::init_service(App::new().service(
    ///                              web::resource("/people")
    ///                                .route(web::post().to(|person: Json<Person>| {
    ///                                       HttpResponse::Ok()
    ///                                          .json(person.into_inner())})
    ///                                 )));
    /// 
    ///     let payload = r#"{"id":"12345","name":"Nikolay Kim"}"#.as_bytes();
    /// 
    ///     let req = test::TestRequest::post()
    ///                      .uri("/people")
    ///                      .header(header::CONTENT_TYPE, "application/json")
    ///                      .set_payload(payload)
    ///                      .to_request();
    /// 
    ///     let result = test::read_response_json<_, _, _, Person>(&mut app, req);
    /// }
    /// ```
    pub fn read_response_json<S, R, B, T>(app: &mut S, req: R) -> T
    where
        B: MessageBody,
        S: Service<Request = R, Response = ServiceResponse<B>, Error = Error>,
        T: DeserializeOwned,
    {
        block_on(app.call(req).and_then(|mut resp: ServiceResponse<B>| {
            resp.take_body()
                .fold(BytesMut::new(), move |mut body, chunk| {
                    body.extend_from_slice(&chunk);
                    Ok::<_, Error>(body)
                })
                .and_then(|body: BytesMut| {
                    ok(serde_json::from_slice(&body).unwrap_or_else(|_| {
                        panic!("read_response_json failed during deserialization")
                    }))
                })
        }))
        .unwrap_or_else(|_| panic!("read_response_json failed at block_on unwrap"))
    }
}


Review comment (suggestion):
change this line to:

```rust
let result: Person = test::read_response_json(&mut app, req);
```

Code after:
    ///
    /// ```rust
    /// use actix_web::{App, test, web, HttpResponse, http::header};
    /// use serde::{Serialize, Deserialize};
    /// 
    /// #[derive(Serialize, Deserialize)]
    /// pub struct Person { id: String, name: String }
    /// 
    /// #[test]
    /// fn test_add_person() {
    ///     let mut app = test::init_service(App::new().service(
    ///                              web::resource("/people")
    ///                                .route(web::post().to(|person: web::Json<Person>| {
    ///                                       HttpResponse::Ok()
    ///                                          .json(person.into_inner())})
    ///                                 )));
    /// 
    ///     let payload = r#"{"id":"12345","name":"Nikolay Kim"}"#.as_bytes();
    /// 
    ///     let req = test::TestRequest::post()
    ///                      .uri("/people")
    ///                      .header(header::CONTENT_TYPE, "application/json")
    ///                      .set_payload(payload)
    ///                      .to_request();
    /// 
    ///     let result: Person = test::read_response_json(&mut app, req);
    /// }
    /// ```
    pub fn read_response_json<S, B, T>(app: &mut S, req: Request) -> T
    where
        S: Service<Request = Request, Response = ServiceResponse<B>, Error = Error>,
        B: MessageBody,
        T: DeserializeOwned,
    {
        block_on(app.call(req).and_then(|mut resp: ServiceResponse<B>| {
            resp.take_body()
                .fold(BytesMut::new(), move |mut body, chunk| {
                    body.extend_from_slice(&chunk);
                    Ok::<_, Error>(body)
                })
                .and_then(|body: BytesMut| {
                    ok(serde_json::from_slice(&body).unwrap_or_else(|_| {
                        panic!("read_response_json failed during deserialization")
                    }))
                })
        }))
        .unwrap_or_else(|_| panic!("read_response_json failed at block_on unwrap"))
    }
}