fix(wpt): preserve explicit fixture cache controls

This commit is contained in:
ldm0
2026-09-26 19:54:33 +08:00
committed by Donough Liu
parent 772ecc12a3
commit e401b64d28
2 changed files with 37 additions and 1 deletions
@@ -3077,7 +3077,7 @@ def _make_handler(
self.send_header(name, value)
if auto_content_length and not _headers_include(header_block, "Content-Length"):
self.send_header("Content-Length", str(len(body)))
if cache_control is not None:
if cache_control is not None and not _headers_include(header_block, "Cache-Control"):
self.send_header("Cache-Control", cache_control)
self.end_headers()
if emit_body:
@@ -0,0 +1,36 @@
import tempfile
import unittest
from pathlib import Path
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from moli_benchmark.wpt_cross.server import WptFixtureServer
class WptFixtureCacheControlTests(unittest.TestCase):
def test_explicit_cache_control_precedes_fixture_default(self) -> None:
with tempfile.TemporaryDirectory() as root:
root_path = Path(root)
(root_path / "resources").mkdir()
(root_path / "resources/testharness.js").write_text("// testharness")
(root_path / "module.json").write_text('{"value": 1}')
sidecar = root_path / "module.json.headers"
with WptFixtureServer(root_path) as server:
for header, pipe, expected in (
(None, "", ["no-store"]),
("max-age=600", "", ["max-age=600"]),
("", "", [""]),
(None, "header(Cache-Control,max-age=30)", ["max-age=30"]),
(None, "header(cache-control,public)", ["public"]),
):
if header is None:
sidecar.unlink(missing_ok=True)
else:
sidecar.write_text("cAcHe-CoNtRoL: " + header + "\n")
for method in ("GET", "HEAD"):
with self.subTest(header=header, pipe=pipe, method=method):
url = server.base_url + "/module.json?" + urlencode({"pipe": pipe})
with urlopen(Request(url, method=method), timeout=2) as response:
self.assertEqual(response.status, 200)
self.assertEqual(response.headers.get_all("Cache-Control"), expected)
self.assertEqual(response.read(), b"" if method == "HEAD" else b'{"value": 1}')