This commit is contained in:
Mateusz Gruszczyński committed 2026-10-22 14:20:03 +02:00
1 parent 133d2f0f91
commit 3ea00045bf
27 files changed
+446 -136

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
mod varnish_compatible_tests {
use super::*;
use http_body_util::BodyExt;
fn headers(cache_control: &str) -> HeaderMap {
let mut h = HeaderMap::new();
h.insert("cache-control", HeaderValue::from_str(cache_control).unwrap());
h
}
#[test]
fn default_and_legacy_serialization_are_explicit() {
assert!(Settings::default().varnish_compatible);
let legacy: Settings = serde_json::from_str(r#"{"enabled":true}"#).unwrap();
assert!(legacy.varnish_compatible);
let strict: Settings = serde_json::from_str(r#"{"varnish_compatible":false}"#).unwrap();
assert!(!strict.varnish_compatible);
let enabled = serde_json::to_value(&legacy).unwrap();
assert_eq!(enabled["varnish_compatible"], true);
}
#[test]
fn client_freshness_is_ignored_only_in_varnish_mode() {
let mut compatible = Settings::default();
let directives = [
"no-cache", "no-store", "max-age=0", "max-age=1",
"min-fresh=600", "max-stale=9999", "only-if-cached", "invalid directive",
];
for value in directives {
let h = headers(value);
assert_eq!(
request_policy(&Method::GET, &h, true, "/style.css", false, &compatible),
RequestPolicy::Use,
"varnish mode should ignore client directive: {value}"
);
let freshness = request_freshness_headers(&h, &compatible);
assert!(!freshness.contains_key("cache-control"));
assert!(h.contains_key("cache-control"), "do not mutate origin request headers");
}
let mut pragma = HeaderMap::new();
pragma.insert("pragma", HeaderValue::from_static("no-cache"));
assert_eq!(request_policy(&Method::GET, &pragma, true, "/", false, &compatible), RequestPolicy::Use);
assert!(!request_freshness_headers(&pragma, &compatible).contains_key("pragma"));
compatible.varnish_compatible = false;
assert_eq!(request_policy(&Method::GET, &headers("no-cache"), true, "/", false, &compatible), RequestPolicy::Revalidate("request_no_cache"));
assert_eq!(request_policy(&Method::GET, &headers("no-store"), true, "/", false, &compatible), RequestPolicy::Bypass("request_no_store"));
assert!(only_if_cached(&headers("only-if-cached")), "strict-only 504 guard still recognizes directive");
}
#[test]
fn vcl_style_request_lookup_preserves_privacy_and_local_rules() {
let mut settings = Settings::default();
for (name, value) in [
("authorization", "Bearer private"),
("cookie", "session=private"),
("if-match", "\"etag\""),
("upgrade", "websocket"),
] {
let mut h = headers("no-cache");
h.insert(http::HeaderName::from_bytes(name.as_bytes()).unwrap(), HeaderValue::from_str(value).unwrap());
assert!(matches!(request_policy(&Method::GET, &h, true, "/index.html", false, &settings), RequestPolicy::Bypass(_)));
}
settings.bypass_query = true;
assert_eq!(request_policy(&Method::GET, &HeaderMap::new(), true, "/style.css", true, &settings), RequestPolicy::Bypass("query_excluded"));
settings.bypass_query = false;
settings.bypass_prefixes = vec!["/private".into()];
assert_eq!(request_policy(&Method::GET, &HeaderMap::new(), true, "/private/account", false, &settings), RequestPolicy::Bypass("path_excluded"));
assert_eq!(request_policy(&Method::POST, &HeaderMap::new(), true, "/style.css", false, &settings), RequestPolicy::Bypass("request_method"));
assert!(settings.ignore_cookie_for_path("/style.css"), "existing static cookie feature remains enabled");
assert!(!settings.ignore_cookie_for_path("/index.html"));
}
#[test]
fn backend_response_policy_matches_varnish_without_weakening_privacy() {
let compatible = Settings::default();
let mut strict = compatible.clone();
strict.varnish_compatible = false;
for control in ["private, max-age=60", "no-store, max-age=60"] {
assert!(plan(StatusCode::OK, &headers(control), &compatible, Duration::ZERO).is_none());
assert!(plan(StatusCode::OK, &headers(control), &strict, Duration::ZERO).is_none());
}
let mut no_cache = headers("no-cache, max-age=60");
no_cache.insert("etag", HeaderValue::from_static("\"origin\""));
assert!(plan(StatusCode::OK, &no_cache, &compatible, Duration::ZERO).is_none());
assert!(plan(StatusCode::OK, &no_cache, &strict, Duration::ZERO).is_some());
assert_eq!(response_reason(StatusCode::OK, &no_cache, &compatible), "response_cache_control");
let mut surrogate = headers("public, max-age=60");
surrogate.insert("surrogate-control", HeaderValue::from_static("max-age=120; NO-STORE"));
assert!(plan(StatusCode::OK, &surrogate, &compatible, Duration::ZERO).is_none());
assert_eq!(response_reason(StatusCode::OK, &surrogate, &compatible), "response_surrogate_control");
assert!(plan(StatusCode::OK, &surrogate, &strict, Duration::ZERO).is_some());
for control in ["max-age=0", "max-age=60, s-maxage=0"] {
assert!(plan(StatusCode::OK, &headers(control), &compatible, Duration::ZERO).is_none());
assert!(plan(StatusCode::OK, &headers(control), &strict, Duration::ZERO).is_some());
}
let mut cookie = headers("max-age=60");
cookie.insert("set-cookie", HeaderValue::from_static("session=private"));
assert!(plan(StatusCode::OK, &cookie, &compatible, Duration::ZERO).is_none());
let mut vary = headers("max-age=60");
vary.insert("vary", HeaderValue::from_static("*"));
assert!(plan(StatusCode::OK, &vary, &compatible, Duration::ZERO).is_none());
}
#[tokio::test]
async fn varnish_style_hard_reload_keeps_hot_hit_and_age_without_origin_validator() {
let cache = Cache::new(1);
let settings = Settings::default();
let ticket = cache.ticket(1, "https", "example.test", "app", "/style.css", &HeaderMap::new()).unwrap();
let mut response = Response::new(Body::from("public css"));
*response.headers_mut() = headers("public, max-age=600");
let plan = plan(StatusCode::OK, response.headers(), &settings, Duration::ZERO).unwrap();
cache.wrap(response, ticket.clone(), plan, &settings).into_body().collect().await.unwrap();
{
let mut store = cache.store.lock().unwrap();
for entry in store.entries.values_mut() {
entry.received = Instant::now() - Duration::from_secs(45);
}
}
let mut request = headers("no-cache, max-age=0, min-fresh=900");
request.insert("pragma", HeaderValue::from_static("no-cache"));
let compatibility_ticket = cache.ticket(1, "https", "example.test", "app", "/style.css", &request).unwrap().with_varnish_compatible(true);
let first = cache.get(&compatibility_ticket).expect("hard reload should keep a fresh HIT");
assert!(first.response.headers().get("age").unwrap().to_str().unwrap().parse::<u64>().unwrap() >= 45);
assert_eq!(first.hits, 1);
let again = cache.get(&compatibility_ticket).unwrap();
assert_eq!(again.hits, 2, "hard reload must not reset HIT counter");
assert_eq!(cache.view().entries, 1);
let strict = cache.ticket(1, "https", "example.test", "app", "/style.css", &request).unwrap();
assert!(cache.get(&strict).is_none(), "strict max-age=0 requires revalidation");
}
#[tokio::test]
async fn ten_second_grace_and_no_unconditional_stale_outside_window() {
let cache = Cache::new(1);
let settings = Settings::default();
let ticket = cache.ticket(1, "https", "example.test", "app", "/asset.js", &HeaderMap::new()).unwrap();
let mut response = Response::new(Body::from("js"));
*response.headers_mut() = headers("public, max-age=60");
let plan = plan(StatusCode::OK, response.headers(), &settings, Duration::ZERO).unwrap();
assert_eq!(plan.stale_while_revalidate, 10);
cache.wrap(response, ticket.clone(), plan, &settings).into_body().collect().await.unwrap();
{
let mut store = cache.store.lock().unwrap();
for entry in store.entries.values_mut() {
entry.received = Instant::now() - Duration::from_secs(65);
}
}
let hit = cache.get(&ticket).expect("grace HIT");
assert!(hit.stale && hit.background_revalidate);
{
let mut store = cache.store.lock().unwrap();
for entry in store.entries.values_mut() {
entry.received = Instant::now() - Duration::from_secs(75);
}
}
assert!(cache.get(&ticket).is_none(), "expired beyond grace does not produce HIT");
}
}