More tests (#368)

This commit is contained in:
Nikolay Kim 2024-05-29 20:30:14 +05:00 committed by GitHub
parent 34142e1ae2
commit e0b5284fdd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 82 additions and 37 deletions

View file

@ -147,23 +147,43 @@ mod tests {
use super::*;
use std::error::Error as StdError;
#[test]
fn inner_http_error() {
let e = method::Method::from_bytes(b"").unwrap_err();
let err: Error = http::Error::from(e).into();
let ie = err.get_ref();
assert!(ie.is::<http::Error>());
}
#[test]
fn inner_error_is_invalid_status_code() {
if let Err(e) = status::StatusCode::from_u16(6666) {
let err: Error = e.into();
let ie = err.get_ref();
assert!(!ie.is::<header::InvalidHeaderValue>());
assert!(ie.is::<status::InvalidStatusCode>());
ie.downcast_ref::<status::InvalidStatusCode>().unwrap();
let e = status::StatusCode::from_u16(6666).unwrap_err();
let err: Error = e.into();
let ie = err.get_ref();
assert!(!ie.is::<header::InvalidHeaderValue>());
assert!(ie.is::<status::InvalidStatusCode>());
ie.downcast_ref::<status::InvalidStatusCode>().unwrap();
assert!(err.source().is_none());
assert!(!err.is::<InvalidHeaderValue>());
assert!(err.is::<status::InvalidStatusCode>());
assert!(err.source().is_none());
assert!(!err.is::<InvalidHeaderValue>());
assert!(err.is::<status::InvalidStatusCode>());
let s = format!("{:?}", err);
assert!(s.starts_with("ntex_http::Error"));
} else {
panic!("Bad status allowed!");
}
let s = format!("{:?}", err);
assert!(s.starts_with("ntex_http::Error"));
}
#[test]
fn inner_error_is_invalid_method() {
let e = method::Method::from_bytes(b"").unwrap_err();
let err: Error = e.into();
let ie = err.get_ref();
assert!(ie.is::<method::InvalidMethod>());
ie.downcast_ref::<method::InvalidMethod>().unwrap();
assert!(err.source().is_none());
assert!(err.is::<method::InvalidMethod>());
let s = format!("{:?}", err);
assert!(s.starts_with("ntex_http::Error"));
}
}

View file

@ -726,6 +726,7 @@ mod tests {
#[ntex::test]
async fn test_basic() {
let _ = env_logger::try_init();
let (client, server) = IoTest::create();
client.remote_buffer_cap(1024);
client.write("GET /test HTTP/1\r\n\r\n");

View file

@ -685,5 +685,11 @@ mod tests {
let res = lazy(|cx| server2.poll_write_buf(cx, b"123")).await;
assert!(res.is_pending());
let (client, _) = IoTest::create();
let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
let client = crate::Io::new(client.set_peer_addr(addr));
let item = client.query::<crate::types::PeerAddr>();
assert!(format!("{:?}", item).contains("QueryItem(127.0.0.1:8080)"));
}
}

View file

@ -296,6 +296,10 @@ mod tests {
let s = "test".to_string();
assert_eq!(s.host(), "test");
assert_eq!(s.port(), None);
let s = ByteString::from("test");
assert_eq!(s.host(), "test");
assert_eq!(s.port(), None);
}
#[test]

View file

@ -231,7 +231,10 @@ mod tests {
ntex_service::fn_service(|_| async { Ok::<_, ()>(()) })
});
let srv = Connector::default().tag("T").memory_pool(PoolId::P5);
let srv = Connector::default()
.tag("T")
.memory_pool(PoolId::P5)
.clone();
let result = srv.connect("").await;
assert!(result.is_err());
let result = srv.connect("localhost:99999").await;

View file

@ -141,10 +141,12 @@ mod tests {
async fn test_ready() {
let cnt = Rc::new(Cell::new(0));
let cnt_sht = Rc::new(Cell::new(0));
let srv = chain(Srv1(cnt.clone(), cnt_sht.clone()))
.and_then(Srv2(cnt.clone(), cnt_sht.clone()))
.clone()
.into_pipeline();
let srv = Box::new(
chain(Srv1(cnt.clone(), cnt_sht.clone()))
.and_then(Srv2(cnt.clone(), cnt_sht.clone()))
.clone(),
)
.into_pipeline();
let res = srv.ready().await;
assert_eq!(res, Ok(()));
assert_eq!(cnt.get(), 2);
@ -168,9 +170,11 @@ mod tests {
#[ntex::test]
async fn test_call() {
let cnt = Rc::new(Cell::new(0));
let srv = chain(Srv1(cnt.clone(), Rc::new(Cell::new(0))))
.and_then(Srv2(cnt, Rc::new(Cell::new(0))))
.into_pipeline();
let srv = Box::new(
chain(Srv1(cnt.clone(), Rc::new(Cell::new(0))))
.and_then(Srv2(cnt, Rc::new(Cell::new(0)))),
)
.into_pipeline();
let res = srv.call("srv1").await;
assert!(res.is_ok());
assert_eq!(res.unwrap(), ("srv1", "srv2"));

View file

@ -369,20 +369,6 @@ where
}
}
impl<F, S, R, Req, E, C> IntoServiceFactory<FnServiceNoConfig<F, S, R, Req, E>, Req, C>
for F
where
F: Fn() -> R,
R: Future<Output = Result<S, E>>,
S: Service<Req>,
C: 'static,
{
#[inline]
fn into_factory(self) -> FnServiceNoConfig<F, S, R, Req, E> {
FnServiceNoConfig::new(self)
}
}
#[cfg(test)]
mod tests {
use ntex_util::future::lazy;

View file

@ -141,11 +141,13 @@ mod test {
e.as_ref();
e.as_mut();
let e = Either::<(), ()>::Right(());
let mut e = Either::<(), ()>::Right(());
assert!(!e.is_left());
assert!(e.is_right());
assert!(e.left().is_none());
assert!(e.right().is_some());
e.as_ref();
e.as_mut();
assert_eq!(Either::<(), ()>::Left(()).into_inner(), ());
assert_eq!(Either::<(), ()>::Right(()).into_inner(), ());

View file

@ -39,3 +39,22 @@ impl<T, E> From<Result<T, E>> for Ready<T, E> {
}
}
}
#[cfg(test)]
mod test {
use std::future::poll_fn;
use super::*;
#[ntex_macros::rt_test2]
async fn ready() {
let ok = Ok::<_, ()>("ok");
let mut f = Ready::from(ok);
let res = poll_fn(|cx| Pin::new(&mut f).poll(cx)).await;
assert_eq!(res.unwrap(), "ok");
let err = Err::<(), _>("err");
let mut f = Ready::from(err);
let res = poll_fn(|cx| Pin::new(&mut f).poll(cx)).await;
assert_eq!(res.unwrap_err(), "err");
}
}