Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

test(wpt): serve fixture directory listings

ldm0ca5a66beae64
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/server.py b/moli-benchmark/moli_benchmark/wpt_cross/server.py
index fed0d3c63..5183ab57f 100644
--- a/moli-benchmark/moli_benchmark/wpt_cross/server.py
+++ b/moli-benchmark/moli_benchmark/wpt_cross/server.py
@@ -42,7 +42,7 @@ from html import escape as html_escape
from email.utils import formatdate
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
-from urllib.parse import parse_qs, parse_qsl, unquote, urlparse, urlsplit, urlunsplit
+from urllib.parse import parse_qs, parse_qsl, quote, unquote, urlparse, urlsplit, urlunsplit
from .pipes import WptPipeError, parse_pipe_commands
@@ -1665,6 +1665,19 @@ class FetchStash:
return self._values.pop(parsed_key, None)
+def _directory_listing_body(directory: Path, path: str) -> bytes:
+ items = [] if path == "/" else ['<li><a href="../">..</a></li>']
+ for entry in sorted(directory.iterdir(), key=lambda entry: entry.name):
+ suffix = "/" if entry.is_dir() else ""
+ link = quote(entry.name, safe="") + suffix
+ items.append(f'<li><a href="{link}">{html_escape(entry.name)}{suffix}</a></li>')
+ title = f"Directory listing for {html_escape(path)}"
+ return (
+ f'<!doctype html>\n<meta charset="utf-8">\n<title>{title}</title>\n'
+ f'<h1>{title}</h1>\n<ul>\n' + "\n".join(items) + "\n</ul>\n"
+ ).encode("utf-8")
+
+
def _make_handler(
wpt_root: Path,
results_store: "ResultsStore",
@@ -2073,7 +2086,7 @@ def _make_handler(
)
return
cleaned = _legacy_wpt_resource_alias(path) or path.lstrip("/")
- if not cleaned or ".." in cleaned.split("/"):
+ if ".." in cleaned.split("/"):
self.send_error(404)
return
is_any_js_window_wrapper = (
@@ -2102,11 +2115,24 @@ def _make_handler(
self.send_error(404)
return
if file_path.is_dir():
- index = file_path / "index.html"
- if not index.exists():
- self.send_error(404)
+ # wptserve lists directories, including the WPT root. Tests may
+ # fetch "/" without depending on a checkout-specific index file.
+ if not parsed.path.endswith("/"):
+ self._send_bytes(
+ None,
+ b"",
+ emit_body=emit_body,
+ status_code=301,
+ extra_headers=[("Location", parsed._replace(path=parsed.path + "/").geturl())],
+ )
return
- file_path = index
+ try:
+ body = _directory_listing_body(file_path, path)
+ except OSError:
+ self.send_error(500)
+ return
+ self._send_bytes("text/html; charset=utf-8", body, emit_body=emit_body)
+ return
try:
body = file_path.read_bytes()
except OSError:
diff --git a/moli-benchmark/tests/test_wpt_directory_server.py b/moli-benchmark/tests/test_wpt_directory_server.py
new file mode 100644
index 000000000..33e86d1cf
--- /dev/null
+++ b/moli-benchmark/tests/test_wpt_directory_server.py
@@ -0,0 +1,95 @@
+from __future__ import annotations
+
+import tempfile
+import unittest
+from contextlib import ExitStack
+from http.client import HTTPConnection
+from pathlib import Path
+from unittest.mock import patch
+from urllib.parse import quote
+
+from moli_benchmark.wpt_cross.server import WptFixtureServer
+
+
+class DirectoryFixtureTests(unittest.TestCase):
+ def setUp(self) -> None:
+ 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.root / "empty").mkdir()
+ 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, path: str, *, method: str = "GET"):
+ connection = HTTPConnection("127.0.0.1", self.server.port, timeout=2)
+ try:
+ connection.request(method, path)
+ response = connection.getresponse()
+ return response.status, response.headers, response.read()
+ finally:
+ connection.close()
+
+ def test_root_and_empty_directory_have_get_and_head_responses(self) -> None:
+ for path in ("/", "/empty/"):
+ with self.subTest(path=path):
+ status, headers, body = self.request(path)
+ self.assertEqual(status, 200)
+ self.assertEqual(headers["Content-Type"], "text/html; charset=utf-8")
+ self.assertIn(b"<!doctype html>", body)
+ self.assertIn(f"Directory listing for {path}".encode(), body)
+ head_status, head_headers, head_body = self.request(path, method="HEAD")
+ self.assertEqual((head_status, head_body), (200, b""))
+ self.assertEqual(head_headers["Content-Length"], str(len(body)))
+ self.assertEqual(head_headers["Content-Type"], headers["Content-Type"])
+
+ def test_missing_directory_slash_redirects_without_losing_query(self) -> None:
+ (self.root / "space dir").mkdir()
+ for method in ("GET", "HEAD"):
+ with self.subTest(method=method):
+ status, headers, body = self.request("/space%20dir?key=%2F", method=method)
+ self.assertEqual((status, body), (301, b""))
+ self.assertEqual(headers["Location"], "/space%20dir/?key=%2F")
+
+ def test_listing_escapes_names_and_lists_index_as_an_ordinary_file(self) -> None:
+ directory = self.root / "<folder&>"
+ directory.mkdir()
+ filename = '<tag>&"#?目次.txt'
+ (directory / filename).write_bytes(b"file contents")
+ (directory / "index.html").write_text("INDEX MUST NOT REPLACE THE LISTING")
+ (directory / "child").mkdir()
+ path = "/" + quote(directory.name, safe="") + "/"
+ status, _, body = self.request(path)
+ self.assertEqual(status, 200)
+ self.assertIn(b"Directory listing for /&lt;folder&amp;&gt;/", body)
+ self.assertIn(b'&lt;tag&gt;&amp;&quot;#?', body)
+ self.assertIn(f'href="{quote(filename, safe="")}"'.encode(), body)
+ self.assertIn(b'href="child/"', body)
+ self.assertIn(b'href="../"', body)
+ self.assertIn(b'href="index.html"', body)
+ self.assertNotIn(b"INDEX MUST NOT REPLACE THE LISTING", body)
+ self.assertEqual(self.request(path + quote(filename, safe=""))[2], b"file contents")
+
+ def test_missing_paths_and_paths_outside_the_fixture_root_remain_missing(self) -> None:
+ outside = Path(self.stack.enter_context(tempfile.TemporaryDirectory()))
+ (outside / "secret.txt").write_text("outside fixture data")
+ (self.root / "outside").symlink_to(outside, target_is_directory=True)
+ for path in ("/missing", "/missing/", "/../", "/%2e%2e/", "/outside/",
+ "/outside/secret.txt"):
+ with self.subTest(path=path):
+ self.assertEqual(self.request(path)[0], 404)
+
+ def test_explicit_index_file_keeps_its_contents_and_sidecar_headers(self) -> None:
+ (self.root / "index.html").write_bytes(b"<!doctype html><p>index fixture</p>")
+ (self.root / "index.html.headers").write_text("X-Fixture: index-file\n")
+ status, headers, body = self.request("/index.html")
+ self.assertEqual(status, 200)
+ self.assertEqual(headers["X-Fixture"], "index-file")
+ self.assertEqual(body, b"<!doctype html><p>index fixture</p>")
+
+
+if __name__ == "__main__":
+ unittest.main()