use to_bytes in body reading impls

This commit is contained in:
Rob Ede 2021-12-14 22:30:50 +00:00
parent b168fcd606
commit 2463c95dce
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
1 changed files with 12 additions and 18 deletions

View File

@ -12,10 +12,9 @@ use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
use actix_utils::future::{ok, poll_fn};
use serde::{de::DeserializeOwned, Serialize};
#[cfg(feature = "cookies")]
use crate::cookie::{Cookie, CookieJar};
use crate::{
app_service::AppInitServiceState,
body,
body::{BoxBody, MessageBody},
config::AppConfig,
data::Data,
@ -27,6 +26,9 @@ use crate::{
Error, HttpRequest, HttpResponse, HttpResponseBuilder,
};
#[cfg(feature = "cookies")]
use crate::cookie::{Cookie, CookieJar};
/// Creates service that always responds with `200 OK` and no body.
pub fn ok_service(
) -> 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));
let body = resp.into_body();
let mut bytes = BytesMut::new();
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()
read_body(body).await
}
/// Helper function that returns a response body of a ServiceResponse.
///
/// # Examples
/// ```
/// use actix_web::{test, web, App, HttpResponse, http::header};
/// use bytes::Bytes;
@ -201,20 +197,18 @@ where
/// 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
where
B: MessageBody + Unpin,
B::Error: fmt::Debug,
{
let body = res.into_body();
let mut bytes = BytesMut::new();
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()
body::to_bytes(body)
.await
.expect("error reading test response body")
}
/// Helper function that returns a deserialized response body of a ServiceResponse.