Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed May 29, 2024
1 parent 34142e1 commit 06b5a92
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 39 deletions.
52 changes: 36 additions & 16 deletions ntex-http/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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 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>());

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"));
}
}
1 change: 1 addition & 0 deletions ntex-io/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ mod tests {

#[ntex::test]
async fn test_basic() {
env_logger::try_init();

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-pc-windows-msvc

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / coverage

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / 1.75.0 - x86_64-unknown-linux-gnu

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / stable - aarch64-apple-darwin

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / nightly - aarch64-apple-darwin

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-pc-windows-msvc

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-pc-windows-msvc

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / stable - aarch64-apple-darwin

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / coverage

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / 1.75.0 - x86_64-unknown-linux-gnu

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / nightly - aarch64-apple-darwin

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-pc-windows-msvc

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu

unused `Result` that must be used

Check failure on line 729 in ntex-io/src/dispatcher.rs

View workflow job for this annotation

GitHub Actions / nightly - x86_64-unknown-linux-gnu

unused `Result` that must be used
let (client, server) = IoTest::create();
client.remote_buffer_cap(1024);
client.write("GET /test HTTP/1\r\n\r\n");
Expand Down
6 changes: 6 additions & 0 deletions ntex-io/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)"));
}
}
4 changes: 4 additions & 0 deletions ntex-net/src/connect/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion ntex-net/src/connect/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions ntex-service/src/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
Expand Down
14 changes: 0 additions & 14 deletions ntex-service/src/fn_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion ntex-util/src/future/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), ());
Expand Down
19 changes: 19 additions & 0 deletions ntex-util/src/future/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 06b5a92

Please sign in to comment.