From ae79651e2a56efdb837262f751732666a98ae48d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 14 Dec 2021 23:25:14 +0000 Subject: [PATCH] remove unpin bounds from test utils and document panics --- src/test/test_utils.rs | 60 ++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/test/test_utils.rs b/src/test/test_utils.rs index 51907c391..111a4dc61 100644 --- a/src/test/test_utils.rs +++ b/src/test/test_utils.rs @@ -34,6 +34,9 @@ use crate::{ /// assert_eq!(res.status(), StatusCode::OK); /// } /// ``` +/// +/// # Panics +/// Panics if service initialization returns an error. pub async fn init_service( app: R, ) -> impl Service, Error = E> @@ -82,12 +85,17 @@ where /// assert_eq!(res.status(), StatusCode::OK); /// } /// ``` +/// +/// # Panics +/// Panics if service call returns error. pub async fn call_service(app: &S, req: R) -> S::Response where S: Service, Error = E>, E: std::fmt::Debug, { - app.call(req).await.unwrap() + app.call(req) + .await + .expect("test service call returned error") } /// Helper function that returns a response body of a TestRequest @@ -115,17 +123,18 @@ where /// assert_eq!(result, Bytes::from_static(b"welcome!")); /// } /// ``` +/// +/// # Panics +/// Panics if: +/// - service call returns error; +/// - body yields an error while it is being read. pub async fn read_response(app: &S, req: Request) -> Bytes where S: Service, Error = Error>, - B: MessageBody + Unpin, + B: MessageBody, B::Error: fmt::Debug, { - let res = app - .call(req) - .await - .unwrap_or_else(|e| panic!("read_response failed at application call: {:?}", e)); - + let res = call_service(app, req).await; read_body(res).await } @@ -158,10 +167,10 @@ where /// ``` /// /// # Panics -/// Panics if body errors while it is being read. +/// Panics if body yields an error while it is being read. pub async fn read_body(res: ServiceResponse) -> Bytes where - B: MessageBody + Unpin, + B: MessageBody, B::Error: fmt::Debug, { let body = res.into_body(); @@ -207,18 +216,25 @@ where /// let result: Person = test::read_body_json(res).await; /// } /// ``` +/// +/// # Panics +/// Panics if: +/// - body yields an error while it is being read; +/// - received body is not a valid JSON representation of `T`. pub async fn read_body_json(res: ServiceResponse) -> T where - B: MessageBody + Unpin, + B: MessageBody, B::Error: fmt::Debug, T: DeserializeOwned, { let body = read_body(res).await; - serde_json::from_slice(&body).unwrap_or_else(|e| { + serde_json::from_slice(&body).unwrap_or_else(|err| { panic!( - "read_response_json failed during deserialization of body: {:?}, {}", - body, e + "could not deserialize body into a {}\nerr: {}\nbody: {:?}", + std::any::type_name::(), + err, + body, ) }) } @@ -257,21 +273,21 @@ where /// let result: Person = test::read_response_json(&mut app, req).await; /// } /// ``` +/// +/// # Panics +/// Panics if: +/// - service call returns an error body yields an error while it is being read; +/// - body yields an error while it is being read; +/// - received body is not a valid JSON representation of `T`. pub async fn read_response_json(app: &S, req: Request) -> T where S: Service, Error = Error>, - B: MessageBody + Unpin, + B: MessageBody, B::Error: fmt::Debug, T: DeserializeOwned, { - let body = read_response(app, req).await; - - serde_json::from_slice(&body).unwrap_or_else(|_| { - panic!( - "read_response_json failed during deserialization of body: {:?}", - body - ) - }) + let res = call_service(app, req).await; + read_body_json(res).await } #[cfg(test)]