This commit is contained in:
Nikolay Kim 2024-01-19 00:41:07 +06:00
parent 59f1e6ed5c
commit f69acbb929
2 changed files with 29 additions and 16 deletions

View file

@ -106,6 +106,11 @@ impl TestServer {
self.addr self.addr
} }
pub fn set_addr(mut self, addr: net::SocketAddr) -> Self {
self.addr = addr;
self
}
/// Connect to server, return Io /// Connect to server, return Io
pub async fn connect(&self) -> io::Result<Io> { pub async fn connect(&self) -> io::Result<Io> {
tcp_connect(self.addr).await tcp_connect(self.addr).await

View file

@ -4,7 +4,7 @@ use ntex::codec::BytesCodec;
use ntex::connect::Connect; use ntex::connect::Connect;
use ntex::io::{types::PeerAddr, Io}; use ntex::io::{types::PeerAddr, Io};
use ntex::service::{chain_factory, fn_service, Pipeline, ServiceFactory}; use ntex::service::{chain_factory, fn_service, Pipeline, ServiceFactory};
use ntex::{server::test_server, time, util::Bytes}; use ntex::{server::build_test_server, server::test_server, time, util::Bytes};
#[cfg(feature = "openssl")] #[cfg(feature = "openssl")]
fn ssl_acceptor() -> tls_openssl::ssl::SslAcceptor { fn ssl_acceptor() -> tls_openssl::ssl::SslAcceptor {
@ -78,21 +78,29 @@ async fn test_openssl_string() {
x509::X509, x509::X509,
}; };
let srv = test_server(|| { let tcp = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
chain_factory(fn_service(|io: Io<_>| async move { let local_addr = tcp.local_addr().unwrap();
let res = io.read_ready().await;
assert!(res.is_ok()); let tcp = Some(tcp);
Ok(io) let srv = build_test_server(move |srv| {
})) srv.listen("test", tcp.unwrap(), |_| {
.and_then(openssl::SslAcceptor::new(ssl_acceptor())) chain_factory(fn_service(|io: Io<_>| async move {
.and_then(fn_service(|io: Io<_>| async move { let res = io.read_ready().await;
io.send(Bytes::from_static(b"test"), &BytesCodec) assert!(res.is_ok());
.await Ok(io)
.unwrap(); }))
assert_eq!(io.recv(&BytesCodec).await.unwrap().unwrap(), "test"); .and_then(openssl::SslAcceptor::new(ssl_acceptor()))
Ok::<_, Box<dyn std::error::Error>>(()) .and_then(fn_service(|io: Io<_>| async move {
})) io.send(Bytes::from_static(b"test"), &BytesCodec)
}); .await
.unwrap();
assert_eq!(io.recv(&BytesCodec).await.unwrap().unwrap(), "test");
Ok::<_, Box<dyn std::error::Error>>(())
}))
})
.unwrap()
})
.set_addr(local_addr);
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap(); let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
builder.set_verify(SslVerifyMode::NONE); builder.set_verify(SslVerifyMode::NONE);