Skip to main content

mockforge_proxy/
lib.rs

1//! Proxy functionality for forwarding requests to upstream services
2//!
3//! This crate provides proxy/reverse-proxy capabilities for MockForge:
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
10pub mod body_transform;
11pub mod client;
12pub mod conditional;
13pub mod config;
14pub mod handler;
15pub mod middleware;
16pub mod routing;
17
18// Re-export commonly used types
19pub use body_transform::BodyTransformationMiddleware;
20pub use config::{BodyTransform, BodyTransformRule, MigrationMode, TransformOperation};
21pub use middleware::*;
22pub use routing::*;
23
24pub use client::{ProxyClient, ProxyResponse};
25pub use conditional::{evaluate_proxy_condition, find_matching_rule};
26pub use config::{ProxyConfig, ProxyRule};
27pub use handler::ProxyHandler;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use axum::http::Method;
33
34    #[test]
35    fn test_proxy_config() {
36        let mut config = ProxyConfig::new("http://api.example.com".to_string());
37        config.enabled = true;
38        assert!(config.should_proxy(&Method::GET, "/proxy/users"));
39        assert!(!config.should_proxy(&Method::GET, "/api/users"));
40
41        let stripped = config.strip_prefix("/proxy/users");
42        assert_eq!(stripped, "/users");
43    }
44
45    #[test]
46    fn test_proxy_config_no_prefix() {
47        let mut config = ProxyConfig::new("http://api.example.com".to_string());
48        config.prefix = None;
49        config.enabled = true;
50
51        assert!(config.should_proxy(&Method::GET, "/api/users"));
52        assert!(config.should_proxy(&Method::GET, "/any/path"));
53
54        let stripped = config.strip_prefix("/api/users");
55        assert_eq!(stripped, "/api/users");
56    }
57
58    #[test]
59    fn test_proxy_config_with_rules() {
60        let mut config = ProxyConfig::new("http://default.example.com".to_string());
61        config.enabled = true;
62        config.rules.push(ProxyRule {
63            path_pattern: "/api/users/*".to_string(),
64            target_url: "http://users.example.com".to_string(),
65            enabled: true,
66            pattern: "/api/users/*".to_string(),
67            upstream_url: "http://users.example.com".to_string(),
68            migration_mode: MigrationMode::Auto,
69            migration_group: None,
70            condition: None,
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            migration_mode: MigrationMode::Auto,
79            migration_group: None,
80            condition: None,
81        });
82
83        assert!(config.should_proxy(&Method::GET, "/api/users/123"));
84        assert!(config.should_proxy(&Method::GET, "/api/orders/456"));
85
86        assert_eq!(config.get_upstream_url("/api/users/123"), "http://users.example.com");
87        assert_eq!(config.get_upstream_url("/api/orders/456"), "http://orders.example.com");
88        assert_eq!(config.get_upstream_url("/api/products"), "http://default.example.com");
89    }
90
91    #[test]
92    fn test_proxy_config_passthrough() {
93        let mut config = ProxyConfig::new("http://api.example.com".to_string());
94        config.passthrough_by_default = true;
95        config.prefix = None;
96        config.enabled = true;
97
98        assert!(config.should_proxy(&Method::GET, "/api/users"));
99        assert!(config.should_proxy(&Method::POST, "/api/orders"));
100
101        config.passthrough_by_default = false;
102        config.prefix = Some("/proxy".to_string());
103
104        assert!(config.should_proxy(&Method::GET, "/proxy/users"));
105        assert!(!config.should_proxy(&Method::GET, "/api/users"));
106    }
107}