mirror of https://github.com/fafhrd91/actix-web
use to_bytes in body reading impls
This commit is contained in:
parent
b168fcd606
commit
2463c95dce
30
src/test.rs
30
src/test.rs
|
@ -12,10 +12,9 @@ use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
use actix_utils::future::{ok, poll_fn};
|
use actix_utils::future::{ok, poll_fn};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
|
||||||
use crate::cookie::{Cookie, CookieJar};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app_service::AppInitServiceState,
|
app_service::AppInitServiceState,
|
||||||
|
body,
|
||||||
body::{BoxBody, MessageBody},
|
body::{BoxBody, MessageBody},
|
||||||
config::AppConfig,
|
config::AppConfig,
|
||||||
data::Data,
|
data::Data,
|
||||||
|
@ -27,6 +26,9 @@ use crate::{
|
||||||
Error, HttpRequest, HttpResponse, HttpResponseBuilder,
|
Error, HttpRequest, HttpResponse, HttpResponseBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "cookies")]
|
||||||
|
use crate::cookie::{Cookie, CookieJar};
|
||||||
|
|
||||||
/// Creates service that always responds with `200 OK` and no body.
|
/// Creates service that always responds with `200 OK` and no body.
|
||||||
pub fn ok_service(
|
pub fn ok_service(
|
||||||
) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
|
) -> impl Service<ServiceRequest, Response = ServiceResponse<BoxBody>, Error = Error> {
|
||||||
|
@ -165,18 +167,12 @@ where
|
||||||
.unwrap_or_else(|e| panic!("read_response failed at application call: {:?}", e));
|
.unwrap_or_else(|e| panic!("read_response failed at application call: {:?}", e));
|
||||||
|
|
||||||
let body = resp.into_body();
|
let body = resp.into_body();
|
||||||
let mut bytes = BytesMut::new();
|
read_body(body).await
|
||||||
|
|
||||||
actix_rt::pin!(body);
|
|
||||||
while let Some(item) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
|
|
||||||
bytes.extend_from_slice(&item.map_err(Into::into).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes.freeze()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function that returns a response body of a ServiceResponse.
|
/// Helper function that returns a response body of a ServiceResponse.
|
||||||
///
|
///
|
||||||
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::{test, web, App, HttpResponse, http::header};
|
/// use actix_web::{test, web, App, HttpResponse, http::header};
|
||||||
/// use bytes::Bytes;
|
/// use bytes::Bytes;
|
||||||
|
@ -201,20 +197,18 @@ where
|
||||||
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
|
/// assert_eq!(result, Bytes::from_static(b"welcome!"));
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
/// Panics if body errors 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 + Unpin,
|
||||||
B::Error: fmt::Debug,
|
B::Error: fmt::Debug,
|
||||||
{
|
{
|
||||||
let body = res.into_body();
|
let body = res.into_body();
|
||||||
let mut bytes = BytesMut::new();
|
body::to_bytes(body)
|
||||||
|
.await
|
||||||
actix_rt::pin!(body);
|
.expect("error reading test response body")
|
||||||
while let Some(item) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
|
|
||||||
bytes.extend_from_slice(&item.map_err(Into::into).unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
bytes.freeze()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function that returns a deserialized response body of a ServiceResponse.
|
/// Helper function that returns a deserialized response body of a ServiceResponse.
|
||||||
|
|
Loading…
Reference in New Issue