1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

move typed headers and implement FromRequest (#2094)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Ibraheem Ahmed
2021-04-01 11:42:18 -04:00
committed by GitHub
parent c8ed8dd1a4
commit 50dc13f280
41 changed files with 445 additions and 248 deletions

View File

@ -7,8 +7,10 @@
### Changed
* `ConnectorService` type is renamed to `BoxConnectorService`. [#2081]
* Fix http/https encoding when enabling `compress` feature. [#2116]
* Rename `TestResponse::header` to `append_header`, `set` to `insert_header`. `TestResponse` header methods now take `IntoHeaderPair` tuples. [#2094]
[#2081]: https://github.com/actix/actix-web/pull/2081
[#2094]: https://github.com/actix/actix-web/pull/2094
[#2114]: https://github.com/actix/actix-web/pull/2114
[#2116]: https://github.com/actix/actix-web/pull/2116

View File

@ -189,12 +189,12 @@ impl ClientRequest {
/// # #[actix_rt::main]
/// # async fn main() {
/// # use awc::Client;
/// use awc::http::header::ContentType;
/// use awc::http::header::CONTENT_TYPE;
///
/// Client::new()
/// .get("http://www.rust-lang.org")
/// .insert_header(("X-TEST", "value"))
/// .insert_header(ContentType(mime::APPLICATION_JSON));
/// .insert_header((CONTENT_TYPE, mime::APPLICATION_JSON));
/// # }
/// ```
pub fn append_header<H>(mut self, header: H) -> Self
@ -548,6 +548,8 @@ impl fmt::Debug for ClientRequest {
mod tests {
use std::time::SystemTime;
use actix_http::http::header::HttpDate;
use super::*;
use crate::Client;
@ -564,7 +566,7 @@ mod tests {
let req = Client::new()
.put("/")
.version(Version::HTTP_2)
.insert_header(header::Date(SystemTime::now().into()))
.insert_header((header::DATE, HttpDate::from(SystemTime::now())))
.content_type("plain/text")
.append_header((header::SERVER, "awc"));

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, 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,27 +45,26 @@ 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);
/// Insert a header
pub fn insert_header<H>(mut self, header: H) -> Self
where
H: IntoHeaderPair,
{
if let Ok((key, value)) = header.try_into_header_pair() {
self.head.headers.insert(key, value);
return self;
}
panic!("Can not set 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
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");
}
@ -115,6 +110,8 @@ impl TestResponse {
mod tests {
use std::time::SystemTime;
use actix_http::http::header::HttpDate;
use super::*;
use crate::{cookie, http::header};
@ -122,7 +119,7 @@ mod tests {
fn test_basics() {
let res = TestResponse::default()
.version(Version::HTTP_2)
.set(header::Date(SystemTime::now().into()))
.insert_header((header::DATE, HttpDate::from(SystemTime::now())))
.cookie(cookie::Cookie::build("name", "value").finish())
.finish();
assert!(res.headers().contains_key(header::SET_COOKIE));