mirror of https://github.com/fafhrd91/actix-web
parent
54d33b791e
commit
ae79651e2a
|
@ -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)]
|
||||||
|
|
Loading…
Reference in New Issue