refactor awc testresponse to use intoheaderpair

This commit is contained in:
ibraheemdev 2021-03-25 13:35:36 -04:00
parent e06d42cf40
commit 3ccc28611d
2 changed files with 23 additions and 41 deletions

View File

@ -449,13 +449,13 @@ mod tests {
#[actix_rt::test]
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() {
PayloadError::UnknownLength => {}
_ => 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() {
PayloadError::Overflow => {}
_ => unreachable!("error"),
@ -497,23 +497,23 @@ mod tests {
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
let mut req = TestResponse::default()
.header(
.insert_header((
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/text"),
)
))
.finish();
let json = JsonBody::<_, MyObject>::new(&mut req).await;
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
let mut req = TestResponse::default()
.header(
.insert_header((
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
)
.header(
))
.insert_header((
header::CONTENT_LENGTH,
header::HeaderValue::from_static("10000"),
)
))
.finish();
let json = JsonBody::<_, MyObject>::new(&mut req).limit(100).await;
@ -523,14 +523,14 @@ mod tests {
));
let mut req = TestResponse::default()
.header(
.insert_header((
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
)
.header(
))
.insert_header((
header::CONTENT_LENGTH,
header::HeaderValue::from_static("16"),
)
))
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
.finish();

View File

@ -1,8 +1,6 @@
//! Test helpers for actix http client to use during testing.
use std::convert::TryFrom;
use actix_http::http::header::{Header, IntoHeaderPair, IntoHeaderValue};
use actix_http::http::{Error as HttpError, HeaderName, StatusCode, Version};
use actix_http::http::header::IntoHeaderPair;
use actix_http::http::{StatusCode, Version};
#[cfg(feature = "cookies")]
use actix_http::{
cookie::{Cookie, CookieJar},
@ -34,13 +32,11 @@ impl Default for TestResponse {
impl TestResponse {
/// 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
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
{
Self::default().header(key, value)
Self::default().insert_header(header)
}
/// Set HTTP version of this response
@ -49,17 +45,7 @@ impl TestResponse {
self
}
/// Set 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
/// Insert a header
pub fn insert_header<H>(mut self, header: H) -> Self
where
H: IntoHeaderPair,
@ -72,17 +58,13 @@ impl TestResponse {
}
/// 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
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
{
if let Ok(key) = HeaderName::try_from(key) {
if let Ok(value) = value.try_into_value() {
self.head.headers.append(key, value);
return self;
}
if let Ok((key, value)) = header.try_into_header_pair() {
self.head.headers.append(key, value);
return self;
}
panic!("Can not create header");
}