The user wants an HTTP cache compliance test suite.
The key areas are:
- Vary header semantics (and pitfalls).
- Etag semantics (and pitfalls).
- max-age (and general Cache-Control) semantics (and pitfalls).
The goal is to provide a structured set of tests that a developer or QA engineer could use to verify if their HTTP implementation (client, server, or proxy/cache) handles these headers correctly according to RFC 7234 (and friends).
Plan:
- Introduction: Briefly explain the scope (RFC 7234, RFC 7232) and the tools needed (cURL is usually the best lingua franca for these examples).
- Test Suite Structure: Group tests by the specific mechanism (Vary, Etag, max-age).
- Category 1:
Cache-Control: max-age & Freshness:
- Basic caching (200 OK + max-age).
- Stale content handling (expired max-age).
no-cache vs no-store.
must-revalidate.
- Category 2:
ETag & Validation (Conditional Requests):
- Basic ETag generation.
- Conditional GET (
If-None-Match -> 304).
- Weak Etags (
W/"...") vs Strong Etags.
- Pitfall: Range requests requiring Strong Etags.
- Category 3:
Vary & Secondary Keys:
- Basic Vary (e.g., Accept-Encoding).
- Mismatched Vary headers (cache miss vs hit).
- The
* wildcard (cache poison/unusable).
- Pitfall: Varying on User-Agent (cache fragmentation/bloat).
- Category 4: Edge Cases & Common Pitfalls:
- Query strings.
- Cookies (and
Cache-Control: private).
- Heuristic freshness (no Cache-Control header).
- Method semantics (POST/GET interaction).
- Format: For each test, provide:
- Test Name.
- Description: What are we testing?
- Setup/Request: What to send.
- Expected Behavior: What the cache should do.
- Common Pitfall: Why this fails often.
Refining the content:
- Vary: This is usually the hardest. I need to emphasize that
Vary acts as a secondary key. If Vary: Accept-Encoding is set, the cache key becomes URI + Accept-Encoding.
- Etag: Need to distinguish between the validation loop (304 Not Modified) and serving fresh content. The
If-Match vs If-None-Match distinction is crucial for concurrency (PUT), but the prompt focuses on "cache compliance," so GET is the priority.
- max-age: Simple in theory, hard in practice due to
Date header sync issues (clock skew) or lack of Expires vs max-age precedence.
Self-Correction during drafting:
I shouldn't just list headers. I should frame them as executable test cases. I'll use a standardized format like Gherkin (Given/When/Then) or just a clear Request/Response flow for clarity. I will stick to a clear Request -> Response -> Next Request flow.
Let's create a suite of "Test Cases".
- Test 1: The Happy Path (max-age). GET url, get 200 with max-age=60. GET again immediately, expect HIT (HIT header or speed).
- Test 2: The Validator (ETag). GET url, get 200 + Etag. Wait for expire. GET again with
If-None-Match. Expect 304.
- Test 3: The Content Negotiator (Vary). GET url (Accept: json) -> 200 (Vary: Accept). GET url (Accept: xml) -> Miss/200. GET url (Accept: json) -> Hit.
- Test 4: The Poison (Vary: *). Response has
Vary: *. Expectation: Cache must not store (or strictly revalidate everything).
Formatting:
I will use Markdown with code blocks for headers.
Final Polish:
Ensure I mention specific RFCs (7230-7235 series, now updated to something newer but 7234 is the caching bible for most existing code, though RFC 9110 is current. I will stick to the terminology widely known).
Let's write it out.
Structure:
- Prerequisites: How to run (Proxy/Client/Server).
- Section 1: Freshness (max-age).
- Section 2: Validation (ETag/Last-Modified).
- Section 3: Selection (Vary).
- Section 4: The "Gotchas" (Complex Interactions).
Let's add specific "Pitfalls" sections for each.
- max-age Pitfall: Clock skew. The
Age header calculation.
- ETag Pitfall:
W/ weak validators on range requests. Context validation (not just byte equality).
- Vary Pitfall: Varying on things that change too much (User-Agent).
Okay, ready to generate the response.
This is a comprehensive compliance test suite designed to validate HTTP caching implementations (Client, Server, or Proxy/CDN). It covers the interaction between Cache-Control, Vary, and ETag/Last-Modified, highlighting common pitfalls defined in RFC 7234 and RFC 9110.
Each test case follows a Given / When / Then structure.
Part 1: Freshness Semantics (max-age)
These tests verify if the cache understands how long a resource is fresh and how to calculate its age.
Test 1.1: Basic Freshness (max-age)
- Goal: Verify the cache serves a response from storage without contacting the server within the freshness window.
- Given: Server responds to
GET /data with:
200 OK
Cache-Control: max-age=60
Date: [Current Time]
- When: Client requests
GET /data immediately (T+0s) and again at T+5s.
- Then:
- Request 1: Cache misses (or passes through), stores response.
- Request 2: Cache hits. The response is instant, and the
Age header (if present) is near 5.
- Pitfall: Ignoring the
Date header and using local cache time (causing clock skew issues).
Test 1.2: The Age Header Calculation
- Goal: Verify the cache correctly calculates the current age of the response.
- Given: Server responds with
Cache-Control: max-age=100.
- When: Client requests the resource, and the proxy responds with
Age: 80.
- Then: The resource is considered "stale" in 20 seconds. If requested again at T+15s, the cache must serve it (fresh remaining time: 5s).
- Pitfall: A common mistake is resetting
Age to 0 when serving from a nested cache layer, causing downstream caches to think the object is brand new.
Test 1.3: Heuristic Freshness (The "Last-Modified" fallback)
- Goal: Verify behavior when
Cache-Control is missing.
- Given: Server responds with:
200 OK
- No
Cache-Control
Last-Modified: 1 year ago
- When: Client requests the resource.
- Then: The cache may store it. If it does, it should calculate freshness heuristically (e.g., 10% of time since modification).
- Pitfall: Many caches treat responses with no
Cache-Control as non-cacheable by default (safer), whereas others cache indefinitely.
Part 2: Content Negotiation (Vary)
The Vary header is the most common source of cache "poisoning" or "miss" errors.
Test 2.1: The Basic Variance
- Goal: Ensure the cache creates unique entries based on request headers.
- Given: Server responds to
GET /page with:
Vary: Accept-Encoding
Content-Encoding: gzip
- When:
- Client A requests with
Accept-Encoding: gzip.
- Client B requests with
Accept-Encoding: br (Brotli).
- Client C requests with
Accept-Encoding: gzip.
- Then:
- Client A hits server (stores Gzip variant).
- Client B hits server (stores Brotli variant).
- Client C hits cache (retrieves Gzip variant).
- Pitfall: Caches that ignore
Vary and serve the Gzip version to Client B (causing decoding errors) or serve the Brotli version to Client C.
Test 2.2: The Normalization Pitfall
- Goal: Verify how caches handle differently ordered header values.
- Given: Server responds with
Vary: Accept.
- When:
- Request 1:
Accept: text/html, application/json
- Request 2:
Accept: application/json, text/html (Same values, different order)
- Then:
- Strict Compliance: This is a Cache MISS (different string key).
- Smart Compliance: This is a Cache HIT (values are normalized).
- Pitfall: If the cache doesn't normalize, you get low hit rates. If it normalizes too aggressively, it might serve the wrong content type.
Test 2.3: The "Vary: *" (Nuclear Option)
- Goal: Verify total non-caching based on headers.
- Given: Server responds with:
Cache-Control: public, max-age=3600
Vary: *
- When: Client requests the resource again.
- Then: Cache MISS. The response is effectively uncacheable by standard RFC definitions.
- Pitfall: Ignoring
Vary: * because max-age exists. Vary: * usually supersedes storage capability.
Part 3: Validation (ETag vs. Last-Modified)
This tests the "Conditional Request" mechanism (Revalidation).
Test 3.1: Strong ETag Validation
- Goal: Verify byte-for-byte identity.
- Given:
- Client
GET /data -> Server sends body "ABC" with ETag: "123".
- Resource changes to "ABD" with
ETag: "456".
- When: Client requests
GET /data with If-None-Match: "123".
- Then: Server responds with
200 OK and body "ABD".
- Pitfall: Sending a
304 Not Modified even though the ETag implies the content changed.
Test 3.2: The 304 Not Modified
- Goal: bandwidth saving.
- Given: Client has cached resource with
ETag: "123".
- When: Client sends
If-None-Match: "123". The server content has not changed.
- Then: Server responds
304 Not Modified with an empty body.
- Pitfall: Sending a body with a 304 response (protocol violation) or failing to update the
Cache-Control headers in the 304 response (refreshing the freshness lifetime).
Test 3.3: Last-Modified vs. ETag Precedence
- Goal: Determine the winner if both are present.
- Given: Client sends both
If-Modified-Since (old date) and If-None-Match (current ETag).
- When: The server checks. The timestamp is old, but the ETag is still valid.
- Then: Server returns
304 Not Modified.
- Logic: RFC 7232 states that
If-None-Match (ETag) takes precedence. The server should not evaluate If-Modified-Since if the ETag check passes.
Part 4: The Edge Cases & Pitfalls (The "Fuzzer" Section)
Test 4.1: The Stale-While-Revalidate
- Goal: Serve stale content while fetching new content in the background.
- Given:
Cache-Control: max-age=10, stale-while-revalidate=20.
- When:
- T+0: Request (Miss, Store).
- T+15: Request (Stale, but within SWR window).
- Then: Cache returns the Stale content immediately (Response Code 200, usually with Warning header), and triggers an asynchronous background refresh.
- Pitfall: Holding the client request while performing the background refresh (blocking).
Test 4.2: The Authentication Trap (Authorization)
- Goal: Verify handling of authenticated requests.
- Given:
- Request includes
Authorization: Bearer xyz.
- Response includes
Cache-Control: max-age=60.
- Then: Cache MISS (default behavior).
- The Exception: Unless the response includes
Cache-Control: public, the cache must not store this response (to prevent leaking private data to unauthenticated users).
- Pitfall: Caching an
Authorization protected response without the public directive.
Test 4.3: The Uncacheable Error
- Goal: Don't cache failures (unless told to).
- Given: Server responds
500 Internal Server Error.
- When: Client requests again.
- Then: Cache forwards request to server (does not serve the 500 error again), unless
Cache-Control explicitly allows storing errors (e.g., stale-if-error).
Test 4.4: Query String Invalidation
- Goal: Do query params affect the cache key?
- Given:
GET /api?q=1 is cached.
- When:
GET /api?q=2.
- Then: Cache MISS (Standard HTTP cache keys include the query string).
- Pitfall: Some CDNs (like legacy Akamai/Varnish configs) might ignore query strings by default, serving the same JSON for different queries.
Implementation Checklist (Server Headers)
To pass a compliance test, ensure your server sends these headers for a standard cacheable API response:
Date: Always present (crucial for age calculation).
Cache-Control:
public (if behind a CDN/Proxy).
max-age=<seconds> (explicit freshness).
immutable (prevents revalidation on refresh if content never changes).
ETag: A strong validator (hash of content).
Vary: Strictly list headers that change the representation (e.g., Accept-Encoding, Accept-Language). Never use User-Agent (it fragments the cache too much).