mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-05 13:57:39 +03:00
rename test methods; update tests
This commit is contained in:
parent
15c5a3bcfb
commit
34695f4bce
8 changed files with 169 additions and 62 deletions
|
@ -2,8 +2,11 @@ use net2::TcpBuilder;
|
|||
use std::sync::mpsc;
|
||||
use std::{net, thread, time::Duration};
|
||||
|
||||
#[cfg(feature = "ssl")]
|
||||
use openssl::ssl::SslAcceptorBuilder;
|
||||
|
||||
use actix_http::Response;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_web::{test, web, App, HttpServer};
|
||||
|
||||
fn unused_addr() -> net::SocketAddr {
|
||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||
|
@ -76,3 +79,77 @@ fn test_start() {
|
|||
thread::sleep(Duration::from_millis(100));
|
||||
let _ = sys.stop();
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssl")]
|
||||
fn ssl_acceptor() -> std::io::Result<SslAcceptorBuilder> {
|
||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||
// load ssl keys
|
||||
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
builder
|
||||
.set_private_key_file("tests/key.pem", SslFiletype::PEM)
|
||||
.unwrap();
|
||||
builder
|
||||
.set_certificate_chain_file("tests/cert.pem")
|
||||
.unwrap();
|
||||
Ok(builder)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "ssl")]
|
||||
fn test_start_ssl() {
|
||||
let addr = unused_addr();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(move || {
|
||||
let sys = actix_rt::System::new("test");
|
||||
let builder = ssl_acceptor().unwrap();
|
||||
|
||||
let srv = HttpServer::new(|| {
|
||||
App::new().service(
|
||||
web::resource("/").route(web::to(|| Response::Ok().body("test"))),
|
||||
)
|
||||
})
|
||||
.workers(1)
|
||||
.shutdown_timeout(1)
|
||||
.system_exit()
|
||||
.disable_signals()
|
||||
.bind_ssl(format!("{}", addr), builder)
|
||||
.unwrap()
|
||||
.start();
|
||||
|
||||
let _ = tx.send((srv, actix_rt::System::current()));
|
||||
let _ = sys.run();
|
||||
});
|
||||
let (srv, sys) = rx.recv().unwrap();
|
||||
|
||||
let client = test::run_on(|| {
|
||||
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
|
||||
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
builder.set_verify(SslVerifyMode::NONE);
|
||||
let _ = builder
|
||||
.set_alpn_protos(b"\x02h2\x08http/1.1")
|
||||
.map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));
|
||||
|
||||
Ok::<_, ()>(
|
||||
awc::Client::build()
|
||||
.connector(
|
||||
awc::Connector::new()
|
||||
.ssl(builder.build())
|
||||
.timeout(Duration::from_millis(100))
|
||||
.service(),
|
||||
)
|
||||
.finish(),
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let host = format!("https://{}", addr);
|
||||
|
||||
let response = test::block_on(client.get(host.clone()).send()).unwrap();
|
||||
assert!(response.status().is_success());
|
||||
|
||||
// stop
|
||||
let _ = srv.stop(false);
|
||||
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
let _ = sys.stop();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue