From ab2a06d2a1e63cdedfd06717114f128e1e9dc1a8 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 25 Jun 2025 14:39:13 -0600 Subject: test: start to extract builders --- tests/authorization/cedar_authorizer_test.rs | 10 +++++----- tests/common/factories.rs | 0 tests/common/mod.rs | 20 +++++++++++++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 tests/common/factories.rs (limited to 'tests') 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 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 { + return please::build(); +} + +pub fn build_with(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 { - let mut headers = HashMap::new(); - headers.insert("authorization".to_string(), auth_value.to_string()); + let mut headers = build::>(); + headers.insert(String::from("authorization"), auth_value.to_string()); headers } -- cgit v1.2.3