mirror of https://github.com/fafhrd91/actix-web
use to_bytes in tests
This commit is contained in:
parent
a3b0e5820f
commit
427f39bf70
|
@ -108,7 +108,7 @@ async fn test_h2_body() -> io::Result<()> {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|mut req: Request<_>| async move {
|
.h2(|mut req: Request<_>| async move {
|
||||||
let body = load_body(req.take_payload()).await?;
|
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())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
|
@ -245,7 +245,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
async fn test_h2_body2() {
|
async fn test_h2_body2() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -263,7 +263,7 @@ async fn test_h2_body2() {
|
||||||
async fn test_h2_head_empty() {
|
async fn test_h2_head_empty() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.finish(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -287,7 +287,7 @@ async fn test_h2_head_empty() {
|
||||||
async fn test_h2_head_binary() {
|
async fn test_h2_head_binary() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
@ -310,7 +310,7 @@ async fn test_h2_head_binary() {
|
||||||
async fn test_h2_head_binary2() {
|
async fn test_h2_head_binary2() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.openssl(tls_config())
|
.openssl(tls_config())
|
||||||
.map_err(|_| ())
|
.map_err(|_| ())
|
||||||
})
|
})
|
||||||
|
|
|
@ -123,7 +123,7 @@ async fn test_h2_body1() -> io::Result<()> {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|mut req: Request<_>| async move {
|
.h2(|mut req: Request<_>| async move {
|
||||||
let body = load_body(req.take_payload()).await?;
|
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())
|
.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() {
|
async fn test_h2_body2() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -274,7 +274,7 @@ async fn test_h2_body2() {
|
||||||
async fn test_h2_head_empty() {
|
async fn test_h2_head_empty() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.finish(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.finish(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -300,7 +300,7 @@ async fn test_h2_head_empty() {
|
||||||
async fn test_h2_head_binary() {
|
async fn test_h2_head_binary() {
|
||||||
let mut srv = test_server(move || {
|
let mut srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -325,7 +325,7 @@ async fn test_h2_head_binary() {
|
||||||
async fn test_h2_head_binary2() {
|
async fn test_h2_head_binary2() {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h2(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h2(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.rustls(tls_config())
|
.rustls(tls_config())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -454,7 +454,7 @@ const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
async fn test_h1_body() {
|
async fn test_h1_body() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -471,7 +471,7 @@ async fn test_h1_body() {
|
||||||
async fn test_h1_head_empty() {
|
async fn test_h1_head_empty() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -496,7 +496,7 @@ async fn test_h1_head_empty() {
|
||||||
async fn test_h1_head_binary() {
|
async fn test_h1_head_binary() {
|
||||||
let mut srv = test_server(|| {
|
let mut srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
@ -521,7 +521,7 @@ async fn test_h1_head_binary() {
|
||||||
async fn test_h1_head_binary2() {
|
async fn test_h1_head_binary2() {
|
||||||
let srv = test_server(|| {
|
let srv = test_server(|| {
|
||||||
HttpService::build()
|
HttpService::build()
|
||||||
.h1(|_| ok::<_, ()>(Response::build(StatusCode::OK).body(STR)))
|
.h1(|_| ok::<_, ()>(Response::ok().set_body(STR)))
|
||||||
.tcp()
|
.tcp()
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -423,26 +423,16 @@ impl Future for HttpResponseBuilder {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use bytes::{Bytes, BytesMut};
|
use actix_http::body;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::dev::{Body, MessageBody, ResponseBody};
|
use crate::{
|
||||||
use crate::http::header::{self, HeaderValue, CONTENT_TYPE};
|
dev::Body,
|
||||||
use crate::http::StatusCode;
|
http::{
|
||||||
|
header::{self, HeaderValue, CONTENT_TYPE},
|
||||||
// TODO: replace with body::to_bytes when merged
|
StatusCode,
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_basic_builder() {
|
fn test_basic_builder() {
|
||||||
|
@ -486,7 +476,7 @@ mod tests {
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
read_body(resp.take_body()).await.as_ref(),
|
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
||||||
br#"["v1","v2","v3"]"#
|
br#"["v1","v2","v3"]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -494,7 +484,7 @@ mod tests {
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
assert_eq!(ct, HeaderValue::from_static("application/json"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
read_body(resp.take_body()).await.as_ref(),
|
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
||||||
br#"["v1","v2","v3"]"#
|
br#"["v1","v2","v3"]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -505,7 +495,7 @@ mod tests {
|
||||||
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
let ct = resp.headers().get(CONTENT_TYPE).unwrap();
|
||||||
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
assert_eq!(ct, HeaderValue::from_static("text/json"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
read_body(resp.take_body()).await.as_ref(),
|
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
||||||
br#"["v1","v2","v3"]"#
|
br#"["v1","v2","v3"]"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -517,7 +507,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
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"}"#
|
br#"{"test-key":"test-value"}"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue