From d8f55decb0776a854d8a0d64688e7fbd6c0cf544 Mon Sep 17 00:00:00 2001 From: msga-mmm <109926431+msga-mmm@users.noreply.github.com> Date: Mon, 13 May 2024 03:41:26 -0500 Subject: [PATCH] test: improve test coverage (#358) * test(ntex-util): incrase time module coverage * test(middleware): increase coverage in defaultheaders * test(middleware/compress): improve test coverage --- ntex-util/src/time/mod.rs | 7 +++ ntex/src/web/middleware/compress.rs | 71 +++++++++++++++++++++++ ntex/src/web/middleware/defaultheaders.rs | 12 ++++ 3 files changed, 90 insertions(+) diff --git a/ntex-util/src/time/mod.rs b/ntex-util/src/time/mod.rs index 8356397a..a33ab6f1 100644 --- a/ntex-util/src/time/mod.rs +++ b/ntex-util/src/time/mod.rs @@ -454,6 +454,13 @@ mod tests { dl.reset(Millis::ZERO); assert!(lazy(|cx| dl.poll_elapsed(cx)).await.is_pending()); + let mut dl = deadline(Millis(1)); + dl.reset(Millis(100)); + let first_time = now(); + dl.await; + let second_time = now(); + assert!(second_time - first_time >= time::Duration::from_millis(100)); + let mut dl = deadline(Millis(0)); assert!(dl.is_elapsed()); dl.reset(Millis(1)); diff --git a/ntex/src/web/middleware/compress.rs b/ntex/src/web/middleware/compress.rs index 0072021f..2bf38126 100644 --- a/ntex/src/web/middleware/compress.rs +++ b/ntex/src/web/middleware/compress.rs @@ -163,3 +163,74 @@ impl AcceptEncoding { ContentEncoding::Identity } } + +#[cfg(test)] +mod tests { + use std::cmp::Ordering; + + use super::*; + + #[test] + fn test_accepting_encodings_equal() { + let accepting_encoding = AcceptEncoding { + encoding: ContentEncoding::Auto, + quality: 0.0, + }; + let accepting_encoding2 = AcceptEncoding { + encoding: ContentEncoding::Br, + quality: 0.0, + }; + + assert!(accepting_encoding == accepting_encoding2); + } + + #[test] + fn test_accepting_encodings_not_equal() { + let accepting_encoding = AcceptEncoding { + encoding: ContentEncoding::Auto, + quality: 1.0, + }; + let accepting_encoding2 = AcceptEncoding { + encoding: ContentEncoding::Br, + quality: 0.0, + }; + + assert!(accepting_encoding != accepting_encoding2); + } + + #[test] + fn test_accepting_encodings_cmp_order_less() { + let accepting_encoding = AcceptEncoding { + encoding: ContentEncoding::Auto, + quality: 1.0, + }; + let accepting_encoding2 = AcceptEncoding { + encoding: ContentEncoding::Br, + quality: 0.0, + }; + + assert_eq!(accepting_encoding.cmp(&accepting_encoding2), Ordering::Less); + } + + #[test] + fn test_accepting_encodings_cmp_order_equal() { + let accepting_encoding = AcceptEncoding { + encoding: ContentEncoding::Auto, + quality: 1.0, + }; + + assert_eq!(accepting_encoding.cmp(&accepting_encoding), Ordering::Equal); + } + + #[test] + fn test_accepting_encoding_from_tag_with_valid_quality() { + let accepting_encoding = AcceptEncoding::new("gzip;0.8").unwrap(); + assert_eq!(accepting_encoding.quality, 0.8); + } + + #[test] + fn test_accepting_encoding_from_tag_with_invalid_quality() { + let accepting_encoding = AcceptEncoding::new("gzip;q=abc").unwrap(); + assert_eq!(accepting_encoding.quality, 0.0); + } +} diff --git a/ntex/src/web/middleware/defaultheaders.rs b/ntex/src/web/middleware/defaultheaders.rs index 2e9d9ae9..54b96605 100644 --- a/ntex/src/web/middleware/defaultheaders.rs +++ b/ntex/src/web/middleware/defaultheaders.rs @@ -175,6 +175,18 @@ mod tests { assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002"); } + #[crate::rt_test] + #[should_panic(expected = "Cannot create header name")] + async fn test_invalid_header_name() { + DefaultHeaders::new().header("no existing header name", "0001"); + } + + #[crate::rt_test] + #[should_panic(expected = "Cannot create header value")] + async fn test_invalid_header_value() { + DefaultHeaders::new().header(CONTENT_TYPE, "\n"); + } + #[crate::rt_test] async fn test_content_type() { let srv = |req: WebRequest| async move {