mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-04 21:37:58 +03:00
More tests (#285)
This commit is contained in:
parent
5a04548e13
commit
e8c175a17f
9 changed files with 84 additions and 18 deletions
|
@ -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]);
|
||||
|
|
|
@ -140,5 +140,6 @@ mod tests {
|
|||
.call(Connect::new("").set_addr(Some(server.addr())))
|
||||
.await;
|
||||
assert!(result.is_err());
|
||||
assert!(format!("{:?}", srv).contains("Connector"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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; "));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()));
|
||||
|
|
|
@ -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<dyn std::error::Error>>(())
|
||||
}))
|
||||
});
|
||||
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<dyn std::error::Error>>(())
|
||||
}))
|
||||
})
|
||||
.unwrap()
|
||||
})
|
||||
.set_addr(local_addr);
|
||||
|
||||
let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
builder.set_verify(SslVerifyMode::NONE);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue