1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-21 13:15:38 +02:00

response header rework (#1869)

This commit is contained in:
Rob Ede
2021-01-15 02:11:10 +00:00
committed by GitHub
parent 4edeb5ce47
commit b1dd8d28bc
76 changed files with 1568 additions and 1347 deletions

View File

@@ -1,13 +1,13 @@
//! Various helpers for Actix applications to use during testing.
use std::convert::TryFrom;
use std::net::SocketAddr;
use std::rc::Rc;
use std::sync::mpsc;
use std::{fmt, net, thread, time};
use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_http::http::header::{ContentType, Header, HeaderName, IntoHeaderValue};
use actix_http::http::{Error as HttpError, Method, StatusCode, Uri, Version};
use actix_http::http::header::{ContentType, IntoHeaderPair};
use actix_http::http::{Method, StatusCode, Uri, Version};
use actix_http::test::TestRequest as HttpTestRequest;
use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request};
use actix_router::{Path, ResourceDef, Url};
@@ -349,7 +349,7 @@ where
///
/// #[test]
/// fn test_index() {
/// let req = test::TestRequest::with_header("content-type", "text/plain")
/// let req = test::TestRequest::default().insert_header("content-type", "text/plain")
/// .to_http_request();
///
/// let resp = index(req).await.unwrap();
@@ -389,21 +389,6 @@ impl TestRequest {
TestRequest::default().uri(path)
}
/// Create TestRequest and set header
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
TestRequest::default().set(hdr)
}
/// Create TestRequest and set header
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
{
TestRequest::default().header(key, value)
}
/// Create TestRequest and set method to `Method::GET`
pub fn get() -> TestRequest {
TestRequest::default().method(Method::GET)
@@ -447,24 +432,25 @@ impl TestRequest {
self
}
/// Set a header
pub fn set<H: Header>(mut self, hdr: H) -> Self {
self.req.set(hdr);
self
}
/// Set a header
pub fn header<K, V>(mut self, key: K, value: V) -> Self
/// Insert a header, replacing any that were set with an equivalent field name.
pub fn insert_header<H>(mut self, header: H) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
{
self.req.header(key, value);
self.req.insert_header(header);
self
}
/// Set cookie for this request
/// Append a header, keeping any that were set with an equivalent field name.
pub fn append_header<H>(mut self, header: H) -> Self
where
H: IntoHeaderPair,
{
self.req.append_header(header);
self
}
/// Set cookie for this request.
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
self.req.cookie(cookie);
self
@@ -494,7 +480,7 @@ impl TestRequest {
let bytes = serde_urlencoded::to_string(data)
.expect("Failed to serialize test data as a urlencoded form");
self.req.set_payload(bytes);
self.req.set(ContentType::form_url_encoded());
self.req.insert_header(ContentType::form_url_encoded());
self
}
@@ -504,7 +490,7 @@ impl TestRequest {
let bytes =
serde_json::to_string(data).expect("Failed to serialize test data to json");
self.req.set_payload(bytes);
self.req.set(ContentType::json());
self.req.insert_header(ContentType::json());
self
}
@@ -1029,9 +1015,10 @@ mod tests {
#[actix_rt::test]
async fn test_basics() {
let req = TestRequest::with_hdr(header::ContentType::json())
let req = TestRequest::default()
.version(Version::HTTP_2)
.set(header::Date(SystemTime::now().into()))
.insert_header(header::ContentType::json())
.insert_header(header::Date(SystemTime::now().into()))
.param("test", "123")
.data(10u32)
.app_data(20u64)
@@ -1068,7 +1055,7 @@ mod tests {
let put_req = TestRequest::put()
.uri("/index.html")
.header(header::CONTENT_TYPE, "application/json")
.insert_header((header::CONTENT_TYPE, "application/json"))
.to_request();
let result = read_response(&mut app, put_req).await;
@@ -1076,7 +1063,7 @@ mod tests {
let patch_req = TestRequest::patch()
.uri("/index.html")
.header(header::CONTENT_TYPE, "application/json")
.insert_header((header::CONTENT_TYPE, "application/json"))
.to_request();
let result = read_response(&mut app, patch_req).await;
@@ -1099,7 +1086,7 @@ mod tests {
let req = TestRequest::post()
.uri("/index.html")
.header(header::CONTENT_TYPE, "application/json")
.insert_header((header::CONTENT_TYPE, "application/json"))
.to_request();
let result = read_response(&mut app, req).await;
@@ -1144,7 +1131,7 @@ mod tests {
let req = TestRequest::post()
.uri("/people")
.header(header::CONTENT_TYPE, "application/json")
.insert_header((header::CONTENT_TYPE, "application/json"))
.set_payload(payload)
.to_request();
@@ -1165,7 +1152,7 @@ mod tests {
let resp = TestRequest::post()
.uri("/people")
.header(header::CONTENT_TYPE, "application/json")
.insert_header((header::CONTENT_TYPE, "application/json"))
.set_payload(payload)
.send_request(&mut app)
.await;