File size: 4,121 Bytes
6eb6c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""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 = """<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>Dracull Deterministic Test Page</title>

</head>

<body>

  <main>

    <h1>Dracull deterministic test page</h1>

    <form id="test-form">

      <label for="message">Message</label>

      <input id="message" name="message" type="text" value="" aria-label="Message">

      <button id="submit-button" type="submit">Submit</button>

    </form>

    <output id="form-result" aria-live="polite"></output>



    <button id="change-button" type="button">Change text</button>

    <p id="changed-text">Original text</p>



    <button id="mutate-button" type="button">Append element</button>

    <div id="mutation-container"></div>



    <iframe id="test-iframe" title="Dracull deterministic iframe" src="/iframe"></iframe>

  </main>

  <script>

    document.getElementById("test-form").addEventListener("submit", (event) => {

      event.preventDefault();

      const message = document.getElementById("message").value;

      document.getElementById("form-result").textContent = "Submitted: " + message;

    });

    document.getElementById("change-button").addEventListener("click", () => {

      document.getElementById("changed-text").textContent = "Changed text";

    });

    let mutationCount = 0;

    document.getElementById("mutate-button").addEventListener("click", () => {

      mutationCount += 1;

      const item = document.createElement("p");

      item.className = "mutated-item";

      item.textContent = "Mutation " + mutationCount;

      document.getElementById("mutation-container").appendChild(item);

    });

  </script>

</body>

</html>

"""

_IFRAME_HTML = """<!doctype html>

<html lang="en">

<head>

  <meta charset="utf-8">

  <title>Dracull Deterministic Iframe</title>

</head>

<body>

  <p id="iframe-distinct-text">Distinct iframe content</p>

</body>

</html>

"""


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,
    }