v0.41.0
This commit is contained in:
1 parent
27b640a0df
commit
aa27866531
46 files changed
+3368
-189
No files matched your search
@@ -0,0 +1,159 @@
|
||||
#[cfg(test)]
|
||||
mod routing_conditions_tests {
|
||||
use super::*;
|
||||
|
||||
const FIXTURE: &str = include_str!("../../tests/fixtures/routing_conditions_cases.json");
|
||||
|
||||
fn policy() -> ValidationPolicy {
|
||||
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,
|
||||
}
|
||||
}
|
||||
fn config(vhost: serde_json::Value) -> anyhow::Result<Snapshot> {
|
||||
let cfg = serde_json::from_value(serde_json::json!({
|
||||
"listeners":{"endpoints":[{"id":"http","binding":{"address":"127.0.0.1:8080"}}]},
|
||||
"vhosts":[vhost]
|
||||
}))?;
|
||||
compile(cfg, 0, &policy())
|
||||
}
|
||||
|
||||
/// Shared with tools/dev/test_routing_tester.js (panel tester parity).
|
||||
#[test]
|
||||
fn fixture_cases_select_the_expected_location() {
|
||||
let fixture: serde_json::Value = serde_json::from_str(FIXTURE).unwrap();
|
||||
let snapshot = config(fixture["vhost"].clone()).unwrap();
|
||||
let local: std::net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
|
||||
for case in fixture["cases"].as_array().unwrap() {
|
||||
let name = case["name"].as_str().unwrap();
|
||||
let method: http::Method = case["method"].as_str().unwrap_or("GET").parse().unwrap();
|
||||
let host = case["host"].as_str().unwrap_or("site.test");
|
||||
let path = case["path"].as_str().unwrap();
|
||||
let mut headers = http::HeaderMap::new();
|
||||
if let Some(map) = case["headers"].as_object() {
|
||||
for (key, value) in map {
|
||||
headers.insert(
|
||||
http::HeaderName::from_bytes(key.as_bytes()).unwrap(),
|
||||
value.as_str().unwrap().parse().unwrap(),
|
||||
);
|
||||
}
|
||||
}
|
||||
let facts = crate::vhost::RouteRequest {
|
||||
method: &method,
|
||||
headers: &headers,
|
||||
query: case["query"].as_str(),
|
||||
host,
|
||||
client_ip: case["client_ip"].as_str().unwrap_or("203.0.113.5").parse().unwrap(),
|
||||
country: case["country"].as_str(),
|
||||
};
|
||||
let route = snapshot
|
||||
.select_on_request(host, path, local, false, Some(&facts))
|
||||
.unwrap_or_else(|| panic!("{name}: no route"));
|
||||
assert_eq!(route.name, case["expect"].as_str().unwrap(), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conditional_locations_are_skipped_without_request_facts_and_never_cached() {
|
||||
let fixture: serde_json::Value = serde_json::from_str(FIXTURE).unwrap();
|
||||
let snapshot = config(fixture["vhost"].clone()).unwrap();
|
||||
let local: std::net::SocketAddr = "127.0.0.1:8080".parse().unwrap();
|
||||
let route = snapshot.select_on("site.test", "/api/x", local, false).unwrap();
|
||||
assert_eq!(route.name, "api");
|
||||
let route = snapshot.select_on("site.test", "/a.mp3", local, false).unwrap();
|
||||
assert_eq!(route.name, "mp3");
|
||||
let frontend = snapshot.frontend("site.test", local, false).unwrap();
|
||||
assert!(frontend.conditional_routes);
|
||||
assert!(frontend.route_conditions_country);
|
||||
let fixture_routes = fixture["vhost"]["routes"].as_array().unwrap();
|
||||
let conditional = fixture_routes
|
||||
.iter()
|
||||
.filter(|r| r.get("conditions").is_some())
|
||||
.count();
|
||||
assert!(conditional >= 5);
|
||||
// A conditional location never uses the cache.
|
||||
let headers = {
|
||||
let mut h = http::HeaderMap::new();
|
||||
h.insert("x-beta", "1".parse().unwrap());
|
||||
h
|
||||
};
|
||||
let facts = crate::vhost::RouteRequest {
|
||||
method: &http::Method::GET,
|
||||
headers: &headers,
|
||||
query: None,
|
||||
host: "site.test",
|
||||
client_ip: "203.0.113.5".parse().unwrap(),
|
||||
country: None,
|
||||
};
|
||||
let beta = snapshot
|
||||
.select_on_request("site.test", "/api/x", local, false, Some(&facts))
|
||||
.unwrap();
|
||||
assert_eq!(beta.name, "api-beta");
|
||||
assert_eq!(beta.cache, Some(false));
|
||||
assert_eq!(beta.pool.as_ref().unwrap().members.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_conditions_are_rejected() {
|
||||
let vhost = |routes: serde_json::Value| {
|
||||
serde_json::json!({
|
||||
"id":"v","frontend":{"hosts":["v.test"],"listener_ids":["http"]},
|
||||
"backends":[{"id":"a","url":"http://127.0.0.1:9001","allow_private":true}],
|
||||
"load_balancing":{"health_check":{"mode":"off"}},
|
||||
"routes":routes
|
||||
})
|
||||
};
|
||||
for routes in [
|
||||
serde_json::json!([{"id":"x","match_type":"any"}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"header","values":["1"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"header","name":"bad name","values":["1"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"ip","values":["10.0.0.0/8"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"ip","operator":"cidr","values":["nope"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"method","operator":"prefix","values":["GE"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"country","values":["POL"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"header","name":"a","operator":"exists","values":["1"]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"header","name":"a","operator":"regex","values":["("]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","conditions":[{"source":"header","name":"a","values":[]}]}]),
|
||||
serde_json::json!([{"id":"x","path_prefix":"/x","cache":true,"conditions":[{"source":"method","values":["GET"]}]}]),
|
||||
serde_json::json!([{"id":"x","internal":true,"conditions":[{"source":"method","values":["GET"]}]}]),
|
||||
serde_json::json!([
|
||||
{"id":"x","match_type":"regex","path_prefix":"\\.js$"},
|
||||
{"id":"y","match_type":"regex","path_prefix":"\\.js$","conditions":[{"source":"method","values":["GET"]}]}
|
||||
]),
|
||||
serde_json::json!([
|
||||
{"id":"x","path_prefix":"/x"},
|
||||
{"id":"y","path_prefix":"/x"}
|
||||
]),
|
||||
] {
|
||||
assert!(config(vhost(routes.clone())).is_err(), "{routes} should be rejected");
|
||||
}
|
||||
// A conditional copy before the unconditional regex is fine, and two
|
||||
// conditional prefixes may share a path.
|
||||
assert!(config(vhost(serde_json::json!([
|
||||
{"id":"y","match_type":"regex","path_prefix":"\\.js$","conditions":[{"source":"method","values":["GET"]}]},
|
||||
{"id":"x","match_type":"regex","path_prefix":"\\.js$"},
|
||||
{"id":"p1","path_prefix":"/p","conditions":[{"source":"header","name":"a","operator":"exists"}]},
|
||||
{"id":"p2","path_prefix":"/p","conditions":[{"source":"header","name":"b","operator":"exists"}]}
|
||||
])))
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_routes_serialize_without_new_fields() {
|
||||
let route: crate::vhost::PathRoute =
|
||||
serde_json::from_value(serde_json::json!({"id":"a","path_prefix":"/a"})).unwrap();
|
||||
let value = serde_json::to_value(&route).unwrap();
|
||||
for key in ["conditions", "backend_ids", "load_balancing"] {
|
||||
assert!(value.get(key).is_none(), "{key} should be omitted");
|
||||
}
|
||||
let condition: crate::vhost::RouteCondition = serde_json::from_value(
|
||||
serde_json::json!({"source":"header","name":"X-A","values":["1"]}),
|
||||
)
|
||||
.unwrap();
|
||||
let value = serde_json::to_value(&condition).unwrap();
|
||||
assert!(value.get("negate").is_none() && value.get("ignore_case").is_none());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user