mod http_policy_tests { use super::*; fn validation() -> ValidationPolicy { ValidationPolicy { tls_addr: None, cert_dir: None, data_dir: None, proxy_addr: "127.0.0.1:8080".parse().unwrap(), admin_addr: "127.0.0.1:9090".parse().unwrap(), } } fn settings(stage: &str, action: serde_json::Value) -> crate::policy::Settings { serde_json::from_value( serde_json::json!({"rules":[{"id":"test","stage":stage,"actions":[action]}]}), ) .unwrap() } #[test] fn legacy_configuration_defaults_to_no_http_policies() { let config: Config = serde_json::from_str("{}").unwrap(); assert!(config.policy.is_empty()); assert!(serde_json::to_value(&config) .unwrap() .get("policy") .is_none()); let snapshot = compile( demo_config("127.0.0.1:9000".parse().unwrap()), 1, &validation(), ) .unwrap(); assert!(snapshot.policy.is_empty()); let route = snapshot.select("localhost", "/").unwrap(); assert!(route.policy.is_empty()); assert!(!route.frontend.as_ref().unwrap().has_policies); } #[test] fn location_pre_route_and_unknown_backend_are_rejected_before_activation() { let mut config = demo_config("127.0.0.1:9000".parse().unwrap()); config.vhosts[0].routes[0].policy = settings( "pre_route", serde_json::json!({"type":"set_path","value":"/new"}), ); assert!(compile(config.clone(), 1, &validation()) .unwrap_err() .to_string() .contains("pre_route")); config.vhosts[0].routes[0].policy = settings( "request", serde_json::json!({"type":"use_backend","id":"missing"}), ); assert!(compile(config.clone(), 1, &validation()) .unwrap_err() .to_string() .contains("backend")); config.vhosts[0].routes[0].policy = settings( "request", serde_json::json!({"type":"use_backend","id":"demo"}), ); assert!(compile(config, 1, &validation()).is_ok()); } #[test] fn policy_on_legacy_root_route_is_preserved_instead_of_migrated_away() { let mut config = demo_config("127.0.0.1:9000".parse().unwrap()); config.vhosts[0].routes[0].policy = settings( "after_response", serde_json::json!({"type":"set_header","name":"X-Location","value":"root"}), ); assert!(!config.vhosts[0].routes[0].is_legacy_main_fallback()); let snapshot = compile(config, 1, &validation()).unwrap(); let route = snapshot.select("localhost", "/").unwrap(); assert!(!route.policy.is_empty()); assert!(route.frontend.as_ref().unwrap().has_policies); } }