1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-30 00:14:58 +02:00

remove BodyEncoding trait (#2565)

This commit is contained in:
Rob Ede
2022-01-03 18:46:04 +00:00
committed by GitHub
parent 19a46e3925
commit 0bc4ae9158
20 changed files with 355 additions and 403 deletions

View File

@ -1,14 +1,12 @@
use actix_http::ContentEncoding;
use actix_web::{
dev::BodyEncoding as _,
http::{header, StatusCode},
middleware::Compress,
web, App, HttpResponse,
};
use bytes::Bytes;
mod test_utils;
use test_utils::{brotli, gzip, zstd};
mod utils;
static LOREM: &[u8] = include_bytes!("fixtures/lorem.txt");
static LOREM_GZIP: &[u8] = include_bytes!("fixtures/lorem.txt.gz");
@ -27,7 +25,6 @@ macro_rules! test_server {
web::to(|| {
HttpResponse::Ok()
// signal to compressor that content should not be altered
.encode_with(ContentEncoding::Identity)
// signal to client that content is encoded
.insert_header(ContentEncoding::Gzip)
.body(LOREM_GZIP)
@ -38,7 +35,6 @@ macro_rules! test_server {
web::to(|| {
HttpResponse::Ok()
// signal to compressor that content should not be altered
.encode_with(ContentEncoding::Identity)
// signal to client that content is encoded
.insert_header(ContentEncoding::Brotli)
.body(LOREM_BR)
@ -49,7 +45,6 @@ macro_rules! test_server {
web::to(|| {
HttpResponse::Ok()
// signal to compressor that content should not be altered
.encode_with(ContentEncoding::Identity)
// signal to client that content is encoded
.insert_header(ContentEncoding::Zstd)
.body(LOREM_ZSTD)
@ -60,7 +55,6 @@ macro_rules! test_server {
web::to(|| {
HttpResponse::Ok()
// signal to compressor that content should not be altered
.encode_with(ContentEncoding::Identity)
// signal to client that content is encoded as 7zip
.insert_header((header::CONTENT_ENCODING, "xz"))
.body(LOREM_XZ)
@ -117,7 +111,7 @@ async fn negotiate_encoding_gzip() {
.await
.unwrap();
let bytes = res.body().await.unwrap();
assert_eq!(gzip::decode(bytes), LOREM);
assert_eq!(utils::gzip::decode(bytes), LOREM);
srv.stop().await;
}
@ -146,7 +140,7 @@ async fn negotiate_encoding_br() {
.await
.unwrap();
let bytes = res.body().await.unwrap();
assert_eq!(brotli::decode(bytes), LOREM);
assert_eq!(utils::brotli::decode(bytes), LOREM);
srv.stop().await;
}
@ -175,7 +169,7 @@ async fn negotiate_encoding_zstd() {
.await
.unwrap();
let bytes = res.body().await.unwrap();
assert_eq!(zstd::decode(bytes), LOREM);
assert_eq!(utils::zstd::decode(bytes), LOREM);
srv.stop().await;
}

View File

@ -12,11 +12,7 @@ use std::{
use actix_web::{
cookie::{Cookie, CookieBuilder},
dev::BodyEncoding,
http::{
header::{self, ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, TRANSFER_ENCODING},
StatusCode,
},
http::{header, StatusCode},
middleware::{Compress, NormalizePath, TrailingSlash},
web, App, Error, HttpResponse,
};
@ -31,30 +27,10 @@ use openssl::{
x509::X509,
};
mod test_utils;
use test_utils::{brotli, deflate, gzip, zstd};
mod utils;
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World";
const S: &str = "Hello World ";
const STR: &str = const_str::repeat!(S, 100);
#[cfg(feature = "openssl")]
fn openssl_config() -> SslAcceptor {
@ -129,51 +105,52 @@ async fn test_body() {
srv.stop().await;
}
#[actix_rt::test]
async fn test_body_encoding_override() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new()
.wrap(Compress::default())
.service(web::resource("/").route(web::to(|| {
HttpResponse::Ok()
.encode_with(ContentEncoding::Deflate)
.body(STR)
})))
.service(web::resource("/raw").route(web::to(|| {
let mut res = HttpResponse::with_body(actix_web::http::StatusCode::OK, STR);
res.encode_with(ContentEncoding::Deflate);
res.map_into_boxed_body()
})))
});
// enforcing an encoding per-response is removed
// #[actix_rt::test]
// async fn test_body_encoding_override() {
// let srv = actix_test::start_with(actix_test::config().h1(), || {
// App::new()
// .wrap(Compress::default())
// .service(web::resource("/").route(web::to(|| {
// HttpResponse::Ok()
// .encode_with(ContentEncoding::Deflate)
// .body(STR)
// })))
// .service(web::resource("/raw").route(web::to(|| {
// let mut res = HttpResponse::with_body(actix_web::http::StatusCode::OK, STR);
// res.encode_with(ContentEncoding::Deflate);
// res.map_into_boxed_body()
// })))
// });
// Builder
let mut res = srv
.get("/")
.no_decompress()
.append_header((ACCEPT_ENCODING, "deflate"))
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
// // Builder
// let mut res = srv
// .get("/")
// .no_decompress()
// .append_header((ACCEPT_ENCODING, "deflate"))
// .send()
// .await
// .unwrap();
// assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(deflate::decode(bytes), STR.as_bytes());
// let bytes = res.body().await.unwrap();
// assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
// Raw Response
let mut res = srv
.request(actix_web::http::Method::GET, srv.url("/raw"))
.no_decompress()
.append_header((ACCEPT_ENCODING, "deflate"))
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
// // Raw Response
// let mut res = srv
// .request(actix_web::http::Method::GET, srv.url("/raw"))
// .no_decompress()
// .append_header((ACCEPT_ENCODING, "deflate"))
// .send()
// .await
// .unwrap();
// assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(deflate::decode(bytes), STR.as_bytes());
// let bytes = res.body().await.unwrap();
// assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
srv.stop().await;
}
// srv.stop().await;
// }
#[actix_rt::test]
async fn body_gzip_large() {
@ -191,14 +168,14 @@ async fn body_gzip_large() {
let mut res = srv
.get("/")
.no_decompress()
.append_header((ACCEPT_ENCODING, "gzip"))
.append_header((header::ACCEPT_ENCODING, "gzip"))
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(gzip::decode(bytes), data.as_bytes());
assert_eq!(utils::gzip::decode(bytes), data.as_bytes());
srv.stop().await;
}
@ -222,14 +199,14 @@ async fn test_body_gzip_large_random() {
let mut res = srv
.get("/")
.no_decompress()
.append_header((ACCEPT_ENCODING, "gzip"))
.append_header((header::ACCEPT_ENCODING, "gzip"))
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(gzip::decode(bytes), data.as_bytes());
assert_eq!(utils::gzip::decode(bytes), data.as_bytes());
srv.stop().await;
}
@ -248,15 +225,18 @@ async fn test_body_chunked_implicit() {
let mut res = srv
.get("/")
.no_decompress()
.append_header((ACCEPT_ENCODING, "gzip"))
.append_header((header::ACCEPT_ENCODING, "gzip"))
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.headers().get(TRANSFER_ENCODING).unwrap(), "chunked");
assert_eq!(
res.headers().get(header::TRANSFER_ENCODING).unwrap(),
"chunked"
);
let bytes = res.body().await.unwrap();
assert_eq!(gzip::decode(bytes), STR.as_bytes());
assert_eq!(utils::gzip::decode(bytes), STR.as_bytes());
srv.stop().await;
}
@ -274,7 +254,7 @@ async fn test_body_br_streaming() {
let mut res = srv
.get("/")
.append_header((ACCEPT_ENCODING, "br"))
.append_header((header::ACCEPT_ENCODING, "br"))
.no_decompress()
.send()
.await
@ -282,7 +262,7 @@ async fn test_body_br_streaming() {
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(brotli::decode(bytes), STR.as_bytes());
assert_eq!(utils::brotli::decode(bytes), STR.as_bytes());
srv.stop().await;
}
@ -319,7 +299,7 @@ async fn test_no_chunking() {
let mut res = srv.get("/").send().await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert!(!res.headers().contains_key(TRANSFER_ENCODING));
assert!(!res.headers().contains_key(header::TRANSFER_ENCODING));
let bytes = res.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
@ -337,7 +317,7 @@ async fn test_body_deflate() {
let mut res = srv
.get("/")
.append_header((ACCEPT_ENCODING, "deflate"))
.append_header((header::ACCEPT_ENCODING, "deflate"))
.no_decompress()
.send()
.await
@ -345,7 +325,7 @@ async fn test_body_deflate() {
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(deflate::decode(bytes), STR.as_bytes());
assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
srv.stop().await;
}
@ -360,7 +340,7 @@ async fn test_body_brotli() {
let mut res = srv
.get("/")
.append_header((ACCEPT_ENCODING, "br"))
.append_header((header::ACCEPT_ENCODING, "br"))
.no_decompress()
.send()
.await
@ -368,7 +348,7 @@ async fn test_body_brotli() {
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(brotli::decode(bytes), STR.as_bytes());
assert_eq!(utils::brotli::decode(bytes), STR.as_bytes());
srv.stop().await;
}
@ -383,7 +363,7 @@ async fn test_body_zstd() {
let mut res = srv
.get("/")
.append_header((ACCEPT_ENCODING, "zstd"))
.append_header((header::ACCEPT_ENCODING, "zstd"))
.no_decompress()
.send()
.await
@ -391,7 +371,7 @@ async fn test_body_zstd() {
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(zstd::decode(bytes), STR.as_bytes());
assert_eq!(utils::zstd::decode(bytes), STR.as_bytes());
srv.stop().await;
}
@ -409,7 +389,7 @@ async fn test_body_zstd_streaming() {
let mut res = srv
.get("/")
.append_header((ACCEPT_ENCODING, "zstd"))
.append_header((header::ACCEPT_ENCODING, "zstd"))
.no_decompress()
.send()
.await
@ -417,7 +397,7 @@ async fn test_body_zstd_streaming() {
assert_eq!(res.status(), StatusCode::OK);
let bytes = res.body().await.unwrap();
assert_eq!(zstd::decode(bytes), STR.as_bytes());
assert_eq!(utils::zstd::decode(bytes), STR.as_bytes());
srv.stop().await;
}
@ -432,8 +412,8 @@ async fn test_zstd_encoding() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "zstd"))
.send_body(zstd::encode(STR));
.append_header((header::CONTENT_ENCODING, "zstd"))
.send_body(utils::zstd::encode(STR));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -463,8 +443,8 @@ async fn test_zstd_encoding_large() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "zstd"))
.send_body(zstd::encode(&data));
.append_header((header::CONTENT_ENCODING, "zstd"))
.send_body(utils::zstd::encode(&data));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -484,8 +464,8 @@ async fn test_encoding() {
let request = srv
.post("/")
.insert_header((CONTENT_ENCODING, "gzip"))
.send_body(gzip::encode(STR));
.insert_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(STR));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -505,8 +485,8 @@ async fn test_gzip_encoding() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "gzip"))
.send_body(gzip::encode(STR));
.append_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(STR));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -527,8 +507,8 @@ async fn test_gzip_encoding_large() {
let req = srv
.post("/")
.append_header((CONTENT_ENCODING, "gzip"))
.send_body(gzip::encode(&data));
.append_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(&data));
let mut res = req.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -554,8 +534,8 @@ async fn test_reading_gzip_encoding_large_random() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "gzip"))
.send_body(gzip::encode(&data));
.append_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(&data));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -575,8 +555,8 @@ async fn test_reading_deflate_encoding() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "deflate"))
.send_body(deflate::encode(STR));
.append_header((header::CONTENT_ENCODING, "deflate"))
.send_body(utils::deflate::encode(STR));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -597,8 +577,8 @@ async fn test_reading_deflate_encoding_large() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "deflate"))
.send_body(deflate::encode(&data));
.append_header((header::CONTENT_ENCODING, "deflate"))
.send_body(utils::deflate::encode(&data));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -624,8 +604,8 @@ async fn test_reading_deflate_encoding_large_random() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "deflate"))
.send_body(deflate::encode(&data));
.append_header((header::CONTENT_ENCODING, "deflate"))
.send_body(utils::deflate::encode(&data));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -646,8 +626,8 @@ async fn test_brotli_encoding() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "br"))
.send_body(brotli::encode(STR));
.append_header((header::CONTENT_ENCODING, "br"))
.send_body(utils::brotli::encode(STR));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -677,8 +657,8 @@ async fn test_brotli_encoding_large() {
let request = srv
.post("/")
.append_header((CONTENT_ENCODING, "br"))
.send_body(brotli::encode(&data));
.append_header((header::CONTENT_ENCODING, "br"))
.send_body(utils::brotli::encode(&data));
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -697,8 +677,9 @@ async fn test_brotli_encoding_large_openssl() {
let srv =
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
// echo decompressed request body back in response
HttpResponse::Ok()
.encode_with(ContentEncoding::Identity)
.insert_header(header::ContentEncoding::Identity)
.body(bytes)
})))
});
@ -706,7 +687,7 @@ async fn test_brotli_encoding_large_openssl() {
let mut res = srv
.post("/")
.append_header((header::CONTENT_ENCODING, "br"))
.send_body(brotli::encode(&data))
.send_body(utils::brotli::encode(&data))
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -758,16 +739,20 @@ mod plus_rustls {
let srv = actix_test::start_with(actix_test::config().rustls(tls_config()), || {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
// echo decompressed request body back in response
HttpResponse::Ok()
.encode_with(ContentEncoding::Identity)
.insert_header(header::ContentEncoding::Identity)
.body(bytes)
})))
});
let req = srv
.post("/")
.insert_header((actix_web::http::header::CONTENT_ENCODING, "deflate"))
.send_stream(TestBody::new(Bytes::from(deflate::encode(&data)), 1024));
.insert_header((header::CONTENT_ENCODING, "deflate"))
.send_stream(TestBody::new(
Bytes::from(utils::deflate::encode(&data)),
1024,
));
let mut res = req.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
@ -931,14 +916,14 @@ async fn test_accept_encoding_no_match() {
let mut res = srv
.get("/")
.insert_header((ACCEPT_ENCODING, "xz, identity;q=0"))
.insert_header((header::ACCEPT_ENCODING, "xz, identity;q=0"))
.no_decompress()
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
assert_eq!(res.headers().get(CONTENT_ENCODING), None);
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None);
let bytes = res.body().await.unwrap();
// body should contain the supported encodings