| 1 |
# mypy: allow-untyped-defs |
| 2 |
|
| 3 |
import logging |
| 4 |
import os |
| 5 |
import pickle |
| 6 |
import platform |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
import localpaths # type: ignore |
| 11 |
from . import serve |
| 12 |
from .serve import ConfigBuilder, inject_script |
| 13 |
|
| 14 |
|
| 15 |
logger = logging.getLogger() |
| 16 |
|
| 17 |
@pytest.mark.skipif(platform.uname()[0] == "Windows", |
| 18 |
reason="Expected contents are platform-dependent") |
| 19 |
def test_make_hosts_file_nix(): |
| 20 |
with ConfigBuilder(logger, |
| 21 |
ports={"http": [8000]}, |
| 22 |
browser_host="foo.bar", |
| 23 |
alternate_hosts={"alt": "foo2.bar"}, |
| 24 |
subdomains={"a", "b"}, |
| 25 |
not_subdomains={"x, y"}) as c: |
| 26 |
hosts = serve.make_hosts_file(c, "192.168.42.42") |
| 27 |
lines = hosts.split("\n") |
| 28 |
assert lines == [ |
| 29 |
"# Start web-platform-tests hosts", |
| 30 |
"192.168.42.42\tfoo.bar", |
| 31 |
"192.168.42.42\ta.foo.bar", |
| 32 |
"192.168.42.42\tb.foo.bar", |
| 33 |
"192.168.42.42\tfoo2.bar", |
| 34 |
"192.168.42.42\ta.foo2.bar", |
| 35 |
"192.168.42.42\tb.foo2.bar", |
| 36 |
"# End web-platform-tests hosts", |
| 37 |
"", |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.skipif(platform.uname()[0] != "Windows", |
| 42 |
reason="Expected contents are platform-dependent") |
| 43 |
def test_make_hosts_file_windows(): |
| 44 |
with ConfigBuilder(logger, |
| 45 |
ports={"http": [8000]}, |
| 46 |
browser_host="foo.bar", |
| 47 |
alternate_hosts={"alt": "foo2.bar"}, |
| 48 |
subdomains={"a", "b"}, |
| 49 |
not_subdomains={"x", "y"}) as c: |
| 50 |
hosts = serve.make_hosts_file(c, "192.168.42.42") |
| 51 |
lines = hosts.split("\n") |
| 52 |
assert lines == [ |
| 53 |
"# Start web-platform-tests hosts", |
| 54 |
"192.168.42.42\tfoo.bar", |
| 55 |
"192.168.42.42\ta.foo.bar", |
| 56 |
"192.168.42.42\tb.foo.bar", |
| 57 |
"192.168.42.42\tfoo2.bar", |
| 58 |
"192.168.42.42\ta.foo2.bar", |
| 59 |
"192.168.42.42\tb.foo2.bar", |
| 60 |
"0.0.0.0\tx.foo.bar", |
| 61 |
"0.0.0.0\ty.foo.bar", |
| 62 |
"0.0.0.0\tx.foo2.bar", |
| 63 |
"0.0.0.0\ty.foo2.bar", |
| 64 |
"# End web-platform-tests hosts", |
| 65 |
"", |
| 66 |
] |
| 67 |
|
| 68 |
|
| 69 |
def test_ws_doc_root_default(): |
| 70 |
with ConfigBuilder(logger) as c: |
| 71 |
assert c.doc_root == localpaths.repo_root |
| 72 |
assert c.ws_doc_root == os.path.join(localpaths.repo_root, "websockets", "handlers") |
| 73 |
assert c.paths["ws_doc_root"] == c.ws_doc_root |
| 74 |
|
| 75 |
|
| 76 |
def test_init_ws_doc_root(): |
| 77 |
with ConfigBuilder(logger, ws_doc_root="/") as c: |
| 78 |
assert c.doc_root == localpaths.repo_root # check this hasn't changed |
| 79 |
assert c.ws_doc_root == "/" |
| 80 |
assert c.paths["ws_doc_root"] == c.ws_doc_root |
| 81 |
|
| 82 |
|
| 83 |
def test_set_ws_doc_root(): |
| 84 |
cb = ConfigBuilder(logger) |
| 85 |
cb.ws_doc_root = "/" |
| 86 |
with cb as c: |
| 87 |
assert c.doc_root == localpaths.repo_root # check this hasn't changed |
| 88 |
assert c.ws_doc_root == "/" |
| 89 |
assert c.paths["ws_doc_root"] == c.ws_doc_root |
| 90 |
|
| 91 |
|
| 92 |
def test_pickle(): |
| 93 |
# Ensure that the config object can be pickled |
| 94 |
with ConfigBuilder(logger) as c: |
| 95 |
pickle.dumps(c) |
| 96 |
|
| 97 |
|
| 98 |
def test_alternate_host_unspecified(): |
| 99 |
ConfigBuilder(logger, browser_host="web-platform.test") |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize("primary, alternate", [ |
| 103 |
("web-platform.test", "web-platform.test"), |
| 104 |
("a.web-platform.test", "web-platform.test"), |
| 105 |
("web-platform.test", "a.web-platform.test"), |
| 106 |
("a.web-platform.test", "a.web-platform.test"), |
| 107 |
]) |
| 108 |
def test_alternate_host_invalid(primary, alternate): |
| 109 |
with pytest.raises(ValueError): |
| 110 |
ConfigBuilder(logger, browser_host=primary, alternate_hosts={"alt": alternate}) |
| 111 |
|
| 112 |
@pytest.mark.parametrize("primary, alternate", [ |
| 113 |
("web-platform.test", "not-web-platform.test"), |
| 114 |
("a.web-platform.test", "b.web-platform.test"), |
| 115 |
("web-platform-tests.dev", "web-platform-tests.live"), |
| 116 |
]) |
| 117 |
def test_alternate_host_valid(primary, alternate): |
| 118 |
ConfigBuilder(logger, browser_host=primary, alternate_hosts={"alt": alternate}) |
| 119 |
|
| 120 |
|
| 121 |
# A token marking the location of expected script injection. |
| 122 |
INJECT_SCRIPT_MARKER = b"<!-- inject here -->" |
| 123 |
|
| 124 |
|
| 125 |
def test_inject_script_after_head(): |
| 126 |
html = b"""<!DOCTYPE html> |
| 127 |
<html> |
| 128 |
<head> |
| 129 |
<!-- inject here --><script src="test.js"></script> |
| 130 |
</head> |
| 131 |
<body> |
| 132 |
</body> |
| 133 |
</html>""" |
| 134 |
assert INJECT_SCRIPT_MARKER in html |
| 135 |
assert inject_script(html.replace(INJECT_SCRIPT_MARKER, b""), INJECT_SCRIPT_MARKER) == html |
| 136 |
|
| 137 |
|
| 138 |
def test_inject_script_no_html_head(): |
| 139 |
html = b"""<!DOCTYPE html> |
| 140 |
<!-- inject here --><div></div>""" |
| 141 |
assert INJECT_SCRIPT_MARKER in html |
| 142 |
assert inject_script(html.replace(INJECT_SCRIPT_MARKER, b""), INJECT_SCRIPT_MARKER) == html |
| 143 |
|
| 144 |
|
| 145 |
def test_inject_script_no_doctype(): |
| 146 |
html = b"""<!-- inject here --><div></div>""" |
| 147 |
assert INJECT_SCRIPT_MARKER in html |
| 148 |
assert inject_script(html.replace(INJECT_SCRIPT_MARKER, b""), INJECT_SCRIPT_MARKER) == html |
| 149 |
|
| 150 |
|
| 151 |
def test_inject_script_parse_error(): |
| 152 |
html = b"""<!--<!-- inject here --><div></div>""" |
| 153 |
assert INJECT_SCRIPT_MARKER in html |
| 154 |
# On a parse error, the script should not be injected and the original content should be |
| 155 |
# returned. |
| 156 |
assert INJECT_SCRIPT_MARKER not in inject_script(html.replace(INJECT_SCRIPT_MARKER, b""), INJECT_SCRIPT_MARKER) |