-
-
Notifications
You must be signed in to change notification settings - Fork 883
/
Copy pathtest_whatwg.py
54 lines (45 loc) · 1.97 KB
/
test_whatwg.py
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
# The WHATWG have various tests that can be used to validate the URL parsing.
#
# https://url.spec.whatwg.org/
import json
from pathlib import Path
import pytest
from httpx._urlparse import urlparse
# URL test cases from...
# https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json
whatwg_path = Path(__file__).parent.parent / "models" / "whatwg.json"
with whatwg_path.open("r", encoding="utf-8") as input:
test_cases = json.load(input)
test_cases = [
item
for item in test_cases
if not isinstance(item, str) and not item.get("failure")
]
@pytest.mark.parametrize("test_case", test_cases)
def test_urlparse(test_case):
if test_case["href"] in ("a: foo.com", "lolscheme:x x#x%20x"):
# Skip these two test cases.
# WHATWG cases where are not using percent-encoding for the space character.
# Anyone know what's going on here?
return
p = urlparse(test_case["href"])
# Test cases include the protocol with the trailing ":"
protocol = p.scheme + ":"
# Include the square brackets for IPv6 addresses.
hostname = f"[{p.host}]" if ":" in p.host else p.host
# The test cases use a string representation of the port.
port = "" if p.port is None else str(p.port)
# I have nothing to say about this one.
path = p.path
# The 'search' and 'hash' components in the whatwg tests are semantic, not literal.
# Our parsing differentiates between no query/hash and empty-string query/hash.
search = "" if p.query in (None, "") else "?" + str(p.query)
hash = "" if p.fragment in (None, "") else "#" + str(p.fragment)
# URL hostnames are case-insensitive.
# We normalize these, unlike the WHATWG test cases.
assert protocol == test_case["protocol"]
assert hostname.lower() == test_case["hostname"].lower()
assert port == test_case["port"]
assert path == test_case["pathname"]
assert search == test_case["search"]
assert hash == test_case["hash"]