diff --git a/ntex-connect/src/message.rs b/ntex-connect/src/message.rs index 2838db00..5130c97a 100644 --- a/ntex-connect/src/message.rs +++ b/ntex-connect/src/message.rs @@ -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]); diff --git a/ntex-connect/src/openssl.rs b/ntex-connect/src/openssl.rs index 5e00bfd0..9d51bbe0 100644 --- a/ntex-connect/src/openssl.rs +++ b/ntex-connect/src/openssl.rs @@ -140,5 +140,6 @@ mod tests { .call(Connect::new("").set_addr(Some(server.addr()))) .await; assert!(result.is_err()); + assert!(format!("{:?}", srv).contains("Connector")); } } diff --git a/ntex-connect/src/service.rs b/ntex-connect/src/service.rs index 7295c73f..5316c73b 100644 --- a/ntex-connect/src/service.rs +++ b/ntex-connect/src/service.rs @@ -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; diff --git a/ntex-util/src/channel/condition.rs b/ntex-util/src/channel/condition.rs index 45aa8b0f..8a95e4af 100644 --- a/ntex-util/src/channel/condition.rs +++ b/ntex-util/src/channel/condition.rs @@ -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); } } diff --git a/ntex/src/http/request.rs b/ntex/src/http/request.rs index 39b8b23e..6a4afefe 100644 --- a/ntex/src/http/request.rs +++ b/ntex/src/http/request.rs @@ -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::().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:")); } } diff --git a/ntex/src/http/response.rs b/ntex/src/http/response.rs index 4260a245..9e36ceb1 100644 --- a/ntex/src/http/response.rs +++ b/ntex/src/http/response.rs @@ -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; ")); diff --git a/ntex/src/server/test.rs b/ntex/src/server/test.rs index 1267ab06..76a8be67 100644 --- a/ntex/src/server/test.rs +++ b/ntex/src/server/test.rs @@ -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 { tcp_connect(self.addr).await diff --git a/ntex/src/web/guard.rs b/ntex/src/web/guard.rs index b4b060f8..1fca9137 100644 --- a/ntex/src/web/guard.rs +++ b/ntex/src/web/guard.rs @@ -87,6 +87,15 @@ where } } +impl fmt::Debug for FnGuard +where + F: Fn(&RequestHead) -> bool, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Guard::fmt(self, f) + } +} + impl 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())); diff --git a/ntex/tests/connect.rs b/ntex/tests/connect.rs index 3e53eea1..4da988dd 100644 --- a/ntex/tests/connect.rs +++ b/ntex/tests/connect.rs @@ -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,21 +78,29 @@ async fn test_openssl_string() { x509::X509, }; - let srv = test_server(|| { - chain_factory(fn_service(|io: Io<_>| async move { - let res = io.read_ready().await; - assert!(res.is_ok()); - Ok(io) - })) - .and_then(openssl::SslAcceptor::new(ssl_acceptor())) - .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>(()) - })) - }); + 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()); + Ok(io) + })) + .and_then(openssl::SslAcceptor::new(ssl_acceptor())) + .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>(()) + })) + }) + .unwrap() + }) + .set_addr(local_addr); let mut builder = SslConnector::builder(SslMethod::tls()).unwrap(); builder.set_verify(SslVerifyMode::NONE);