Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(wpt): model Fetch redirect fixtures
ldm0a4604a193944
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/case_set.py b/moli-benchmark/moli_benchmark/wpt_cross/case_set.pyindex ac30d06a9..33259ac24 100644--- a/moli-benchmark/moli_benchmark/wpt_cross/case_set.py+++ b/moli-benchmark/moli_benchmark/wpt_cross/case_set.py@@ -857,7 +857,7 @@ def _supported_wptserve_handler_references(supported: tuple[re.Pattern[str], ...] = ()if rel is not None:supported += _service_worker_script_handler_reference_patterns(posixpath.dirname(rel) or ".")- supported += _fetch_preflight_handler_reference_patterns(posixpath.dirname(rel) or ".")+ supported += _fetch_resource_handler_reference_patterns(posixpath.dirname(rel) or ".")if rel is not None and rel.startswith("fetch/api/"):supported += _empty_location_handler_reference_patterns(posixpath.dirname(rel))if rel is not None:@@ -1686,8 +1686,8 @@ def _service_worker_script_handler_reference_patterns(directory: str) -> tuple[r@lru_cache(maxsize=None)-def _fetch_preflight_handler_reference_patterns(directory: str) -> tuple[re.Pattern[str], ...]:- names = ("preflight.py", "clean-stash.py")+def _fetch_resource_handler_reference_patterns(directory: str) -> tuple[re.Pattern[str], ...]:+ names = ("preflight.py", "clean-stash.py", "inspect-headers.py", "redirect.py")references = []for name in names:resource = "fetch/api/resources/" + name@@ -1707,6 +1707,11 @@ def _fetch_preflight_handler_reference_patterns(directory: str) -> tuple[re.Patt+ "|".join(re.escape(name) for name in names)+ rf"){WPTSERVE_HANDLER_TRAILING_BOUNDARY}"))+ patterns.append(re.compile(+ r"`\$\{\s*RESOURCES_DIR\s*\}(?:"+ + "|".join(re.escape(name) for name in names)+ + rf"){WPTSERVE_HANDLER_TRAILING_BOUNDARY}"+ ))return tuple(patterns)diff --git a/moli-benchmark/moli_benchmark/wpt_cross/server.py b/moli-benchmark/moli_benchmark/wpt_cross/server.pyindex f6b20cee5..7ae1aef8b 100644--- a/moli-benchmark/moli_benchmark/wpt_cross/server.py+++ b/moli-benchmark/moli_benchmark/wpt_cross/server.py@@ -50,7 +50,7 @@ from email.utils import formatdatefrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServerfrom itertools import productfrom pathlib import Path-from urllib.parse import parse_qs, parse_qsl, quote, unquote, urlparse, urlsplit, urlunsplit+from urllib.parse import parse_qs, parse_qsl, quote, unquote, urlencode, urlparse, urlsplit, urlunsplitfrom .pipes import WptPipeError, parse_pipe_commands@@ -129,6 +129,7 @@ IFRAME_STASH_PATH = "/html/semantics/embedded-content/the-iframe-element/stash.pCOMMON_ECHO_PATH = "/common/echo.py"COMMON_REDIRECT_PATH = "/common/redirect.py"+FETCH_REDIRECT_RESOURCE_PATH = "/fetch/api/resources/redirect.py"FETCH_EMPTY_LOCATION_PATH = "/fetch/api/resources/redirect-empty-location.py"XHR_URL_RESOURCE_PATHS = {"/xhr/resources/requri.py",@@ -1307,6 +1308,40 @@ def _redirect_fixture_response(query: str) -> tuple[int, str] | None:return status, location+def _fetch_redirect_form_status(method: str, content_type: str | None, body: bytes) -> str | None:+ """Read the first redirect_status field like wptserve's CGI form parser."""+ if content_type is None:+ content_type = "application/x-www-form-urlencoded" if method == "POST" else "text/plain"+ media_type = content_type.partition(";")[0].strip()+ if media_type == "application/x-www-form-urlencoded":+ params = parse_qs(body.decode("latin-1"), keep_blank_values=True, encoding="latin-1")+ return params.get("redirect_status", [None])[0]+ if media_type.startswith("multipart/"):+ message = BytesParser(policy=policy.HTTP).parsebytes(+ f"Content-Type: {content_type}\r\n\r\n".encode("latin-1") + body+ )+ boundary = message.get_boundary()+ if boundary is None or re.fullmatch(r"[ -~]{0,200}[!-~]", boundary) is None:+ raise ValueError("Invalid multipart boundary")+ for part in message.iter_parts():+ if part.get_param("name", header="content-disposition") == "redirect_status":+ if part.get_filename():+ raise ValueError("A file is not a redirect status")+ # CGI leaves transfer encodings untouched and treats form+ # values as isomorphic bytes, regardless of part charset.+ del part["Content-Transfer-Encoding"]+ payload = part.get_payload(decode=True)+ if payload is None:+ raise ValueError("A multipart value is not a redirect status")+ return payload.decode("latin-1")+ return None+ if body:+ # Upstream FieldStorage's non-form binary read raises TypeError when+ # writing a nonempty upload into its text buffer.+ raise ValueError("Unsupported non-form upload")+ return None++def _content_security_policy_resource_response() -> tuple[bytes, list[tuple[str, str]]]:"""Return the minimal CSP resource.py fixture used by worker CSP WPT."""@@ -2091,6 +2126,11 @@ requestExecutor("{executor_uuid}", {start_on_js});self._serve(emit_body=False)def do_OPTIONS(self) -> None: # noqa: N802+ if unquote(urlsplit(self.path).path) == FETCH_REDIRECT_RESOURCE_PATH:+ self._serve_fetch_redirect_resource(+ urlsplit(self.path).query, emit_body=self.command != "HEAD",+ )+ returnif self._serve_remote_context_resource():returnif self._serve_content_type_resource():@@ -2147,6 +2187,11 @@ requestExecutor("{executor_uuid}", {start_on_js});self.send_error(404)def do_POST(self) -> None: # noqa: N802+ if unquote(urlsplit(self.path).path) == FETCH_REDIRECT_RESOURCE_PATH:+ self._serve_fetch_redirect_resource(+ urlsplit(self.path).query, emit_body=self.command != "HEAD",+ )+ returnif self._serve_remote_context_resource():returnif self._serve_content_type_resource():@@ -2235,6 +2280,11 @@ requestExecutor("{executor_uuid}", {start_on_js});self.end_headers()def _serve_fetch_resource_method(self) -> None:+ if unquote(urlsplit(self.path).path) == FETCH_REDIRECT_RESOURCE_PATH:+ self._serve_fetch_redirect_resource(+ urlsplit(self.path).query, emit_body=self.command != "HEAD",+ )+ returnif self._serve_remote_context_resource():returnif self._serve_content_type_resource():@@ -2290,6 +2340,11 @@ requestExecutor("{executor_uuid}", {start_on_js});do_DELETE = _serve_fetch_resource_methoddef do_YO(self) -> None: # noqa: N802 (WPT custom method)+ if unquote(urlsplit(self.path).path) == FETCH_REDIRECT_RESOURCE_PATH:+ self._serve_fetch_redirect_resource(+ urlsplit(self.path).query, emit_body=self.command != "HEAD",+ )+ returnif self._serve_remote_context_resource():returnif self._serve_content_type_resource():@@ -2506,6 +2561,11 @@ requestExecutor("{executor_uuid}", {start_on_js});self.send_error(500, "Invalid WPT template or pipe")def _serve_response(self, *, emit_body: bool) -> None:+ if unquote(urlsplit(self.path).path) == FETCH_REDIRECT_RESOURCE_PATH:+ self._serve_fetch_redirect_resource(+ urlsplit(self.path).query, emit_body=self.command != "HEAD",+ )+ returnif self._serve_common_echo_resource():returnif self._serve_common_redirect_resource():@@ -2615,7 +2675,6 @@ requestExecutor("{executor_uuid}", {start_on_js});returnif path in {- "/fetch/api/resources/redirect.py","/common/redirect-opt-in.py",}:redirect = _redirect_fixture_response(parsed.query)@@ -3309,6 +3368,87 @@ requestExecutor("{executor_uuid}", {start_on_js});)return True+ def _serve_fetch_redirect_resource(self, query: str, *, emit_body: bool) -> None:+ connection_headers = []+ if (self.headers.get("Transfer-Encoding") is not None+ or self.headers.get("Content-Length", "0").strip() not in {"", "0"}):+ # A query status or an ordinary OPTIONS response never reads+ # the upload in upstream redirect.py. Do not wait for EOF.+ self.close_connection = True+ connection_headers.append(("Connection", "close"))+ if unquote(urlsplit(self.path).path) == "/fetch/api/resources/redirect-empty-location.py":+ self._send_bytes(+ None, b"", emit_body=emit_body, status_code=302,+ extra_headers=[*connection_headers, ("Location", "")],+ cache_control=None,+ )+ return+ params = parse_qs(query, keep_blank_values=True, encoding="latin-1")+ stash_path = urlsplit(self.path).path+ headers = [*connection_headers, ("Content-Type", "text/plain"), ("Pragma", "no-cache")]+ if "Origin" in self.headers:+ headers.extend([+ ("Access-Control-Allow-Origin", self.headers.get("Origin", "")),+ ("Access-Control-Allow-Credentials", "true"),+ ])+ else:+ headers.append(("Access-Control-Allow-Origin", "*"))+ token = params.get("token", [None])[0]+ data = {"count": 0, "preflight": "0"}+ try:+ if "token" in params:+ data = fetch_stash.take(token, path=stash_path) or data+ if self.command == "OPTIONS":+ if "allow_headers" in params:+ headers.append(("Access-Control-Allow-Headers", params["allow_headers"][0]))+ data["preflight"] = "1"+ if "redirect_preflight" not in params:+ if token:+ fetch_stash.put(token, data, path=stash_path)+ self._send_bytes(None, b"", emit_body=emit_body, extra_headers=headers,+ cache_control="no-cache")+ return++ status = 302+ if "redirect_status" in params:+ status = int(params["redirect_status"][0].encode("latin-1"))+ elif self.command not in {"GET", "HEAD"}:+ # wptserve's CGI input is bounded by Content-Length even+ # when Transfer-Encoding is also present.+ body = self._read_content_length_request_body(ignore_transfer_encoding=True)+ if body is None:+ return+ form_status = _fetch_redirect_form_status(+ self.command, self.headers.get("Content-Type"), body+ )+ if form_status is not None:+ status = int(form_status.encode("latin-1"))+ data["count"] += 1+ if "location" in params:+ location = params["location"][0]+ if "simple" not in params and urlparse(location).scheme in {"", "http", "https"}:+ location += "&" if "?" in location else "?"+ location += urlencode({name: values[0] for name, values in params.items()})+ location += "&count=" + str(data["count"])+ headers.append(("Location", location))+ if "redirect_referrerpolicy" in params:+ headers.append(("Referrer-Policy", params["redirect_referrerpolicy"][0]))+ if "delay" in params:+ time.sleep(float(params["delay"][0].encode("latin-1")) / 1000)+ if token:+ fetch_stash.put(token, data, path=stash_path)+ if "max_count" in params and data["count"] > int(params["max_count"][0].encode("latin-1")):+ # Upstream returns a plain body instead of its tuple;+ # none of the redirect/CORS headers survive that return.+ self._send_bytes(None, str(data["count"] - 1).encode(), emit_body=emit_body,+ extra_headers=connection_headers, cache_control=None)+ return+ self._send_bytes(None, b"", emit_body=emit_body, extra_headers=headers,+ status_code=status, cache_control="no-cache")+ except (KeyError, ValueError, TypeError, OverflowError):+ self.send_error(500)++def _serve_xhr_delay(self, query: str, *, emit_body: bool) -> None:delay_seconds = _wpt_delay_seconds(query)if delay_seconds is None:@@ -3354,6 +3494,8 @@ requestExecutor("{executor_uuid}", {start_on_js});)def __getattr__(self, name: str):+ if name.startswith("do_") and unquote(urlsplit(self.path).path) == FETCH_REDIRECT_RESOURCE_PATH:+ return self._serve_fetch_resource_methodif name.startswith("do_") and unquote(urlsplit(self.path).path) in REMOTE_CONTEXT_RESOURCE_PATHS:return self._serve_remote_context_resourceif name.startswith("do_") and unquote(urlsplit(self.path).path) == FETCH_CONTENT_TYPE_PATH:diff --git a/moli-benchmark/tests/test_wpt_fetch_redirect_server.py b/moli-benchmark/tests/test_wpt_fetch_redirect_server.pynew file mode 100644index 000000000..b036d0c76--- /dev/null+++ b/moli-benchmark/tests/test_wpt_fetch_redirect_server.py@@ -0,0 +1,290 @@+from __future__ import annotations++import tempfile+import unittest+import uuid+from concurrent.futures import ThreadPoolExecutor+from contextlib import ExitStack+from http.client import HTTPConnection+from pathlib import Path+from threading import Event+from unittest.mock import patch+from urllib.parse import urlencode++from moli_benchmark.wpt_cross.case_set import enumerate_cases+from moli_benchmark.wpt_cross.server import WptFixtureServer+++REDIRECT = "/fetch/api/resources/redirect.py"+EMPTY_LOCATION = "/fetch/api/resources/redirect-empty-location.py"+++class FetchRedirectFixtureTests(unittest.TestCase):+ def setUp(self):+ self.stack = ExitStack()+ self.addCleanup(self.stack.close)+ self.root = Path(self.stack.enter_context(tempfile.TemporaryDirectory()))+ (self.root / "resources").mkdir()+ (self.root / "resources/testharness.js").write_text("// testharness")+ self.stack.enter_context(patch(+ "moli_benchmark.wpt_cross.server._global_ipv6_address", return_value=None+ ))+ self.server = self.stack.enter_context(WptFixtureServer(self.root))++ def request(self, query="", *, method="GET", headers=None, body=None, port=None, path=REDIRECT):+ connection = HTTPConnection("127.0.0.1", port or self.server.port, timeout=2)+ try:+ connection.request(method, path + "?" + query, body, headers or {})+ response = connection.getresponse()+ return response.status, response.headers, response.read()+ finally:+ connection.close()++ def test_standard_and_extension_methods_echo_the_request_origin(self):+ query = "redirect_status=303&location=%2Ftarget"+ for method in ("GET", "HEAD", "POST", "PUT", "PATCH", "patcH", "DELETE", "YO", "chicken", "*"):+ with self.subTest(method=method):+ status, headers, body = self.request(query, method=method, body=b"payload", headers={+ "Origin": "https://caller.test",+ })+ self.assertEqual((status, body), (303, b""))+ self.assertEqual(headers["Content-Type"], "text/plain")+ self.assertEqual(headers.get_all("Cache-Control"), ["no-cache"])+ self.assertEqual(headers["Pragma"], "no-cache")+ self.assertEqual(headers["Access-Control-Allow-Origin"], "https://caller.test")+ self.assertEqual(headers["Access-Control-Allow-Credentials"], "true")+ self.assertEqual(headers["Location"], "/target?redirect_status=303&location=%2Ftarget&count=1")+ for origin in (None, "", "null", "\xff"):+ with self.subTest(origin=origin):+ _, headers, _ = self.request(query, headers={} if origin is None else {"Origin": origin})+ self.assertEqual(headers["Access-Control-Allow-Origin"], "*" if origin is None else origin)+ self.assertEqual(headers.get("Access-Control-Allow-Credentials"), None if origin is None else "true")++ def test_location_inherits_first_query_values_and_preserves_existing_syntax(self):+ query = "location=%2Ftarget%3Fexisting%3D1&location=%2Fignored&a=x+y&a=ignored&count=20"+ self.assertEqual(self.request(query)[1]["Location"],+ "/target?existing=1&location=%2Ftarget%3Fexisting%3D1&a=x+y&count=20&count=1")+ for simple in ("", "false", "1"):+ with self.subTest(simple=simple):+ self.assertEqual(self.request(query + "&simple=" + simple)[1]["Location"], "/target?existing=1")+ self.assertIsNone(self.request()[1].get("Location"))+ self.assertEqual(self.request("location=")[1]["Location"], "?location=&count=1")+ self.assertEqual(self.request("location=&simple")[1]["Location"], "")+ self.assertEqual(self.request("location=%2Ftarget%23fragment")[1]["Location"],+ "/target#fragment?location=%2Ftarget%23fragment&count=1")+ self.assertEqual(self.request("location=%2Ftarget&raw=%FF")[1]["Location"],+ "/target?location=%2Ftarget&raw=%C3%BF&count=1")++ def test_non_http_locations_skip_parameter_inheritance(self):+ for location in ("data:text/plain,hello", "blob:https://a.test/id", "about:blank", "file:///tmp/example"):+ with self.subTest(location=location):+ self.assertEqual(self.request(urlencode({"location": location}))[1]["Location"], location)+ location = "http://user:[email protected]/path"+ self.assertEqual(self.request(urlencode({"location": location, "simple": ""}))[1]["Location"], location)++ def test_options_only_redirect_when_the_flag_is_present(self):+ query = "redirect_status=307&location=%2Ftarget&allow_headers=X-One%2C+x-two&allow_headers=ignored"+ status, headers, body = self.request(query, method="OPTIONS", headers={"Origin": "https://caller.test"})+ self.assertEqual((status, body), (200, b""))+ self.assertIsNone(headers.get("Location"))+ self.assertEqual(headers["Access-Control-Allow-Headers"], "X-One, x-two")+ self.assertIsNone(headers.get("Access-Control-Allow-Methods"))+ self.assertEqual(headers["Access-Control-Allow-Origin"], "https://caller.test")+ self.assertEqual(headers["Access-Control-Allow-Credentials"], "true")+ status, headers, body = self.request(query + "&redirect_preflight=false", method="OPTIONS")+ self.assertEqual((status, body), (307, b""))+ self.assertIn("redirect_preflight=false&count=1", headers["Location"])++ def test_counter_is_shared_between_origins_and_stops_without_redirect_headers(self):+ key = str(uuid.uuid4())+ query = "token=" + key + "&location=%2Ftarget&max_count=2"+ for count in range(1, 5):+ with self.subTest(count=count):+ status, headers, body = self.request(query, port=self.server.port if count % 2 else self.server.alternate_port)+ if count <= 2:+ self.assertEqual((status, body), (302, b""))+ self.assertTrue(headers["Location"].endswith("&count=" + str(count)))+ else:+ self.assertEqual((status, body), (200, str(count - 1).encode()))+ for name in ("Location", "Content-Type", "Cache-Control", "Pragma", "Access-Control-Allow-Origin"):+ self.assertIsNone(headers.get(name), name)+ key = str(uuid.uuid4())+ query = "token=" + key + "&location=%2Ftarget&max_count=0"+ self.assertEqual(self.request(query, method="OPTIONS")[0], 200)+ self.assertEqual(self.request(query)[2], b"0")++ def test_counter_uses_the_complete_raw_request_path(self):+ key = str(uuid.uuid4())+ query = "token=" + key + "&location=%2Ftarget"+ encoded = REDIRECT.replace("redirect.py", "%72edirect.py")+ for count in (1, 2):+ for path in (REDIRECT, encoded):+ with self.subTest(path=path, count=count):+ self.assertTrue(self.request(query, path=path)[1]["Location"].endswith("&count=" + str(count)))+ self.request("token=" + key, path="/fetch/api/resources/clean-stash.py")+ self.assertTrue(self.request(query)[1]["Location"].endswith("&count=3"))++ def test_status_uses_query_before_urlencoded_and_multipart_form_fields(self):+ form = b"redirect_status=307&redirect_status=301"+ for method in ("POST", "PUT"):+ with self.subTest(method=method):+ status, _, _ = self.request("location=%2Ftarget", method=method, body=form,+ headers={"Content-Type": "application/x-www-form-urlencoded"})+ self.assertEqual(status, 307)+ self.assertEqual(self.request("location=%2Ftarget", method="POST", body=form)[0], 307)+ self.assertEqual(self.request("location=%2Ftarget&redirect_status=301&redirect_status=308",+ method="POST", body=form)[0], 301)+ self.assertEqual(self.request("location=%2Ftarget", body=form,+ headers={"Content-Type": "application/x-www-form-urlencoded"})[0], 302)+ body = b'--boundary\r\nContent-Disposition: form-data; name="redirect_status"\r\n\r\n308\r\n--boundary--\r\n'+ self.assertEqual(self.request("location=%2Ftarget", method="POST", body=body,+ headers={"Content-Type": "multipart/form-data; boundary=boundary"})[0], 308)+ self.assertEqual(self.request("location=%2Ftarget", method="POST", body=form,+ headers={"Content-Type": "text/plain"})[0], 500)+ for content_type in ("", "APPLICATION/X-WWW-FORM-URLENCODED"):+ with self.subTest(content_type=content_type):+ self.assertEqual(self.request("location=%2Ftarget", method="POST", body=form,+ headers={"Content-Type": content_type})[0], 500)++ def test_non_redirect_statuses_and_referrer_policy_are_preserved(self):+ for code in (200, 201, 204, 299, 301, 302, 303, 304, 307, 308, 399, 400, 418, 500, 599):+ with self.subTest(code=code):+ status, headers, _ = self.request("redirect_status=" + str(code) + "&location=%2Ftarget")+ self.assertEqual(status, code)+ self.assertIsNotNone(headers.get("Location"))+ _, headers, _ = self.request("location=%2Ftarget&redirect_referrerpolicy=no-referrer&redirect_referrerpolicy=unsafe-url")+ self.assertEqual(headers["Referrer-Policy"], "no-referrer")+ for query in ("redirect_status=bad", "redirect_status=", "token=", "token=bad", "delay=bad", "delay=-1",+ "redirect_status=%A0302%A0", "delay=%A01%A0",+ "token=" + str(uuid.uuid4()) + "&max_count=%A01%A0"):+ with self.subTest(query=query):+ self.assertEqual(self.request(query)[0], 500)++ def test_multipart_status_uses_raw_first_field_bytes(self):+ for parts, expected in [+ ([(b'', b'307'), (b'', b'308')], 307),+ ([(b'Content-Transfer-Encoding: base64\r\n', b'MzA3')], 500),+ ([(b'Content-Type: text/plain; charset=utf-8\r\n', b'\xa0307\xa0')], 500),+ ]:+ with self.subTest(parts=parts):+ body = b''.join(+ b'--boundary\r\nContent-Disposition: form-data; name="redirect_status"\r\n'+ + headers + b'\r\n' + value + b'\r\n' for headers, value in parts+ ) + b'--boundary--\r\n'+ self.assertEqual(self.request("location=%2Ftarget", method="POST", body=body,+ headers={"Content-Type": "multipart/form-data; boundary=boundary"})[0], expected)+ body = b'--boundary\r\nContent-Disposition: form-data; name="redirect_status"; filename="status.txt"\r\n\r\n307\r\n--boundary--\r\n'+ self.assertEqual(self.request("location=%2Ftarget", method="POST", body=body,+ headers={"Content-Type": "multipart/form-data; boundary=boundary"})[0], 500)++ def test_delay_precedes_response_headers(self):+ entered, release = Event(), Event()++ def delay(seconds):+ self.assertEqual(seconds, 0.025)+ entered.set()+ release.wait(2)++ with patch("moli_benchmark.wpt_cross.server.time.sleep", side_effect=delay), ThreadPoolExecutor() as executor:+ future = executor.submit(self.request, "location=%2Ftarget&delay=25")+ try:+ self.assertTrue(entered.wait(2), "the redirect handler must delay its response")+ self.assertFalse(future.done(), "headers must not arrive before the delay completes")+ finally:+ release.set()+ self.assertEqual(future.result(timeout=2)[0], 302)++ def test_handlers_respond_before_unused_uploads_finish(self):+ for method in ("GET", "POST", "OPTIONS"):+ for framing in (("Content-Length", "1000000"), ("Transfer-Encoding", "chunked")):+ with self.subTest(method=method, framing=framing):+ connection = HTTPConnection("127.0.0.1", self.server.port, timeout=2)+ try:+ connection.putrequest(method, REDIRECT + "?redirect_status=302&location=%2Ftarget")+ connection.putheader(*framing)+ connection.endheaders()+ response = connection.getresponse()+ self.assertEqual((response.status, response.read()), (200 if method == "OPTIONS" else 302, b""))+ self.assertEqual(response.headers["Connection"], "close")+ finally:+ connection.close()++ def test_empty_location_is_present_for_every_method_without_cors_grants(self):+ for method in ("GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH", "DELETE", "YO", "chicken"):+ with self.subTest(method=method):+ status, headers, body = self.request(+ "redirect_status=307&location=/ignored", path=EMPTY_LOCATION,+ method=method, headers={"Origin": "https://caller.test"}, body=b"upload",+ )+ self.assertEqual((status, body), (302, b""))+ self.assertEqual(headers.get_all("Location"), [""])+ self.assertEqual(headers["Content-Length"], "0")+ for name in ("Content-Type", "Cache-Control", "Access-Control-Allow-Origin"):+ self.assertIsNone(headers[name], name)+ for path in (EMPTY_LOCATION + "2", "/wrong" + EMPTY_LOCATION):+ with self.subTest(path=path):+ self.assertEqual(self.request(path=path)[0], 404)++ def test_empty_location_responds_before_unused_uploads_finish(self):+ for method in ("POST", "PUT", "OPTIONS"):+ for framing in (("Content-Length", "1000000"), ("Transfer-Encoding", "chunked")):+ with self.subTest(method=method, framing=framing):+ connection = HTTPConnection("127.0.0.1", self.server.port, timeout=2)+ try:+ connection.putrequest(method, EMPTY_LOCATION)+ connection.putheader(*framing)+ connection.endheaders()+ response = connection.getresponse()+ self.assertEqual((response.status, response.read()), (302, b""))+ self.assertEqual(response.headers.get_all("Location"), [""])+ self.assertEqual(response.headers["Connection"], "close")+ finally:+ connection.close()++ def test_case_selection_recognizes_empty_location_references(self):+ sources = {+ "absolute": f"fetch('{EMPTY_LOCATION}');",+ "concat": 'fetch(RESOURCES_DIR + "redirect-empty-location.py");',+ "relative": "fetch('../resources/redirect-empty-location.py');",+ "template": "fetch(`${RESOURCES_DIR}redirect-empty-location.py?ignored=1`);",+ "bare": "fetch('redirect-empty-location.py');",+ "prefix": f"fetch('/wrong{EMPTY_LOCATION}');",+ "suffix": "fetch('../resources/redirect-empty-location.py2');",+ "unknown": f"fetch('{EMPTY_LOCATION}'); fetch('../resources/unknown.py');",+ }+ for name, source in sources.items():+ path = self.root / f"fetch/api/redirect/{name}.any.js"+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text("// META: global=window,worker\n" + source)+ selected = enumerate_cases(self.root, dir_prefixes=("fetch",), any_js_global="both")+ self.assertEqual([case.case_path for case in selected], [+ f"fetch/api/redirect/{name}.any.js?moli-wpt-any={realm}"+ for name in ("absolute", "concat", "relative", "template")+ for realm in ("dedicatedworker", "window")+ ])++ def test_case_selection_recognizes_fetch_redirect_references(self):+ sources = {+ "absolute": "fetch('/fetch/api/resources/redirect.py');",+ "concat": 'fetch(RESOURCES_DIR + "redirect.py");',+ "relative": "fetch('../resources/redirect.py');",+ "template": "fetch(`${RESOURCES_DIR}redirect.py?token=${token}`); fetch(`${RESOURCES_DIR}clean-stash.py`);",+ "bare": "fetch('redirect.py');",+ "prefix": "fetch('/wrong/fetch/api/resources/redirect.py');",+ "suffix": "fetch('../resources/redirect.py2');",+ "unknown": "fetch('../resources/redirect.py'); fetch('../resources/unknown.py');",+ }+ for name, source in sources.items():+ path = self.root / f"fetch/api/redirect/{name}.any.js"+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text("// META: global=window,worker\n" + source)+ selected = enumerate_cases(self.root, dir_prefixes=("fetch",), any_js_global="both")+ self.assertEqual([case.case_path for case in selected], [+ f"fetch/api/redirect/{name}.any.js?moli-wpt-any={realm}"+ for name in ("absolute", "concat", "relative", "template")+ for realm in ("dedicatedworker", "window")+ ])+++if __name__ == "__main__":+ unittest.main()