mockforge_core/
proxy.rs

1//! Proxy functionality for forwarding requests to upstream services
2//!
3//! This module has been refactored into sub-modules for better organization:
4//! - config: Proxy configuration and rule management
5//! - handler: Request/response handling and processing
6//! - client: HTTP client functionality for upstream requests
7//! - middleware: Proxy middleware and request transformation
8//! - routing: Route matching and rule evaluation
9
10// Re-export sub-modules for backward compatibility
11pub mod client;
12pub mod config;
13pub mod handler;
14pub mod middleware;
15pub mod routing;
16
17// Re-export commonly used types
18pub use middleware::*;
19pub use routing::*;
20
21// Legacy imports for compatibility
22
23pub use client::{ProxyClient, ProxyResponse};
24/// Legacy types and structures - moved to sub-modules
25/// These are kept for backward compatibility
26// Re-export the main types from sub-modules
27pub use config::{ProxyConfig, ProxyRule};
28pub use handler::ProxyHandler;
29
30// The config and handler modules provide the methods directly
31
32#[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        // With passthrough enabled, all requests should be proxied
96        assert!(config.should_proxy(&Method::GET, "/api/users"));
97        assert!(config.should_proxy(&Method::POST, "/api/orders"));
98
99        // Disable passthrough
100        config.passthrough_by_default = false;
101        config.prefix = Some("/proxy".to_string());
102
103        // Now only requests with the prefix should be proxied
104        assert!(config.should_proxy(&Method::GET, "/proxy/users"));
105        assert!(!config.should_proxy(&Method::GET, "/api/users"));
106    }
107}