Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

186 lines · 6.2 KiB · Python

Last changed by

Update IDNA encoding to 2008 spec (#161)Stephen Brown IIbefore adoption40849bf

History of this file

Take it

1import pytest2import rfc398634from httpx import URL5from httpx.exceptions import InvalidURL678@pytest.mark.parametrize(9    "given,idna,host,scheme,port",10    [11        (12            "http://中国.icom.museum:80/",13            "http://xn--fiqs8s.icom.museum:80/",14            "xn--fiqs8s.icom.museum",15            "http",16            80,17        ),18        (19            "http://Königsgäßchen.de",20            "http://xn--knigsgchen-b4a3dun.de",21            "xn--knigsgchen-b4a3dun.de",22            "http",23            80,24        ),25        ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),26        (27            "https://βόλος.com:443",28            "https://xn--nxasmm1c.com:443",29            "xn--nxasmm1c.com",30            "https",31            443,32        ),33        (34            "http://ශ්‍රී.com:444",35            "http://xn--10cl1a0b660p.com:444",36            "xn--10cl1a0b660p.com",37            "http",38            444,39        ),40        (41            "https://نامه‌ای.com:4433",42            "https://xn--mgba3gch31f060k.com:4433",43            "xn--mgba3gch31f060k.com",44            "https",45            4433,46        ),47    ],48    ids=[49        "http_with_port",50        "unicode_tr46_compat",51        "https_without_port",52        "https_with_port",53        "http_with_custom_port",54        "https_with_custom_port",55    ],56)57def test_idna_url(given, idna, host, scheme, port):58    url = URL(given)59    assert url == URL(idna)60    assert url.host == host61    assert url.scheme == scheme62    assert url.port == port636465def test_url():66    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")67    assert url.scheme == "https"68    assert url.host == "example.org"69    assert url.port == 12370    assert url.authority == "example.org:123"71    assert url.path == "/path/to/somewhere"72    assert url.query == "abc=123"73    assert url.fragment == "anchor"74    assert (75        repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"76    )7778    new = url.copy_with(scheme="http")79    assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")80    assert new.scheme == "http"818283def test_url_eq_str():84    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")85    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"86    assert str(url) == url878889def test_url_params():90    url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})91    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"9293    url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})94    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"959697def test_url_join():98    """99    Some basic URL joining tests.100    """101    url = URL("https://example.org:123/path/to/somewhere")102    assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"103    assert (104        url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"105    )106    assert (107        url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"108    )109    assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"110111112def test_url_join_rfc3986():113    """114    URL joining tests, as-per reference examples in RFC 3986.115116    https://tools.ietf.org/html/rfc3986#section-5.4117    """118119    url = URL("http://example.com/b/c/d;p?q")120121    with pytest.raises(InvalidURL):122        assert url.join("g:h") == "g:h"123124    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/b/c/g/"127    assert url.join("/g") == "http://example.com/g"128    assert url.join("//g") == "http://g"129    assert url.join("?y") == "http://example.com/b/c/d;p?y"130    assert url.join("g?y") == "http://example.com/b/c/g?y"131    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"132    assert url.join("g#s") == "http://example.com/b/c/g#s"133    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"134    assert url.join(";x") == "http://example.com/b/c/;x"135    assert url.join("g;x") == "http://example.com/b/c/g;x"136    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"137    assert url.join("") == "http://example.com/b/c/d;p?q"138    assert url.join(".") == "http://example.com/b/c/"139    assert url.join("./") == "http://example.com/b/c/"140    assert url.join("..") == "http://example.com/b/"141    assert url.join("../") == "http://example.com/b/"142    assert url.join("../g") == "http://example.com/b/g"143    assert url.join("../..") == "http://example.com/"144    assert url.join("../../") == "http://example.com/"145    assert url.join("../../g") == "http://example.com/g"146147    assert url.join("../../../g") == "http://example.com/g"148    assert url.join("../../../../g") == "http://example.com/g"149150    assert url.join("/./g") == "http://example.com/g"151    assert url.join("/../g") == "http://example.com/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.."155    assert url.join("..g") == "http://example.com/b/c/..g"156157    assert url.join("./../g") == "http://example.com/b/g"158    assert url.join("./g/.") == "http://example.com/b/c/g/"159    assert url.join("g/./h") == "http://example.com/b/c/g/h"160    assert url.join("g/../h") == "http://example.com/b/c/h"161    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"162    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"163164    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"165    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"166    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"167    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"168169170def test_url_set():171    urls = (172        URL("http://example.org:123/path/to/somewhere"),173        URL("http://example.org:123/path/to/somewhere/else"),174    )175176    url_set = set(urls)177178    assert all(url in urls for url in url_set)179180181def test_hsts_preload_converted_to_https():182    url = URL("http://www.paypal.com")183184    assert url.is_ssl185    assert url.scheme == "https"186    assert url == "https://www.paypal.com"