Skip to content
Myr

test_whatwg.py

This file

Path

tests/models/test_whatwg.py

52 lines · 1.9 KiB · Python

Last changed by

Ensure JSON representation is compact. #3363 (#3367)BERRADA-Omarbefore adoption9fd6f0c

History of this file

Take it

1# The WHATWG have various tests that can be used to validate the URL parsing.2#3# https://url.spec.whatwg.org/45import json67import pytest89from httpx._urlparse import urlparse1011# URL test cases from...12# https://github.com/web-platform-tests/wpt/blob/master/url/resources/urltestdata.json13with open("tests/models/whatwg.json", "r", encoding="utf-8") as input:14    test_cases = json.load(input)15    test_cases = [16        item17        for item in test_cases18        if not isinstance(item, str) and not item.get("failure")19    ]202122@pytest.mark.parametrize("test_case", test_cases)23def test_urlparse(test_case):24    if test_case["href"] in ("a: foo.com", "lolscheme:x x#x%20x"):25        # Skip these two test cases.26        # WHATWG cases where are not using percent-encoding for the space character.27        # Anyone know what's going on here?28        return2930    p = urlparse(test_case["href"])3132    # Test cases include the protocol with the trailing ":"33    protocol = p.scheme + ":"34    # Include the square brackets for IPv6 addresses.35    hostname = f"[{p.host}]" if ":" in p.host else p.host36    # The test cases use a string representation of the port.37    port = "" if p.port is None else str(p.port)38    # I have nothing to say about this one.39    path = p.path40    # The 'search' and 'hash' components in the whatwg tests are semantic, not literal.41    # Our parsing differentiates between no query/hash and empty-string query/hash.42    search = "" if p.query in (None, "") else "?" + str(p.query)43    hash = "" if p.fragment in (None, "") else "#" + str(p.fragment)4445    # URL hostnames are case-insensitive.46    # We normalize these, unlike the WHATWG test cases.47    assert protocol == test_case["protocol"]48    assert hostname.lower() == test_case["hostname"].lower()49    assert port == test_case["port"]50    assert path == test_case["pathname"]51    assert search == test_case["search"]52    assert hash == test_case["hash"]