Skip to content

Commit

Permalink
Add tests for the Middleware::enabled method for each middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski committed May 15, 2024
1 parent 036810e commit 63d4dc5
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/config/service/http/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,39 @@ impl<T: Default> MiddlewareConfig<T> {
#[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<bool>,
#[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
Expand Down
37 changes: 37 additions & 0 deletions src/service/http/middleware/catch_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,40 @@ impl<S: Send + Sync + 'static> Middleware<S> 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<bool>,
#[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);
}
}
66 changes: 66 additions & 0 deletions src/service/http/middleware/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,69 @@ impl<S: Send + Sync + 'static> Middleware<S> 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<bool>,
#[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<bool>,
#[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);
}
}
66 changes: 66 additions & 0 deletions src/service/http/middleware/request_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,69 @@ impl<S: Send + Sync + 'static> Middleware<S> 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<bool>,
#[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<bool>,
#[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);
}
}
68 changes: 66 additions & 2 deletions src/service/http/middleware/sensitive_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub struct SensitiveResponseHeadersConfig {
}

pub struct SensitiveRequestHeadersMiddleware;

impl<S: Send + Sync + 'static> Middleware<S> for SensitiveRequestHeadersMiddleware {
fn name(&self) -> String {
"sensitive-request-headers".to_string()
Expand Down Expand Up @@ -104,7 +103,6 @@ impl<S: Send + Sync + 'static> Middleware<S> for SensitiveRequestHeadersMiddlewa
}

pub struct SensitiveResponseHeadersMiddleware;

impl<S: Send + Sync + 'static> Middleware<S> for SensitiveResponseHeadersMiddleware {
fn name(&self) -> String {
"sensitive-response-headers".to_string()
Expand Down Expand Up @@ -150,3 +148,69 @@ impl<S: Send + Sync + 'static> Middleware<S> 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<bool>,
#[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<bool>,
#[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);
}
}
37 changes: 37 additions & 0 deletions src/service/http/middleware/size_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,40 @@ impl<S: Send + Sync + 'static> Middleware<S> 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<bool>,
#[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);
}
}
30 changes: 30 additions & 0 deletions src/service/http/middleware/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,33 @@ impl<S: Send + Sync + 'static> Middleware<S> 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<bool>,
#[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);
}
}
Loading

0 comments on commit 63d4dc5

Please sign in to comment.