Skip to content

Commit

Permalink
Add client timeout test (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored Nov 19, 2024
1 parent c657b9c commit daeded8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
73 changes: 40 additions & 33 deletions ntex/src/http/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,44 +251,13 @@ where

let (system, server, addr) = rx.recv().unwrap();

let client = {
let connector = {
#[cfg(feature = "openssl")]
{
use tls_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!("Cannot set alpn protocol: {:?}", e));
Connector::default()
.timeout(Millis(30_000))
.openssl(builder.build())
.configure_http2(|cfg| {
cfg.max_header_list_size(256 * 1024);
cfg.max_header_continuation_frames(96);
})
.finish()
}
#[cfg(not(feature = "openssl"))]
{
Connector::default().timeout(Millis(30_000)).finish()
}
};

Client::build()
.timeout(Seconds(30))
.connector(connector)
.finish()
};

TestServer {
addr,
client,
system,
server,
client: Client::build().finish(),
}
.set_client_timeout(Seconds(30), Millis(30_000))
}

#[derive(Debug)]
Expand All @@ -301,6 +270,44 @@ pub struct TestServer {
}

impl TestServer {
/// Set client timeout
pub fn set_client_timeout(mut self, timeout: Seconds, connect_timeout: Millis) -> Self {
let client = {
let connector = {
#[cfg(feature = "openssl")]
{
use tls_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!("Cannot set alpn protocol: {:?}", e));
Connector::default()
.timeout(connect_timeout)
.openssl(builder.build())
.configure_http2(|cfg| {
cfg.max_header_list_size(256 * 1024);
cfg.max_header_continuation_frames(96);
})
.finish()
}
#[cfg(not(feature = "openssl"))]
{
Connector::default().timeout(connect_timeout).finish()
}
};

Client::build()
.timeout(timeout)
.connector(connector)
.finish()
};

self.client = client;
self
}

/// Construct test server url
pub fn addr(&self) -> net::SocketAddr {
self.addr
Expand Down
26 changes: 24 additions & 2 deletions ntex/tests/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::io;

use ntex::http::test::server as test_server;
use ntex::http::{HttpService, Method, Request, Response};
use ntex::http::{client::error::SendRequestError, HttpService, Method, Request, Response};
use ntex::service::ServiceFactory;
use ntex::util::{Bytes, Ready};
use ntex::{time, util::Bytes, util::Ready};

const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
Hello World Hello World Hello World Hello World Hello World \
Expand Down Expand Up @@ -87,3 +87,25 @@ async fn test_with_query_parameter() {
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}

#[ntex::test]
async fn test_client_timeout() {
let srv = test_server(move || {
HttpService::build()
.finish(|_| async {
time::sleep(time::Seconds(10)).await;
Ok::<_, io::Error>(Response::Ok().body(STR))
})
.map(|_| ())
})
.set_client_timeout(time::Seconds(1), time::Millis(30_000));

let err = srv
.request(Method::GET, "/")
.force_close()
.send()
.await
.err()
.unwrap();
assert!(matches!(err, SendRequestError::Timeout));
}

0 comments on commit daeded8

Please sign in to comment.