1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Utils methods to write tests.
use ServiceResponse;
use StatusCode;
use read_body;
use Bytes;
/// Check the response has the status passed, otherwise fail
/// with the response body printed out. If success
/// return the `Bytes` of the body, that can be later serialized
/// into a struct object with `serde_json::from_slice()`.
/// # Example
/// ```rust
/// #[cfg(test)]
/// mod tests {
/// use actix_contrib_rest::test::assert_status;
///
/// use actix_web::http::header::{Accept, ContentType};
/// use actix_web::http::StatusCode;
/// use actix_web::test::{call_service, TestRequest};
/// use serde_json::{json, serde_json};
///
/// #[actix_web::test]
/// async fn test_post_sale() {
/// let req = TestRequest::post()
/// .uri("/sales")
/// .insert_header(Accept::json())
/// .insert_header(ContentType::json())
/// .set_json(json!({ "customer_id": 1123, "prod_id": 9982, qty: 1 }))
/// .to_request();
/// let resp = call_service(&app, req).await;
///
/// // Check status, if isn't OK, the test will fail, printing out
/// // the response body in the form of "Response Body: ..."
/// let body = assert_status(resp, StatusCode::OK).await;
///
/// let sale: SalePayload = serde_json::from_slice(&body).unwrap();
/// assert_eq!(sale.customer_name, "John");
/// // ...
/// }
/// }
/// ```
pub async