789 lines
26 KiB
Rust
789 lines
26 KiB
Rust
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);
|
|
assert_eq!(Settings::default().varnish_grace_seconds, 60);
|
|
let legacy: Settings = serde_json::from_str(r#"{"enabled":true}"#).unwrap();
|
|
assert!(legacy.varnish_compatible);
|
|
assert_eq!(legacy.varnish_grace_seconds, 60);
|
|
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);
|
|
assert_eq!(enabled["varnish_grace_seconds"], 60);
|
|
let customized: Settings = serde_json::from_str(r#"{"varnish_grace_seconds":17}"#).unwrap();
|
|
assert_eq!(customized.varnish_grace_seconds, 17);
|
|
let mut invalid = customized.clone();
|
|
invalid.varnish_grace_seconds = 604_801;
|
|
assert!(
|
|
invalid.validate().is_err(),
|
|
"oversized grace must be rejected"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn compatible_mode_ignores_hard_reload_but_preserves_client_restrictions() {
|
|
let mut compatible = Settings::default();
|
|
for value in ["no-cache", "max-age=0", "max-age=\"0\"", "max-age=000"] {
|
|
let h = headers(value);
|
|
assert_eq!(
|
|
request_policy(&Method::GET, &h, true, "/style.css", false, &compatible),
|
|
RequestPolicy::Use
|
|
);
|
|
let freshness = request_freshness_headers(&h, &compatible);
|
|
assert!(
|
|
!freshness.contains_key("cache-control"),
|
|
"hard reload must not constrain lookup: {value}"
|
|
);
|
|
assert!(
|
|
h.contains_key("cache-control"),
|
|
"origin request and Vary must remain unchanged"
|
|
);
|
|
}
|
|
for value in [
|
|
"max-age=1",
|
|
"min-fresh=600",
|
|
"max-stale=9999",
|
|
"only-if-cached",
|
|
] {
|
|
let h = headers(value);
|
|
assert_eq!(
|
|
request_policy(&Method::GET, &h, true, "/style.css", false, &compatible),
|
|
RequestPolicy::Use
|
|
);
|
|
assert!(
|
|
request_freshness_headers(&h, &compatible).contains_key("cache-control"),
|
|
"preserve {value}"
|
|
);
|
|
}
|
|
assert_eq!(
|
|
request_policy(
|
|
&Method::GET,
|
|
&headers("no-store"),
|
|
true,
|
|
"/",
|
|
false,
|
|
&compatible
|
|
),
|
|
RequestPolicy::Bypass("request_no_store")
|
|
);
|
|
assert_eq!(
|
|
request_policy(
|
|
&Method::GET,
|
|
&headers("invalid directive"),
|
|
true,
|
|
"/",
|
|
false,
|
|
&compatible
|
|
),
|
|
RequestPolicy::Bypass("request_cache_control_invalid")
|
|
);
|
|
assert!(only_if_cached(&headers("only-if-cached")));
|
|
let h = headers("no-cache, max-age=0, min-fresh=45, max-stale=90, only-if-cached");
|
|
let freshness = request_freshness_headers(&h, &compatible);
|
|
let parsed = directives(&freshness).unwrap();
|
|
assert!(!parsed.contains_key("no-cache"));
|
|
assert!(!parsed.contains_key("max-age"));
|
|
assert_eq!(parsed["min-fresh"].as_deref(), Some("45"));
|
|
assert_eq!(parsed["max-stale"].as_deref(), Some("90"));
|
|
assert!(parsed.contains_key("only-if-cached"));
|
|
let mut pragma = HeaderMap::new();
|
|
pragma.insert("pragma", HeaderValue::from_static("no-cache, custom-token"));
|
|
assert_eq!(
|
|
request_policy(&Method::GET, &pragma, true, "/", false, &compatible),
|
|
RequestPolicy::Use
|
|
);
|
|
assert_eq!(
|
|
request_freshness_headers(&pragma, &compatible)["pragma"],
|
|
"custom-token"
|
|
);
|
|
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("max-age=0"),
|
|
true,
|
|
"/",
|
|
false,
|
|
&compatible
|
|
),
|
|
RequestPolicy::Revalidate("request_max_age")
|
|
);
|
|
assert_eq!(
|
|
request_policy(
|
|
&Method::GET,
|
|
&headers("no-store"),
|
|
true,
|
|
"/",
|
|
false,
|
|
&compatible
|
|
),
|
|
RequestPolicy::Bypass("request_no_store")
|
|
);
|
|
}
|
|
|
|
#[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")
|
|
);
|
|
settings.bypass_prefixes = vec!["/api/".into()];
|
|
for path in ["/api/", "/api/health"] {
|
|
assert_eq!(
|
|
request_policy(&Method::GET, &HeaderMap::new(), true, path, false, &settings),
|
|
RequestPolicy::Bypass("path_excluded"),
|
|
"{path}"
|
|
);
|
|
}
|
|
assert_ne!(
|
|
request_policy(&Method::GET, &HeaderMap::new(), true, "/apix", false, &settings),
|
|
RequestPolicy::Bypass("path_excluded")
|
|
);
|
|
settings.bypass_prefixes = vec!["/private".into()];
|
|
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_preserves_validators_fallback_and_surrogate() {
|
|
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\""));
|
|
let validator_plan = plan(StatusCode::OK, &no_cache, &compatible, Duration::ZERO)
|
|
.expect("no-cache with ETag remains revalidatable without fallback");
|
|
assert!(validator_plan.requires_revalidation);
|
|
assert!(plan(StatusCode::OK, &no_cache, &strict, Duration::ZERO).is_some());
|
|
assert!(plan(
|
|
StatusCode::OK,
|
|
&headers("no-cache, max-age=60"),
|
|
&compatible,
|
|
Duration::ZERO
|
|
)
|
|
.is_none());
|
|
|
|
let mut fallback = compatible.clone();
|
|
fallback.default_ttl_seconds = 42;
|
|
let public_no_cache = headers("public, no-cache, max-age=600");
|
|
let fallback_plan = plan(StatusCode::OK, &public_no_cache, &fallback, Duration::ZERO)
|
|
.expect("explicit fallback TTL for public origin no-cache");
|
|
assert_eq!(fallback_plan.ttl, 42);
|
|
assert!(!fallback_plan.requires_revalidation);
|
|
let nonpublic_no_cache = headers("no-cache, max-age=60");
|
|
assert!(plan(
|
|
StatusCode::OK,
|
|
&nonpublic_no_cache,
|
|
&fallback,
|
|
Duration::ZERO
|
|
)
|
|
.is_none());
|
|
let mut surrogate = public_no_cache.clone();
|
|
surrogate.insert(
|
|
"surrogate-control",
|
|
HeaderValue::from_static("max-age=120; NO-STORE"),
|
|
);
|
|
assert!(plan(StatusCode::OK, &surrogate, &fallback, Duration::ZERO).is_none());
|
|
assert_eq!(
|
|
response_reason(StatusCode::OK, &surrogate, &fallback),
|
|
"response_surrogate_control"
|
|
);
|
|
assert!(
|
|
plan(StatusCode::OK, &surrogate, &strict, Duration::ZERO).is_none(),
|
|
"strict no-cache without ETag remains uncached"
|
|
);
|
|
let mut private = headers("public, private, no-cache");
|
|
private.insert("etag", HeaderValue::from_static("\"secret\""));
|
|
assert!(plan(StatusCode::OK, &private, &fallback, Duration::ZERO).is_none());
|
|
|
|
for control in [
|
|
"max-age=0",
|
|
"max-age=60, s-maxage=0",
|
|
"max-age=0, stale-if-error=120",
|
|
] {
|
|
let p = plan(
|
|
StatusCode::OK,
|
|
&headers(control),
|
|
&compatible,
|
|
Duration::ZERO,
|
|
)
|
|
.expect("zero TTL is retained for max-stale, validation or stale-if-error");
|
|
assert_eq!(p.ttl, 0, "{control}");
|
|
assert_eq!(
|
|
p.stale_while_revalidate, 0,
|
|
"zero TTL must not receive implicit grace"
|
|
);
|
|
}
|
|
let mut cookie = headers("max-age=60");
|
|
cookie.insert("set-cookie", HeaderValue::from_static("session=private"));
|
|
assert!(plan(StatusCode::OK, &cookie, &fallback, Duration::ZERO).is_none());
|
|
let mut vary = headers("max-age=60");
|
|
vary.insert("vary", HeaderValue::from_static("*"));
|
|
assert!(plan(StatusCode::OK, &vary, &fallback, Duration::ZERO).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_zero_grace_and_stale_if_error_are_preserved() {
|
|
let compatible = Settings::default();
|
|
let with_zero = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=60, stale-while-revalidate=0"),
|
|
&compatible,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(with_zero.stale_while_revalidate, 0);
|
|
let with_grace = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=60"),
|
|
&compatible,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(with_grace.stale_while_revalidate, 60);
|
|
let mut custom_settings = compatible.clone();
|
|
custom_settings.varnish_grace_seconds = 25;
|
|
let with_custom_default = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=60"),
|
|
&custom_settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(with_custom_default.stale_while_revalidate, 25);
|
|
custom_settings.varnish_grace_seconds = 0;
|
|
let grace_disabled = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=60"),
|
|
&custom_settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(grace_disabled.stale_while_revalidate, 0);
|
|
custom_settings.varnish_compatible = false;
|
|
custom_settings.varnish_grace_seconds = 60;
|
|
let strict = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=60"),
|
|
&custom_settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(strict.stale_while_revalidate, 0);
|
|
let with_custom = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=60, stale-while-revalidate=30"),
|
|
&compatible,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(with_custom.stale_while_revalidate, 30);
|
|
let zero = plan(
|
|
StatusCode::OK,
|
|
&headers("public, max-age=0, stale-if-error=120"),
|
|
&compatible,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(zero.ttl, 0);
|
|
assert_eq!(zero.stale_if_error, 120);
|
|
assert_eq!(zero.stale_while_revalidate, 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn origin_no_cache_etag_can_still_revalidate_without_fallback() {
|
|
let cache = Cache::new(1);
|
|
let settings = Settings::default();
|
|
let ticket = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/backend.css",
|
|
&HeaderMap::new(),
|
|
)
|
|
.unwrap();
|
|
let mut response = Response::new(Body::from("etag css"));
|
|
*response.headers_mut() = headers("public, no-cache, max-age=600");
|
|
response
|
|
.headers_mut()
|
|
.insert("etag", HeaderValue::from_static("\"origin-v1\""));
|
|
let p = plan(
|
|
StatusCode::OK,
|
|
response.headers(),
|
|
&settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert!(p.requires_revalidation);
|
|
cache
|
|
.wrap(response, ticket.clone(), p, &settings)
|
|
.into_body()
|
|
.collect()
|
|
.await
|
|
.unwrap();
|
|
assert!(
|
|
cache.get(&ticket).is_none(),
|
|
"backend no-cache cannot produce an unvalidated HIT"
|
|
);
|
|
let stored = cache
|
|
.revalidation(&ticket)
|
|
.expect("validator and body retained for 304");
|
|
let mut request = HeaderMap::new();
|
|
assert!(
|
|
stored.apply_validators(&mut request),
|
|
"conditional request can validate stored object"
|
|
);
|
|
assert_eq!(request["if-none-match"], "\"origin-v1\"");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn positive_fallback_ttl_reuses_only_public_no_cache_entries() {
|
|
let cache = Cache::new(1);
|
|
let settings = Settings {
|
|
default_ttl_seconds: 75,
|
|
..Default::default()
|
|
};
|
|
let ticket = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/backend.css",
|
|
&HeaderMap::new(),
|
|
)
|
|
.unwrap();
|
|
let mut response = Response::new(Body::from("public css"));
|
|
*response.headers_mut() = headers("public, no-cache");
|
|
let p = plan(
|
|
StatusCode::OK,
|
|
response.headers(),
|
|
&settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(p.ttl, 75);
|
|
assert!(!p.requires_revalidation);
|
|
cache
|
|
.wrap(response, ticket.clone(), p, &settings)
|
|
.into_body()
|
|
.collect()
|
|
.await
|
|
.unwrap();
|
|
let hit = cache
|
|
.get(&ticket)
|
|
.expect("explicit public override allows fresh HIT without ETag");
|
|
assert!(hit.remaining_ttl > 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn zero_ttl_stale_if_error_fallback_survives_in_varnish_mode() {
|
|
let cache = Cache::new(1);
|
|
let settings = Settings::default();
|
|
let ticket = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fallback.css",
|
|
&HeaderMap::new(),
|
|
)
|
|
.unwrap();
|
|
let mut response = Response::new(Body::from("public fallback"));
|
|
*response.headers_mut() = headers("public, max-age=0, stale-if-error=120");
|
|
let p = plan(
|
|
StatusCode::OK,
|
|
response.headers(),
|
|
&settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
cache
|
|
.wrap(response, ticket.clone(), p, &settings)
|
|
.into_body()
|
|
.collect()
|
|
.await
|
|
.unwrap();
|
|
assert!(
|
|
cache.get(&ticket).is_none(),
|
|
"zero TTL must never create a fresh HIT"
|
|
);
|
|
assert!(
|
|
cache.revalidation(&ticket).unwrap().can_stale_if_error(),
|
|
"body remains available on backend failure"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn min_fresh_and_max_stale_still_constrain_compatibility_hits() {
|
|
let cache = Cache::new(1);
|
|
let settings = Settings::default();
|
|
let ticket = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fresh.css",
|
|
&HeaderMap::new(),
|
|
)
|
|
.unwrap();
|
|
let mut response = Response::new(Body::from("css"));
|
|
*response.headers_mut() = headers("public, max-age=60, stale-while-revalidate=0");
|
|
let p = plan(
|
|
StatusCode::OK,
|
|
response.headers(),
|
|
&settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
cache
|
|
.wrap(response, ticket.clone(), p, &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 too_fresh = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fresh.css",
|
|
&headers("no-cache, max-age=0, min-fresh=20"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
assert!(
|
|
cache.get(&too_fresh).is_none(),
|
|
"min-fresh requires 20s but only 15s remain"
|
|
);
|
|
let enough = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fresh.css",
|
|
&headers("min-fresh=10"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
assert!(
|
|
cache.get(&enough).is_some(),
|
|
"10s remaining freshness is available"
|
|
);
|
|
{
|
|
let mut store = cache.store.lock().unwrap();
|
|
for entry in store.entries.values_mut() {
|
|
entry.received = Instant::now() - Duration::from_secs(90);
|
|
}
|
|
}
|
|
let max_stale = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fresh.css",
|
|
&headers("no-cache, max-stale=45"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
let stale = cache
|
|
.get(&max_stale)
|
|
.expect("max-stale explicitly allows retained stale object");
|
|
assert!(stale.stale);
|
|
assert!(
|
|
!stale.background_revalidate,
|
|
"explicit max-stale never triggers implicit background refresh"
|
|
);
|
|
let too_old = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fresh.css",
|
|
&headers("max-stale=10"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
assert!(cache.get(&too_old).is_none());
|
|
let default = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/fresh.css",
|
|
&HeaderMap::new(),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
assert!(
|
|
cache.get(&default).is_none(),
|
|
"expired object cannot be served without stale allowance"
|
|
);
|
|
}
|
|
|
|
#[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");
|
|
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 sixty_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, 60);
|
|
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 max_stale_zero = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/asset.js",
|
|
&headers("max-stale=0"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
assert!(
|
|
cache.get(&max_stale_zero).is_none(),
|
|
"explicit max-stale=0 must override implicit grace"
|
|
);
|
|
let max_stale_small = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/asset.js",
|
|
&headers("no-cache, max-stale=3"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
assert!(
|
|
cache.get(&max_stale_small).is_none(),
|
|
"max-stale=3 rejects object stale by 5s, even in grace"
|
|
);
|
|
let max_stale_enough = cache
|
|
.ticket(
|
|
1,
|
|
"https",
|
|
"example.test",
|
|
"app",
|
|
"/asset.js",
|
|
&headers("no-cache, max-stale=9"),
|
|
)
|
|
.unwrap()
|
|
.with_varnish_compatible(true);
|
|
let accepted = cache
|
|
.get(&max_stale_enough)
|
|
.expect("max-stale=9 admits object 5s stale");
|
|
assert!(accepted.stale);
|
|
{
|
|
let mut store = cache.store.lock().unwrap();
|
|
for entry in store.entries.values_mut() {
|
|
entry.received = Instant::now() - Duration::from_secs(125);
|
|
}
|
|
}
|
|
assert!(
|
|
cache.get(&ticket).is_none(),
|
|
"expired beyond grace does not produce HIT"
|
|
);
|
|
}
|
|
}
|