diff --git a/src/config/service/http/middleware.rs b/src/config/service/http/middleware.rs index 8a41f5c3..1289785f 100644 --- a/src/config/service/http/middleware.rs +++ b/src/config/service/http/middleware.rs @@ -170,8 +170,39 @@ impl MiddlewareConfig { #[cfg(test)] mod tests { use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; use serde_json::Value; + #[rstest] + #[case(true, None, true)] + #[case(true, Some(true), true)] + #[case(true, Some(false), false)] + #[case(false, None, false)] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn common_config_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let common_config = CommonConfig { + enable, + ..Default::default() + }; + + // Act/Assert + assert_eq!(common_config.enabled(&context), expected_enabled); + } + #[test] fn custom_config() { // Note: since we're parsing into a Middleware config struct directly, we don't diff --git a/src/service/http/middleware/catch_panic.rs b/src/service/http/middleware/catch_panic.rs index 0ae9342d..7c1bab6e 100644 --- a/src/service/http/middleware/catch_panic.rs +++ b/src/service/http/middleware/catch_panic.rs @@ -45,3 +45,40 @@ impl Middleware for CatchPanicMiddleware { Ok(router) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .catch_panic + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = CatchPanicMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +} diff --git a/src/service/http/middleware/compression.rs b/src/service/http/middleware/compression.rs index 565a4854..14028a60 100644 --- a/src/service/http/middleware/compression.rs +++ b/src/service/http/middleware/compression.rs @@ -88,3 +88,69 @@ impl Middleware for RequestDecompressionMiddleware Ok(router) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn response_compression_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .response_compression + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = ResponseCompressionMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn request_decompression_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .request_decompression + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = RequestDecompressionMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +} diff --git a/src/service/http/middleware/request_id.rs b/src/service/http/middleware/request_id.rs index f5da8ef9..7847e547 100644 --- a/src/service/http/middleware/request_id.rs +++ b/src/service/http/middleware/request_id.rs @@ -137,3 +137,69 @@ impl Middleware for PropagateRequestIdMiddleware { Ok(router) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn set_request_id_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .set_request_id + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = SetRequestIdMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn propagate_request_id_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .propagate_request_id + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = PropagateRequestIdMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +} diff --git a/src/service/http/middleware/sensitive_headers.rs b/src/service/http/middleware/sensitive_headers.rs index a3aa1524..4d876cde 100644 --- a/src/service/http/middleware/sensitive_headers.rs +++ b/src/service/http/middleware/sensitive_headers.rs @@ -56,7 +56,6 @@ pub struct SensitiveResponseHeadersConfig { } pub struct SensitiveRequestHeadersMiddleware; - impl Middleware for SensitiveRequestHeadersMiddleware { fn name(&self) -> String { "sensitive-request-headers".to_string() @@ -104,7 +103,6 @@ impl Middleware for SensitiveRequestHeadersMiddlewa } pub struct SensitiveResponseHeadersMiddleware; - impl Middleware for SensitiveResponseHeadersMiddleware { fn name(&self) -> String { "sensitive-response-headers".to_string() @@ -150,3 +148,69 @@ impl Middleware for SensitiveResponseHeadersMiddlew Ok(router) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn sensitive_request_headers_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .sensitive_request_headers + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = SensitiveRequestHeadersMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn sensitive_response_headers_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .sensitive_response_headers + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = SensitiveResponseHeadersMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +} diff --git a/src/service/http/middleware/size_limit.rs b/src/service/http/middleware/size_limit.rs index 276beff1..643a6dd1 100644 --- a/src/service/http/middleware/size_limit.rs +++ b/src/service/http/middleware/size_limit.rs @@ -77,3 +77,40 @@ impl Middleware for RequestBodyLimitMiddleware { Ok(router) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn size_limit_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config + .service + .http + .custom + .middleware + .size_limit + .common + .enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = RequestBodyLimitMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +} diff --git a/src/service/http/middleware/timeout.rs b/src/service/http/middleware/timeout.rs index 50f32154..601e7c4b 100644 --- a/src/service/http/middleware/timeout.rs +++ b/src/service/http/middleware/timeout.rs @@ -69,3 +69,33 @@ impl Middleware for TimeoutMiddleware { Ok(router) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn timeout_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config.service.http.custom.middleware.timeout.common.enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = TimeoutMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +} diff --git a/src/service/http/middleware/tracing.rs b/src/service/http/middleware/tracing.rs index 53e3db42..fa59eb9c 100644 --- a/src/service/http/middleware/tracing.rs +++ b/src/service/http/middleware/tracing.rs @@ -166,3 +166,33 @@ impl OnResponse for CustomOnResponse { self.default.on_response(response, latency, span); } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(false, Some(true), true)] + #[case(false, Some(false), false)] + fn tracing_enabled( + #[case] default_enable: bool, + #[case] enable: Option, + #[case] expected_enabled: bool, + ) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + config.service.http.custom.middleware.tracing.common.enable = enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + let middleware = TracingMiddleware; + + // Act/Assert + assert_eq!(middleware.enabled(&context), expected_enabled); + } +}