Skip to main content

agent_proxy_rust_core/
testing.rs

1//! Shared test helpers for the core crate and downstream middleware crates.
2
3use bytes::Bytes;
4use http::{HeaderMap, Method};
5
6use crate::types::{AgentType, ApiFormat, ChannelConfig, ConnectionContext, ProxyRequest};
7
8/// Creates a minimal test [`ProxyRequest`] for `/v1/messages`.
9#[must_use]
10pub fn test_proxy_request() -> ProxyRequest {
11    ProxyRequest::new(
12        Method::POST,
13        "/v1/messages".into(),
14        HeaderMap::new(),
15        Bytes::from(
16            r#"{"model":"claude-sonnet","max_tokens":1024,"messages":[{"role":"user","content":"hello"}]}"#,
17        ),
18    )
19}
20
21/// Creates a streaming test [`ProxyRequest`].
22#[must_use]
23pub fn test_proxy_request_streaming() -> ProxyRequest {
24    ProxyRequest::new(
25        Method::POST,
26        "/v1/messages".into(),
27        HeaderMap::new(),
28        Bytes::from(
29            r#"{"model":"claude-sonnet","max_tokens":1024,"messages":[{"role":"user","content":"hello"}],"stream":true}"#,
30        ),
31    )
32}
33
34/// Creates a test [`ProxyRequest`] with a custom body.
35#[must_use]
36pub fn test_proxy_request_with_body(body: impl Into<Bytes>) -> ProxyRequest {
37    ProxyRequest::new(
38        Method::POST,
39        "/v1/messages".into(),
40        HeaderMap::new(),
41        body.into(),
42    )
43}
44
45/// Creates a minimal test [`ConnectionContext`].
46#[must_use]
47pub fn test_connection_context() -> ConnectionContext {
48    ConnectionContext::new(
49        1,
50        AgentType::Claude,
51        None,
52        Some(ApiFormat::AnthropicMessages),
53    )
54}
55
56/// Creates a test [`ConnectionContext`] with a specific role.
57#[must_use]
58pub fn test_connection_context_with_role(role: impl Into<String>) -> ConnectionContext {
59    ConnectionContext::new(
60        1,
61        AgentType::Claude,
62        Some(role.into()),
63        Some(ApiFormat::AnthropicMessages),
64    )
65}
66
67/// Creates a test [`ChannelConfig`] pointing to a local upstream.
68#[must_use]
69pub fn test_channel_config(base_url: impl Into<String>) -> ChannelConfig {
70    ChannelConfig {
71        url: base_url.into(),
72        api_key: secrecy::SecretString::from("sk-test-channel-key"),
73        protocol: ApiFormat::AnthropicMessages,
74        name: "test-channel".into(),
75        rewrite_path: None,
76    }
77}
78
79/// Creates a test [`ChannelConfig`] for `OpenAI` Chat.
80#[must_use]
81pub fn test_channel_config_openai(base_url: impl Into<String>) -> ChannelConfig {
82    ChannelConfig {
83        url: base_url.into(),
84        api_key: secrecy::SecretString::from("sk-test-openai-key"),
85        protocol: ApiFormat::OpenaiChat,
86        name: "test-openai-channel".into(),
87        rewrite_path: None,
88    }
89}