1pub mod client;
12pub mod config;
13pub mod handler;
14pub mod middleware;
15pub mod routing;
16
17pub use middleware::*;
19pub use routing::*;
20
21pub use client::{ProxyClient, ProxyResponse};
24pub use config::{ProxyConfig, ProxyRule};
28pub use handler::ProxyHandler;
29
30#[cfg(test)]
33mod tests {
34 use super::*;
35 use axum::http::Method;
36
37 #[test]
38 fn test_proxy_config() {
39 let mut config = ProxyConfig::new("http://api.example.com".to_string());
40 config.enabled = true;
41 assert!(config.should_proxy(&Method::GET, "/proxy/users"));
42 assert!(!config.should_proxy(&Method::GET, "/api/users"));
43
44 let stripped = config.strip_prefix("/proxy/users");
45 assert_eq!(stripped, "/users");
46 }
47
48 #[test]
49 fn test_proxy_config_no_prefix() {
50 let mut config = ProxyConfig::new("http://api.example.com".to_string());
51 config.prefix = None;
52 config.enabled = true;
53
54 assert!(config.should_proxy(&Method::GET, "/api/users"));
55 assert!(config.should_proxy(&Method::GET, "/any/path"));
56
57 let stripped = config.strip_prefix("/api/users");
58 assert_eq!(stripped, "/api/users");
59 }
60
61 #[test]
62 fn test_proxy_config_with_rules() {
63 let mut config = ProxyConfig::new("http://default.example.com".to_string());
64 config.enabled = true;
65 config.rules.push(ProxyRule {
66 path_pattern: "/api/users/*".to_string(),
67 target_url: "http://users.example.com".to_string(),
68 enabled: true,
69 pattern: "/api/users/*".to_string(),
70 upstream_url: "http://users.example.com".to_string(),
71 });
72 config.rules.push(ProxyRule {
73 path_pattern: "/api/orders/*".to_string(),
74 target_url: "http://orders.example.com".to_string(),
75 enabled: true,
76 pattern: "/api/orders/*".to_string(),
77 upstream_url: "http://orders.example.com".to_string(),
78 });
79
80 assert!(config.should_proxy(&Method::GET, "/api/users/123"));
81 assert!(config.should_proxy(&Method::GET, "/api/orders/456"));
82
83 assert_eq!(config.get_upstream_url("/api/users/123"), "http://users.example.com");
84 assert_eq!(config.get_upstream_url("/api/orders/456"), "http://orders.example.com");
85 assert_eq!(config.get_upstream_url("/api/products"), "http://default.example.com");
86 }
87
88 #[test]
89 fn test_proxy_config_passthrough() {
90 let mut config = ProxyConfig::new("http://api.example.com".to_string());
91 config.passthrough_by_default = true;
92 config.prefix = None;
93 config.enabled = true;
94
95 assert!(config.should_proxy(&Method::GET, "/api/users"));
97 assert!(config.should_proxy(&Method::POST, "/api/orders"));
98
99 config.passthrough_by_default = false;
101 config.prefix = Some("/proxy".to_string());
102
103 assert!(config.should_proxy(&Method::GET, "/proxy/users"));
105 assert!(!config.should_proxy(&Method::GET, "/api/users"));
106 }
107}