simplify test_server tests

This commit is contained in:
Rob Ede 2022-01-02 08:07:13 +00:00
parent c65889649e
commit d981dda09b
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
6 changed files with 245 additions and 343 deletions

View File

@ -1,8 +1,6 @@
name: Benchmark name: Benchmark
on: on:
pull_request:
types: [opened, synchronize, reopened]
push: push:
branches: branches:
- master - master

View File

@ -299,7 +299,7 @@ impl ContentEncoder {
Some(ContentEncoder::Zstd(encoder)) Some(ContentEncoder::Zstd(encoder))
} }
_ => None, ContentEncoding::Identity => None,
} }
} }

View File

@ -248,6 +248,7 @@ impl PayloadConfig {
} }
} }
} }
Ok(()) Ok(())
} }

View File

@ -7,6 +7,9 @@ use actix_web::{
}; };
use bytes::Bytes; use bytes::Bytes;
mod test_utils;
use test_utils::{brotli, gzip, zstd};
static LOREM: &[u8] = include_bytes!("fixtures/lorem.txt"); static LOREM: &[u8] = include_bytes!("fixtures/lorem.txt");
static LOREM_GZIP: &[u8] = include_bytes!("fixtures/lorem.txt.gz"); static LOREM_GZIP: &[u8] = include_bytes!("fixtures/lorem.txt.gz");
static LOREM_BR: &[u8] = include_bytes!("fixtures/lorem.txt.br"); static LOREM_BR: &[u8] = include_bytes!("fixtures/lorem.txt.br");
@ -106,6 +109,16 @@ async fn negotiate_encoding_gzip() {
let bytes = res.body().await.unwrap(); let bytes = res.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(LOREM)); assert_eq!(bytes, Bytes::from_static(LOREM));
let mut res = srv
.post("/static")
.no_decompress()
.insert_header((header::ACCEPT_ENCODING, "gzip,br,zstd"))
.send()
.await
.unwrap();
let bytes = res.body().await.unwrap();
assert_eq!(gzip::decode(bytes), LOREM);
srv.stop().await; srv.stop().await;
} }
@ -125,6 +138,16 @@ async fn negotiate_encoding_br() {
let bytes = res.body().await.unwrap(); let bytes = res.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(LOREM)); assert_eq!(bytes, Bytes::from_static(LOREM));
let mut res = srv
.post("/static")
.no_decompress()
.insert_header((header::ACCEPT_ENCODING, "br,zstd,gzip"))
.send()
.await
.unwrap();
let bytes = res.body().await.unwrap();
assert_eq!(brotli::decode(bytes), LOREM);
srv.stop().await; srv.stop().await;
} }
@ -144,6 +167,16 @@ async fn negotiate_encoding_zstd() {
let bytes = res.body().await.unwrap(); let bytes = res.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(LOREM)); assert_eq!(bytes, Bytes::from_static(LOREM));
let mut res = srv
.post("/static")
.no_decompress()
.insert_header((header::ACCEPT_ENCODING, "zstd,gzip,br"))
.send()
.await
.unwrap();
let bytes = res.body().await.unwrap();
assert_eq!(zstd::decode(bytes), LOREM);
srv.stop().await; srv.stop().await;
} }
@ -261,7 +294,7 @@ async fn deny_identity_for_manual_coding() {
let srv = test_server!(); let srv = test_server!();
let req = srv let req = srv
.post("/static") .post("/static-xz")
// don't decompress response body // don't decompress response body
.no_decompress() .no_decompress()
// signal that we want a compressed body // signal that we want a compressed body

View File

@ -11,30 +11,28 @@ use std::{
}; };
use actix_web::{ use actix_web::{
cookie::{Cookie, CookieBuilder},
dev::BodyEncoding, dev::BodyEncoding,
http::header::{ http::{
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING, header::{self, ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, TRANSFER_ENCODING},
StatusCode,
}, },
middleware::{Compress, NormalizePath, TrailingSlash}, middleware::{Compress, NormalizePath, TrailingSlash},
web, App, Error, HttpResponse, web, App, Error, HttpResponse,
}; };
use brotli2::write::{BrotliDecoder, BrotliEncoder};
use bytes::Bytes; use bytes::Bytes;
use cookie::{Cookie, CookieBuilder};
use flate2::{
read::GzDecoder,
write::{GzEncoder, ZlibDecoder, ZlibEncoder},
Compression,
};
use futures_core::ready; use futures_core::ready;
use rand::{distributions::Alphanumeric, Rng as _};
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
use openssl::{ use openssl::{
pkey::PKey, pkey::PKey,
ssl::{SslAcceptor, SslMethod}, ssl::{SslAcceptor, SslMethod},
x509::X509, x509::X509,
}; };
use rand::{distributions::Alphanumeric, Rng};
use zstd::stream::{read::Decoder as ZstdDecoder, write::Encoder as ZstdEncoder}; mod test_utils;
use test_utils::{brotli, deflate, gzip, zstd};
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \ 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 \
@ -122,74 +120,15 @@ async fn test_body() {
App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR)))) App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
}); });
let mut response = srv.get("/").send().await.unwrap(); let mut res = srv.get("/").send().await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
#[actix_rt::test]
async fn test_body_gzip() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new()
.wrap(Compress::new(ContentEncoding::Gzip))
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
});
let mut response = srv
.get("/")
.no_decompress()
.append_header((ACCEPT_ENCODING, "gzip"))
.send()
.await
.unwrap();
assert!(response.status().is_success());
// read response
let bytes = response.body().await.unwrap();
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await;
}
#[actix_rt::test]
async fn test_body_gzip2() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new()
.wrap(Compress::new(ContentEncoding::Gzip))
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
});
let mut response = srv
.get("/")
.no_decompress()
.append_header((ACCEPT_ENCODING, "gzip"))
.send()
.await
.unwrap();
assert!(response.status().is_success());
// read response
let bytes = response.body().await.unwrap();
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await;
}
#[actix_rt::test] #[actix_rt::test]
async fn test_body_encoding_override() { async fn test_body_encoding_override() {
let srv = actix_test::start_with(actix_test::config().h1(), || { let srv = actix_test::start_with(actix_test::config().h1(), || {
@ -201,61 +140,49 @@ async fn test_body_encoding_override() {
.body(STR) .body(STR)
}))) })))
.service(web::resource("/raw").route(web::to(|| { .service(web::resource("/raw").route(web::to(|| {
let mut response = let mut res = HttpResponse::with_body(actix_web::http::StatusCode::OK, STR);
HttpResponse::with_body(actix_web::http::StatusCode::OK, STR); res.encoding(ContentEncoding::Deflate);
response.encoding(ContentEncoding::Deflate); res.map_into_boxed_body()
response.map_into_boxed_body()
}))) })))
}); });
// Builder // Builder
let mut response = srv let mut res = srv
.get("/") .get("/")
.no_decompress() .no_decompress()
.append_header((ACCEPT_ENCODING, "deflate")) .append_header((ACCEPT_ENCODING, "deflate"))
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(deflate::decode(bytes), STR.as_bytes());
// decode
let mut e = ZlibDecoder::new(Vec::new());
e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
// Raw Response // Raw Response
let mut response = srv let mut res = srv
.request(actix_web::http::Method::GET, srv.url("/raw")) .request(actix_web::http::Method::GET, srv.url("/raw"))
.no_decompress() .no_decompress()
.append_header((ACCEPT_ENCODING, "deflate")) .append_header((ACCEPT_ENCODING, "deflate"))
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(deflate::decode(bytes), STR.as_bytes());
// decode
let mut e = ZlibDecoder::new(Vec::new());
e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
#[actix_rt::test] #[actix_rt::test]
async fn test_body_gzip_large() { async fn body_gzip_large() {
let data = STR.repeat(10); let data = STR.repeat(10);
let srv_data = data.clone(); let srv_data = data.clone();
let srv = actix_test::start_with(actix_test::config().h1(), move || { let srv = actix_test::start_with(actix_test::config().h1(), move || {
let data = srv_data.clone(); let data = srv_data.clone();
App::new() App::new()
.wrap(Compress::new(ContentEncoding::Gzip)) .wrap(Compress::new(ContentEncoding::Gzip))
.service( .service(
@ -264,23 +191,17 @@ async fn test_body_gzip_large() {
) )
}); });
let mut response = srv let mut res = srv
.get("/") .get("/")
.no_decompress() .no_decompress()
.append_header((ACCEPT_ENCODING, "gzip")) .append_header((ACCEPT_ENCODING, "gzip"))
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(gzip::decode(bytes), data.as_bytes());
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from(data));
srv.stop().await; srv.stop().await;
} }
@ -304,24 +225,17 @@ async fn test_body_gzip_large_random() {
) )
}); });
let mut response = srv let mut res = srv
.get("/") .get("/")
.no_decompress() .no_decompress()
.append_header((ACCEPT_ENCODING, "gzip")) .append_header((ACCEPT_ENCODING, "gzip"))
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(gzip::decode(bytes), data.as_bytes());
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(dec.len(), data.len());
assert_eq!(Bytes::from(dec), Bytes::from(data));
srv.stop().await; srv.stop().await;
} }
@ -337,27 +251,18 @@ async fn test_body_chunked_implicit() {
}))) })))
}); });
let mut response = srv let mut res = srv
.get("/") .get("/")
.no_decompress() .no_decompress()
.append_header((ACCEPT_ENCODING, "gzip")) .append_header((ACCEPT_ENCODING, "gzip"))
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
assert_eq!( assert_eq!(res.headers().get(TRANSFER_ENCODING).unwrap(), "chunked");
response.headers().get(TRANSFER_ENCODING).unwrap(),
&b"chunked"[..]
);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(gzip::decode(bytes), STR.as_bytes());
// decode
let mut e = GzDecoder::new(&bytes[..]);
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -373,25 +278,17 @@ async fn test_body_br_streaming() {
}))) })))
}); });
let mut response = srv let mut res = srv
.get("/") .get("/")
.append_header((ACCEPT_ENCODING, "br")) .append_header((ACCEPT_ENCODING, "br"))
.no_decompress() .no_decompress()
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(brotli::decode(bytes), STR.as_bytes());
println!("TEST: {:?}", bytes.len());
// decode br
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap();
println!("T: {:?}", Bytes::copy_from_slice(&dec));
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -404,16 +301,13 @@ async fn test_head_binary() {
) )
}); });
let mut response = srv.head("/").send().await.unwrap(); let mut res = srv.head("/").send().await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
{ let len = res.headers().get(header::CONTENT_LENGTH).unwrap();
let len = response.headers().get(CONTENT_LENGTH).unwrap(); assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert!(bytes.is_empty()); assert!(bytes.is_empty());
srv.stop().await; srv.stop().await;
@ -429,12 +323,11 @@ async fn test_no_chunking() {
}))) })))
}); });
let mut response = srv.get("/").send().await.unwrap(); let mut res = srv.get("/").send().await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
assert!(!response.headers().contains_key(TRANSFER_ENCODING)); assert!(!res.headers().contains_key(TRANSFER_ENCODING));
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
@ -448,23 +341,17 @@ async fn test_body_deflate() {
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) .service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
}); });
// client request let mut res = srv
let mut response = srv
.get("/") .get("/")
.append_header((ACCEPT_ENCODING, "deflate")) .append_header((ACCEPT_ENCODING, "deflate"))
.no_decompress() .no_decompress()
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(deflate::decode(bytes), STR.as_bytes());
let mut e = ZlibDecoder::new(Vec::new());
e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -477,24 +364,17 @@ async fn test_body_brotli() {
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) .service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
}); });
// client request let mut res = srv
let mut response = srv
.get("/") .get("/")
.append_header((ACCEPT_ENCODING, "br")) .append_header((ACCEPT_ENCODING, "br"))
.no_decompress() .no_decompress()
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(brotli::decode(bytes), STR.as_bytes());
// decode brotli
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
e.write_all(bytes.as_ref()).unwrap();
let dec = e.finish().unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -507,24 +387,17 @@ async fn test_body_zstd() {
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR)))) .service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
}); });
// client request let mut res = srv
let mut response = srv
.get("/") .get("/")
.append_header((ACCEPT_ENCODING, "zstd")) .append_header((ACCEPT_ENCODING, "zstd"))
.no_decompress() .no_decompress()
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(zstd::decode(bytes), STR.as_bytes());
// decode
let mut e = ZstdDecoder::new(&bytes[..]).unwrap();
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -540,24 +413,17 @@ async fn test_body_zstd_streaming() {
}))) })))
}); });
// client request let mut res = srv
let mut response = srv
.get("/") .get("/")
.append_header((ACCEPT_ENCODING, "zstd")) .append_header((ACCEPT_ENCODING, "zstd"))
.no_decompress() .no_decompress()
.send() .send()
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(zstd::decode(bytes), STR.as_bytes());
// decode
let mut e = ZstdDecoder::new(&bytes[..]).unwrap();
let mut dec = Vec::new();
e.read_to_end(&mut dec).unwrap();
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -570,20 +436,14 @@ async fn test_zstd_encoding() {
) )
}); });
let mut e = ZstdEncoder::new(Vec::new(), 5).unwrap();
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "zstd")) .append_header((CONTENT_ENCODING, "zstd"))
.send_body(enc.clone()); .send_body(zstd::encode(STR));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
@ -607,21 +467,15 @@ async fn test_zstd_encoding_large() {
) )
}); });
let mut e = ZstdEncoder::new(Vec::new(), 5).unwrap();
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "zstd")) .append_header((CONTENT_ENCODING, "zstd"))
.send_body(enc.clone()); .send_body(zstd::encode(&data));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().limit(320_000).await.unwrap();
let bytes = response.body().limit(320_000).await.unwrap(); assert_eq!(bytes, data.as_bytes());
assert_eq!(bytes, Bytes::from(data));
srv.stop().await; srv.stop().await;
} }
@ -634,20 +488,14 @@ async fn test_encoding() {
) )
}); });
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv let request = srv
.post("/") .post("/")
.insert_header((CONTENT_ENCODING, "gzip")) .insert_header((CONTENT_ENCODING, "gzip"))
.send_body(enc.clone()); .send_body(gzip::encode(STR));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
@ -661,21 +509,15 @@ async fn test_gzip_encoding() {
) )
}); });
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "gzip")) .append_header((CONTENT_ENCODING, "gzip"))
.send_body(enc.clone()); .send_body(gzip::encode(STR));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(bytes, STR.as_bytes());
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
} }
@ -689,21 +531,15 @@ async fn test_gzip_encoding_large() {
) )
}); });
// client request let req = srv
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "gzip")) .append_header((CONTENT_ENCODING, "gzip"))
.send_body(enc.clone()); .send_body(gzip::encode(&data));
let mut response = request.await.unwrap(); let mut res = req.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(bytes, data);
assert_eq!(bytes, Bytes::from(data));
srv.stop().await; srv.stop().await;
} }
@ -722,22 +558,15 @@ async fn test_reading_gzip_encoding_large_random() {
) )
}); });
// client request
let mut e = GzEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "gzip")) .append_header((CONTENT_ENCODING, "gzip"))
.send_body(enc.clone()); .send_body(gzip::encode(&data));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap(); assert_eq!(bytes, data.as_bytes());
assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data));
srv.stop().await; srv.stop().await;
} }
@ -750,20 +579,14 @@ async fn test_reading_deflate_encoding() {
) )
}); });
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "deflate")) .append_header((CONTENT_ENCODING, "deflate"))
.send_body(enc.clone()); .send_body(deflate::encode(STR));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
@ -778,20 +601,14 @@ async fn test_reading_deflate_encoding_large() {
) )
}); });
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "deflate")) .append_header((CONTENT_ENCODING, "deflate"))
.send_body(enc.clone()); .send_body(deflate::encode(&data));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
srv.stop().await; srv.stop().await;
@ -811,20 +628,14 @@ async fn test_reading_deflate_encoding_large_random() {
) )
}); });
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "deflate")) .append_header((CONTENT_ENCODING, "deflate"))
.send_body(enc.clone()); .send_body(deflate::encode(&data));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes.len(), data.len()); assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
@ -839,20 +650,14 @@ async fn test_brotli_encoding() {
) )
}); });
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(STR.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "br")) .append_header((CONTENT_ENCODING, "br"))
.send_body(enc.clone()); .send_body(brotli::encode(STR));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
srv.stop().await; srv.stop().await;
@ -876,20 +681,14 @@ async fn test_brotli_encoding_large() {
) )
}); });
let mut e = BrotliEncoder::new(Vec::new(), 5);
e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap();
// client request
let request = srv let request = srv
.post("/") .post("/")
.append_header((CONTENT_ENCODING, "br")) .append_header((CONTENT_ENCODING, "br"))
.send_body(enc.clone()); .send_body(brotli::encode(&data));
let mut response = request.await.unwrap(); let mut res = request.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().limit(320_000).await.unwrap();
let bytes = response.body().limit(320_000).await.unwrap();
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
srv.stop().await; srv.stop().await;
@ -898,6 +697,8 @@ async fn test_brotli_encoding_large() {
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
#[actix_rt::test] #[actix_rt::test]
async fn test_brotli_encoding_large_openssl() { async fn test_brotli_encoding_large_openssl() {
use actix_web::http::header;
let data = STR.repeat(10); let data = STR.repeat(10);
let srv = let srv =
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || { actix_test::start_with(actix_test::config().openssl(openssl_config()), move || {
@ -908,22 +709,15 @@ async fn test_brotli_encoding_large_openssl() {
}))) })))
}); });
// body let mut res = srv
let mut enc = BrotliEncoder::new(Vec::new(), 3);
enc.write_all(data.as_ref()).unwrap();
let enc = enc.finish().unwrap();
// client request
let mut response = srv
.post("/") .post("/")
.append_header((actix_web::http::header::CONTENT_ENCODING, "br")) .append_header((header::CONTENT_ENCODING, "br"))
.send_body(enc) .send_body(brotli::encode(&data))
.await .await
.unwrap(); .unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
srv.stop().await; srv.stop().await;
@ -981,17 +775,15 @@ mod plus_rustls {
e.write_all(data.as_ref()).unwrap(); e.write_all(data.as_ref()).unwrap();
let enc = e.finish().unwrap(); let enc = e.finish().unwrap();
// client request
let req = srv let req = srv
.post("/") .post("/")
.insert_header((actix_web::http::header::CONTENT_ENCODING, "deflate")) .insert_header((actix_web::http::header::CONTENT_ENCODING, "deflate"))
.send_stream(TestBody::new(Bytes::from(enc), 1024)); .send_stream(TestBody::new(Bytes::from(enc), 1024));
let mut response = req.await.unwrap(); let mut res = req.await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
// read response let bytes = res.body().await.unwrap();
let bytes = response.body().await.unwrap();
assert_eq!(bytes.len(), data.len()); assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data)); assert_eq!(bytes, Bytes::from(data));
@ -1084,8 +876,8 @@ async fn test_normalize() {
.service(web::resource("/one").route(web::to(HttpResponse::Ok))) .service(web::resource("/one").route(web::to(HttpResponse::Ok)))
}); });
let response = srv.get("/one/").send().await.unwrap(); let res = srv.get("/one/").send().await.unwrap();
assert!(response.status().is_success()); assert_eq!(res.status(), StatusCode::OK);
srv.stop().await srv.stop().await
} }
@ -1148,15 +940,20 @@ async fn test_accept_encoding_no_match() {
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().finish()))) .service(web::resource("/").route(web::to(move || HttpResponse::Ok().finish())))
}); });
let response = srv let mut res = srv
.get("/") .get("/")
.append_header((ACCEPT_ENCODING, "compress, identity;q=0")) .insert_header((ACCEPT_ENCODING, "xz, identity;q=0"))
.no_decompress() .no_decompress()
.send() .send()
.await .await
.unwrap(); .unwrap();
assert_eq!(response.status().as_u16(), 406); assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
assert_eq!(res.headers().get(CONTENT_ENCODING), None);
let bytes = res.body().await.unwrap();
// body should contain the supported encodings
assert!(!bytes.is_empty());
srv.stop().await; srv.stop().await;
} }

