mod default_listener_tests { use super::*; use serde_json::json; #[test] fn tls_defaults_to_h3_and_explicit_null_survives_a_roundtrip() { let auto: Listener = serde_json::from_value(json!({"address":"127.0.0.1:8443","tls":true})).unwrap(); assert_eq!(auto.http3, Some(crate::http3::Settings::default())); assert!(auto.http2); let disabled: Listener = serde_json::from_value(json!({"address":"127.0.0.1:8443","tls":true,"http3":null})) .unwrap(); let wire = serde_json::to_value(disabled).unwrap(); assert!(wire["http3"].is_null()); assert!(serde_json::from_value::(wire) .unwrap() .http3 .is_none()); let plain: Listener = serde_json::from_value(json!({"address":"127.0.0.1:8080"})).unwrap(); assert!(plain.http3.is_none()); } #[test] fn listener_plan_inherits_legacy_h2_limits_and_prefers_listener_override() { let mut config = Config { http2: crate::settings::Http2Connections { idle_timeout_seconds: 111, max_connection_lifetime_seconds: 222, drain_timeout_seconds: 33, }, ..Default::default() }; config.listeners.endpoints = serde_json::from_value(json!([ { "id": "tls", "name": "TLS", "scope": "public", "enabled": true, "binding": { "address": "127.0.0.1:8443", "tls": true, "http2": true, "http3": null } }, { "id": "h2c", "name": "H2C", "scope": "internal", "enabled": true, "binding": { "address": "127.0.0.1:8081", "tls": false, "http2": false, "h2c": true, "http2_settings": { "idle_timeout_seconds": 444, "max_connection_lifetime_seconds": 555, "drain_timeout_seconds": 66 }, "http3": null } } ])) .unwrap(); assert!(config.listeners.endpoints[0].binding.http2_settings.is_none()); let policy = ValidationPolicy { proxy_addr: "127.0.0.1:18080".parse().unwrap(), admin_addr: "127.0.0.1:19090".parse().unwrap(), tls_addr: None, cert_dir: None, data_dir: None, }; let plan = listener_plan(&config, &policy).unwrap(); let tls = plan.iter().find(|item| item.address.port() == 8443).unwrap(); assert_eq!(tls.http2_settings, Some(config.http2)); let h2c = plan.iter().find(|item| item.address.port() == 8081).unwrap(); assert_eq!( h2c.http2_settings, Some(crate::settings::Http2Connections { idle_timeout_seconds: 444, max_connection_lifetime_seconds: 555, drain_timeout_seconds: 66, }) ); } #[test] fn content_type_policy_defaults_off_and_normalizes_exact_and_wildcard_types() { let old: ContentTypePolicy = serde_json::from_value(json!({})).unwrap(); assert!(!old.enabled); assert!(!old.blocks(&http::HeaderMap::new())); let mut policy: ContentTypePolicy = serde_json::from_value(json!({ "enabled": true, "blocked": [" Multipart/Form-Data ", "IMAGE/*", "application/problem+json"] })).unwrap(); policy.validate().unwrap(); assert_eq!(policy.blocked, vec!["multipart/form-data", "image/*", "application/problem+json"]); let header = |value: &'static str| { let mut map = http::HeaderMap::new(); map.insert(http::header::CONTENT_TYPE, http::HeaderValue::from_static(value)); map }; assert!(policy.blocks(&header("MULTIPART/FORM-DATA; boundary=abc123"))); assert!(policy.blocks(&header("multipart/form-data;charset=utf-8"))); assert!(policy.blocks(&header("multipart/form-data; boundary=\"a,b\""))); assert!(policy.blocks(&header("image/png"))); assert!(policy.blocks(&header("application/problem+json"))); assert!(!policy.blocks(&header("application/json; charset=utf-8"))); assert!(!policy.blocks(&header("application/json; profile=\"a,b\""))); assert!(policy.blocks(&header("application/json; profile=unquoted,second"))); assert!(policy.blocks(&header("application/json; profile=\"unterminated"))); assert!(!policy.blocks(&header("text/plain"))); assert!(!policy.blocks(&http::HeaderMap::new())); let mut double = header("application/json"); double.append(http::header::CONTENT_TYPE, http::HeaderValue::from_static("multipart/form-data")); assert!(policy.blocks(&double)); assert!(policy.blocks(&header("application/json, multipart/form-data"))); assert!(policy.blocks(&header("application/json / text"))); policy.enabled = false; assert!(!policy.blocks(&header("multipart/form-data"))); assert!(!policy.blocks(&double)); } #[test] fn content_type_policy_rejects_invalid_entries_and_empty_enabled_policy() { for entry in ["multipart", "*/form-data", "image/p*", "text/plain; charset=utf-8", "a/", "a//b", "image /png"] { let mut policy = ContentTypePolicy { enabled: true, blocked: vec![entry.into()] }; assert!(policy.validate().is_err(), "accepted invalid pattern {entry}"); } let mut empty = ContentTypePolicy { enabled: true, blocked: vec![] }; assert!(empty.validate().is_err()); let mut duplicate = ContentTypePolicy { enabled: true, blocked: vec!["TEXT/PLAIN".into(), " text/plain ".into()] }; assert!(duplicate.validate().is_err()); let mut many = ContentTypePolicy { enabled: true, blocked: vec!["text/plain".into(); 65] }; assert!(many.validate().is_err()); } #[test] fn method_policy_normalizes_and_enforces_allow_and_block_modes() { let mut allow: MethodPolicy = serde_json::from_value(json!({ "mode": "allow", "methods": ["get", "HEAD", "post"] })) .unwrap(); allow.validate().unwrap(); assert_eq!(allow.methods, vec!["GET", "HEAD", "POST"]); assert!(allow.allows(&http::Method::GET)); assert!(!allow.allows(&http::Method::DELETE)); assert_eq!(allow.allow_header().unwrap().to_str().unwrap(), "GET, HEAD, POST"); let mut block: MethodPolicy = serde_json::from_value(json!({ "mode": "block", "methods": ["delete"] })) .unwrap(); block.validate().unwrap(); assert!(!block.allows(&http::Method::DELETE)); assert!(block.allows(&http::Method::GET)); assert!(block.allow_header().is_none()); } #[test] fn h2c_is_cleartext_only_and_defaults_off() { let plain: Listener = serde_json::from_value(json!({"address":"127.0.0.1:8080","tls":false})).unwrap(); assert!(!plain.h2c); let h2c: Listener = serde_json::from_value( json!({"address":"127.0.0.1:8080","tls":false,"h2c":true}), ) .unwrap(); assert!(validate_listener(h2c).is_ok()); let invalid: Listener = serde_json::from_value( json!({"address":"127.0.0.1:8443","tls":true,"h2c":true}), ) .unwrap(); assert!(validate_listener(invalid).is_err()); } #[test] fn proxy_timeouts_have_explicit_safe_defaults_but_keep_null_compatibility() { let defaults: ProxyTimeouts = serde_json::from_value(json!({})).unwrap(); assert_eq!(defaults.connect_timeout_seconds, Some(10)); assert_eq!(defaults.response_header_timeout_seconds, Some(120)); assert_eq!(defaults.read_timeout_seconds, Some(60)); assert_eq!(defaults.send_timeout_seconds, Some(60)); let inherited: ProxyTimeouts = serde_json::from_value(json!({"read_timeout_seconds":null})).unwrap(); assert_eq!(inherited.read_timeout_seconds, None); assert_eq!(inherited.connect_timeout_seconds, Some(10)); } #[test] fn vhost_hostname_count_is_not_artificially_capped() { let hosts = (0..40) .map(|index| format!("host-{index}.example.test")) .collect::>(); let mut vhost: Vhost = serde_json::from_value(json!({ "id": "many-hosts", "frontend": {"hosts": hosts, "listener_ids": ["https"]}, "backends": [{"id": "origin", "url": "http://127.0.0.1:9001", "allow_private": true}] })) .unwrap(); assert!(vhost.validate().is_ok()); } #[test] fn error_fallback_requires_internal_target_and_rejects_cycles() { let base = json!({ "id": "fallback-test", "frontend": {"hosts": ["example.test"], "listener_ids": ["http"]}, "backends": [{"id": "origin", "url": "http://127.0.0.1:9001", "allow_private": true}], "error_fallback": {"target": "redirect-check", "statuses": [503, 404, 404]}, "routes": [{ "id": "redirect-check", "internal": true, "path_prefix": "/", "source": {"type": "reverse_proxy"} }] }); let mut valid: Vhost = serde_json::from_value(base.clone()).unwrap(); valid.validate().unwrap(); assert_eq!(valid.error_fallback.as_ref().unwrap().statuses, vec![404, 503]); let mut public_target: Vhost = serde_json::from_value(base.clone()).unwrap(); public_target.routes[0].internal = false; assert!(public_target .validate() .unwrap_err() .to_string() .contains("internal location")); let mut cyclic: Vhost = serde_json::from_value(base).unwrap(); cyclic.routes[0].error_fallback = Some(ErrorFallback { target: "redirect-check".into(), statuses: vec![404], }); assert!(cyclic .validate() .unwrap_err() .to_string() .contains("recursive error fallback")); } #[test] fn collision_explains_addresses_owners_and_protocol_limits() { let mut config = Config::default(); config.listeners.endpoints = serde_json::from_value(json!([ {"id":"public","name":"Public TLS","binding":{"address":"0.0.0.0:8443","tls":true}}, {"id":"private","name":"Private TLS","binding":{"address":"127.0.0.1:8443","tls":true,"http3":{"max_concurrent_connections":12}}} ])).unwrap(); let policy = ValidationPolicy { proxy_addr: "127.0.0.1:8080".parse().unwrap(), admin_addr: "127.0.0.1:9090".parse().unwrap(), tls_addr: None, cert_dir: None, data_dir: None, }; let message = listener_plan(&config, &policy).unwrap_err().to_string(); for expected in [ "0.0.0.0:8443", "127.0.0.1:8443", "Public TLS", "Private TLS", "H3 limits", "max_concurrent_connections", ] { assert!(message.contains(expected), "{message}"); } } } mod websocket_protocol_flags_tests { use super::Vhost; #[test] fn rfc8441_vhost_flag_deserializes_explicitly() { let value = serde_json::json!({ "id": "ws-h2", "frontend": {"hosts": ["example.test"], "listener_ids": ["http"]}, "backends": [{"id": "origin", "url": "http://127.0.0.1:9000", "allow_private": true}], "websockets": true, "websocket_rfc8441": true, "http2_enabled": true }); let host: Vhost = serde_json::from_value(value).unwrap(); assert!(host.websockets && host.websocket_rfc8441 && host.http2_enabled); } #[test] fn rfc9220_vhost_flag_deserializes_explicitly() { let value = serde_json::json!({ "id": "ws-h3", "frontend": {"hosts": ["example.test"], "listener_ids": ["https"]}, "backends": [{"id": "origin", "url": "http://127.0.0.1:9000", "allow_private": true}], "websockets": true, "websocket_rfc9220": true }); let host: Vhost = serde_json::from_value(value).unwrap(); assert!(host.websockets && host.websocket_rfc9220); assert!(r#"{"websocket_rfc9220":true}"#.contains("\"websocket_rfc9220\":true")); } } mod timing_allow_origin_tests { use super::*; fn value(origins: &[&str]) -> Result> { timing_allow_origin_value(&origins.iter().map(|v| v.to_string()).collect::>()) } #[test] fn origins_and_wildcard_are_accepted() { assert!(value(&[]).unwrap().is_none()); assert_eq!(value(&["*"]).unwrap().unwrap(), "*"); assert_eq!( value(&["https://app.example.com", "http://localhost:3000"]).unwrap().unwrap(), "https://app.example.com, http://localhost:3000" ); } #[test] fn paths_wildcard_mixes_and_other_schemes_are_rejected() { for bad in [ &["https://app.example.com/"][..], &["https://app.example.com/path"], &["https://user@app.example.com"], &["ftp://app.example.com"], &["app.example.com"], &["*", "https://app.example.com"], ] { assert!(value(bad).is_err(), "{bad:?}"); } } } mod local_web_service_redirect_tests { use super::Vhost; use serde_json::json; #[test] fn web_service_with_local_redirect_does_not_require_proxy_backend() { let mut host: Vhost = serde_json::from_value(json!({ "id": "web-with-redirect", "frontend": { "hosts": ["example.test"], "listener_ids": ["http"] }, "source": { "type": "web_server", "web_service_id": "site" }, "routes": [ { "id": "root-redirect", "match_type": "exact", "path_prefix": "/", "source": { "type": "reverse_proxy" }, "redirect": { "status": 302, "target": "https://example.test/app/" } }, { "id": "local-error", "match_type": "exact", "path_prefix": "/maintenance", "source": { "type": "reverse_proxy" }, "response": { "status": 503, "body": "Maintenance" } } ] })) .unwrap(); assert!(host.validate().is_ok(), "local actions require no backend"); host.routes.push(serde_json::from_value(json!({ "id": "unused-proxy", "path_prefix": "/disabled/", "enabled": false, "source": { "type": "reverse_proxy" } })).unwrap()); assert!(host.validate().is_ok(), "disabled proxy route needs no backend"); host.routes.last_mut().unwrap().enabled = true; assert!(host.validate().unwrap_err().to_string().contains("backend"), "an active reverse-proxy route still requires a backend"); } }