remove unpin bounds from test utils

and document panics
This commit is contained in:
Rob Ede 2021-12-14 23:25:14 +00:00
parent 54d33b791e
commit ae79651e2a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
1 changed files with 38 additions and 22 deletions

View File

@ -34,6 +34,9 @@ use crate::{
/// assert_eq!(res.status(), StatusCode::OK); /// assert_eq!(res.status(), StatusCode::OK);
/// } /// }
/// ``` /// ```
///
/// # Panics
/// Panics if service initialization returns an error.
pub async fn init_service<R, S, B, E>( pub async fn init_service<R, S, B, E>(
app: R, app: R,
) -> impl Service<Request, Response = ServiceResponse<B>, Error = E> ) -> impl Service<Request, Response = ServiceResponse<B>, Error = E>
@ -82,12 +85,17 @@ where
/// assert_eq!(res.status(), StatusCode::OK); /// assert_eq!(res.status(), StatusCode::OK);
/// } /// }
/// ``` /// ```
///
/// # Panics
/// Panics if service call returns error.
pub async fn call_service<S, R, B, E>(app: &S, req: R) -> S::Response pub async fn call_service<S, R, B, E>(app: &S, req: R) -> S::Response
where where
S: Service<R, Response = ServiceResponse<B>, Error = E>, S: Service<R, Response = ServiceResponse<B>, Error = E>,
E: std::fmt::Debug, 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 /// Helper function that returns a response body of a TestRequest
@ -115,17 +123,18 @@ where
/// assert_eq!(result, Bytes::from_static(b"welcome!")); /// 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<S, B>(app: &S, req: Request) -> Bytes pub async fn read_response<S, B>(app: &S, req: Request) -> Bytes
where where
S: Service<Request, Response = ServiceResponse<B>, Error = Error>, S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody + Unpin, B: MessageBody,
B::Error: fmt::Debug, B::Error: fmt::Debug,
{ {
let res = app let res = call_service(app, req).await;
.call(req)
.await
.unwrap_or_else(|e| panic!("read_response failed at application call: {:?}", e));
read_body(res).await read_body(res).await
} }
@ -158,10 +167,10 @@ where
/// ``` /// ```
/// ///
/// # Panics /// # 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<B>(res: ServiceResponse<B>) -> Bytes pub async fn read_body<B>(res: ServiceResponse<B>) -> Bytes
where where
B: MessageBody + Unpin, B: MessageBody,
B::Error: fmt::Debug, B::Error: fmt::Debug,
{ {
let body = res.into_body(); let body = res.into_body();
@ -207,18 +216,25 @@ where
/// let result: Person = test::read_body_json(res).await; /// 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<T, B>(res: ServiceResponse<B>) -> T pub async fn read_body_json<T, B>(res: ServiceResponse<B>) -> T
where where
B: MessageBody + Unpin, B: MessageBody,
B::Error: fmt::Debug, B::Error: fmt::Debug,
T: DeserializeOwned, T: DeserializeOwned,
{ {
let body = read_body(res).await; 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!( panic!(
"read_response_json failed during deserialization of body: {:?}, {}", "could not deserialize body into a {}\nerr: {}\nbody: {:?}",
body, e std::any::type_name::<T>(),
err,
body,
) )
}) })
} }
@ -257,21 +273,21 @@ where
/// let result: Person = test::read_response_json(&mut app, req).await; /// 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<S, B, T>(app: &S, req: Request) -> T pub async fn read_response_json<S, B, T>(app: &S, req: Request) -> T
where where
S: Service<Request, Response = ServiceResponse<B>, Error = Error>, S: Service<Request, Response = ServiceResponse<B>, Error = Error>,
B: MessageBody + Unpin, B: MessageBody,
B::Error: fmt::Debug, B::Error: fmt::Debug,
T: DeserializeOwned, T: DeserializeOwned,
{ {
let body = read_response(app, req).await; let res = call_service(app, req).await;
read_body_json(res).await
serde_json::from_slice(&body).unwrap_or_else(|_| {
panic!(
"read_response_json failed during deserialization of body: {:?}",
body
)
})
} }
#[cfg(test)] #[cfg(test)]