fix(wpt): implement the link stylesheet request counter

Serve the link-element stylesheet.py resource through the shared WPT stash.
Preserve consuming counter reads, per-ID state, cross-port sharing and HEAD
request behavior so link load-event tests observe real request counts.

Validate the fixture with 170 Python tests and the six upstream multiple-load
subtests in Moli and Chromium, then record the previously unlisted passing case.
This commit is contained in:
ldm0
2026-09-27 06:43:23 +08:00
parent dafea266a3
commit 004dbda690
3 changed files with 58 additions and 0 deletions
@@ -159,6 +159,9 @@ FETCH_REDIRECT_RESOURCE_PATHS = {
"/fetch/api/resources/redirect-empty-location.py",
}
FETCH_INSPECT_HEADERS_PATH = "/fetch/api/resources/inspect-headers.py"
LINK_STYLESHEET_COUNTER_PATH = (
"/html/semantics/document-metadata/the-link-element/stylesheet.py"
)
BENCH_TIMEOUT_MULTIPLIER_QUERY = "__moli_bench_timeout_multiplier"
FORM_ECHO_PATH = "/html/semantics/forms/form-submission-0/form-echo.py"
FORM_SUBMISSION_PATH = (
@@ -2933,6 +2936,30 @@ def _make_handler(
],
)
def _serve_link_stylesheet_counter(self, query: str, *, emit_body: bool) -> None:
params = parse_qs(query, keep_blank_values=True)
try:
count = int(fetch_stash.take(params["id"][0], path=LINK_STYLESHEET_COUNTER_PATH))
except (KeyError, TypeError, ValueError):
count = 0
if "count" in params:
self._send_bytes(
"text/html", str(count).encode("ascii"),
emit_body=emit_body, cache_control=None,
)
return
try:
fetch_stash.put(
params["id"][0], str(count + 1), path=LINK_STYLESHEET_COUNTER_PATH,
)
except (KeyError, ValueError):
self.send_error(500)
return
self._send_bytes(
"text/css", b"body {color: red;}",
emit_body=emit_body, cache_control=None,
)
def _serve_script_with_content_type(self, query: str, *, emit_body: bool) -> None:
params = parse_qs(query, keep_blank_values=True, encoding="latin-1")
directory = wpt_root / "html/semantics/scripting-1/the-script-element"
+30
View File
@@ -4304,6 +4304,36 @@ test(() => {}, "ok");
response.read(), b"FAIL" if method == "GET" else b""
)
def test_fixture_server_counts_link_stylesheet_requests_and_clears_on_read(self) -> None:
with tempfile.TemporaryDirectory() as root:
root_path = Path(root)
(root_path / "resources").mkdir()
(root_path / "resources" / "testharness.js").write_text("// testharness", encoding="utf-8")
resource = "/html/semantics/document-metadata/the-link-element/stylesheet.py"
fixture = root_path / resource.lstrip("/")
fixture.parent.mkdir(parents=True)
fixture.write_text("# dynamic stylesheet resource", encoding="utf-8")
first = "11111111-1111-4111-8111-111111111111"
second = "22222222-2222-4222-8222-222222222222"
with WptFixtureServer(root_path) as server:
def request(query: str, *, method: str = "GET", alternate: bool = False) -> tuple[str, bytes]:
port = server.alternate_port if alternate else server.port
url = f"http://127.0.0.1:{port}{resource}?{query}"
with urlopen(Request(url, method=method), timeout=2) as response:
self.assertEqual(response.status, 200)
return response.headers["Content-Type"], response.read()
self.assertEqual(request(f"id={first}&count="), ("text/html", b"0"))
self.assertEqual(request(f"id={first}"), ("text/css", b"body {color: red;}"))
self.assertEqual(request(f"id={first}", alternate=True), ("text/css", b"body {color: red;}"))
self.assertEqual(request(f"id={first}", method="HEAD"), ("text/css", b""))
self.assertEqual(request(f"id={second}"), ("text/css", b"body {color: red;}"))
self.assertEqual(request(f"id={first}&count=foo"), ("text/html", b"3"))
self.assertEqual(request(f"id={second}&count=foo"), ("text/html", b"1"))
self.assertEqual(request(f"id={first}&count=foo"), ("text/html", b"0"))
self.assertEqual(request(f"id={first}"), ("text/css", b"body {color: red;}"))
self.assertEqual(request(f"id={first}&count=foo"), ("text/html", b"1"))
def test_fixture_server_parses_fetch_status_parameters_as_bytes(self) -> None:
self.assertEqual(_fetch_status_response(""), (200, "OMG", "", b""))
self.assertEqual(
@@ -6020,6 +6020,7 @@ html/semantics/document-metadata/the-base-element/base_target_does_not_affect_lo
html/semantics/document-metadata/the-link-element/attr-link-fetchpriority.html
html/semantics/document-metadata/the-link-element/link-href-attribute-crash.html
html/semantics/document-metadata/the-link-element/link-load-event.html
html/semantics/document-metadata/the-link-element/link-multiple-load-events.html
html/semantics/document-metadata/the-link-element/link-rel-attribute-tokenization.html
html/semantics/document-metadata/the-link-element/link-rel-attribute.html
html/semantics/document-metadata/the-link-element/link-rellist.html