Skip to content
Myr

test_url.py

This file

Path

tests/models/test_url.py

205 lines · 6.7 KiB · Python

Last changed by

params argument on URL should merge, not replace. (#653)Tom Christiebefore adoption74e5115

History of this file

Take it

1import pytest23from httpx import URL, InvalidURL, Origin456@pytest.mark.parametrize(7    "given,idna,host,scheme,port",8    [9        (10            "http://中国.icom.museum:80/",11            "http://xn--fiqs8s.icom.museum:80/",12            "xn--fiqs8s.icom.museum",13            "http",14            80,15        ),16        (17            "http://Königsgäßchen.de",18            "http://xn--knigsgchen-b4a3dun.de",19            "xn--knigsgchen-b4a3dun.de",20            "http",21            80,22        ),23        ("https://faß.de", "https://xn--fa-hia.de", "xn--fa-hia.de", "https", 443),24        (25            "https://βόλος.com:443",26            "https://xn--nxasmm1c.com:443",27            "xn--nxasmm1c.com",28            "https",29            443,30        ),31        (32            "http://ශ්‍රී.com:444",33            "http://xn--10cl1a0b660p.com:444",34            "xn--10cl1a0b660p.com",35            "http",36            444,37        ),38        (39            "https://نامه‌ای.com:4433",40            "https://xn--mgba3gch31f060k.com:4433",41            "xn--mgba3gch31f060k.com",42            "https",43            4433,44        ),45    ],46    ids=[47        "http_with_port",48        "unicode_tr46_compat",49        "https_without_port",50        "https_with_port",51        "http_with_custom_port",52        "https_with_custom_port",53    ],54)55def test_idna_url(given, idna, host, scheme, port):56    url = URL(given)57    assert url == URL(idna)58    assert url.host == host59    assert url.scheme == scheme60    assert url.port == port616263def test_url():64    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")65    assert url.scheme == "https"66    assert url.host == "example.org"67    assert url.port == 12368    assert url.authority == "example.org:123"69    assert url.path == "/path/to/somewhere"70    assert url.query == "abc=123"71    assert url.fragment == "anchor"72    assert (73        repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"74    )7576    new = url.copy_with(scheme="http", port=None)77    assert new == URL("http://example.org/path/to/somewhere?abc=123#anchor")78    assert new.scheme == "http"798081def test_url_eq_str():82    url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")83    assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"84    assert str(url) == url858687def test_url_params():88    url = URL("https://example.org:123/path/to/somewhere", params={"a": "123"})89    assert str(url) == "https://example.org:123/path/to/somewhere?a=123"9091    url = URL("https://example.org:123/path/to/somewhere?b=456", params={"a": "123"})92    assert str(url) == "https://example.org:123/path/to/somewhere?b=456&a=123"939495def test_url_join():96    """97    Some basic URL joining tests.98    """99    url = URL("https://example.org:123/path/to/somewhere")100    assert url.join("/somewhere-else") == "https://example.org:123/somewhere-else"101    assert (102        url.join("somewhere-else") == "https://example.org:123/path/to/somewhere-else"103    )104    assert (105        url.join("../somewhere-else") == "https://example.org:123/path/somewhere-else"106    )107    assert url.join("../../somewhere-else") == "https://example.org:123/somewhere-else"108109110def test_url_join_rfc3986():111    """112    URL joining tests, as-per reference examples in RFC 3986.113114    https://tools.ietf.org/html/rfc3986#section-5.4115    """116117    url = URL("http://example.com/b/c/d;p?q")118119    with pytest.raises(InvalidURL):120        assert url.join("g:h") == "g:h"121122    assert url.join("g") == "http://example.com/b/c/g"123    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/g"126    assert url.join("//g") == "http://g"127    assert url.join("?y") == "http://example.com/b/c/d;p?y"128    assert url.join("g?y") == "http://example.com/b/c/g?y"129    assert url.join("#s") == "http://example.com/b/c/d;p?q#s"130    assert url.join("g#s") == "http://example.com/b/c/g#s"131    assert url.join("g?y#s") == "http://example.com/b/c/g?y#s"132    assert url.join(";x") == "http://example.com/b/c/;x"133    assert url.join("g;x") == "http://example.com/b/c/g;x"134    assert url.join("g;x?y#s") == "http://example.com/b/c/g;x?y#s"135    assert url.join("") == "http://example.com/b/c/d;p?q"136    assert url.join(".") == "http://example.com/b/c/"137    assert url.join("./") == "http://example.com/b/c/"138    assert url.join("..") == "http://example.com/b/"139    assert url.join("../") == "http://example.com/b/"140    assert url.join("../g") == "http://example.com/b/g"141    assert url.join("../..") == "http://example.com/"142    assert url.join("../../") == "http://example.com/"143    assert url.join("../../g") == "http://example.com/g"144145    assert url.join("../../../g") == "http://example.com/g"146    assert url.join("../../../../g") == "http://example.com/g"147148    assert url.join("/./g") == "http://example.com/g"149    assert url.join("/../g") == "http://example.com/g"150    assert url.join("g.") == "http://example.com/b/c/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"154155    assert url.join("./../g") == "http://example.com/b/g"156    assert url.join("./g/.") == "http://example.com/b/c/g/"157    assert url.join("g/./h") == "http://example.com/b/c/g/h"158    assert url.join("g/../h") == "http://example.com/b/c/h"159    assert url.join("g;x=1/./y") == "http://example.com/b/c/g;x=1/y"160    assert url.join("g;x=1/../y") == "http://example.com/b/c/y"161162    assert url.join("g?y/./x") == "http://example.com/b/c/g?y/./x"163    assert url.join("g?y/../x") == "http://example.com/b/c/g?y/../x"164    assert url.join("g#s/./x") == "http://example.com/b/c/g#s/./x"165    assert url.join("g#s/../x") == "http://example.com/b/c/g#s/../x"166167168def test_url_set():169    urls = (170        URL("http://example.org:123/path/to/somewhere"),171        URL("http://example.org:123/path/to/somewhere/else"),172    )173174    url_set = set(urls)175176    assert all(url in urls for url in url_set)177178179def test_url_full_path_setter():180    url = URL("http://example.org")181182    url.full_path = "http://example.net"183    assert url.full_path == "http://example.net"184185186def test_origin_from_url_string():187    origin = Origin("https://example.com")188    assert origin.scheme == "https"189    assert origin.is_ssl190    assert origin.host == "example.com"191    assert origin.port == 443192193194def test_url_copywith_for_authority():195    copy_with_kwargs = {196        "username": "username",197        "password": "password",198        "port": 444,199        "host": "example.net",200    }201    url = URL("https://example.org")202    new = url.copy_with(**copy_with_kwargs)203    for k, v in copy_with_kwargs.items():204        assert getattr(new, k) == v205    assert str(new) == "https://username:password@example.net:444"