1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00
actix-web/tests/test_server.rs

945 lines
27 KiB
Rust
Raw Normal View History

2021-02-07 04:54:58 +01:00
#[cfg(feature = "openssl")]
extern crate tls_openssl as openssl;
#[cfg(feature = "rustls")]
extern crate tls_rustls as rustls;
use std::{
future::Future,
io::{Read, Write},
pin::Pin,
task::{Context, Poll},
};
use actix_web::{
cookie::Cookie,
2022-01-03 19:46:04 +01:00
http::{header, StatusCode},
middleware::{Compress, NormalizePath, TrailingSlash},
web, App, Error, HttpResponse,
2019-03-02 07:51:32 +01:00
};
use bytes::Bytes;
2021-04-01 16:26:13 +02:00
use futures_core::ready;
2022-01-03 14:17:57 +01:00
use rand::{distributions::Alphanumeric, Rng as _};
2021-02-27 21:58:44 +01:00
#[cfg(feature = "openssl")]
use openssl::{
pkey::PKey,
ssl::{SslAcceptor, SslMethod},
x509::X509,
};
2022-01-03 14:17:57 +01:00
2022-01-03 19:46:04 +01:00
mod utils;
const S: &str = "Hello World ";
const STR: &str = const_str::repeat!(S, 100);
2018-01-11 06:47:30 +01:00
2021-02-27 21:58:44 +01:00
#[cfg(feature = "openssl")]
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 08:50:07 +01:00
struct TestBody {
data: Bytes,
chunk_size: usize,
delay: Pin<Box<actix_rt::time::Sleep>>,
2019-12-20 08:50:07 +01:00
}
impl TestBody {
fn new(data: Bytes, chunk_size: usize) -> Self {
TestBody {
data,
chunk_size,
delay: Box::pin(actix_rt::time::sleep(std::time::Duration::from_millis(10))),
2019-12-20 08:50:07 +01:00
}
}
}
2020-05-18 04:47:20 +02:00
impl futures_core::stream::Stream for TestBody {
2019-12-20 08:50:07 +01:00
type Item = Result<Bytes, Error>;
2021-02-12 00:03:17 +01:00
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
2019-12-20 08:50:07 +01:00
ready!(Pin::new(&mut self.delay).poll(cx));
2021-02-12 00:03:17 +01:00
self.delay = Box::pin(actix_rt::time::sleep(std::time::Duration::from_millis(10)));
2019-12-20 08:50:07 +01: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 06:25:50 +01:00
#[actix_rt::test]
async fn test_body() {
let srv = actix_test::start(|| {
App::new().service(
web::resource("/").route(web::to(|| async { HttpResponse::Ok().body(STR) })),
)
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv.get("/").send().await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-11 07:42:26 +01:00
}
2022-01-03 19:46:04 +01:00
// 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);
// 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);
// let bytes = res.body().await.unwrap();
// assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
// srv.stop().await;
// }
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
2022-01-03 14:17:57 +01:00
async fn body_gzip_large() {
2019-11-26 06:25:50 +01:00
let data = STR.repeat(10);
let srv_data = data.clone();
let srv = actix_test::start_with(actix_test::config().h1(), move || {
2019-11-26 06:25:50 +01:00
let data = srv_data.clone();
2022-01-03 14:17:57 +01:00
App::new()
.wrap(Compress::default())
.service(web::resource("/").route(web::to(move || {
let data = data.clone();
async move { HttpResponse::Ok().body(data.clone()) }
})))
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-11-26 06:25:50 +01:00
.get("/")
.no_decompress()
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "gzip"))
2019-11-26 06:25:50 +01:00
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::gzip::decode(bytes), data.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_body_gzip_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(70_000)
2020-12-23 00:45:31 +01:00
.map(char::from)
2019-11-26 06:25:50 +01:00
.collect::<String>();
let srv_data = data.clone();
let srv = actix_test::start_with(actix_test::config().h1(), move || {
2019-11-26 06:25:50 +01:00
let data = srv_data.clone();
App::new()
.wrap(Compress::default())
.service(web::resource("/").route(web::to(move || {
let data = data.clone();
async move { HttpResponse::Ok().body(data.clone()) }
})))
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-11-26 06:25:50 +01:00
.get("/")
.no_decompress()
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "gzip"))
2019-11-26 06:25:50 +01:00
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::gzip::decode(bytes), data.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_body_chunked_implicit() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
2019-12-12 17:28:47 +01:00
App::new()
2022-01-03 14:17:57 +01:00
.wrap(Compress::default())
.service(web::resource("/").route(web::get().to(|| async {
2019-12-12 17:28:47 +01:00
HttpResponse::Ok()
2019-12-20 08:50:07 +01:00
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
2019-12-12 17:28:47 +01:00
})))
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-11-26 06:25:50 +01:00
.get("/")
.no_decompress()
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "gzip"))
2019-11-26 06:25:50 +01:00
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 19:46:04 +01:00
assert_eq!(
res.headers().get(header::TRANSFER_ENCODING).unwrap(),
"chunked"
);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::gzip::decode(bytes), STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-12 00:26:46 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_body_br_streaming() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
2021-02-12 00:03:17 +01:00
App::new()
2022-01-03 14:17:57 +01:00
.wrap(Compress::default())
.service(web::resource("/").route(web::to(|| async {
2019-12-12 17:28:47 +01:00
HttpResponse::Ok()
2019-12-20 08:50:07 +01:00
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
2021-02-12 00:03:17 +01:00
})))
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-11-26 06:25:50 +01:00
.get("/")
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "br"))
2019-11-26 06:25:50 +01:00
.no_decompress()
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::brotli::decode(bytes), STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-12 01:22:27 +01:00
}
2018-01-21 01:12:38 +01:00
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_head_binary() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
2020-05-21 10:56:53 +02:00
App::new().service(
web::resource("/")
.route(web::head().to(move || async { HttpResponse::Ok().body(STR) })),
2020-05-21 10:56:53 +02:00
)
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv.head("/").send().await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let len = res.headers().get(header::CONTENT_LENGTH).unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert!(bytes.is_empty());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-21 01:12:38 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_no_chunking() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move || async {
2019-12-12 17:28:47 +01:00
HttpResponse::Ok()
.no_chunking(STR.len() as u64)
2019-12-20 08:50:07 +01:00
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
2019-12-12 17:28:47 +01:00
})))
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv.get("/").send().await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 19:46:04 +01:00
assert!(!res.headers().contains_key(header::TRANSFER_ENCODING));
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-12 01:22:27 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_body_deflate() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().wrap(Compress::default()).service(
web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })),
)
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-11-26 06:25:50 +01:00
.get("/")
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "deflate"))
2019-11-26 06:25:50 +01:00
.no_decompress()
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::deflate::decode(bytes), STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-11 06:47:30 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_body_brotli() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().wrap(Compress::default()).service(
web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })),
)
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-11-26 06:25:50 +01:00
.get("/")
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "br"))
2019-11-26 06:25:50 +01:00
.no_decompress()
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::brotli::decode(bytes), STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2018-01-11 06:47:30 +01:00
}
#[actix_rt::test]
async fn test_body_zstd() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().wrap(Compress::default()).service(
web::resource("/").route(web::to(move || async { HttpResponse::Ok().body(STR) })),
)
});
2022-01-03 14:17:57 +01:00
let mut res = srv
.get("/")
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "zstd"))
.no_decompress()
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::zstd::decode(bytes), STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
#[actix_rt::test]
async fn test_body_zstd_streaming() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new()
2022-01-03 14:17:57 +01:00
.wrap(Compress::default())
.service(web::resource("/").route(web::to(move || async {
HttpResponse::Ok()
.streaming(TestBody::new(Bytes::from_static(STR.as_ref()), 24))
})))
});
2022-01-03 14:17:57 +01:00
let mut res = srv
.get("/")
2022-01-03 19:46:04 +01:00
.append_header((header::ACCEPT_ENCODING, "zstd"))
.no_decompress()
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2022-01-03 19:46:04 +01:00
assert_eq!(utils::zstd::decode(bytes), STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
#[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| async {
HttpResponse::Ok().body(body)
})))
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "zstd"))
.send_body(utils::zstd::encode(STR));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
#[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| async {
HttpResponse::Ok().streaming(TestBody::new(body, 10240))
})),
)
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "zstd"))
.send_body(utils::zstd::encode(&data));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 14:17:57 +01:00
let bytes = res.body().limit(320_000).await.unwrap();
assert_eq!(bytes, data.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new()
.wrap(Compress::default())
.service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.insert_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(STR));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_gzip_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(STR));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
assert_eq!(bytes, STR.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-03-02 07:51:32 +01:00
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_gzip_encoding_large() {
let data = STR.repeat(10);
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
2022-01-03 14:17:57 +01:00
let req = srv
2019-11-26 06:25:50 +01:00
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(&data));
2022-01-03 14:17:57 +01:00
let mut res = req.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
assert_eq!(bytes, data);
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-03-02 07:51:32 +01:00
2019-11-26 06:25:50 +01: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-23 00:45:31 +01:00
.map(char::from)
2019-11-26 06:25:50 +01:00
.collect::<String>();
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "gzip"))
.send_body(utils::gzip::encode(&data));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
assert_eq!(bytes, data.as_bytes());
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-03-02 07:51:32 +01:00
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_reading_deflate_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "deflate"))
.send_body(utils::deflate::encode(STR));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-03-02 07:51:32 +01:00
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_reading_deflate_encoding_large() {
let data = STR.repeat(10);
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "deflate"))
.send_body(utils::deflate::encode(&data));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from(data));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-11-26 06:25:50 +01: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-23 00:45:31 +01:00
.map(char::from)
2019-11-26 06:25:50 +01:00
.collect::<String>();
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "deflate"))
.send_body(utils::deflate::encode(&data));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_brotli_encoding() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new().service(web::resource("/").route(web::to(move |body: Bytes| async {
HttpResponse::Ok().body(body)
})))
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "br"))
.send_body(utils::brotli::encode(STR));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_brotli_encoding_large() {
2019-12-20 08:50:07 +01:00
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(320_000)
2020-12-23 00:45:31 +01:00
.map(char::from)
2019-12-20 08:50:07 +01:00
.collect::<String>();
let srv = actix_test::start_with(actix_test::config().h1(), || {
2019-12-12 17:28:47 +01:00
App::new().service(
web::resource("/")
.app_data(web::PayloadConfig::new(320_000))
.route(web::to(move |body: Bytes| async {
2019-12-20 08:50:07 +01:00
HttpResponse::Ok().streaming(TestBody::new(body, 10240))
})),
2019-12-12 17:28:47 +01:00
)
2019-11-26 06:25:50 +01:00
});
let request = srv
.post("/")
2022-01-03 19:46:04 +01:00
.append_header((header::CONTENT_ENCODING, "br"))
.send_body(utils::brotli::encode(&data));
2022-01-03 14:17:57 +01:00
let mut res = request.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2019-11-26 06:25:50 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().limit(320_000).await.unwrap();
2019-11-26 06:25:50 +01:00
assert_eq!(bytes, Bytes::from(data));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-03-26 23:14:32 +01:00
}
2019-03-02 07:51:32 +01:00
2019-12-12 17:28:47 +01:00
#[cfg(feature = "openssl")]
#[actix_rt::test]
async fn test_brotli_encoding_large_openssl() {
2022-01-03 14:17:57 +01:00
use actix_web::http::header;
2019-12-12 17:28:47 +01:00
let data = STR.repeat(10);
let srv =
actix_test::start_with(actix_test::config().openssl(openssl_config()), move || {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| async {
2022-01-03 19:46:04 +01:00
// echo decompressed request body back in response
HttpResponse::Ok()
2022-01-03 19:46:04 +01:00
.insert_header(header::ContentEncoding::Identity)
.body(bytes)
})))
});
2019-03-02 07:51:32 +01:00
2022-01-03 14:17:57 +01:00
let mut res = srv
2019-12-12 17:28:47 +01:00
.post("/")
2022-01-03 14:17:57 +01:00
.append_header((header::CONTENT_ENCODING, "br"))
2022-01-03 19:46:04 +01:00
.send_body(utils::brotli::encode(&data))
2019-12-12 17:28:47 +01:00
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::OK);
2019-03-02 07:51:32 +01:00
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
2019-12-12 17:28:47 +01:00
assert_eq!(bytes, Bytes::from(data));
2021-11-15 05:03:33 +01:00
srv.stop().await;
2019-12-12 17:28:47 +01:00
}
2019-03-02 07:51:32 +01:00
2021-06-23 21:47:17 +02:00
#[cfg(feature = "rustls")]
mod plus_rustls {
2019-11-26 06:25:50 +01:00
use std::io::BufReader;
use rustls::{Certificate, PrivateKey, ServerConfig as RustlsServerConfig};
use rustls_pemfile::{certs, pkcs8_private_keys};
2019-11-26 06:25:50 +01:00
use super::*;
2019-12-12 17:28:47 +01:00
fn tls_config() -> RustlsServerConfig {
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 06:25:50 +01:00
let cert_file = &mut BufReader::new(cert_file.as_bytes());
let key_file = &mut BufReader::new(key_file.as_bytes());
2019-11-26 06:25:50 +01:00
let cert_chain = certs(cert_file)
.unwrap()
.into_iter()
.map(Certificate)
.collect();
let mut keys = pkcs8_private_keys(key_file).unwrap();
2019-11-26 06:25:50 +01:00
RustlsServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(cert_chain, PrivateKey(keys.remove(0)))
.unwrap()
}
2019-11-26 06:25:50 +01: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>();
let srv = actix_test::start_with(actix_test::config().rustls(tls_config()), || {
App::new().service(web::resource("/").route(web::to(|bytes: Bytes| async {
2022-01-03 19:46:04 +01:00
// echo decompressed request body back in response
HttpResponse::Ok()
2022-01-03 19:46:04 +01:00
.insert_header(header::ContentEncoding::Identity)
.body(bytes)
})))
});
let req = srv
.post("/")
2022-01-03 19:46:04 +01:00
.insert_header((header::CONTENT_ENCODING, "deflate"))
.send_stream(TestBody::new(
Bytes::from(utils::deflate::encode(&data)),
1024,
));
2022-01-03 14:17:57 +01:00
let mut res = req.await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
assert_eq!(bytes.len(), data.len());
assert_eq!(bytes, Bytes::from(data));
2021-11-15 05:03:33 +01:00
srv.stop().await;
}
2019-03-30 00:28:19 +01:00
}
2019-03-02 07:51:32 +01:00
2021-01-17 06:19:32 +01:00
#[actix_rt::test]
async fn test_server_cookies() {
use actix_web::http;
2021-01-17 06:19:32 +01:00
let srv = actix_test::start(|| {
App::new().default_service(web::to(|| async {
2021-01-17 06:19:32 +01:00
HttpResponse::Ok()
.cookie(
Cookie::build("first", "first_value")
2021-01-17 06:19:32 +01:00
.http_only(true)
.finish(),
)
.cookie(Cookie::new("second", "first_value"))
.cookie(Cookie::new("second", "second_value"))
2021-01-17 06:19:32 +01:00
.finish()
}))
});
2021-02-13 16:08:43 +01:00
let req = srv.get("/");
let res = req.send().await.unwrap();
assert!(res.status().is_success());
let first_cookie = Cookie::build("first", "first_value")
2021-01-17 06:19:32 +01:00
.http_only(true)
.finish();
let second_cookie = Cookie::new("second", "first_value");
2021-01-17 06:19:32 +01:00
let cookies = res.cookies().expect("To have cookies");
assert_eq!(cookies.len(), 3);
2021-01-17 06:19:32 +01:00
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(), 3);
2021-01-17 06:19:32 +01:00
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 05:03:33 +01:00
srv.stop().await;
2021-01-17 06:19:32 +01:00
}
2019-03-02 07:51:32 +01:00
2019-12-12 17:28:47 +01:00
#[actix_rt::test]
async fn test_slow_request() {
use std::net;
2019-03-02 07:51:32 +01:00
let srv = actix_test::start_with(actix_test::config().client_timeout(200), || {
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok)))
2019-12-12 17:28:47 +01:00
});
2019-03-02 07:51:32 +01:00
2019-12-12 17:28:47 +01: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 07:51:32 +01:00
2019-12-12 17:28:47 +01: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 05:03:33 +01:00
srv.stop().await;
2019-12-12 17:28:47 +01:00
}
2019-03-02 07:51:32 +01:00
#[actix_rt::test]
async fn test_normalize() {
let srv = actix_test::start_with(actix_test::config().h1(), || {
App::new()
.wrap(NormalizePath::new(TrailingSlash::Trim))
2021-05-15 02:13:33 +02:00
.service(web::resource("/one").route(web::to(HttpResponse::Ok)))
});
2022-01-03 14:17:57 +01:00
let res = srv.get("/one/").send().await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
2021-11-15 05:03:33 +01:00
srv.stop().await
}
// allow deprecated App::data
#[allow(deprecated)]
#[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);
}
#[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(HttpResponse::Ok)))
});
2022-01-03 14:17:57 +01:00
let mut res = srv
.get("/")
2022-01-03 19:46:04 +01:00
.insert_header((header::ACCEPT_ENCODING, "xz, identity;q=0"))
.no_decompress()
.send()
.await
.unwrap();
2022-01-03 14:17:57 +01:00
assert_eq!(res.status(), StatusCode::NOT_ACCEPTABLE);
2022-01-03 19:46:04 +01:00
assert_eq!(res.headers().get(header::CONTENT_ENCODING), None);
2022-01-03 14:17:57 +01:00
let bytes = res.body().await.unwrap();
// body should contain the supported encodings
assert!(!bytes.is_empty());
2021-11-15 05:03:33 +01:00
srv.stop().await;
}