summaryrefslogtreecommitdiff
path: root/tests/support/factory_bot.rs
blob: f06d0b4f83d3d76db73649ac462efd0f3a57e878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use envoy_types::pb::envoy::service::auth::v3::AttributeContext;
use envoy_types::pb::envoy::service::auth::v3::CheckRequest;
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 {
    please::build()
}

pub fn build_with<T, F>(initializer: F) -> T
where
    T: Default,
    F: std::ops::FnOnce(&mut T),
{
    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> {
    build_with(|item: &mut HashMap<String, String>| {
        for (key, value) in headers {
            item.insert(key, value);
        }
    })
}

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)
}

pub fn build_spice_authorizer() -> authzd::authorization::spice::Authorizer {
    authzd::authorization::spice::Authorizer::default()
}