mirror of
https://github.com/fafhrd91/actix-web
synced 2025-09-02 01:31:57 +02:00
response header rework (#1869)
This commit is contained in:
@@ -381,11 +381,11 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_form() {
|
||||
let (req, mut pl) =
|
||||
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(CONTENT_LENGTH, "11")
|
||||
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
||||
.insert_header((CONTENT_LENGTH, 11))
|
||||
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
||||
.to_http_parts();
|
||||
|
||||
let Form(s) = Form::<Info>::from_request(&req, &mut pl).await.unwrap();
|
||||
assert_eq!(
|
||||
@@ -414,25 +414,26 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_urlencoded_error() {
|
||||
let (req, mut pl) =
|
||||
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(CONTENT_LENGTH, "xxxx")
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
||||
.insert_header((CONTENT_LENGTH, "xxxx"))
|
||||
.to_http_parts();
|
||||
let info = UrlEncoded::<Info>::new(&req, &mut pl).await;
|
||||
assert!(eq(info.err().unwrap(), UrlencodedError::UnknownLength));
|
||||
|
||||
let (req, mut pl) =
|
||||
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(CONTENT_LENGTH, "1000000")
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
||||
.insert_header((CONTENT_LENGTH, "1000000"))
|
||||
.to_http_parts();
|
||||
let info = UrlEncoded::<Info>::new(&req, &mut pl).await;
|
||||
assert!(eq(
|
||||
info.err().unwrap(),
|
||||
UrlencodedError::Overflow { size: 0, limit: 0 }
|
||||
));
|
||||
|
||||
let (req, mut pl) = TestRequest::with_header(CONTENT_TYPE, "text/plain")
|
||||
.header(CONTENT_LENGTH, "10")
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((CONTENT_TYPE, "text/plain"))
|
||||
.insert_header((CONTENT_LENGTH, 10))
|
||||
.to_http_parts();
|
||||
let info = UrlEncoded::<Info>::new(&req, &mut pl).await;
|
||||
assert!(eq(info.err().unwrap(), UrlencodedError::ContentType));
|
||||
@@ -440,11 +441,11 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_urlencoded() {
|
||||
let (req, mut pl) =
|
||||
TestRequest::with_header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.header(CONTENT_LENGTH, "11")
|
||||
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
||||
.insert_header((CONTENT_LENGTH, 11))
|
||||
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
||||
.to_http_parts();
|
||||
|
||||
let info = UrlEncoded::<Info>::new(&req, &mut pl).await.unwrap();
|
||||
assert_eq!(
|
||||
@@ -455,13 +456,14 @@ mod tests {
|
||||
}
|
||||
);
|
||||
|
||||
let (req, mut pl) = TestRequest::with_header(
|
||||
CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded; charset=utf-8",
|
||||
)
|
||||
.header(CONTENT_LENGTH, "11")
|
||||
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((
|
||||
CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded; charset=utf-8",
|
||||
))
|
||||
.insert_header((CONTENT_LENGTH, 11))
|
||||
.set_payload(Bytes::from_static(b"hello=world&counter=123"))
|
||||
.to_http_parts();
|
||||
|
||||
let info = UrlEncoded::<Info>::new(&req, &mut pl).await.unwrap();
|
||||
assert_eq!(
|
||||
@@ -497,8 +499,8 @@ mod tests {
|
||||
let ctype = HeaderValue::from_static("application/x-www-form-urlencoded");
|
||||
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.header(CONTENT_TYPE, ctype)
|
||||
.header(CONTENT_LENGTH, HeaderValue::from_static("20"))
|
||||
.insert_header((CONTENT_TYPE, ctype))
|
||||
.insert_header((CONTENT_LENGTH, HeaderValue::from_static("20")))
|
||||
.set_payload(Bytes::from_static(b"hello=test&counter=4"))
|
||||
.app_data(web::Data::new(FormConfig::default().limit(10)))
|
||||
.to_http_parts();
|
||||
|
@@ -427,7 +427,7 @@ mod tests {
|
||||
use crate::{
|
||||
error::InternalError,
|
||||
http::{
|
||||
header::{self, HeaderValue, CONTENT_LENGTH, CONTENT_TYPE},
|
||||
header::{self, CONTENT_LENGTH, CONTENT_TYPE},
|
||||
StatusCode,
|
||||
},
|
||||
test::{load_stream, TestRequest},
|
||||
@@ -469,14 +469,14 @@ mod tests {
|
||||
#[actix_rt::test]
|
||||
async fn test_custom_error_responder() {
|
||||
let (req, mut pl) = TestRequest::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\"}"))
|
||||
.app_data(JsonConfig::default().limit(10).error_handler(|err, _| {
|
||||
let msg = MyObject {
|
||||
@@ -500,14 +500,14 @@ mod tests {
|
||||
#[actix_rt::test]
|
||||
async fn test_extract() {
|
||||
let (req, mut pl) = TestRequest::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\"}"))
|
||||
.to_http_parts();
|
||||
|
||||
@@ -521,14 +521,14 @@ mod tests {
|
||||
);
|
||||
|
||||
let (req, mut pl) = TestRequest::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\"}"))
|
||||
.app_data(JsonConfig::default().limit(10))
|
||||
.to_http_parts();
|
||||
@@ -538,14 +538,14 @@ mod tests {
|
||||
.contains("Json payload size is bigger than allowed"));
|
||||
|
||||
let (req, mut pl) = TestRequest::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\"}"))
|
||||
.app_data(
|
||||
JsonConfig::default()
|
||||
@@ -564,23 +564,23 @@ mod tests {
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.header(
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/text"),
|
||||
)
|
||||
))
|
||||
.to_http_parts();
|
||||
let json = JsonBody::<MyObject>::new(&req, &mut pl, None).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.header(
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
)
|
||||
.header(
|
||||
))
|
||||
.insert_header((
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("10000"),
|
||||
)
|
||||
))
|
||||
.to_http_parts();
|
||||
|
||||
let json = JsonBody::<MyObject>::new(&req, &mut pl, None)
|
||||
@@ -589,14 +589,14 @@ mod tests {
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::Overflow));
|
||||
|
||||
let (req, mut pl) = TestRequest::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\"}"))
|
||||
.to_http_parts();
|
||||
|
||||
@@ -611,17 +611,18 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_with_json_and_bad_content_type() {
|
||||
let (req, mut pl) = TestRequest::with_header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("text/plain"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
)
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(JsonConfig::default().limit(4096))
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("text/plain"),
|
||||
))
|
||||
.insert_header((
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
))
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(JsonConfig::default().limit(4096))
|
||||
.to_http_parts();
|
||||
|
||||
let s = Json::<MyObject>::from_request(&req, &mut pl).await;
|
||||
assert!(s.is_err())
|
||||
@@ -629,19 +630,20 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_with_json_and_good_custom_content_type() {
|
||||
let (req, mut pl) = TestRequest::with_header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("text/plain"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
)
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(JsonConfig::default().content_type(|mime: mime::Mime| {
|
||||
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||
}))
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("text/plain"),
|
||||
))
|
||||
.insert_header((
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
))
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(JsonConfig::default().content_type(|mime: mime::Mime| {
|
||||
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||
}))
|
||||
.to_http_parts();
|
||||
|
||||
let s = Json::<MyObject>::from_request(&req, &mut pl).await;
|
||||
assert!(s.is_ok())
|
||||
@@ -649,19 +651,20 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_with_json_and_bad_custom_content_type() {
|
||||
let (req, mut pl) = TestRequest::with_header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("text/html"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
)
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(JsonConfig::default().content_type(|mime: mime::Mime| {
|
||||
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||
}))
|
||||
.to_http_parts();
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("text/html"),
|
||||
))
|
||||
.insert_header((
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
))
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(JsonConfig::default().content_type(|mime: mime::Mime| {
|
||||
mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||
}))
|
||||
.to_http_parts();
|
||||
|
||||
let s = Json::<MyObject>::from_request(&req, &mut pl).await;
|
||||
assert!(s.is_err())
|
||||
@@ -670,8 +673,8 @@ mod tests {
|
||||
#[actix_rt::test]
|
||||
async fn test_with_config_in_data_wrapper() {
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
||||
.header(CONTENT_LENGTH, HeaderValue::from_static("16"))
|
||||
.insert_header((CONTENT_TYPE, mime::APPLICATION_JSON))
|
||||
.insert_header((CONTENT_LENGTH, 16))
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.app_data(web::Data::new(JsonConfig::default().limit(10)))
|
||||
.to_http_parts();
|
||||
|
@@ -364,14 +364,13 @@ mod tests {
|
||||
let cfg = PayloadConfig::default().mimetype(mime::APPLICATION_JSON);
|
||||
assert!(cfg.check_mimetype(&req).is_err());
|
||||
|
||||
let req = TestRequest::with_header(
|
||||
header::CONTENT_TYPE,
|
||||
"application/x-www-form-urlencoded",
|
||||
)
|
||||
.to_http_request();
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::CONTENT_TYPE, "application/x-www-form-urlencoded"))
|
||||
.to_http_request();
|
||||
assert!(cfg.check_mimetype(&req).is_err());
|
||||
|
||||
let req = TestRequest::with_header(header::CONTENT_TYPE, "application/json")
|
||||
let req = TestRequest::default()
|
||||
.insert_header((header::CONTENT_TYPE, "application/json"))
|
||||
.to_http_request();
|
||||
assert!(cfg.check_mimetype(&req).is_ok());
|
||||
}
|
||||
@@ -432,25 +431,25 @@ mod tests {
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
|
||||
let req = TestRequest::with_uri("/bytes-app-data")
|
||||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON)
|
||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||
.to_request();
|
||||
let resp = call_service(&mut srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::with_uri("/bytes-data")
|
||||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON)
|
||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||
.to_request();
|
||||
let resp = call_service(&mut srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::with_uri("/string-app-data")
|
||||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON)
|
||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||
.to_request();
|
||||
let resp = call_service(&mut srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::with_uri("/string-data")
|
||||
.header(header::CONTENT_TYPE, mime::APPLICATION_JSON)
|
||||
.insert_header(header::ContentType(mime::APPLICATION_JSON))
|
||||
.to_request();
|
||||
let resp = call_service(&mut srv, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
@@ -458,7 +457,8 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_bytes() {
|
||||
let (req, mut pl) = TestRequest::with_header(header::CONTENT_LENGTH, "11")
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((header::CONTENT_LENGTH, "11"))
|
||||
.set_payload(Bytes::from_static(b"hello=world"))
|
||||
.to_http_parts();
|
||||
|
||||
@@ -468,7 +468,8 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_string() {
|
||||
let (req, mut pl) = TestRequest::with_header(header::CONTENT_LENGTH, "11")
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((header::CONTENT_LENGTH, "11"))
|
||||
.set_payload(Bytes::from_static(b"hello=world"))
|
||||
.to_http_parts();
|
||||
|
||||
@@ -478,7 +479,8 @@ mod tests {
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_message_body() {
|
||||
let (req, mut pl) = TestRequest::with_header(header::CONTENT_LENGTH, "xxxx")
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((header::CONTENT_LENGTH, "xxxx"))
|
||||
.to_srv_request()
|
||||
.into_parts();
|
||||
let res = HttpMessageBody::new(&req, &mut pl).await;
|
||||
@@ -487,7 +489,8 @@ mod tests {
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
|
||||
let (req, mut pl) = TestRequest::with_header(header::CONTENT_LENGTH, "1000000")
|
||||
let (req, mut pl) = TestRequest::default()
|
||||
.insert_header((header::CONTENT_LENGTH, "1000000"))
|
||||
.to_srv_request()
|
||||
.into_parts();
|
||||
let res = HttpMessageBody::new(&req, &mut pl).await;
|
||||
|
Reference in New Issue
Block a user