mirror of https://github.com/fafhrd91/actix-web
fix tests
This commit is contained in:
parent
b3709d1cb1
commit
310506b257
|
@ -87,6 +87,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResponseBody<Body> {
|
impl ResponseBody<Body> {
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) fn get_ref(&self) -> &[u8] {
|
pub(crate) fn get_ref(&self) -> &[u8] {
|
||||||
match *self {
|
match *self {
|
||||||
ResponseBody::Body(ref b) => b.get_ref(),
|
ResponseBody::Body(ref b) => b.get_ref(),
|
||||||
|
|
|
@ -630,8 +630,7 @@ mod tests {
|
||||||
async fn test_no_content_length() {
|
async fn test_no_content_length() {
|
||||||
let mut bytes = BytesMut::with_capacity(2048);
|
let mut bytes = BytesMut::with_capacity(2048);
|
||||||
|
|
||||||
let mut res: Response<()> =
|
let mut res = Response::new(StatusCode::SWITCHING_PROTOCOLS).drop_body();
|
||||||
Response::new(StatusCode::SWITCHING_PROTOCOLS).into_body::<()>();
|
|
||||||
res.headers_mut().insert(DATE, HeaderValue::from_static(""));
|
res.headers_mut().insert(DATE, HeaderValue::from_static(""));
|
||||||
res.headers_mut()
|
res.headers_mut()
|
||||||
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
||||||
|
|
|
@ -264,7 +264,7 @@ pub(crate) mod tests {
|
||||||
let resp = srv.call(req).await.unwrap();
|
let resp = srv.call(req).await.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
match resp.response().body() {
|
match resp.response().body() {
|
||||||
ResponseBody::Body(Body::Bytes(ref b)) => {
|
Body::Bytes(ref b) => {
|
||||||
let bytes = b.clone();
|
let bytes = b.clone();
|
||||||
assert_eq!(bytes, Bytes::from_static(b"some"));
|
assert_eq!(bytes, Bytes::from_static(b"some"));
|
||||||
}
|
}
|
||||||
|
@ -277,16 +277,28 @@ pub(crate) mod tests {
|
||||||
fn body(&self) -> &Body;
|
fn body(&self) -> &Body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl BodyTest for Body {
|
||||||
|
fn bin_ref(&self) -> &[u8] {
|
||||||
|
match self {
|
||||||
|
Body::Bytes(ref bin) => &bin,
|
||||||
|
_ => unreachable!("bug in test impl"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn body(&self) -> &Body {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl BodyTest for ResponseBody<Body> {
|
impl BodyTest for ResponseBody<Body> {
|
||||||
fn bin_ref(&self) -> &[u8] {
|
fn bin_ref(&self) -> &[u8] {
|
||||||
match self {
|
match self {
|
||||||
ResponseBody::Body(ref b) => match b {
|
ResponseBody::Body(ref b) => match b {
|
||||||
Body::Bytes(ref bin) => &bin,
|
Body::Bytes(ref bin) => &bin,
|
||||||
_ => panic!(),
|
_ => unreachable!("bug in test impl"),
|
||||||
},
|
},
|
||||||
ResponseBody::Other(ref b) => match b {
|
ResponseBody::Other(ref b) => match b {
|
||||||
Body::Bytes(ref bin) => &bin,
|
Body::Bytes(ref bin) => &bin,
|
||||||
_ => panic!(),
|
_ => unreachable!("bug in test impl"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,42 +479,42 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_json() {
|
async fn test_json() {
|
||||||
let mut resp = HttpResponse::Ok().json(vec!["v1", "v2", "v3"]);
|
let resp = HttpResponse::Ok().json(vec!["v1", "v2", "v3"]);
|
||||||
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!(
|
||||||
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
body::to_bytes(resp.into_body()).await.unwrap().as_ref(),
|
||||||
br#"["v1","v2","v3"]"#
|
br#"["v1","v2","v3"]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut resp = HttpResponse::Ok().json(&["v1", "v2", "v3"]);
|
let resp = HttpResponse::Ok().json(&["v1", "v2", "v3"]);
|
||||||
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!(
|
||||||
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
body::to_bytes(resp.into_body()).await.unwrap().as_ref(),
|
||||||
br#"["v1","v2","v3"]"#
|
br#"["v1","v2","v3"]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
// content type override
|
// content type override
|
||||||
let mut resp = HttpResponse::Ok()
|
let resp = HttpResponse::Ok()
|
||||||
.insert_header((CONTENT_TYPE, "text/json"))
|
.insert_header((CONTENT_TYPE, "text/json"))
|
||||||
.json(&vec!["v1", "v2", "v3"]);
|
.json(&vec!["v1", "v2", "v3"]);
|
||||||
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!(
|
||||||
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
body::to_bytes(resp.into_body()).await.unwrap().as_ref(),
|
||||||
br#"["v1","v2","v3"]"#
|
br#"["v1","v2","v3"]"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_serde_json_in_body() {
|
async fn test_serde_json_in_body() {
|
||||||
let mut resp = HttpResponse::Ok().body(
|
let resp = HttpResponse::Ok().body(
|
||||||
serde_json::to_vec(&serde_json::json!({ "test-key": "test-value" })).unwrap(),
|
serde_json::to_vec(&serde_json::json!({ "test-key": "test-value" })).unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
body::to_bytes(resp.take_body()).await.unwrap().as_ref(),
|
body::to_bytes(resp.into_body()).await.unwrap().as_ref(),
|
||||||
br#"{"test-key":"test-value"}"#
|
br#"{"test-key":"test-value"}"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -578,7 +578,7 @@ mod tests {
|
||||||
use actix_utils::future::ok;
|
use actix_utils::future::ok;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
|
||||||
use crate::dev::{Body, ResponseBody};
|
use crate::dev::{Body};
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||||
use crate::middleware::DefaultHeaders;
|
use crate::middleware::DefaultHeaders;
|
||||||
use crate::service::ServiceRequest;
|
use crate::service::ServiceRequest;
|
||||||
|
@ -748,7 +748,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
match resp.response().body() {
|
match resp.response().body() {
|
||||||
ResponseBody::Body(Body::Bytes(ref b)) => {
|
Body::Bytes(ref b) => {
|
||||||
let bytes = b.clone();
|
let bytes = b.clone();
|
||||||
assert_eq!(bytes, Bytes::from_static(b"project: project1"));
|
assert_eq!(bytes, Bytes::from_static(b"project: project1"));
|
||||||
}
|
}
|
||||||
|
@ -849,7 +849,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
match resp.response().body() {
|
match resp.response().body() {
|
||||||
ResponseBody::Body(Body::Bytes(ref b)) => {
|
Body::Bytes(ref b) => {
|
||||||
let bytes = b.clone();
|
let bytes = b.clone();
|
||||||
assert_eq!(bytes, Bytes::from_static(b"project: project_1"));
|
assert_eq!(bytes, Bytes::from_static(b"project: project_1"));
|
||||||
}
|
}
|
||||||
|
@ -877,7 +877,7 @@ mod tests {
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
match resp.response().body() {
|
match resp.response().body() {
|
||||||
ResponseBody::Body(Body::Bytes(ref b)) => {
|
Body::Bytes(ref b) => {
|
||||||
let bytes = b.clone();
|
let bytes = b.clone();
|
||||||
assert_eq!(bytes, Bytes::from_static(b"project: test - 1"));
|
assert_eq!(bytes, Bytes::from_static(b"project: test - 1"));
|
||||||
}
|
}
|
||||||
|
|
10
src/test.rs
10
src/test.rs
|
@ -6,7 +6,7 @@ pub use actix_http::test::TestBuffer;
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
http::{header::IntoHeaderPair, Method, StatusCode, Uri, Version},
|
http::{header::IntoHeaderPair, Method, StatusCode, Uri, Version},
|
||||||
test::TestRequest as HttpTestRequest,
|
test::TestRequest as HttpTestRequest,
|
||||||
Extensions, Request,
|
Extensions, Request, body
|
||||||
};
|
};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
||||||
|
@ -275,6 +275,14 @@ where
|
||||||
Ok(data.freeze())
|
Ok(data.freeze())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn load_body<B>(body: B) -> Result<Bytes, Error>
|
||||||
|
where
|
||||||
|
B: MessageBody + Unpin,
|
||||||
|
B::Error: Into<Error>,
|
||||||
|
{
|
||||||
|
body::to_bytes(body).await.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper function that returns a deserialized response body of a TestRequest
|
/// Helper function that returns a deserialized response body of a TestRequest
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|
|
@ -435,7 +435,7 @@ mod tests {
|
||||||
header::{self, CONTENT_LENGTH, CONTENT_TYPE},
|
header::{self, CONTENT_LENGTH, CONTENT_TYPE},
|
||||||
StatusCode,
|
StatusCode,
|
||||||
},
|
},
|
||||||
test::{load_stream, TestRequest},
|
test::{load_body, TestRequest},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
@ -492,10 +492,10 @@ mod tests {
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
||||||
let s = Json::<MyObject>::from_request(&req, &mut pl).await;
|
let s = Json::<MyObject>::from_request(&req, &mut pl).await;
|
||||||
let mut resp = HttpResponse::from_error(s.err().unwrap());
|
let resp = HttpResponse::from_error(s.err().unwrap());
|
||||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let body = load_stream(resp.take_body()).await.unwrap();
|
let body = load_body(resp.into_body()).await.unwrap();
|
||||||
let msg: MyObject = serde_json::from_slice(&body).unwrap();
|
let msg: MyObject = serde_json::from_slice(&body).unwrap();
|
||||||
assert_eq!(msg.name, "invalid request");
|
assert_eq!(msg.name, "invalid request");
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ use rand::{distributions::Alphanumeric, Rng};
|
||||||
|
|
||||||
use actix_web::dev::BodyEncoding;
|
use actix_web::dev::BodyEncoding;
|
||||||
use actix_web::middleware::{Compress, NormalizePath, TrailingSlash};
|
use actix_web::middleware::{Compress, NormalizePath, TrailingSlash};
|
||||||
use actix_web::{dev, web, App, Error, HttpResponse};
|
use actix_web::{web, App, Error, HttpResponse};
|
||||||
|
|
||||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
Hello World Hello World Hello World Hello World Hello World \
|
Hello World Hello World Hello World Hello World Hello World \
|
||||||
|
@ -160,9 +160,7 @@ async fn test_body_gzip2() {
|
||||||
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(Compress::new(ContentEncoding::Gzip))
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
||||||
.service(web::resource("/").route(web::to(|| {
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
||||||
HttpResponse::Ok().body(STR).into_body::<dev::Body>()
|
|
||||||
})))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = srv
|
let mut response = srv
|
||||||
|
|
Loading…
Reference in New Issue