1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-18 09:53:11 +01:00
actix-web/actix-http/tests/test_rustls.rs

441 lines
15 KiB
Rust
Raw Normal View History

2019-11-19 13:54:19 +01:00
#![cfg(feature = "rustls")]
2021-02-07 04:54:58 +01:00
extern crate tls_rustls as rustls;
2019-11-19 13:54:19 +01:00
use actix_http::error::PayloadError;
use actix_http::http::header::{self, HeaderName, HeaderValue};
use actix_http::http::{Method, StatusCode, Version};
use actix_http::{body, error, Error, HttpService, Request, Response};
2019-12-12 18:08:38 +01:00
use actix_http_test::test_server;
2019-12-08 14:25:24 +01:00
use actix_service::{fn_factory_with_config, fn_service};
2019-11-19 13:54:19 +01:00
use bytes::{Bytes, BytesMut};
2020-05-18 04:45:26 +02:00
use futures_util::future::{self, err, ok};
use futures_util::stream::{once, Stream, StreamExt};
2021-02-07 04:54:58 +01:00
use rustls::{
2019-11-19 13:54:19 +01:00
internal::pemfile::{certs, pkcs8_private_keys},
NoClientAuth, ServerConfig as RustlsServerConfig,
};
use std::fs::File;
use std::io::{self, BufReader};
async fn load_body<S>(mut stream: S) -> Result<BytesMut, PayloadError>
where
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin,
{
let mut body = BytesMut::new();
while let Some(item) = stream.next().await {
body.extend_from_slice(&item?)
}
Ok(body)
}
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-19 13:54:19 +01:00
let mut config = RustlsServerConfig::new(NoClientAuth::new());
let cert_file = &mut BufReader::new(cert_file.as_bytes());
let key_file = &mut BufReader::new(key_file.as_bytes());
2019-11-19 13:54:19 +01:00
let cert_chain = certs(cert_file).unwrap();
let mut keys = pkcs8_private_keys(key_file).unwrap();
config.set_single_cert(cert_chain, keys.remove(0)).unwrap();
2019-12-05 18:35:43 +01:00
config
}
#[actix_rt::test]
async fn test_h1() -> io::Result<()> {
2019-12-12 18:08:38 +01:00
let srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h1(|_| future::ok::<_, Error>(Response::Ok().finish()))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-19 13:54:19 +01:00
2019-12-05 18:35:43 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
Ok(())
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2() -> io::Result<()> {
2019-12-12 18:08:38 +01:00
let srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| future::ok::<_, Error>(Response::Ok().finish()))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-12-05 18:35:43 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
Ok(())
}
#[actix_rt::test]
async fn test_h1_1() -> io::Result<()> {
2019-12-12 18:08:38 +01:00
let srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h1(|req: Request| {
assert!(req.peer_addr().is_some());
assert_eq!(req.version(), Version::HTTP_11);
future::ok::<_, Error>(Response::Ok().finish())
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
Ok(())
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_1() -> io::Result<()> {
2019-12-12 18:08:38 +01:00
let srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.finish(|req: Request| {
assert!(req.peer_addr().is_some());
assert_eq!(req.version(), Version::HTTP_2);
future::ok::<_, Error>(Response::Ok().finish())
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
Ok(())
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_body1() -> io::Result<()> {
let data = "HELLOWORLD".to_owned().repeat(64 * 1024);
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
2020-02-27 03:10:55 +01:00
.h2(|mut req: Request<_>| async move {
let body = load_body(req.take_payload()).await?;
Ok::<_, Error>(Response::Ok().body(body))
2019-12-05 18:35:43 +01:00
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send_body(data.clone()).await.unwrap();
assert!(response.status().is_success());
let body = srv.load_body(response).await.unwrap();
assert_eq!(&body, data.as_bytes());
Ok(())
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_content_length() {
2019-12-12 18:08:38 +01:00
let srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|req: Request| {
let indx: usize = req.uri().path()[1..].parse().unwrap();
let statuses = [
StatusCode::NO_CONTENT,
StatusCode::CONTINUE,
StatusCode::SWITCHING_PROTOCOLS,
StatusCode::PROCESSING,
StatusCode::OK,
StatusCode::NOT_FOUND,
];
future::ok::<_, ()>(Response::new(statuses[indx]))
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let header = HeaderName::from_static("content-length");
let value = HeaderValue::from_static("0");
{
for i in 0..4 {
let req = srv
.request(Method::GET, srv.surl(&format!("/{}", i)))
.send();
let response = req.await.unwrap();
assert_eq!(response.headers().get(&header), None);
let req = srv
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
.send();
let response = req.await.unwrap();
assert_eq!(response.headers().get(&header), None);
}
for i in 4..6 {
let req = srv
.request(Method::GET, srv.surl(&format!("/{}", i)))
.send();
let response = req.await.unwrap();
assert_eq!(response.headers().get(&header), Some(&value));
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
}
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_headers() {
let data = STR.repeat(10);
let data2 = data.clone();
2019-11-19 13:54:19 +01:00
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-11-26 06:25:50 +01:00
let data = data.clone();
2019-11-19 13:54:19 +01:00
HttpService::build().h2(move |_| {
let mut config = Response::Ok();
for idx in 0..90 {
2021-01-15 03:11:10 +01:00
config.insert_header((
2019-11-19 13:54:19 +01:00
format!("X-TEST-{}", idx).as_str(),
"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST \
TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ",
2021-01-15 03:11:10 +01:00
));
2019-11-19 13:54:19 +01:00
}
future::ok::<_, ()>(config.body(data.clone()))
2019-12-05 18:35:43 +01:00
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
}).await;
2019-11-19 13:54:19 +01:00
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
2019-11-19 13:54:19 +01:00
2019-11-26 06:25:50 +01:00
// read response
let bytes = srv.load_body(response).await.unwrap();
assert_eq!(bytes, Bytes::from(data2));
2019-11-19 13:54:19 +01: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";
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_body2() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| future::ok::<_, ()>(Response::Ok().body(STR)))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.load_body(response).await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_head_empty() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.finish(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
assert_eq!(response.version(), Version::HTTP_2);
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
2019-11-19 13:54:19 +01:00
2019-11-26 06:25:50 +01:00
// read response
let bytes = srv.load_body(response).await.unwrap();
assert!(bytes.is_empty());
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_head_binary() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
2019-11-19 13:54:19 +01:00
2019-11-26 06:25:50 +01:00
// read response
let bytes = srv.load_body(response).await.unwrap();
assert!(bytes.is_empty());
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_head_binary2() {
2019-12-12 18:08:38 +01:00
let srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| ok::<_, ()>(Response::Ok().body(STR)))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.shead("/").send().await.unwrap();
assert!(response.status().is_success());
{
let len = response
.headers()
.get(http::header::CONTENT_LENGTH)
.unwrap();
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
}
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_body_length() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| {
let body = once(ok(Bytes::from_static(STR.as_ref())));
ok::<_, ()>(
Response::Ok().body(body::SizedStream::new(STR.len() as u64, body)),
)
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
// read response
let bytes = srv.load_body(response).await.unwrap();
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_body_chunked_explicit() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| {
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
ok::<_, ()>(
Response::Ok()
2021-01-15 03:11:10 +01:00
.insert_header((header::TRANSFER_ENCODING, "chunked"))
2019-12-05 18:35:43 +01:00
.streaming(body),
)
})
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert!(response.status().is_success());
assert!(!response.headers().contains_key(header::TRANSFER_ENCODING));
// read response
let bytes = srv.load_body(response).await.unwrap();
// decode
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
}
#[actix_rt::test]
async fn test_h2_response_http_error_handling() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
2019-12-08 14:25:24 +01:00
.h2(fn_factory_with_config(|_: ()| {
ok::<_, ()>(fn_service(|_| {
2019-12-05 18:35:43 +01:00
let broken_header = Bytes::from_static(b"\0\0\0");
ok::<_, ()>(
Response::Ok()
2021-01-15 03:11:10 +01:00
.insert_header((http::header::CONTENT_TYPE, broken_header))
2019-12-05 18:35:43 +01:00
.body(STR),
)
}))
}))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-19 13:54:19 +01:00
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
2019-11-19 13:54:19 +01:00
2019-11-26 06:25:50 +01:00
// read response
let bytes = srv.load_body(response).await.unwrap();
assert_eq!(bytes, Bytes::from_static(b"failed to parse header value"));
2019-11-19 13:54:19 +01:00
}
2019-11-26 06:25:50 +01:00
#[actix_rt::test]
async fn test_h2_service_error() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h2(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
2019-12-05 18:35:43 +01:00
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
// read response
let bytes = srv.load_body(response).await.unwrap();
assert_eq!(bytes, Bytes::from_static(b"error"));
}
#[actix_rt::test]
async fn test_h1_service_error() {
2019-12-12 18:08:38 +01:00
let mut srv = test_server(move || {
2019-12-05 18:35:43 +01:00
HttpService::build()
.h1(|_| err::<Response, Error>(error::ErrorBadRequest("error")))
.rustls(tls_config())
2020-03-08 08:42:45 +01:00
})
.await;
2019-11-26 06:25:50 +01:00
let response = srv.sget("/").send().await.unwrap();
assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
// read response
let bytes = srv.load_body(response).await.unwrap();
assert_eq!(bytes, Bytes::from_static(b"error"));
2019-11-19 13:54:19 +01:00
}