use to_bytes in tests

This commit is contained in:
Rob Ede 2021-04-13 23:54:43 +01:00
parent a3b0e5820f
commit 427f39bf70
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 26 additions and 36 deletions

View File

@ -108,7 +108,7 @@ async fn test_h2_body() -> io::Result<()> {
HttpService::build()
.h2(|mut req: Request<_>| async move {
let body = load_body(req.take_payload()).await?;
Ok::<_, Error>(Response::build(StatusCode::OK).body(body))
Ok::<_, Error>(Response::ok().set_body(body))
})
.openssl(tls_config())
.map_err(|_| ())
@ -245,7 +245,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
async fn test_h2_body2() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.openssl(tls_config())
.map_err(|_| ())
})
@ -263,7 +263,7 @@ async fn test_h2_body2() {
async fn test_h2_head_empty() {
let mut srv = test_server(move || {
HttpService::build()
.finish(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.finish(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.openssl(tls_config())
.map_err(|_| ())
})
@ -287,7 +287,7 @@ async fn test_h2_head_empty() {
async fn test_h2_head_binary() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.openssl(tls_config())
.map_err(|_| ())
})
@ -310,7 +310,7 @@ async fn test_h2_head_binary() {
async fn test_h2_head_binary2() {
let srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.openssl(tls_config())
.map_err(|_| ())
})

View File

@ -123,7 +123,7 @@ async fn test_h2_body1() -> io::Result<()> {
HttpService::build()
.h2(|mut req: Request<_>| async move {
let body = load_body(req.take_payload()).await?;
Ok::<_, Error>(Response::build(StatusCode::OK).body(body))
Ok::<_, Error>(Response::ok().set_body(body))
})
.rustls(tls_config())
})
@ -257,7 +257,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
async fn test_h2_body2() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.rustls(tls_config())
})
.await;
@ -274,7 +274,7 @@ async fn test_h2_body2() {
async fn test_h2_head_empty() {
let mut srv = test_server(move || {
HttpService::build()
.finish(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.finish(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.rustls(tls_config())
})
.await;
@ -300,7 +300,7 @@ async fn test_h2_head_empty() {
async fn test_h2_head_binary() {
let mut srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.rustls(tls_config())
})
.await;
@ -325,7 +325,7 @@ async fn test_h2_head_binary() {
async fn test_h2_head_binary2() {
let srv = test_server(move || {
HttpService::build()
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.rustls(tls_config())
})
.await;

View File

@ -454,7 +454,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
async fn test_h1_body() {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.tcp()
})
.await;
@ -471,7 +471,7 @@ async fn test_h1_body() {
async fn test_h1_head_empty() {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.tcp()
})
.await;
@ -496,7 +496,7 @@ async fn test_h1_head_empty() {
async fn test_h1_head_binary() {
let mut srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.tcp()
})
.await;
@ -521,7 +521,7 @@ async fn test_h1_head_binary() {
async fn test_h1_head_binary2() {
let srv = test_server(|| {
HttpService::build()
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
.tcp()
})
.await;

View File

@ -423,26 +423,16 @@ impl Future for HttpResponseBuilder {
#[cfg(test)]
mod tests {
use bytes::{Bytes, BytesMut};
use actix_http::body;
use super::*;
use crate::dev::{Body, MessageBody, ResponseBody};
use crate::http::header::{self, HeaderValue, CONTENT_TYPE};
use crate::http::StatusCode;
// TODO: replace with body::to_bytes when merged
pub async fn read_body<B>(mut body: ResponseBody<B>) -> Bytes
where
B: MessageBody + Unpin,
{
use futures_util::StreamExt as _;
let mut bytes = BytesMut::new();
while let Some(item) = body.next().await {
bytes.extend_from_slice(&item.unwrap());
}
bytes.freeze()
}
use crate::{
dev::Body,
http::{
header::{self, HeaderValue, CONTENT_TYPE},
StatusCode,
},
};
#[test]
fn test_basic_builder() {
@ -486,7 +476,7 @@ mod tests {
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
assert_eq!(ct, HeaderValue::from_static("application/json"));
assert_eq!(
read_body(resp.take_body()).await.as_ref(),
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
br#"["v1","v2","v3"]"#
);
@ -494,7 +484,7 @@ mod tests {
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
assert_eq!(ct, HeaderValue::from_static("application/json"));
assert_eq!(
read_body(resp.take_body()).await.as_ref(),
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
br#"["v1","v2","v3"]"#
);
@ -505,7 +495,7 @@ mod tests {
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
assert_eq!(ct, HeaderValue::from_static("text/json"));
assert_eq!(
read_body(resp.take_body()).await.as_ref(),
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
br#"["v1","v2","v3"]"#
);
}
@ -517,7 +507,7 @@ mod tests {
);
assert_eq!(
read_body(resp.take_body()).await.as_ref(),
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
br#"{"test-key":"test-value"}"#
);
}