73
tests/test_utils.rs Normal file
View File

@ -0,0 +1,73 @@
use std::io::{Read as _, Write as _};
pub mod gzip {
use super::*;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
encoder.write_all(bytes.as_ref()).unwrap();
encoder.finish().unwrap()
}
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut decoder = GzDecoder::new(bytes.as_ref());
let mut buf = Vec::new();
decoder.read_to_end(&mut buf).unwrap();
buf
}
}
pub mod deflate {
use super::*;
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::fast());
encoder.write_all(bytes.as_ref()).unwrap();
encoder.finish().unwrap()
}
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut decoder = ZlibDecoder::new(bytes.as_ref());
let mut buf = Vec::new();
decoder.read_to_end(&mut buf).unwrap();
buf
}
}
pub mod brotli {
use super::*;
use ::brotli2::{read::BrotliDecoder, write::BrotliEncoder};
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut encoder = BrotliEncoder::new(Vec::new(), 3);
encoder.write_all(bytes.as_ref()).unwrap();
encoder.finish().unwrap()
}
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut decoder = BrotliDecoder::new(bytes.as_ref());
let mut buf = Vec::new();
decoder.read_to_end(&mut buf).unwrap();
buf
}
}
pub mod zstd {
use super::*;
use ::zstd::stream::{read::Decoder, write::Encoder};
pub fn encode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut encoder = Encoder::new(Vec::new(), 3).unwrap();
encoder.write_all(bytes.as_ref()).unwrap();
encoder.finish().unwrap()
}
pub fn decode(bytes: impl AsRef<[u8]>) -> Vec<u8> {
let mut decoder = Decoder::new(bytes.as_ref()).unwrap();
let mut buf = Vec::new();
decoder.read_to_end(&mut buf).unwrap();
buf
}
}