More tests (#285)

This commit is contained in:
Nikolay Kim 2024-01-19 16:05:07 +06:00 committed by GitHub
parent 5a04548e13
commit e8c175a17f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 84 additions and 18 deletions

View file

@ -284,6 +284,7 @@ mod tests {
let addrs = connect.addrs().clone();
assert_eq!(format!("{:?}", addrs), "[]");
assert!(connect.addrs().next().is_none());
assert!(format!("{:?}", connect.clone()).contains("Connect"));
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
connect = connect.set_addrs(vec![addr]);

View file

@ -140,5 +140,6 @@ mod tests {
.call(Connect::new("").set_addr(Some(server.addr())))
.await;
assert!(result.is_err());
assert!(format!("{:?}", srv).contains("Connector"));
}
}

View file

@ -232,11 +232,12 @@ mod tests {
ntex_service::fn_service(|_| async { Ok::<_, ()>(()) })
});
let srv = Connector::default().memory_pool(PoolId::P5);
let srv = Connector::default().tag("T").memory_pool(PoolId::P5);
let result = srv.connect("").await;
assert!(result.is_err());
let result = srv.connect("localhost:99999").await;
assert!(result.is_err());
assert!(format!("{:?}", srv).contains("Connector"));
let srv = Connector::default();
let result = srv.connect(format!("{}", server.addr())).await;

View file

@ -117,6 +117,8 @@ mod tests {
Poll::Pending
);
cond.notify();
assert!(format!("{:?}", cond).contains("Condition"));
assert!(format!("{:?}", waiter).contains("Waiter"));
assert_eq!(waiter.await, ());
let mut waiter = cond.wait();
@ -145,10 +147,14 @@ mod tests {
let waiter2 = waiter.clone();
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Pending);
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Pending);
assert_eq!(lazy(|cx| waiter2.poll_ready(cx)).await, Poll::Pending);
assert_eq!(lazy(|cx| waiter2.poll_ready(cx)).await, Poll::Pending);
drop(cond);
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Ready(()));
assert_eq!(lazy(|cx| waiter.poll_ready(cx)).await, Poll::Pending);
assert_eq!(lazy(|cx| waiter2.poll_ready(cx)).await, Poll::Ready(()));
assert_eq!(lazy(|cx| waiter2.poll_ready(cx)).await, Poll::Pending);
}
}

View file

@ -210,17 +210,40 @@ mod tests {
fn test_basics() {
let msg = Message::new();
let mut req = Request::from(msg);
assert!(req.io().is_none());
req.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("text/plain"),
);
assert!(req.headers().contains_key(header::CONTENT_TYPE));
req.extensions_mut()
.insert(header::HeaderValue::from_static("text/plain"));
assert_eq!(
req.extensions().get::<header::HeaderValue>().unwrap(),
header::HeaderValue::from_static("text/plain")
);
req.head_mut().headers_mut().insert(
header::CONTENT_LENGTH,
header::HeaderValue::from_static("100"),
);
assert!(req.headers().contains_key(header::CONTENT_LENGTH));
req.head_mut().no_chunking(true);
assert!(!req.head().chunked());
req.head_mut().no_chunking(false);
assert!(req.head().chunked());
*req.uri_mut() = Uri::try_from("/index.html?q=1").unwrap();
assert_eq!(req.uri().path(), "/index.html");
assert_eq!(req.uri().query(), Some("q=1"));
let s = format!("{:?}", req);
assert!(s.contains("Request HTTP/1.1 GET:/index.html"));
let s = format!("{:?}", req.head());
assert!(s.contains("RequestHead { uri:"));
}
}

View file

@ -819,6 +819,8 @@ mod tests {
.finish();
let dbg = format!("{:?}", resp);
assert!(dbg.contains("Response"));
let dbg = format!("{:?}", resp.head());
assert!(dbg.contains("ResponseHead"));
let mut resp = Response::Ok();
resp.header(COOKIE, HeaderValue::from_static("cookie1=value1; "));

View file

@ -55,7 +55,7 @@ where
sys.run(|| {
Server::build()
.listen("test", tcp, move |_| factory())?
.set_tag("test", "TEST: ")
.set_tag("test", "TEST-SERVER")
.workers(1)
.disable_signals()
.run();
@ -106,6 +106,11 @@ impl TestServer {
self.addr
}
pub fn set_addr(mut self, addr: net::SocketAddr) -> Self {
self.addr = addr;
self
}
/// Connect to server, return Io
pub async fn connect(&self) -> io::Result<Io> {
tcp_connect(self.addr).await

View file

@ -87,6 +87,15 @@ where
}
}
impl<F> fmt::Debug for FnGuard<F>
where
F: Fn(&RequestHead) -> bool,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Guard::fmt(self, f)
}
}
impl<F> Guard for F
where
F: Fn(&RequestHead) -> bool,
@ -421,6 +430,8 @@ mod tests {
let pred = Header("content-type", "other");
assert!(!pred.check(req.head()));
assert!(format!("{:?}", pred).contains("Header"));
}
#[test]
@ -480,6 +491,8 @@ mod tests {
let pred = Host("localhost");
assert!(!pred.check(req.head()));
assert!(format!("{:?}", pred).contains("Host"));
}
#[test]
@ -558,6 +571,7 @@ mod tests {
.to_http_request();
assert!(Trace().check(r.head()));
assert!(!Trace().check(req.head()));
assert!(format!("{:?}", Trace()).contains("MethodGuard(TRACE)"));
}
#[test]
@ -568,12 +582,15 @@ mod tests {
assert!(Not(Get()).check(r.head()));
assert!(!Not(Trace()).check(r.head()));
assert!(format!("{:?}", Not(Get())).contains("NotGuard"));
assert!(All(Trace()).and(Trace()).check(r.head()));
assert!(!All(Get()).and(Trace()).check(r.head()));
assert!(format!("{:?}", All(Get())).contains("AllGuard"));
assert!(Any(Get()).or(Trace()).check(r.head()));
assert!(!Any(Get()).or(Get()).check(r.head()));
assert!(format!("{:?}", Any(Get())).contains("AnyGuard"));
}
#[test]
@ -583,6 +600,8 @@ mod tests {
let g = fn_guard(|req| req.headers().contains_key("content-type"));
assert!(g.check(req.head()));
let g = FnGuard(|req| req.headers().contains_key("content-type"));
assert!(format!("{:?}", g).contains("FnGuard"));
let g = |req: &RequestHead| req.headers().contains_key("content-type");
assert!(g.check(req.head()));

View file

@ -4,7 +4,7 @@ use ntex::codec::BytesCodec;
use ntex::connect::Connect;
use ntex::io::{types::PeerAddr, Io};
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")]
fn ssl_acceptor() -> tls_openssl::ssl::SslAcceptor {
@ -78,7 +78,12 @@ async fn test_openssl_string() {
x509::X509,
};
let srv = test_server(|| {
let tcp = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let local_addr = tcp.local_addr().unwrap();
let tcp = Some(tcp);
let srv = build_test_server(move |srv| {
srv.listen("test", tcp.unwrap(), |_| {
chain_factory(fn_service(|io: Io<_>| async move {
let res = io.read_ready().await;
assert!(res.is_ok());
@ -92,7 +97,10 @@ async fn test_openssl_string() {
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();
builder.set_verify(SslVerifyMode::NONE);