1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-19 16:11:07 +00:00
actix-web/awc/tests/test_rustls_client.rs

104 lines
3.2 KiB
Rust
Raw Normal View History

2019-11-20 18:35:07 +00:00
#![cfg(feature = "rustls")]
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use actix_http::HttpService;
2019-12-12 17:08:38 +00:00
use actix_http_test::test_server;
use actix_service::{map_config, pipeline_factory, ServiceFactory};
use actix_web::http::Version;
2019-12-20 11:36:48 +00:00
use actix_web::{dev::AppConfig, web, App, HttpResponse};
2019-11-20 18:35:07 +00:00
use futures::future::ok;
use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod, SslVerifyMode};
2019-12-05 17:35:43 +00:00
use rust_tls::ClientConfig;
2020-02-07 14:16:32 +00:00
#[allow(unused)]
2019-12-05 17:35:43 +00:00
fn ssl_acceptor() -> SslAcceptor {
// load ssl keys
2019-11-20 18:35:07 +00:00
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_verify_callback(SslVerifyMode::NONE, |_, _| true);
builder
.set_private_key_file("../tests/key.pem", SslFiletype::PEM)
.unwrap();
builder
.set_certificate_chain_file("../tests/cert.pem")
.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(open_ssl::ssl::AlpnError::NOACK)
}
});
2019-12-05 17:35:43 +00:00
builder.set_alpn_protos(b"\x02h2").unwrap();
builder.build()
}
mod danger {
pub struct NoCertificateVerification {}
2019-11-20 18:35:07 +00:00
impl rust_tls::ServerCertVerifier for NoCertificateVerification {
fn verify_server_cert(
&self,
2019-11-20 18:35:07 +00:00
_roots: &rust_tls::RootCertStore,
_presented_certs: &[rust_tls::Certificate],
_dns_name: webpki::DNSNameRef<'_>,
_ocsp: &[u8],
2019-11-20 18:35:07 +00:00
) -> Result<rust_tls::ServerCertVerified, rust_tls::TLSError> {
Ok(rust_tls::ServerCertVerified::assertion())
}
}
}
2019-11-26 05:25:50 +00:00
// #[actix_rt::test]
async fn _test_connection_reuse_h2() {
let num = Arc::new(AtomicUsize::new(0));
let num2 = num.clone();
2019-12-12 17:08:38 +00:00
let srv = test_server(move || {
2019-11-26 05:25:50 +00:00
let num2 = num2.clone();
pipeline_factory(move |io| {
num2.fetch_add(1, Ordering::Relaxed);
ok(io)
})
.and_then(
HttpService::build()
2019-12-20 11:36:48 +00:00
.h2(map_config(
2019-12-22 12:39:25 +00:00
App::new().service(
web::resource("/").route(web::to(|| HttpResponse::Ok())),
),
2019-12-20 11:36:48 +00:00
|_| AppConfig::default(),
2019-12-20 11:27:32 +00:00
))
2019-12-05 17:35:43 +00:00
.openssl(ssl_acceptor())
2019-11-26 05:25:50 +00:00
.map_err(|_| ()),
)
2020-03-08 07:42:45 +00:00
})
.await;
2019-11-26 05:25:50 +00:00
// disable ssl verification
let mut config = ClientConfig::new();
let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
config.set_protocols(&protos);
config
.dangerous()
.set_certificate_verifier(Arc::new(danger::NoCertificateVerification {}));
2019-11-26 05:25:50 +00:00
let client = awc::Client::build()
.connector(awc::Connector::new().rustls(Arc::new(config)).finish())
.finish();
2019-11-26 05:25:50 +00:00
// req 1
let request = client.get(srv.surl("/")).send();
let response = request.await.unwrap();
assert!(response.status().is_success());
2019-11-26 05:25:50 +00:00
// req 2
let req = client.post(srv.surl("/"));
let response = req.send().await.unwrap();
assert!(response.status().is_success());
assert_eq!(response.version(), Version::HTTP_2);
2019-11-26 05:25:50 +00:00
// one connection
assert_eq!(num.load(Ordering::Relaxed), 1);
}