Dracull deterministic test page
Original text
"""A deterministic HTTP page used by the Phase 4 browser test matrix. The server binds only to ``127.0.0.1`` on an operating-system-selected free port. It serves one page containing a form, a text-changing button, an in-page DOM mutation button, and an iframe that points at a second route on the same server. """ from __future__ import annotations import socket from collections.abc import Awaitable, Callable from typing import Any from aiohttp import web PAGE_TITLE = "Dracull Deterministic Test Page" IFRAME_TITLE = "Dracull Deterministic Iframe" FORM_RESULT_PREFIX = "Submitted:" _PAGE_HTML = """
Original text
Distinct iframe content
""" async def _main_page(_request: web.Request) -> web.Response: return web.Response(text=_PAGE_HTML, content_type="text/html") async def _iframe_page(_request: web.Request) -> web.Response: return web.Response(text=_IFRAME_HTML, content_type="text/html") async def start_test_server() -> tuple[str, Callable[[], Awaitable[None]]]: """Start the local test server and return ``(base_url, cleanup)``. The socket is explicitly bound to loopback with port ``0``. The caller owns cleanup and must await it in a ``finally`` block so no listener is left behind. """ listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(("127.0.0.1", 0)) listener.listen() port = int(listener.getsockname()[1]) app = web.Application() app.router.add_get("/", _main_page) app.router.add_get("/iframe", _iframe_page) runner = web.AppRunner(app) try: await runner.setup() site = web.SockSite(runner, listener) await site.start() except BaseException: listener.close() await runner.cleanup() raise async def cleanup() -> None: await runner.cleanup() return f"http://127.0.0.1:{port}/", cleanup def page_facts() -> dict[str, Any]: """Return the stable facts a test may compare with browser read-back.""" return { "title": PAGE_TITLE, "iframe_title": IFRAME_TITLE, "form_result_prefix": FORM_RESULT_PREFIX, }