mirror of https://github.com/fafhrd91/actix-web
refactor awc testresponse to use intoheaderpair
This commit is contained in:
parent
e06d42cf40
commit
3ccc28611d
|
@ -449,13 +449,13 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_body() {
|
async fn test_body() {
|
||||||
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "xxxx").finish();
|
let mut req = TestResponse::with_header((header::CONTENT_LENGTH, "xxxx")).finish();
|
||||||
match req.body().await.err().unwrap() {
|
match req.body().await.err().unwrap() {
|
||||||
PayloadError::UnknownLength => {}
|
PayloadError::UnknownLength => {}
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "10000000").finish();
|
let mut req = TestResponse::with_header((header::CONTENT_LENGTH, "10000000")).finish();
|
||||||
match req.body().await.err().unwrap() {
|
match req.body().await.err().unwrap() {
|
||||||
PayloadError::Overflow => {}
|
PayloadError::Overflow => {}
|
||||||
_ => unreachable!("error"),
|
_ => unreachable!("error"),
|
||||||
|
@ -497,23 +497,23 @@ mod tests {
|
||||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||||
|
|
||||||
let mut req = TestResponse::default()
|
let mut req = TestResponse::default()
|
||||||
.header(
|
.insert_header((
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/text"),
|
header::HeaderValue::from_static("application/text"),
|
||||||
)
|
))
|
||||||
.finish();
|
.finish();
|
||||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||||
|
|
||||||
let mut req = TestResponse::default()
|
let mut req = TestResponse::default()
|
||||||
.header(
|
.insert_header((
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/json"),
|
header::HeaderValue::from_static("application/json"),
|
||||||
)
|
))
|
||||||
.header(
|
.insert_header((
|
||||||
header::CONTENT_LENGTH,
|
header::CONTENT_LENGTH,
|
||||||
header::HeaderValue::from_static("10000"),
|
header::HeaderValue::from_static("10000"),
|
||||||
)
|
))
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
let json = JsonBody::<_, MyObject>::new(&mut req).limit(100).await;
|
let json = JsonBody::<_, MyObject>::new(&mut req).limit(100).await;
|
||||||
|
@ -523,14 +523,14 @@ mod tests {
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut req = TestResponse::default()
|
let mut req = TestResponse::default()
|
||||||
.header(
|
.insert_header((
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/json"),
|
header::HeaderValue::from_static("application/json"),
|
||||||
)
|
))
|
||||||
.header(
|
.insert_header((
|
||||||
header::CONTENT_LENGTH,
|
header::CONTENT_LENGTH,
|
||||||
header::HeaderValue::from_static("16"),
|
header::HeaderValue::from_static("16"),
|
||||||
)
|
))
|
||||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
//! Test helpers for actix http client to use during testing.
|
//! Test helpers for actix http client to use during testing.
|
||||||
use std::convert::TryFrom;
|
use actix_http::http::header::IntoHeaderPair;
|
||||||
|
use actix_http::http::{StatusCode, Version};
|
||||||
use actix_http::http::header::{Header, IntoHeaderPair, IntoHeaderValue};
|
|
||||||
use actix_http::http::{Error as HttpError, HeaderName, StatusCode, Version};
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
cookie::{Cookie, CookieJar},
|
cookie::{Cookie, CookieJar},
|
||||||
|
@ -34,13 +32,11 @@ impl Default for TestResponse {
|
||||||
|
|
||||||
impl TestResponse {
|
impl TestResponse {
|
||||||
/// Create TestResponse and set header
|
/// Create TestResponse and set header
|
||||||
pub fn with_header<K, V>(key: K, value: V) -> Self
|
pub fn with_header<H>(header: H) -> Self
|
||||||
where
|
where
|
||||||
HeaderName: TryFrom<K>,
|
H: IntoHeaderPair,
|
||||||
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
|
||||||
V: IntoHeaderValue,
|
|
||||||
{
|
{
|
||||||
Self::default().header(key, value)
|
Self::default().insert_header(header)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set HTTP version of this response
|
/// Set HTTP version of this response
|
||||||
|
@ -49,17 +45,7 @@ impl TestResponse {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a header
|
/// Insert a header
|
||||||
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
|
||||||
if let Ok(value) = hdr.try_into_value() {
|
|
||||||
self.head.headers.append(H::name(), value);
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
panic!("Can not set header");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Should `set` be removed and replaced with this instead?
|
|
||||||
// It looks like `TestResponse` was missed in the header rework
|
|
||||||
pub fn insert_header<H>(mut self, header: H) -> Self
|
pub fn insert_header<H>(mut self, header: H) -> Self
|
||||||
where
|
where
|
||||||
H: IntoHeaderPair,
|
H: IntoHeaderPair,
|
||||||
|
@ -72,18 +58,14 @@ impl TestResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Append a header
|
/// Append a header
|
||||||
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
pub fn append_header<H>(mut self, header: H) -> Self
|
||||||
where
|
where
|
||||||
HeaderName: TryFrom<K>,
|
H: IntoHeaderPair,
|
||||||
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
|
||||||
V: IntoHeaderValue,
|
|
||||||
{
|
{
|
||||||
if let Ok(key) = HeaderName::try_from(key) {
|
if let Ok((key, value)) = header.try_into_header_pair() {
|
||||||
if let Ok(value) = value.try_into_value() {
|
|
||||||
self.head.headers.append(key, value);
|
self.head.headers.append(key, value);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
panic!("Can not create header");
|
panic!("Can not create header");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue