2021-02-07 03:54:58 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
extern crate tls_openssl as openssl;
|
|
|
|
#[cfg(feature = "rustls")]
|
|
|
|
extern crate tls_rustls as rustls;
|
|
|
|
|
2021-01-05 09:51:58 +00:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
io::{Read, Write},
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2018-03-21 15:03:21 +00:00
|
|
|
|
2021-12-05 14:37:20 +00:00
|
|
|
use actix_http::header::{
|
2021-02-11 23:03:17 +00:00
|
|
|
ContentEncoding, ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_LENGTH, TRANSFER_ENCODING,
|
2019-03-02 06:51:32 +00:00
|
|
|
};
|
2019-12-20 07:50:07 +00:00
|
|
|
use brotli2::write::{BrotliDecoder, BrotliEncoder};
|
2019-03-02 06:51:32 +00:00
|
|
|
use bytes::Bytes;
|
2021-04-09 17:07:10 +00:00
|
|
|
use cookie::{Cookie, CookieBuilder};
|
2021-01-05 09:51:58 +00:00
|
|
|
use flate2::{
|
|
|
|
read::GzDecoder,
|
|
|
|
write::{GzEncoder, ZlibDecoder, ZlibEncoder},
|
|
|
|
Compression,
|
|
|
|
};
|
2021-04-01 14:26:13 +00:00
|
|
|
use futures_core::ready;
|
2021-02-27 20:58:44 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
2021-02-13 17:16:36 +00:00
|
|
|
use openssl::{
|
|
|
|
pkey::PKey,
|
|
|
|
ssl::{SslAcceptor, SslMethod},
|
|
|
|
x509::X509,
|
|
|
|
};
|
2019-03-02 06:51:32 +00:00
|
|
|
use rand::{distributions::Alphanumeric, Rng};
|
2021-06-03 20:32:52 +00:00
|
|
|
use zstd::stream::{read::Decoder as ZstdDecoder, write::Encoder as ZstdEncoder};
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-18 03:30:14 +00:00
|
|
|
use actix_web::dev::BodyEncoding;
|
2021-01-05 09:51:58 +00:00
|
|
|
use actix_web::middleware::{Compress, NormalizePath, TrailingSlash};
|
2021-05-09 19:12:48 +00:00
|
|
|
use actix_web::{web, App, Error, HttpResponse};
|
2018-02-19 21:18:18 +00:00
|
|
|
|
2018-04-13 23:02:01 +00:00
|
|
|
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";
|
2018-01-11 05:47:30 +00:00
|
|
|
|
2021-02-27 20:58:44 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
2021-02-13 17:16:36 +00:00
|
|
|
fn openssl_config() -> SslAcceptor {
|
|
|
|
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
|
|
|
|
let cert_file = cert.serialize_pem().unwrap();
|
|
|
|
let key_file = cert.serialize_private_key_pem();
|
|
|
|
let cert = X509::from_pem(cert_file.as_bytes()).unwrap();
|
|
|
|
let key = PKey::private_key_from_pem(key_file.as_bytes()).unwrap();
|
|
|
|
|
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
|
|
|
builder.set_certificate(&cert).unwrap();
|
|
|
|
builder.set_private_key(&key).unwrap();
|
|
|
|
|
|
|
|
builder.set_alpn_select_callback(|_, protos| {
|
|
|
|
const H2: &[u8] = b"\x02h2";
|
|
|
|
if protos.windows(3).any(|window| window == H2) {
|
|
|
|
Ok(b"h2")
|
|
|
|
} else {
|
|
|
|
Err(openssl::ssl::AlpnError::NOACK)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.set_alpn_protos(b"\x02h2").unwrap();
|
|
|
|
|
|
|
|
builder.build()
|
|
|
|
}
|
|
|
|
|
2019-12-20 07:50:07 +00:00
|
|
|
struct TestBody {
|
|
|
|
data: Bytes,
|
|
|
|
chunk_size: usize,
|
2021-01-03 23:47:04 +00:00
|
|
|
delay: Pin<Box<actix_rt::time::Sleep>>,
|
2019-12-20 07:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestBody {
|
|
|
|
fn new(data: Bytes, chunk_size: usize) -> Self {
|
|
|
|
TestBody {
|
|
|
|
data,
|
|
|
|
chunk_size,
|
2021-01-03 23:47:04 +00:00
|
|
|
delay: Box::pin(actix_rt::time::sleep(std::time::Duration::from_millis(10))),
|
2019-12-20 07:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 02:47:20 +00:00
|
|
|
impl futures_core::stream::Stream for TestBody {
|
2019-12-20 07:50:07 +00:00
|
|
|
type Item = Result<Bytes, Error>;
|
|
|
|
|
2021-02-11 23:03:17 +00:00
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
2019-12-20 07:50:07 +00:00
|
|
|
ready!(Pin::new(&mut self.delay).poll(cx));
|
|
|
|
|
2021-02-11 23:03:17 +00:00
|
|
|
self.delay = Box::pin(actix_rt::time::sleep(std::time::Duration::from_millis(10)));
|
2019-12-20 07:50:07 +00:00
|
|
|
let chunk_size = std::cmp::min(self.chunk_size, self.data.len());
|
|
|
|
let chunk = self.data.split_to(chunk_size);
|
|
|
|
if chunk.is_empty() {
|
|
|
|
Poll::Ready(None)
|
|
|
|
} else {
|
|
|
|
Poll::Ready(Some(Ok(chunk)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-02-11 23:03:17 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-11 06:42:26 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_gzip() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
|
|
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-04-18 00:48:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_gzip2() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
2021-05-09 19:12:48 +00:00
|
|
|
.service(web::resource("/").route(web::to(|| HttpResponse::Ok().body(STR))))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-11 05:47:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_encoding_override() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
|
|
|
.service(web::resource("/").route(web::to(|| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.encoding(ContentEncoding::Deflate)
|
|
|
|
.body(STR)
|
|
|
|
})))
|
|
|
|
.service(web::resource("/raw").route(web::to(|| {
|
|
|
|
let mut response =
|
2021-12-04 19:40:47 +00:00
|
|
|
HttpResponse::with_body(actix_web::http::StatusCode::OK, STR);
|
2019-12-12 16:28:47 +00:00
|
|
|
response.encoding(ContentEncoding::Deflate);
|
2021-12-04 19:40:47 +00:00
|
|
|
response.map_into_boxed_body()
|
2019-12-12 16:28:47 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Builder
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "deflate"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
let mut response = srv
|
|
|
|
.request(actix_web::http::Method::GET, srv.url("/raw"))
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "deflate"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
|
|
|
|
// 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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-27 18:29:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_gzip_large() {
|
|
|
|
let data = STR.repeat(10);
|
|
|
|
let srv_data = data.clone();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = srv_data.clone();
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
|
|
|
.service(
|
|
|
|
web::resource("/")
|
|
|
|
.route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.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(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-03-04 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_gzip_large_random() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(70_000)
|
2020-12-22 23:45:31 +00:00
|
|
|
.map(char::from)
|
2019-11-26 05:25:50 +00:00
|
|
|
.collect::<String>();
|
|
|
|
let srv_data = data.clone();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = srv_data.clone();
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
|
|
|
.service(
|
|
|
|
web::resource("/")
|
|
|
|
.route(web::to(move || HttpResponse::Ok().body(data.clone()))),
|
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.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!(dec.len(), data.len());
|
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-03-06 13:26:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_chunked_implicit() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Gzip))
|
|
|
|
.service(web::resource("/").route(web::get().to(move || {
|
|
|
|
HttpResponse::Ok()
|
2019-12-20 07:50:07 +00:00
|
|
|
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
2019-12-12 16:28:47 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.no_decompress()
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(
|
|
|
|
response.headers().get(TRANSFER_ENCODING).unwrap(),
|
|
|
|
&b"chunked"[..]
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-11 23:26:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_br_streaming() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2021-02-11 23:03:17 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Br))
|
|
|
|
.service(web::resource("/").route(web::to(move || {
|
2019-12-12 16:28:47 +00:00
|
|
|
HttpResponse::Ok()
|
2019-12-20 07:50:07 +00:00
|
|
|
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
2021-02-11 23:03:17 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "br"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
println!("TEST: {:?}", bytes.len());
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
// decode br
|
2019-12-20 07:50:07 +00:00
|
|
|
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
2019-11-26 05:25:50 +00:00
|
|
|
e.write_all(bytes.as_ref()).unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
let dec = e.finish().unwrap();
|
|
|
|
println!("T: {:?}", Bytes::copy_from_slice(&dec));
|
2019-11-26 05:25:50 +00:00
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-12 00:22:27 +00:00
|
|
|
}
|
2018-01-21 00:12:38 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_head_binary() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2020-05-21 08:56:53 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::head().to(move || HttpResponse::Ok().body(STR))),
|
2020-05-21 08:56:53 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv.head("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response.headers().get(CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-21 00:12:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_no_chunking() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(move || {
|
|
|
|
HttpResponse::Ok()
|
2020-05-18 22:46:31 +00:00
|
|
|
.no_chunking(STR.len() as u64)
|
2019-12-20 07:50:07 +00:00
|
|
|
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
2019-12-12 16:28:47 +00:00
|
|
|
})))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut response = srv.get("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert!(!response.headers().contains_key(TRANSFER_ENCODING));
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-12 00:22:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_deflate() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Deflate))
|
2021-02-11 23:03:17 +00:00
|
|
|
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "deflate"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
|
|
|
|
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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-11 05:47:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_brotli() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2021-02-11 23:03:17 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Br))
|
|
|
|
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((ACCEPT_ENCODING, "br"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
|
|
|
|
// decode brotli
|
2019-12-20 07:50:07 +00:00
|
|
|
let mut e = BrotliDecoder::new(Vec::with_capacity(2048));
|
2019-11-26 05:25:50 +00:00
|
|
|
e.write_all(bytes.as_ref()).unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
let dec = e.finish().unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
assert_eq!(Bytes::from(dec), Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2018-01-11 05:47:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:32:52 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_zstd() {
|
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Zstd))
|
|
|
|
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().body(STR))))
|
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.append_header((ACCEPT_ENCODING, "zstd"))
|
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
|
|
|
|
// 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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-06-03 20:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_body_zstd_streaming() {
|
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
|
|
|
App::new()
|
|
|
|
.wrap(Compress::new(ContentEncoding::Zstd))
|
|
|
|
.service(web::resource("/").route(web::to(move || {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
|
|
|
|
})))
|
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let mut response = srv
|
|
|
|
.get("/")
|
|
|
|
.append_header((ACCEPT_ENCODING, "zstd"))
|
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
|
|
|
|
// 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()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-06-03 20:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_zstd_encoding() {
|
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
.post("/")
|
|
|
|
.append_header((CONTENT_ENCODING, "zstd"))
|
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-06-03 20:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_zstd_encoding_large() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(320_000)
|
|
|
|
.map(char::from)
|
|
|
|
.collect::<String>();
|
|
|
|
|
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
|
|
|
App::new().service(
|
|
|
|
web::resource("/")
|
|
|
|
.app_data(web::PayloadConfig::new(320_000))
|
|
|
|
.route(web::to(move |body: Bytes| {
|
|
|
|
HttpResponse::Ok().streaming(TestBody::new(body, 10240))
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
.post("/")
|
|
|
|
.append_header((CONTENT_ENCODING, "zstd"))
|
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().limit(320_000).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-06-03 20:32:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().wrap(Compress::default()).service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((CONTENT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-28 19:32:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_gzip_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_gzip_encoding_large() {
|
|
|
|
let data = STR.repeat(10);
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_reading_gzip_encoding_large_random() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(60_000)
|
2020-12-22 23:45:31 +00:00
|
|
|
.map(char::from)
|
2019-11-26 05:25:50 +00:00
|
|
|
.collect::<String>();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "gzip"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes.len(), data.len());
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_reading_deflate_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "deflate"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_reading_deflate_encoding_large() {
|
|
|
|
let data = STR.repeat(10);
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "deflate"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_reading_deflate_encoding_large_random() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(160_000)
|
2020-12-22 23:45:31 +00:00
|
|
|
.map(char::from)
|
2019-11-26 05:25:50 +00:00
|
|
|
.collect::<String>();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "deflate"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes.len(), data.len());
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_brotli_encoding() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
2021-02-11 23:03:17 +00:00
|
|
|
web::resource("/").route(web::to(move |body: Bytes| HttpResponse::Ok().body(body))),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
2019-12-20 07:50:07 +00:00
|
|
|
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
2019-11-26 05:25:50 +00:00
|
|
|
e.write_all(STR.as_ref()).unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
let enc = e.finish().unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
// client request
|
|
|
|
let request = srv
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "br"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_brotli_encoding_large() {
|
2019-12-20 07:50:07 +00:00
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(320_000)
|
2020-12-22 23:45:31 +00:00
|
|
|
.map(char::from)
|
2019-12-20 07:50:07 +00:00
|
|
|
.collect::<String>();
|
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2019-12-12 16:28:47 +00:00
|
|
|
App::new().service(
|
|
|
|
web::resource("/")
|
2019-12-20 11:13:09 +00:00
|
|
|
.app_data(web::PayloadConfig::new(320_000))
|
2019-12-20 07:50:07 +00:00
|
|
|
.route(web::to(move |body: Bytes| {
|
|
|
|
HttpResponse::Ok().streaming(TestBody::new(body, 10240))
|
|
|
|
})),
|
2019-12-12 16:28:47 +00:00
|
|
|
)
|
2019-11-26 05:25:50 +00:00
|
|
|
});
|
|
|
|
|
2019-12-20 07:50:07 +00:00
|
|
|
let mut e = BrotliEncoder::new(Vec::new(), 5);
|
2019-11-26 05:25:50 +00:00
|
|
|
e.write_all(data.as_ref()).unwrap();
|
2019-12-20 07:50:07 +00:00
|
|
|
let enc = e.finish().unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
// client request
|
|
|
|
let request = srv
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((CONTENT_ENCODING, "br"))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send_body(enc.clone());
|
|
|
|
let mut response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
2019-12-20 07:50:07 +00:00
|
|
|
let bytes = response.body().limit(320_000).await.unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-03-26 22:14:32 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
#[cfg(feature = "openssl")]
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_brotli_encoding_large_openssl() {
|
|
|
|
let data = STR.repeat(10);
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv =
|
|
|
|
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || {
|
|
|
|
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
|
|
|
HttpResponse::Ok()
|
2021-12-05 14:37:20 +00:00
|
|
|
.encoding(ContentEncoding::Identity)
|
2021-04-02 07:26:59 +00:00
|
|
|
.body(bytes)
|
|
|
|
})))
|
|
|
|
});
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
// body
|
2021-11-15 04:03:33 +00:00
|
|
|
let mut enc = BrotliEncoder::new(Vec::new(), 3);
|
|
|
|
enc.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = enc.finish().unwrap();
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
// client request
|
|
|
|
let mut response = srv
|
|
|
|
.post("/")
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((actix_web::http::header::CONTENT_ENCODING, "br"))
|
2019-12-12 16:28:47 +00:00
|
|
|
.send_body(enc)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-12-12 16:28:47 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2021-06-23 19:47:17 +00:00
|
|
|
#[cfg(feature = "rustls")]
|
2021-02-13 17:16:36 +00:00
|
|
|
mod plus_rustls {
|
2019-11-26 05:25:50 +00:00
|
|
|
use std::io::BufReader;
|
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
use rustls::{Certificate, PrivateKey, ServerConfig as RustlsServerConfig};
|
|
|
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2021-02-13 17:16:36 +00:00
|
|
|
use super::*;
|
2019-12-12 16:28:47 +00:00
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
fn tls_config() -> RustlsServerConfig {
|
2021-02-13 17:16:36 +00:00
|
|
|
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
|
|
|
|
let cert_file = cert.serialize_pem().unwrap();
|
|
|
|
let key_file = cert.serialize_private_key_pem();
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2021-02-13 17:16:36 +00:00
|
|
|
let cert_file = &mut BufReader::new(cert_file.as_bytes());
|
|
|
|
let key_file = &mut BufReader::new(key_file.as_bytes());
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
let cert_chain = certs(cert_file)
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(Certificate)
|
|
|
|
.collect();
|
2021-02-13 17:16:36 +00:00
|
|
|
let mut keys = pkcs8_private_keys(key_file).unwrap();
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
RustlsServerConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(cert_chain, PrivateKey(keys.remove(0)))
|
|
|
|
.unwrap()
|
2021-02-13 17:16:36 +00:00
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2021-02-13 17:16:36 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_reading_deflate_encoding_large_random_rustls() {
|
|
|
|
let data = rand::thread_rng()
|
|
|
|
.sample_iter(&Alphanumeric)
|
|
|
|
.take(160_000)
|
|
|
|
.map(char::from)
|
|
|
|
.collect::<String>();
|
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().rustls(tls_config()), || {
|
2021-02-13 17:16:36 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| {
|
|
|
|
HttpResponse::Ok()
|
2021-12-05 14:37:20 +00:00
|
|
|
.encoding(ContentEncoding::Identity)
|
2021-02-13 17:16:36 +00:00
|
|
|
.body(bytes)
|
|
|
|
})))
|
|
|
|
});
|
|
|
|
|
|
|
|
// encode data
|
|
|
|
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
|
|
|
|
e.write_all(data.as_ref()).unwrap();
|
|
|
|
let enc = e.finish().unwrap();
|
|
|
|
|
|
|
|
// client request
|
|
|
|
let req = srv
|
|
|
|
.post("/")
|
|
|
|
.insert_header((actix_web::http::header::CONTENT_ENCODING, "deflate"))
|
|
|
|
.send_stream(TestBody::new(Bytes::from(enc), 1024));
|
|
|
|
|
|
|
|
let mut response = req.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = response.body().await.unwrap();
|
|
|
|
assert_eq!(bytes.len(), data.len());
|
|
|
|
assert_eq!(bytes, Bytes::from(data));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-02-13 17:16:36 +00:00
|
|
|
}
|
2019-03-29 23:28:19 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2021-01-17 05:19:32 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_server_cookies() {
|
2021-04-09 17:07:10 +00:00
|
|
|
use actix_web::http;
|
2021-01-17 05:19:32 +00:00
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start(|| {
|
2021-01-17 05:19:32 +00:00
|
|
|
App::new().default_service(web::to(|| {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.cookie(
|
2021-04-09 17:07:10 +00:00
|
|
|
CookieBuilder::new("first", "first_value")
|
2021-01-17 05:19:32 +00:00
|
|
|
.http_only(true)
|
|
|
|
.finish(),
|
|
|
|
)
|
2021-04-09 17:07:10 +00:00
|
|
|
.cookie(Cookie::new("second", "first_value"))
|
|
|
|
.cookie(Cookie::new("second", "second_value"))
|
2021-01-17 05:19:32 +00:00
|
|
|
.finish()
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
|
2021-02-13 15:08:43 +00:00
|
|
|
let req = srv.get("/");
|
|
|
|
let res = req.send().await.unwrap();
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
2021-04-09 17:07:10 +00:00
|
|
|
let first_cookie = CookieBuilder::new("first", "first_value")
|
2021-01-17 05:19:32 +00:00
|
|
|
.http_only(true)
|
|
|
|
.finish();
|
2021-04-09 17:07:10 +00:00
|
|
|
let second_cookie = Cookie::new("second", "second_value");
|
2021-01-17 05:19:32 +00:00
|
|
|
|
|
|
|
let cookies = res.cookies().expect("To have cookies");
|
|
|
|
assert_eq!(cookies.len(), 2);
|
|
|
|
if cookies[0] == first_cookie {
|
|
|
|
assert_eq!(cookies[1], second_cookie);
|
|
|
|
} else {
|
|
|
|
assert_eq!(cookies[0], second_cookie);
|
|
|
|
assert_eq!(cookies[1], first_cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
let first_cookie = first_cookie.to_string();
|
|
|
|
let second_cookie = second_cookie.to_string();
|
|
|
|
// Check that we have exactly two instances of raw cookie headers
|
|
|
|
let cookies = res
|
|
|
|
.headers()
|
|
|
|
.get_all(http::header::SET_COOKIE)
|
|
|
|
.map(|header| header.to_str().expect("To str").to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(cookies.len(), 2);
|
|
|
|
if cookies[0] == first_cookie {
|
|
|
|
assert_eq!(cookies[1], second_cookie);
|
|
|
|
} else {
|
|
|
|
assert_eq!(cookies[0], second_cookie);
|
|
|
|
assert_eq!(cookies[1], first_cookie);
|
|
|
|
}
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-01-17 05:19:32 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_slow_request() {
|
|
|
|
use std::net;
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().client_timeout(200), || {
|
2020-07-21 23:28:33 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok)))
|
2019-12-12 16:28:47 +00:00
|
|
|
});
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
|
|
|
assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2019-12-12 16:28:47 +00:00
|
|
|
let mut stream = net::TcpStream::connect(srv.addr()).unwrap();
|
|
|
|
let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n");
|
|
|
|
let mut data = String::new();
|
|
|
|
let _ = stream.read_to_string(&mut data);
|
|
|
|
assert!(data.starts_with("HTTP/1.1 408 Request Timeout"));
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2019-12-12 16:28:47 +00:00
|
|
|
}
|
2019-03-02 06:51:32 +00:00
|
|
|
|
2020-09-12 23:55:39 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_normalize() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
2020-09-12 23:55:39 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(NormalizePath::new(TrailingSlash::Trim))
|
2021-05-15 00:13:33 +00:00
|
|
|
.service(web::resource("/one").route(web::to(HttpResponse::Ok)))
|
2020-09-12 23:55:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let response = srv.get("/one/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await
|
2020-09-12 23:55:39 +00:00
|
|
|
}
|
2021-04-02 07:26:59 +00:00
|
|
|
|
2021-06-22 14:50:58 +00:00
|
|
|
// allow deprecated App::data
|
|
|
|
#[allow(deprecated)]
|
2021-04-02 07:26:59 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_data_drop() {
|
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestData(Arc<AtomicUsize>);
|
|
|
|
|
|
|
|
impl TestData {
|
|
|
|
fn new(inner: Arc<AtomicUsize>) -> Self {
|
|
|
|
let _ = inner.fetch_add(1, Ordering::SeqCst);
|
|
|
|
Self(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for TestData {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let inner = self.0.clone();
|
|
|
|
let _ = inner.fetch_add(1, Ordering::SeqCst);
|
|
|
|
Self(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestData {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0.fetch_sub(1, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let data = TestData::new(num.clone());
|
|
|
|
assert_eq!(num.load(Ordering::SeqCst), 1);
|
|
|
|
|
|
|
|
let srv = actix_test::start(move || {
|
|
|
|
let data = data.clone();
|
|
|
|
|
|
|
|
App::new()
|
|
|
|
.data(data)
|
|
|
|
.service(web::resource("/").to(|_data: web::Data<TestData>| async { "ok" }))
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(srv.get("/").send().await.unwrap().status().is_success());
|
|
|
|
srv.stop().await;
|
|
|
|
|
|
|
|
assert_eq!(num.load(Ordering::SeqCst), 0);
|
|
|
|
}
|
2021-09-01 08:08:29 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_accept_encoding_no_match() {
|
|
|
|
let srv = actix_test::start_with(actix_test::config().h1(), || {
|
|
|
|
App::new()
|
|
|
|
.wrap(Compress::default())
|
|
|
|
.service(web::resource("/").route(web::to(move || HttpResponse::Ok().finish())))
|
|
|
|
});
|
|
|
|
|
|
|
|
let response = srv
|
|
|
|
.get("/")
|
|
|
|
.append_header((ACCEPT_ENCODING, "compress, identity;q=0"))
|
|
|
|
.no_decompress()
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(response.status().as_u16(), 406);
|
2021-11-15 04:03:33 +00:00
|
|
|
|
|
|
|
srv.stop().await;
|
2021-09-01 08:08:29 +00:00
|
|
|
}
|