Truly independent web browser
Tests/LibWeb: Avoid a freshness race in the discarded cache test
Ali Mohammad Pur89939d1ddec1
diff --git a/Tests/LibWeb/Text/input/Cache/http-memory-cache-discarded-response.html b/Tests/LibWeb/Text/input/Cache/http-memory-cache-discarded-response.htmlindex c5d8382616c..8ada127d381 100644--- a/Tests/LibWeb/Text/input/Cache/http-memory-cache-discarded-response.html+++ b/Tests/LibWeb/Text/input/Cache/http-memory-cache-discarded-response.html@@ -4,47 +4,26 @@// With no other response for the request pending, a response the fetch discards mid-body leaves no pending// memory-cache entry behind, so a later response for the same request is the one the cache stores.//- // The Accept marker rides each fetch (CORS-safelisted, and the response has no Vary). The server records the last- // marker it saw, which tells a hit from a miss.+ // The fixture echoes each fetch's Accept marker in its body, without Vary, to distinguish a hit from a miss.asyncTest(async done => {const server = httpTestServer();- const delay = ms => new Promise(resolve => setTimeout(resolve, ms));- const lastSeenMarker = async url => {- const response = await fetch(server.recordedRequestHeadersURL(url));- return (await response.json())["Accept"][0];- };-- const token = `discarded-${crypto.randomUUID()}`;- const url = await server.createEcho("GET", `/cache-discarded-${crypto.randomUUID()}`, {- status: 200,- headers: {- "Access-Control-Allow-Origin": "*",- "Cache-Control": "max-age=2",- "Content-Type": "application/octet-stream",- "X-Content-Type-Options": "nosniff",- },- body: "abcdefghij" + "K".repeat(54),- });- // The body is held after its first chunk while the script load discards the response.- const heldUrl = `${url}?chunks=10,54&chunk_unblock_tokens=${token}`;+ const url = `${server.getStaticURL("Text/input/Cache/resources/discarded-response.py")}?token=${crypto.randomUUID()}`;// A nosniff response that is not JavaScript is blocked for a script.const script = document.createElement("script");const blocked = new Promise(resolve => script.addEventListener("error", resolve, { once: true }));- script.src = heldUrl;+ script.src = url;document.body.append(script);await blocked;println("script load was blocked");- await fetch(new URL(`/unblock/${token}`, url));-- // Wait past the discarded response's freshness: an entry it left behind is expired by then.- await delay(3000);+ await fetch(`${url}&unblock`);- const stored = await fetch(heldUrl, { headers: { Accept: "stored" } });+ // The discarded response is already stale. The replacement stays fresh beyond the test's timeout.+ const stored = await fetch(url, { headers: { Accept: "stored" } });println(`stored response arrived whole: ${(await stored.arrayBuffer()).byteLength === 64}`);- await fetch(heldUrl, { headers: { Accept: "check" } });- println(`next fetch served from the cache: ${(await lastSeenMarker(url)) === "stored"}`);+ const checked = await fetch(url, { headers: { Accept: "check" } });+ println(`next fetch served from the cache: ${(await checked.text()) === "stored".padEnd(64, "K")}`);done();});diff --git a/Tests/LibWeb/Text/input/Cache/resources/discarded-response.py b/Tests/LibWeb/Text/input/Cache/resources/discarded-response.pynew file mode 100644index 00000000000..1af46ddc5f5--- /dev/null+++ b/Tests/LibWeb/Text/input/Cache/resources/discarded-response.py@@ -0,0 +1,31 @@+# Copyright (c) 2026-present, the Ladybird developers.+#+# SPDX-License-Identifier: BSD-2-Clause+++def main(request, response):+ token = request.GET.first(b"token")+ response.headers.set(b"Access-Control-Allow-Origin", b"*")++ if b"unblock" in request.GET:+ request.server.stash.take(token).put(True)+ response.headers.set(b"Cache-Control", b"no-store")+ return b""++ response.headers.set(b"Content-Type", b"application/octet-stream")+ response.headers.set(b"X-Content-Type-Options", b"nosniff")+ response.headers.set(b"Content-Length", b"64")++ marker = request.headers.get(b"Accept", b"")+ if marker in (b"stored", b"check"):+ response.headers.set(b"Cache-Control", b"max-age=600")+ return marker.ljust(64, b"K")++ # A leaked pending entry must remain stale even if the replacement body finalizes it.+ response.headers.set(b"Cache-Control", b"max-age=0")+ release = request.server.stash.get_queue()+ request.server.stash.put(token, release)+ response.write_status_headers()+ response.writer.write_content(b"abcdefghij")+ release.get(timeout=120)+ response.writer.write_content(b"K" * 54)