2019-11-19 12:54:19 +00:00
|
|
|
#![cfg(feature = "openssl")]
|
2021-02-07 03:54:58 +00:00
|
|
|
|
|
|
|
extern crate tls_openssl as openssl;
|
|
|
|
|
2021-06-17 16:57:58 +00:00
|
|
|
use std::{convert::Infallible, io};
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2021-04-13 10:16:12 +00:00
|
|
|
use actix_http::{
|
2021-06-17 16:57:58 +00:00
|
|
|
body::{AnyBody, Body, SizedStream},
|
2021-05-14 15:40:00 +00:00
|
|
|
error::PayloadError,
|
2021-04-13 10:16:12 +00:00
|
|
|
http::{
|
|
|
|
header::{self, HeaderName, HeaderValue},
|
|
|
|
Method, StatusCode, Version,
|
|
|
|
},
|
2021-06-17 16:57:58 +00:00
|
|
|
Error, HttpMessage, HttpService, Request, Response,
|
2021-04-13 10:16:12 +00:00
|
|
|
};
|
2021-01-03 23:47:04 +00:00
|
|
|
use actix_http_test::test_server;
|
|
|
|
use actix_service::{fn_service, ServiceFactoryExt};
|
2021-04-01 14:26:13 +00:00
|
|
|
use actix_utils::future::{err, ok, ready};
|
2021-01-03 23:47:04 +00:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2021-05-14 15:40:00 +00:00
|
|
|
use derive_more::{Display, Error};
|
2021-03-11 03:48:38 +00:00
|
|
|
use futures_core::Stream;
|
2021-04-01 14:26:13 +00:00
|
|
|
use futures_util::stream::{once, StreamExt as _};
|
2021-02-13 17:16:36 +00:00
|
|
|
use openssl::{
|
|
|
|
pkey::PKey,
|
|
|
|
ssl::{SslAcceptor, SslMethod},
|
|
|
|
x509::X509,
|
|
|
|
};
|
2019-11-19 12:54:19 +00:00
|
|
|
|
|
|
|
async fn load_body<S>(stream: S) -> Result<BytesMut, PayloadError>
|
|
|
|
where
|
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>>,
|
|
|
|
{
|
|
|
|
let body = stream
|
|
|
|
.map(|res| match res {
|
|
|
|
Ok(chunk) => chunk,
|
|
|
|
Err(_) => panic!(),
|
|
|
|
})
|
|
|
|
.fold(BytesMut::new(), move |mut body, chunk| {
|
|
|
|
body.extend_from_slice(&chunk);
|
|
|
|
ready(body)
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(body)
|
|
|
|
}
|
|
|
|
|
2021-02-13 17:16:36 +00:00
|
|
|
fn tls_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();
|
|
|
|
|
2019-11-19 12:54:19 +00:00
|
|
|
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
2021-02-13 17:16:36 +00:00
|
|
|
builder.set_certificate(&cert).unwrap();
|
|
|
|
builder.set_private_key(&key).unwrap();
|
|
|
|
|
2019-11-19 12:54:19 +00:00
|
|
|
builder.set_alpn_select_callback(|_, protos| {
|
|
|
|
const H2: &[u8] = b"\x02h2";
|
|
|
|
if protos.windows(3).any(|window| window == H2) {
|
|
|
|
Ok(b"h2")
|
|
|
|
} else {
|
2021-02-13 17:16:36 +00:00
|
|
|
Err(openssl::ssl::AlpnError::NOACK)
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
});
|
2021-02-13 17:16:36 +00:00
|
|
|
builder.set_alpn_protos(b"\x02h2").unwrap();
|
2019-12-02 11:33:11 +00:00
|
|
|
|
|
|
|
builder.build()
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2() -> io::Result<()> {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-04-14 01:00:14 +00:00
|
|
|
.h2(|_| ok::<_, Error>(Response::ok()))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
Ok(())
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_1() -> io::Result<()> {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.finish(|req: Request| {
|
|
|
|
assert!(req.peer_addr().is_some());
|
|
|
|
assert_eq!(req.version(), Version::HTTP_2);
|
2021-04-14 01:00:14 +00:00
|
|
|
ok::<_, Error>(Response::ok())
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
Ok(())
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_body() -> io::Result<()> {
|
|
|
|
let data = "HELLOWORLD".to_owned().repeat(64 * 1024);
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2020-02-27 02:10:55 +00:00
|
|
|
.h2(|mut req: Request<_>| async move {
|
|
|
|
let body = load_body(req.take_payload()).await?;
|
2021-04-14 01:00:14 +00:00
|
|
|
Ok::<_, Error>(Response::ok().set_body(body))
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00: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 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_content_length() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h2(|req: Request| {
|
2021-03-04 13:27:54 +00:00
|
|
|
let idx: usize = req.uri().path()[1..].parse().unwrap();
|
2019-12-02 11:33:11 +00:00
|
|
|
let statuses = [
|
|
|
|
StatusCode::CONTINUE,
|
2021-03-04 13:27:54 +00:00
|
|
|
StatusCode::NO_CONTENT,
|
2019-12-02 11:33:11 +00:00
|
|
|
StatusCode::OK,
|
|
|
|
StatusCode::NOT_FOUND,
|
|
|
|
];
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::new(statuses[idx]))
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let header = HeaderName::from_static("content-length");
|
|
|
|
let value = HeaderValue::from_static("0");
|
|
|
|
|
|
|
|
{
|
2021-03-04 13:27:54 +00:00
|
|
|
for &i in &[0] {
|
|
|
|
let req = srv
|
|
|
|
.request(Method::HEAD, srv.surl(&format!("/{}", i)))
|
|
|
|
.send();
|
|
|
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
|
|
|
// assert_eq!(response.headers().get(&header), None);
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let req = srv
|
|
|
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
|
|
|
.send();
|
2021-03-04 13:27:54 +00:00
|
|
|
let _response = req.await.expect_err("should timeout on recv 1xx frame");
|
|
|
|
// assert_eq!(response.headers().get(&header), None);
|
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
|
2021-03-04 13:27:54 +00:00
|
|
|
for &i in &[1] {
|
2019-11-26 05:25:50 +00:00
|
|
|
let req = srv
|
2021-03-04 13:27:54 +00:00
|
|
|
.request(Method::GET, srv.surl(&format!("/{}", i)))
|
2019-11-26 05:25:50 +00:00
|
|
|
.send();
|
|
|
|
let response = req.await.unwrap();
|
|
|
|
assert_eq!(response.headers().get(&header), None);
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:27:54 +00:00
|
|
|
for &i in &[2, 3] {
|
2019-11-26 05:25:50 +00:00
|
|
|
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 12:54:19 +00:00
|
|
|
}
|
2019-11-26 05:25:50 +00:00
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_headers() {
|
|
|
|
let data = STR.repeat(10);
|
|
|
|
let data2 = data.clone();
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let data = data.clone();
|
2019-11-19 12:54:19 +00:00
|
|
|
HttpService::build().h2(move |_| {
|
2021-04-14 01:00:14 +00:00
|
|
|
let mut builder = Response::build(StatusCode::OK);
|
2019-11-19 12:54:19 +00:00
|
|
|
for idx in 0..90 {
|
2021-01-15 02:11:10 +00:00
|
|
|
builder.insert_header(
|
|
|
|
(format!("X-TEST-{}", idx).as_str(),
|
2019-11-19 12:54:19 +00:00
|
|
|
"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 02:11:10 +00:00
|
|
|
));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(builder.body(data.clone()))
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
}).await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from(data2));
|
2019-11-19 12:54:19 +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";
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_body2() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00: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 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_head_empty() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.finish(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.shead("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(response.version(), Version::HTTP_2);
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
{
|
|
|
|
let len = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_head_binary() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.shead("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
{
|
|
|
|
let len = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert!(bytes.is_empty());
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_head_binary2() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| ok::<_, Infallible>(Response::ok().set_body(STR)))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.shead("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
{
|
|
|
|
let len = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(format!("{}", STR.len()), len.to_str().unwrap());
|
|
|
|
}
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_body_length() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-06-17 16:57:58 +00:00
|
|
|
.h2(|_| async {
|
|
|
|
let body = once(async {
|
|
|
|
Ok::<_, Infallible>(Bytes::from_static(STR.as_ref()))
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::ok().set_body(SizedStream::new(STR.len() as u64, body)),
|
2019-12-02 11:33:11 +00:00
|
|
|
)
|
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00: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 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_body_chunked_explicit() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
|
|
|
.h2(|_| {
|
|
|
|
let body = once(ok::<_, Error>(Bytes::from_static(STR.as_ref())));
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((header::TRANSFER_ENCODING, "chunked"))
|
2019-12-02 11:33:11 +00:00
|
|
|
.streaming(body),
|
|
|
|
)
|
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert!(!response.headers().contains_key(header::TRANSFER_ENCODING));
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// decode
|
|
|
|
assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_response_http_error_handling() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2019-12-08 13:25:24 +00:00
|
|
|
.h2(fn_service(|_| {
|
2019-12-02 11:33:11 +00:00
|
|
|
let broken_header = Bytes::from_static(b"\0\0\0");
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(
|
2021-04-14 01:00:14 +00:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.insert_header((header::CONTENT_TYPE, broken_header))
|
2019-12-02 11:33:11 +00:00
|
|
|
.body(STR),
|
|
|
|
)
|
|
|
|
}))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
2019-11-19 12:54:19 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
2021-06-17 16:57:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
bytes,
|
|
|
|
Bytes::from_static(b"error processing HTTP: failed to parse header value")
|
|
|
|
);
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 15:40:00 +00:00
|
|
|
#[derive(Debug, Display, Error)]
|
|
|
|
#[display(fmt = "error")]
|
|
|
|
struct BadRequest;
|
|
|
|
|
2021-06-17 16:57:58 +00:00
|
|
|
impl From<BadRequest> for Response<AnyBody> {
|
|
|
|
fn from(err: BadRequest) -> Self {
|
|
|
|
Response::build(StatusCode::BAD_REQUEST).body(err.to_string())
|
2021-05-14 15:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_service_error() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let mut srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-05-14 15:40:00 +00:00
|
|
|
.h2(|_| err::<Response<Body>, _>(BadRequest))
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.load_body(response).await.unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"error"));
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_h2_on_connect() {
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-12-02 11:33:11 +00:00
|
|
|
HttpService::build()
|
2021-01-15 04:22:42 +00:00
|
|
|
.on_connect_ext(|_, data| {
|
|
|
|
data.insert(20isize);
|
|
|
|
})
|
2019-12-02 11:33:11 +00:00
|
|
|
.h2(|req: Request| {
|
2020-10-30 02:03:26 +00:00
|
|
|
assert!(req.extensions().contains::<isize>());
|
2021-06-17 16:57:58 +00:00
|
|
|
ok::<_, Infallible>(Response::ok())
|
2019-12-02 11:33:11 +00:00
|
|
|
})
|
2021-02-13 17:16:36 +00:00
|
|
|
.openssl(tls_config())
|
2019-12-02 11:33:11 +00:00
|
|
|
.map_err(|_| ())
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-11-26 05:25:50 +00:00
|
|
|
|
|
|
|
let response = srv.sget("/").send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-11-19 12:54:19 +00:00
|
|
|
}
|