mirror of
https://github.com/fafhrd91/actix-web
synced 2025-09-01 01:16:59 +02:00
move typed headers and implement FromRequest (#2094)
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
@@ -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));
|
||||
|
Reference in New Issue
Block a user