proxy_behavior_test/
proxy_behavior_test.rs

1use ai_lib::{AiClientBuilder, Provider};
2use std::env;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    println!("Testing proxy configuration behavior in detail...\n");
7
8    // Test 1: Check current environment
9    println!("1. Current environment:");
10    match env::var("AI_PROXY_URL") {
11        Ok(url) => println!("   AI_PROXY_URL is set to: {}", url),
12        Err(_) => println!("   AI_PROXY_URL is not set"),
13    }
14    println!();
15
16    // Test 2: Default behavior - should not read environment variable
17    println!("2. Default behavior test:");
18    let _client = AiClientBuilder::new(Provider::Groq).build()?;
19    println!("   ✓ Client created with default settings");
20    println!("   ✓ No automatic proxy configuration from environment");
21    println!();
22
23    // Test 3: Explicit no-proxy setting
24    println!("3. Explicit no-proxy test:");
25    let _client = AiClientBuilder::new(Provider::Groq)
26        .without_proxy()
27        .build()?;
28    println!("   ✓ Client created with explicit no-proxy setting");
29    println!("   ✓ This ensures no proxy is used regardless of environment");
30    println!();
31
32    // Test 4: Use environment variable explicitly
33    println!("4. Environment variable proxy test:");
34    let _client = AiClientBuilder::new(Provider::Groq)
35        .with_proxy(None)
36        .build()?;
37    println!("   ✓ Client created with environment variable proxy (if available)");
38    println!("   ✓ This is the only way to use AI_PROXY_URL now");
39    println!();
40
41    // Test 5: Custom proxy URL
42    println!("5. Custom proxy URL test:");
43    let _client = AiClientBuilder::new(Provider::Groq)
44        .with_proxy(Some("http://custom.proxy.com:8080"))
45        .build()?;
46    println!("   ✓ Client created with custom proxy: http://custom.proxy.com:8080");
47    println!("   ✓ Environment variable is ignored when custom URL is provided");
48    println!();
49
50    // Test 6: Verify the new behavior
51    println!("6. Behavior verification:");
52    println!("   ✓ Before: HttpTransport::new() always read AI_PROXY_URL");
53    println!("   ✓ After:  HttpTransport::new_without_proxy() used by default");
54    println!("   ✓ Before: with_proxy() required string parameter");
55    println!("   ✓ After:  with_proxy(Option<&str>) supports both modes");
56    println!("   ✓ New:   without_proxy() explicitly disables proxy");
57    println!();
58
59    println!("All tests passed! 🎉");
60    println!("\nSummary of changes:");
61    println!("- Default behavior no longer automatically reads AI_PROXY_URL");
62    println!("- Users must explicitly choose proxy behavior:");
63    println!("  * .build() - No proxy, no env var reading");
64    println!("  * .without_proxy() - Explicitly no proxy");
65    println!("  * .with_proxy(None) - Use AI_PROXY_URL environment variable");
66    println!("  * .with_proxy(Some(url)) - Use specific proxy URL");
67    println!("\nThis prevents the issue where clearing environment variables");
68    println!("still resulted in proxy usage due to automatic detection.");
69
70    Ok(())
71}