From 7bda8947c80fc507b722f321977522bd50377c17 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 27 Jun 2025 15:02:17 -0600 Subject: test: move helpers to factory_bot module --- tests/authorization/server_test.rs | 20 +++----------------- tests/support/factory_bot.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'tests') diff --git a/tests/authorization/server_test.rs b/tests/authorization/server_test.rs index 40cc270f..55645dd4 100644 --- a/tests/authorization/server_test.rs +++ b/tests/authorization/server_test.rs @@ -1,8 +1,8 @@ #[cfg(test)] mod tests { + use crate::support::factory_bot::*; use std::net::SocketAddr; use tokio::net::TcpListener; - use tonic::transport::Channel; async fn available_port() -> SocketAddr { let listener = TcpListener::bind("127.0.0.1:0") @@ -26,25 +26,11 @@ mod tests { (addr, handle) } - 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") - } - - async fn build_client(addr: SocketAddr, f: F) -> T - where - F: FnOnce(Channel) -> T, - { - f(build_channel(addr).await) - } - #[tokio::test] async fn test_health_check_service() { let (addr, server) = start_server().await; - let mut client = build_client(addr, tonic_health::pb::health_client::HealthClient::new).await; + let mut client = + build_rpc_client(addr, tonic_health::pb::health_client::HealthClient::new).await; let request = tonic::Request::new(tonic_health::pb::HealthCheckRequest { service: String::new(), diff --git a/tests/support/factory_bot.rs b/tests/support/factory_bot.rs index c64b53a2..15c6f1f3 100644 --- a/tests/support/factory_bot.rs +++ b/tests/support/factory_bot.rs @@ -2,6 +2,8 @@ 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 { @@ -39,3 +41,18 @@ pub fn build_cedar_authorizer() -> authzd::CedarAuthorizer { 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(addr: SocketAddr, f: F) -> T +where + F: FnOnce(Channel) -> T, +{ + f(build_channel(addr).await) +} -- cgit v1.2.3