174 lines
6.5 KiB
Rust
174 lines
6.5 KiB
Rust
mod dashboard_tests {
|
|
use super::*;
|
|
use http_body_util::BodyExt;
|
|
async fn fill(cache: &Arc<Cache>, scope: &str, host: &str, path: &str) -> Ticket {
|
|
let t = cache
|
|
.ticket_scoped(
|
|
7,
|
|
scope,
|
|
"http",
|
|
host,
|
|
"origin@127.0.0.1:8080",
|
|
path,
|
|
&HeaderMap::new(),
|
|
)
|
|
.unwrap();
|
|
let settings = Settings {
|
|
enabled: true,
|
|
..Settings::default()
|
|
};
|
|
let mut response = Response::new(Body::from("public representation"));
|
|
response.headers_mut().insert(
|
|
"cache-control",
|
|
HeaderValue::from_static("public, max-age=30"),
|
|
);
|
|
response
|
|
.headers_mut()
|
|
.insert("vary", HeaderValue::from_static("Accept-Language"));
|
|
let p = plan(
|
|
StatusCode::OK,
|
|
response.headers(),
|
|
&settings,
|
|
Duration::ZERO,
|
|
)
|
|
.unwrap();
|
|
cache
|
|
.wrap(response, t.clone(), p, &settings)
|
|
.into_body()
|
|
.collect()
|
|
.await
|
|
.unwrap();
|
|
t
|
|
}
|
|
#[tokio::test]
|
|
async fn dashboard_is_scoped_paginated_and_exposes_invalidation_identity_not_bodies() {
|
|
let cache = Cache::new(7);
|
|
let a = fill(&cache, "alpha", "a.example", "/data?private-query=secret").await;
|
|
fill(&cache, "beta", "b.example", "/other").await;
|
|
cache.record("alpha", "a.example", "MISS", "cache_miss");
|
|
assert!(cache.get(&a).is_some());
|
|
cache.record("alpha", "a.example", "HIT", "cache_hit");
|
|
cache.record("alpha", "a.example", "BYPASS", "request_cookie");
|
|
let d = cache.dashboard(Some("alpha"), 0, 50);
|
|
assert_eq!(d.matching_objects, 1);
|
|
assert_eq!(d.domains.len(), 1);
|
|
assert_eq!(d.domains[0].counters.hits, 1);
|
|
assert_eq!(d.domains[0].counters.stored, 1);
|
|
assert_eq!(d.domains[0].counters.saved_body_bytes, 21);
|
|
assert_eq!(d.objects[0].path, "/data");
|
|
assert_eq!(d.objects[0].target, "/data?private-query=secret");
|
|
assert_eq!(d.objects[0].cache_key.len(), 64);
|
|
assert_eq!(d.objects[0].hits, 1);
|
|
let encoded = serde_json::to_string(&d).unwrap();
|
|
assert!(encoded.contains("private-query=secret"));
|
|
assert!(!encoded.contains("public representation"));
|
|
assert_eq!(cache.dashboard(None, 1, 1).objects.len(), 1);
|
|
assert_eq!(cache.dashboard(None, 99, 999).limit, 200);
|
|
assert_eq!(cache.purge_scope("alpha"), 1);
|
|
assert_eq!(cache.dashboard(Some("beta"), 0, 50).matching_objects, 1);
|
|
assert_eq!(
|
|
cache.dashboard(Some("alpha"), 0, 50).domains[0]
|
|
.counters
|
|
.hits,
|
|
1
|
|
);
|
|
cache.purge(Some(8));
|
|
assert!(cache.dashboard(None, 0, 50).domains.is_empty());
|
|
}
|
|
#[tokio::test]
|
|
async fn dashboard_retains_expired_entries_for_client_max_stale() {
|
|
let cache = Cache::new(7);
|
|
fill(&cache, "a", "a.example", "/").await;
|
|
{
|
|
let mut store = cache.store.lock().unwrap();
|
|
for entry in store.entries.values_mut() {
|
|
entry.received = Instant::now() - Duration::from_secs(2 * 60 * 60);
|
|
}
|
|
store.last_sweep = None;
|
|
}
|
|
assert_eq!(cache.dashboard(None, 0, 50).matching_objects, 1);
|
|
assert_eq!(cache.view().entries, 1);
|
|
assert_eq!(cache.view().expired, 0);
|
|
}
|
|
#[test]
|
|
fn bypass_reasons_are_fixed_and_private_inputs_are_not_echoed() {
|
|
let settings = Settings::default();
|
|
let mut headers = HeaderMap::new();
|
|
headers.insert("cookie", HeaderValue::from_static("session=very-private"));
|
|
assert_eq!(
|
|
request_reason(&Method::GET, &headers, true, "/", false, &settings),
|
|
Some("request_cookie")
|
|
);
|
|
headers.remove("cookie");
|
|
headers.insert("cache-control", HeaderValue::from_static("no-cache"));
|
|
assert_eq!(
|
|
request_reason(&Method::GET, &headers, true, "/", false, &settings),
|
|
None
|
|
);
|
|
assert_eq!(
|
|
request_policy(&Method::GET, &headers, true, "/", false, &settings),
|
|
RequestPolicy::Revalidate("request_no_cache")
|
|
);
|
|
let diagnostics = Settings {
|
|
expose_status: true,
|
|
expose_cache_status: true,
|
|
..settings
|
|
};
|
|
diagnostics.annotate_reason(&mut headers, "BYPASS", 0, 0, "request_cookie");
|
|
assert_eq!(headers["x-cache-reason"], "request_cookie");
|
|
assert_eq!(
|
|
headers["cache-status"],
|
|
"RustProxy; fwd=bypass; detail=\"request_cookie\""
|
|
);
|
|
diagnostics.annotate_transformed(&mut headers, true);
|
|
assert_eq!(
|
|
headers["cache-status"],
|
|
"RustProxy; fwd=bypass; detail=\"request_cookie\"; transformed"
|
|
);
|
|
}
|
|
#[test]
|
|
fn disabled_cache_strips_diagnostics_without_exposing_bypass_headers() {
|
|
let settings = Settings {
|
|
enabled: false,
|
|
expose_status: true,
|
|
expose_hits: true,
|
|
expose_cache_status: true,
|
|
expose_resident_age: true,
|
|
..Settings::default()
|
|
};
|
|
let mut headers = HeaderMap::new();
|
|
headers.insert("x-cache", HeaderValue::from_static("origin"));
|
|
headers.insert("x-cache-hits", HeaderValue::from_static("99"));
|
|
headers.insert("x-cache-status", HeaderValue::from_static("origin"));
|
|
headers.insert("cache-status", HeaderValue::from_static("Origin; hit"));
|
|
headers.insert("x-cache-reason", HeaderValue::from_static("origin"));
|
|
headers.insert("x-cache-resident-age", HeaderValue::from_static("5"));
|
|
settings.annotate_reason(&mut headers, "BYPASS", 0, 0, "cache_disabled");
|
|
settings.annotate_resident_age(&mut headers, 9);
|
|
for name in [
|
|
"x-cache",
|
|
"x-cache-hits",
|
|
"x-cache-status",
|
|
"cache-status",
|
|
"x-cache-reason",
|
|
"x-cache-resident-age",
|
|
] {
|
|
assert!(!headers.contains_key(name), "{name}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn per_domain_counter_cardinality_is_bounded() {
|
|
let cache = Cache::new(7);
|
|
for i in 0..5000 {
|
|
cache.record(
|
|
"legacy",
|
|
&format!("{i}.example"),
|
|
"BYPASS",
|
|
"cache_disabled",
|
|
);
|
|
}
|
|
assert_eq!(cache.dashboard(None, 0, 1).domains.len(), 4096);
|
|
}
|
|
}
|