diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-25 14:39:13 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-25 14:39:13 -0600 |
| commit | ab2a06d2a1e63cdedfd06717114f128e1e9dc1a8 (patch) | |
| tree | 91377a31c98915d08a8c0ff59e19633dfc91be2b /tests | |
| parent | 86c9564a82f56b7c5ee60f4bff9fb07ca3e4a6eb (diff) | |
test: start to extract builders
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/authorization/cedar_authorizer_test.rs | 10 | ||||
| -rw-r--r-- | tests/common/factories.rs | 0 | ||||
| -rw-r--r-- | tests/common/mod.rs | 20 |
3 files changed, 22 insertions, 8 deletions
diff --git a/tests/authorization/cedar_authorizer_test.rs b/tests/authorization/cedar_authorizer_test.rs index 2ed3dd68..4531da67 100644 --- a/tests/authorization/cedar_authorizer_test.rs +++ b/tests/authorization/cedar_authorizer_test.rs @@ -9,12 +9,12 @@ mod tests { #[test] fn test_cedar_authorizer_allows_valid_token() { let authorizer = CedarAuthorizer::new(); - let mut headers = HashMap::new(); - headers.insert( - "authorization".to_string(), - "Bearer valid-token".to_string(), - ); let request = create_request(|item: &mut HttpRequest| { + let mut headers = HashMap::new(); + headers.insert( + "authorization".to_string(), + "Bearer valid-token".to_string(), + ); item.headers = headers; }); diff --git a/tests/common/factories.rs b/tests/common/factories.rs new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/tests/common/factories.rs diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 4e879b6f..aab3a412 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,10 +1,24 @@ +mod factories; + use envoy_types::ext_authz::v3::pb::CheckRequest; use envoy_types::pb::envoy::service::auth::v3::AttributeContext; use envoy_types::pb::envoy::service::auth::v3::attribute_context::{HttpRequest, Request}; use std::collections::HashMap; +pub fn build<T: Default>() -> T { + return please::build(); +} + +pub fn build_with<T, F>(initializer: F) -> T +where + T: Default, + F: std::ops::FnOnce(&mut T), +{ + return please::build_with(initializer); +} + pub fn create_request(f: impl std::ops::FnOnce(&mut HttpRequest)) -> CheckRequest { - please::build_with(|item: &mut CheckRequest| { + build_with(|item: &mut CheckRequest| { item.attributes = Some(please::build_with(|item: &mut AttributeContext| { item.request = Some(please::build_with(|item: &mut Request| { item.http = Some(please::build_with(|item: &mut HttpRequest| f(item))); @@ -22,8 +36,8 @@ pub fn create_test_request_with_headers( } pub fn create_headers_with_auth(auth_value: &str) -> HashMap<String, String> { - let mut headers = HashMap::new(); - headers.insert("authorization".to_string(), auth_value.to_string()); + let mut headers = build::<HashMap<String, String>>(); + headers.insert(String::from("authorization"), auth_value.to_string()); headers } |
