Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

177 lines · 6.0 KiB · Python

Last changed by

Use nox for automation and CI (#128)Seth Michael Larsonbefore adoption80dc3e2

History of this file

Take it

1import pytest23from httpx import URL4from httpx.exceptions import InvalidURL567@pytest.mark.parametrize(8    "given,idna,host,scheme,port",9    [10        (11            "http://中国.icom.museum:80/",12            "http://xn--fiqs8s.icom.museum:80/",13            "xn--fiqs8s.icom.museum",14            "http",15            80,16        ),17        (18            "http://Königsgäßchen.de",19            "http://xn--knigsgchen-b4a3dun.de",20            "xn--knigsgchen-b4a3dun.de",21            "http",22            80,23        ),24        ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),25        (26            "https://βόλος.com:443",27            "https://xn--nxasmm1c.com:443",28            "xn--nxasmm1c.com",29            "https",30            443,31        ),32        (33            "http://ශ්‍රී.com:444",34            "http://xn--10cl1a0b660p.com:444",35            "xn--10cl1a0b660p.com",36            "http",37            444,38        ),39        (40            "https://نامه‌ای.com:4433",41            "https://xn--mgba3gch31f060k.com:4433",42            "xn--mgba3gch31f060k.com",43            "https",44            4433,45        ),46    ],47    ids=[48        "http_with_port",49        "unicode_tr46_compat",50        "https_without_port",51        "https_with_port",52        "http_with_custom_port",53        "https_with_custom_port",54    ],55)56def test_idna_url(given, idna, host, scheme, port):57    url = URL(given)58    assert url == URL(idna)59    assert url.host == host60    assert url.scheme == scheme61    assert url.port == port626364def test_url():65    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")66    assert url.scheme == "https"67    assert url.host == "example.org"68    assert url.port == 12369    assert url.authority == "example.org:123"70    assert url.path == "/path/to/somewhere"71    assert url.query == "abc=123"72    assert url.fragment == "anchor"73    assert (74        repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"75    )7677    new = url.copy_with(scheme="http")78    assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")79    assert new.scheme == "http"808182def test_url_eq_str():83    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")84    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"85    assert str(url) == url868788def test_url_params():89    url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})90    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"9192    url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})93    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"949596def test_url_join():97    """98    Some basic URL joining tests.99    """100    url = URL("https://example.org:123/path/to/somewhere")101    assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"102    assert (103        url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"104    )105    assert (106        url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"107    )108    assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"109110111def test_url_join_rfc3986():112    """113    URL joining tests, as-per reference examples in RFC 3986.114115    https://tools.ietf.org/html/rfc3986#section-5.4116    """117118    url = URL("http://example.com/b/c/d;p?q")119120    with pytest.raises(InvalidURL):121        assert url.join("g:h") == "g:h"122123    assert url.join("g") == "http://example.com/b/c/g"124    assert url.join("./g") == "http://example.com/b/c/g"125    assert url.join("g/") == "http://example.com/b/c/g/"126    assert url.join("/g") == "http://example.com/g"127    assert url.join("//g") == "http://g"128    assert url.join("?y") == "http://example.com/b/c/d;p?y"129    assert url.join("g?y") == "http://example.com/b/c/g?y"130    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"131    assert url.join("g#s") == "http://example.com/b/c/g#s"132    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"133    assert url.join(";x") == "http://example.com/b/c/;x"134    assert url.join("g;x") == "http://example.com/b/c/g;x"135    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"136    assert url.join("") == "http://example.com/b/c/d;p?q"137    assert url.join(".") == "http://example.com/b/c/"138    assert url.join("./") == "http://example.com/b/c/"139    assert url.join("..") == "http://example.com/b/"140    assert url.join("../") == "http://example.com/b/"141    assert url.join("../g") == "http://example.com/b/g"142    assert url.join("../..") == "http://example.com/"143    assert url.join("../../") == "http://example.com/"144    assert url.join("../../g") == "http://example.com/g"145146    assert url.join("../../../g") == "http://example.com/g"147    assert url.join("../../../../g") == "http://example.com/g"148149    assert url.join("/./g") == "http://example.com/g"150    assert url.join("/../g") == "http://example.com/g"151    assert url.join("g.") == "http://example.com/b/c/g."152    assert url.join(".g") == "http://example.com/b/c/.g"153    assert url.join("g..") == "http://example.com/b/c/g.."154    assert url.join("..g") == "http://example.com/b/c/..g"155156    assert url.join("./../g") == "http://example.com/b/g"157    assert url.join("./g/.") == "http://example.com/b/c/g/"158    assert url.join("g/./h") == "http://example.com/b/c/g/h"159    assert url.join("g/../h") == "http://example.com/b/c/h"160    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"161    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"162163    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"164    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"165    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"166    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"167168169def test_url_set():170    urls = (171        URL("http://example.org:123/path/to/somewhere"),172        URL("http://example.org:123/path/to/somewhere/else"),173    )174175    url_set = set(urls)176177    assert all(url in urls for url in url_set)