diff options
Diffstat (limited to 'tests/support')
| -rw-r--r-- | tests/support/factory_bot.rs | 58 | ||||
| -rw-r--r-- | tests/support/mod.rs | 1 |
2 files changed, 59 insertions, 0 deletions
diff --git a/tests/support/factory_bot.rs b/tests/support/factory_bot.rs new file mode 100644 index 00000000..15c6f1f3 --- /dev/null +++ b/tests/support/factory_bot.rs @@ -0,0 +1,58 @@ +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; +use std::net::SocketAddr; +use tonic::transport::Channel; + +#[allow(dead_code)] +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 build_request(f: impl std::ops::FnOnce(&mut HttpRequest)) -> 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))); + })); + })); + }) +} + +pub fn build_headers(headers: Vec<(String, String)>) -> HashMap<String, String> { + return build_with(|item: &mut HashMap<String, String>| { + for (key, value) in headers { + item.insert(key, value); + } + }); +} + +pub fn build_cedar_authorizer() -> authzd::CedarAuthorizer { + let realpath = std::fs::canonicalize("./etc/authzd").unwrap(); + let path = realpath.as_path(); + authzd::CedarAuthorizer::new_from(path) +} + +pub async fn build_channel(addr: SocketAddr) -> Channel { + Channel::from_shared(format!("http://{}", addr)) + .expect("Failed to create channel") + .connect() + .await + .expect("Failed to connect to server") +} + +pub async fn build_rpc_client<T, F>(addr: SocketAddr, f: F) -> T +where + F: FnOnce(Channel) -> T, +{ + f(build_channel(addr).await) +} diff --git a/tests/support/mod.rs b/tests/support/mod.rs new file mode 100644 index 00000000..5e2a6d78 --- /dev/null +++ b/tests/support/mod.rs @@ -0,0 +1 @@ +pub mod factory_bot; |
