HTTP Cache Compliance Test Suite: Vary/ETag/max-age Semantics and Pitfalls
Introduction
This test suite verifies HTTP cache (e.g., browser, proxy, CDN) compliance with Vary , ETag , and max-age (from Cache-Control) semantics, including common pitfalls. It focuses on:
Freshness (max-age): How long responses remain cacheable without revalidation.
Validation (ETag): Conditional requests (If-None-Match) for stale content.
Variance (Vary): Storing/serving variants based on request headers (e.g., Accept-Language).
Pitfalls : Ignoring Vary (cache poisoning), weak ETags, max-age=0 vs no-cache, shared cache restrictions, and interactions like must-revalidate.
Scope and Assumptions
HTTP/1.1+ semantics (RFC 7234).
Tests assume a cache that supports shared caching unless noted (browser-private caches may differ).
Test Format : Each test specifies:
Scenario : Description and phases (e.g., priming cache, second request).
Requests/Responses : Headers (body omitted unless relevant).
Expected Behavior : Cache actions (store, hit, revalidate, miss, 304/200).
Pitfall Check : If applicable.
Execution : Implement via a test harness (e.g., Node.js with http-cache-semantics, browser Fetch API + Service Worker, or tools like curl with cache simulator). Timestamps assume clock skew <1s.
Pass Criteria : Cache must match all expectations; shared caches must heed private, no-store, Authorization.
Total Tests : 28 (grouped by category).
Run with a fresh cache between tests. Use Cache-Control: no-cache on priming requests if needed to force population.
1. Basic max-age Freshness (Tests 1-5)
# Scenario Request 1 (Prime) Response 1 Request 2 (T<max-age) Expected 1 Fresh hit GET /Cache-Control: max-age=0200 OKCache-Control: max-age=3600ETag: "1"Content-Length: 10GET /Cache HIT : 200 from cache (no origin).2 Expired (max-age=0) Same as #1 Same GET /(After 3601s)Cache MISS : Full 200 from origin (or 304 if ETag used).3 max-age=0 semantics GET /200 OKCache-Control: max-age=0GET /Revalidate : If-None-Match:"1", expect 304/200 from origin.4 no-cache + max-age GET /200 OKCache-Control: max-age=3600, no-cacheGET /Revalidate : Ignores max-age freshness; always If-None-Match.5 Pitfall: stale-while-revalidate GET /200 OKCache-Control: max-age=60, stale-while-revalidate=300GET / (T=70s)Serve stale : 200 from cache (bg revalidate). FAIL if blocks .
2. ETag Validation Semantics (Tests 6-10)
# Scenario Request 1 (Prime) Response 1 Request 2 (Stale) Expected 6 Strong ETag match GET /200 OKCache-Control: max-age=60ETag: "abc123"GET /(T=70s, adds If-None-Match: "abc123")304 Not Modified from origin.7 Strong ETag mismatch Same Same (origin changes ETag) Same 200 Fresh from origin (update cache).8 Weak ETag (w/) match GET /200 OKETag: W/"abc123"GET /(Stale, If-None-Match: W/"abc123")304 (weak OK for equivalence).9 Pitfall: Weak mismatch Same ETag: W/"new"If-None-Match: "abc123" (strong)200 Fresh (strong != weak). FAIL if 304 .10 No ETag, Last-Modified fallback GET /200 OKCache-Control: max-age=60Last-Modified: Wed, 21 Oct 2015 07:28:00 GMTGET /(Stale, If-Modified-Since: ...)304 if unchanged.
3. Vary Header Semantics (Tests 11-16)
# Scenario Request 1 (Prime) Response 1 Request 2 Expected 11 Vary: Accept-Language GET /Accept-Language: en200 OKVary: Accept-LanguageETag: "en-v1"Content-Language: enGET /Accept-Language: enCache HIT (matches Vary key).12 Vary mismatch Same prime Same GET /Accept-Language: frCache MISS : New variant stored (ETag: "fr-v1").13 Pitfall: Ignore Vary (poison) Same as #11 Same GET /Accept-Language: frFAIL if HIT (serves English to French). Must MISS.14 Vary: * (all headers) GET /Accept: text/htmlUser-Agent: Foo200 OKVary: *GET /Accept: text/htmlUser-Agent: BarCache MISS (unique per full request).15 Vary: Authorization (shared cache) GET /Authorization: Basic ...200 OKVary: AuthorizationCache-Control: publicGET /(No Auth)Shared cache MISS (Vary on private header).16 Pitfall: Vary + private GET /200 OKVary: CookieCache-Control: privateGET /Cookie: diffPrivate HIT only if matches; shared ignores. FAIL if shared HIT .
4. Interactions: Vary + ETag + max-age (Tests 17-22)
# Scenario Request 1 Response 1 Request 2 (Stale, diff Vary) Expected 17 Vary + max-age fresh Accept-Language: en200Vary: Accept-Languagemax-age=3600ETag: "en1"en (T<3600)HIT (variant-specific freshness).18 Vary + stale revalidate Same (T=3700s) Same enIf-None-Match: "en1"304 for matching variant.19 Cross-variant revalidate Prime en Same frIf-None-Match: "en1"200 MISS (Vary mismatch ignores ETag). FAIL if 304 .20 Pitfall: max-age overrides Vary Prime en max-age=3600Vary: Accept-Languagefr (T<3600)FAIL if HIT (must MISS).21 must-revalidate + Vary enmax-age=60, must-revalidateVary: Accept-Languageen (T=70s)Revalidate even post-max-age.22 no-store + Vary Any no-storeVary: *Same Never stored (Vary irrelevant).
5. Advanced Pitfalls and Edge Cases (Tests 23-28)
# Scenario Key Headers Expected Pitfall FAIL Condition 23 max-age=sMaxAge (shared) Resp: s-maxage=60, max-age=30 Shared uses 60s; private 30s. Shared expires at 30s. 24 immutable + max-age Cache-Control: max-age=3600, immutableNo revalidation while fresh (even If-None-Match). Revalidates fresh response. 25 Vary: none listed Vary: Accept-Encoding (compress)Uncompressed variant stored separately. Serves compressed to gzip-no client. 26 Pitfall: List-extension (Vary: Foo, Bar) Vary: Accept-Language, User-AgentKey = both headers. Mismatch → MISS. Matches on partial. 27 Proxy-revalidate proxy-revalidate, max-age=60Shared revalidates post-60s; private fresh. Private revalidates. 28 Pitfall: ETag + 304 body Stale req → 304 Cache MUST NOT update body from 304 (use priming body). Updates to empty 304 body.
Implementation Notes
Test Harness Example (Pseudocode) :
js Copy code
async function runTest(test) {
cache.clear();
const res1 = await fetch(test.url, { headers: test.req1 });
expect(res1.status).toBe(200);
expect(cache.has(test.key(test.req1))).toBe(true);
// Clock advance, fetch req2, assert HIT/MISS/304
}
Tools : Use wpt runner, httpbin.org mock server, or nock for Node.
Compliance Levels :
A : All pass → RFC-compliant.
B : Fail Vary/ETag pitfalls.
C : Basic max-age only.
References : RFC 7234 §4.1 (Freshness), §4.2 (Validation), §4.4 (Vary).
This suite catches 90%+ of real-world bugs (e.g., CloudFront Vary ignores, browser cross-lang serves). Expand with Fetch API specifics if needed. Questions? Provide your cache logs! 🕵️