Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

378 lines · 12.0 KiB · Python

Last changed by

Perform port normalization for http, https, ws, wss, and ftp schemes (#1603)Tom Christiebefore adoption0a8b44e

History of this file

Take it

1import pytest23import httpx456@pytest.mark.parametrize(7    "given,idna,host,raw_host,scheme,port",8    [9        (10            "http://中国.icom.museum:80/",11            "http://xn--fiqs8s.icom.museum:80/",12            "中国.icom.museum",13            b"xn--fiqs8s.icom.museum",14            "http",15            None,16        ),17        (18            "http://Königsgäßchen.de",19            "http://xn--knigsgchen-b4a3dun.de",20            "königsgäßchen.de",21            b"xn--knigsgchen-b4a3dun.de",22            "http",23            None,24        ),25        (26            "https://faß.de",27            "https://xn--fa-hia.de",28            "faß.de",29            b"xn--fa-hia.de",30            "https",31            None,32        ),33        (34            "https://βόλος.com:443",35            "https://xn--nxasmm1c.com:443",36            "βόλος.com",37            b"xn--nxasmm1c.com",38            "https",39            None,40        ),41        (42            "http://ශ්‍රී.com:444",43            "http://xn--10cl1a0b660p.com:444",44            "ශ්‍රී.com",45            b"xn--10cl1a0b660p.com",46            "http",47            444,48        ),49        (50            "https://نامه‌ای.com:4433",51            "https://xn--mgba3gch31f060k.com:4433",52            "نامه‌ای.com",53            b"xn--mgba3gch31f060k.com",54            "https",55            4433,56        ),57    ],58    ids=[59        "http_with_port",60        "unicode_tr46_compat",61        "https_without_port",62        "https_with_port",63        "http_with_custom_port",64        "https_with_custom_port",65    ],66)67def test_idna_url(given, idna, host, raw_host, scheme, port):68    url = httpx.URL(given)69    assert url == httpx.URL(idna)70    assert url.host == host71    assert url.raw_host == raw_host72    assert url.scheme == scheme73    assert url.port == port747576def test_url():77    url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")78    assert url.scheme == "https"79    assert url.host == "example.org"80    assert url.port == 12381    assert url.path == "/path/to/somewhere"82    assert url.query == b"abc=123"83    assert url.raw_path == b"/path/to/somewhere?abc=123"84    assert url.fragment == "anchor"85    assert (86        repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"87    )8889    new = url.copy_with(scheme="http", port=None)90    assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")91    assert new.scheme == "http"929394def test_url_eq_str():95    url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")96    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"97    assert str(url) == url9899100def test_url_params():101    url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})102    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"103    assert url.params == httpx.QueryParams({"a": "123"})104105    url = httpx.URL(106        "https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}107    )108    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"109    assert url.params == httpx.QueryParams({"a": "123"})110111112def test_url_join():113    """114    Some basic URL joining tests.115    """116    url = httpx.URL("https://example.org:123/path/to/somewhere")117    assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"118    assert (119        url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"120    )121    assert (122        url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"123    )124    assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"125126127def test_url_set_param_manipulation():128    """129    Some basic URL query parameter manipulation.130    """131    url = httpx.URL("https://example.org:123/?a=123")132    assert url.copy_set_param("a", "456") == "https://example.org:123/?a=456"133134135def test_url_add_param_manipulation():136    """137    Some basic URL query parameter manipulation.138    """139    url = httpx.URL("https://example.org:123/?a=123")140    assert url.copy_add_param("a", "456") == "https://example.org:123/?a=123&a=456"141142143def test_url_remove_param_manipulation():144    """145    Some basic URL query parameter manipulation.146    """147    url = httpx.URL("https://example.org:123/?a=123")148    assert url.copy_remove_param("a") == "https://example.org:123/"149150151def test_url_merge_params_manipulation():152    """153    Some basic URL query parameter manipulation.154    """155    url = httpx.URL("https://example.org:123/?a=123")156    assert url.copy_merge_params({"b": "456"}) == "https://example.org:123/?a=123&b=456"157158159def test_relative_url_join():160    url = httpx.URL("/path/to/somewhere")161    assert url.join("/somewhere-else") == "/somewhere-else"162    assert url.join("somewhere-else") == "/path/to/somewhere-else"163    assert url.join("../somewhere-else") == "/path/somewhere-else"164    assert url.join("../../somewhere-else") == "/somewhere-else"165166167def test_url_join_rfc3986():168    """169    URL joining tests, as-per reference examples in RFC 3986.170171    https://tools.ietf.org/html/rfc3986#section-5.4172    """173174    url = httpx.URL("http://example.com/b/c/d;p?q")175176    assert url.join("g") == "http://example.com/b/c/g"177    assert url.join("./g") == "http://example.com/b/c/g"178    assert url.join("g/") == "http://example.com/b/c/g/"179    assert url.join("/g") == "http://example.com/g"180    assert url.join("//g") == "http://g"181    assert url.join("?y") == "http://example.com/b/c/d;p?y"182    assert url.join("g?y") == "http://example.com/b/c/g?y"183    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"184    assert url.join("g#s") == "http://example.com/b/c/g#s"185    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"186    assert url.join(";x") == "http://example.com/b/c/;x"187    assert url.join("g;x") == "http://example.com/b/c/g;x"188    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"189    assert url.join("") == "http://example.com/b/c/d;p?q"190    assert url.join(".") == "http://example.com/b/c/"191    assert url.join("./") == "http://example.com/b/c/"192    assert url.join("..") == "http://example.com/b/"193    assert url.join("../") == "http://example.com/b/"194    assert url.join("../g") == "http://example.com/b/g"195    assert url.join("../..") == "http://example.com/"196    assert url.join("../../") == "http://example.com/"197    assert url.join("../../g") == "http://example.com/g"198199    assert url.join("../../../g") == "http://example.com/g"200    assert url.join("../../../../g") == "http://example.com/g"201202    assert url.join("/./g") == "http://example.com/g"203    assert url.join("/../g") == "http://example.com/g"204    assert url.join("g.") == "http://example.com/b/c/g."205    assert url.join(".g") == "http://example.com/b/c/.g"206    assert url.join("g..") == "http://example.com/b/c/g.."207    assert url.join("..g") == "http://example.com/b/c/..g"208209    assert url.join("./../g") == "http://example.com/b/g"210    assert url.join("./g/.") == "http://example.com/b/c/g/"211    assert url.join("g/./h") == "http://example.com/b/c/g/h"212    assert url.join("g/../h") == "http://example.com/b/c/h"213    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"214    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"215216    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"217    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"218    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"219    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"220221222def test_url_set():223    urls = (224        httpx.URL("http://example.org:123/path/to/somewhere"),225        httpx.URL("http://example.org:123/path/to/somewhere/else"),226    )227228    url_set = set(urls)229230    assert all(url in urls for url in url_set)231232233def test_url_copywith_authority_subcomponents():234    copy_with_kwargs = {235        "username": "username",236        "password": "password",237        "port": 444,238        "host": "example.net",239    }240    url = httpx.URL("https://example.org")241    new = url.copy_with(**copy_with_kwargs)242    assert str(new) == "https://username:password@example.net:444"243244245def test_url_copywith_netloc():246    copy_with_kwargs = {247        "netloc": b"example.net:444",248    }249    url = httpx.URL("https://example.org")250    new = url.copy_with(**copy_with_kwargs)251    assert str(new) == "https://example.net:444"252253254def test_url_copywith_userinfo_subcomponents():255    copy_with_kwargs = {256        "username": "tom@example.org",257        "password": "abc123@ %",258    }259    url = httpx.URL("https://example.org")260    new = url.copy_with(**copy_with_kwargs)261    assert str(new) == "https://tom%40example.org:abc123%40%20%25@example.org"262    assert new.username == "tom@example.org"263    assert new.password == "abc123@ %"264    assert new.userinfo == b"tom%40example.org:abc123%40%20%25"265266267def test_url_copywith_invalid_component():268    url = httpx.URL("https://example.org")269    with pytest.raises(TypeError):270        url.copy_with(pathh="/incorrect-spelling")271    with pytest.raises(TypeError):272        url.copy_with(userinfo="should be bytes")273274275def test_url_copywith_urlencoded_path():276    url = httpx.URL("https://example.org")277    url = url.copy_with(path="/path to somewhere")278    assert url.path == "/path to somewhere"279    assert url.query == b""280    assert url.raw_path == b"/path%20to%20somewhere"281282283def test_url_copywith_query():284    url = httpx.URL("https://example.org")285    url = url.copy_with(query=b"a=123")286    assert url.path == "/"287    assert url.query == b"a=123"288    assert url.raw_path == b"/?a=123"289290291def test_url_copywith_raw_path():292    url = httpx.URL("https://example.org")293    url = url.copy_with(raw_path=b"/some/path")294    assert url.path == "/some/path"295    assert url.query == b""296    assert url.raw_path == b"/some/path"297298    url = httpx.URL("https://example.org")299    url = url.copy_with(raw_path=b"/some/path?")300    assert url.path == "/some/path"301    assert url.query == b""302    assert url.raw_path == b"/some/path?"303304    url = httpx.URL("https://example.org")305    url = url.copy_with(raw_path=b"/some/path?a=123")306    assert url.path == "/some/path"307    assert url.query == b"a=123"308    assert url.raw_path == b"/some/path?a=123"309310311def test_url_invalid():312    with pytest.raises(httpx.InvalidURL):313        httpx.URL("https://😇/")314315316def test_url_invalid_type():317    class ExternalURLClass:  # representing external URL class318        pass319320    with pytest.raises(TypeError):321        httpx.URL(ExternalURLClass())  # type: ignore322323324def test_url_with_empty_query():325    """326    URLs with and without a trailing `?` but an empty query component327    should preserve the information on the raw path.328    """329    url = httpx.URL("https://www.example.com/path")330    assert url.path == "/path"331    assert url.query == b""332    assert url.raw_path == b"/path"333334    url = httpx.URL("https://www.example.com/path?")335    assert url.path == "/path"336    assert url.query == b""337    assert url.raw_path == b"/path?"338339340def test_url_with_url_encoded_path():341    url = httpx.URL("https://www.example.com/path%20to%20somewhere")342    assert url.path == "/path to somewhere"343    assert url.query == b""344    assert url.raw_path == b"/path%20to%20somewhere"345346347def test_ipv6_url():348    url = httpx.URL("http://[::ffff:192.168.0.1]:5678/")349350    assert url.host == "::ffff:192.168.0.1"351    assert url.netloc == b"[::ffff:192.168.0.1]:5678"352353354@pytest.mark.parametrize(355    "url_str",356    [357        "http://127.0.0.1:1234",358        "http://example.com:1234",359        "http://[::ffff:127.0.0.1]:1234",360    ],361)362@pytest.mark.parametrize("new_host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])363def test_ipv6_url_copy_with_host(url_str, new_host):364    url = httpx.URL(url_str).copy_with(host=new_host)365366    assert url.host == "::ffff:192.168.0.1"367    assert url.netloc == b"[::ffff:192.168.0.1]:1234"368    assert str(url) == "http://[::ffff:192.168.0.1]:1234"369370371@pytest.mark.parametrize("host", [b"[::ffff:192.168.0.1]", b"::ffff:192.168.0.1"])372def test_ipv6_url_from_raw_url(host):373    raw_url = (b"https", host, 443, b"/")374    url = httpx.URL(raw_url)375376    assert url.host == "::ffff:192.168.0.1"377    assert url.netloc == b"[::ffff:192.168.0.1]"378    assert str(url) == "https://[::ffff:192.168.0.1]/"