Skip to content
Myr

test_urlparse.py

This file

Path

tests/test_urlparse.py

232 lines · 6.2 KiB · Python

Last changed by

Drop `rfc3986` requirement. (#2252)Tom Christiebefore adoption57daabf

History of this file

Take it

1import pytest23import httpx4from httpx._urlparse import urlparse567def test_urlparse():8    url = urlparse("https://www.example.com/")910    assert url.scheme == "https"11    assert url.userinfo == ""12    assert url.netloc == "www.example.com"13    assert url.host == "www.example.com"14    assert url.port is None15    assert url.path == "/"16    assert url.query is None17    assert url.fragment is None1819    assert str(url) == "https://www.example.com/"202122def test_urlparse_no_scheme():23    url = urlparse("://example.com")24    assert url.scheme == ""25    assert url.host == "example.com"26    assert url.path == ""272829def test_urlparse_no_authority():30    url = urlparse("http://")31    assert url.scheme == "http"32    assert url.host == ""33    assert url.path == ""343536# Tests for different host types373839def test_urlparse_valid_host():40    url = urlparse("https://example.com/")41    assert url.host == "example.com"424344def test_urlparse_normalized_host():45    url = urlparse("https://EXAMPLE.com/")46    assert url.host == "example.com"474849def test_urlparse_valid_ipv4():50    url = urlparse("https://1.2.3.4/")51    assert url.host == "1.2.3.4"525354def test_urlparse_invalid_ipv4():55    with pytest.raises(httpx.InvalidURL) as exc:56        urlparse("https://999.999.999.999/")57    assert str(exc.value) == "Invalid IPv4 address"585960def test_urlparse_valid_ipv6():61    url = urlparse("https://[2001:db8::ff00:42:8329]/")62    assert url.host == "2001:db8::ff00:42:8329"636465def test_urlparse_invalid_ipv6():66    with pytest.raises(httpx.InvalidURL) as exc:67        urlparse("https://[2001]/")68    assert str(exc.value) == "Invalid IPv6 address"697071def test_urlparse_unescaped_idna_host():72    url = urlparse("https://中国.icom.museum/")73    assert url.host == "xn--fiqs8s.icom.museum"747576def test_urlparse_escaped_idna_host():77    url = urlparse("https://xn--fiqs8s.icom.museum/")78    assert url.host == "xn--fiqs8s.icom.museum"798081def test_urlparse_invalid_idna_host():82    with pytest.raises(httpx.InvalidURL) as exc:83        urlparse("https://☃.com/")84    assert str(exc.value) == "Invalid IDNA hostname"858687# Tests for different port types888990def test_urlparse_valid_port():91    url = urlparse("https://example.com:123/")92    assert url.port == 123939495def test_urlparse_normalized_port():96    # If the port matches the scheme default it is normalized to None.97    url = urlparse("https://example.com:443/")98    assert url.port is None99100101def test_urlparse_invalid_port():102    with pytest.raises(httpx.InvalidURL) as exc:103        urlparse("https://example.com:abc/")104    assert str(exc.value) == "Invalid port"105106107# Tests for path handling108109110def test_urlparse_normalized_path():111    url = urlparse("https://example.com/abc/def/../ghi/./jkl")112    assert url.path == "/abc/ghi/jkl"113114115def test_urlparse_escaped_path():116    url = urlparse("https://example.com/ /🌟/")117    assert url.path == "/%20/%F0%9F%8C%9F/"118119120def test_urlparse_leading_dot_prefix_on_absolute_url():121    url = urlparse("https://example.com/../abc")122    assert url.path == "/abc"123124125def test_urlparse_leading_dot_prefix_on_relative_url():126    url = urlparse("../abc")127    assert url.path == "../abc"128129130# Tests for invalid URLs131132133def test_urlparse_excessively_long_url():134    with pytest.raises(httpx.InvalidURL) as exc:135        urlparse("https://www.example.com/" + "x" * 100_000)136    assert str(exc.value) == "URL too long"137138139def test_urlparse_excessively_long_component():140    with pytest.raises(httpx.InvalidURL) as exc:141        urlparse("https://www.example.com", path="/" + "x" * 100_000)142    assert str(exc.value) == "URL component 'path' too long"143144145def test_urlparse_non_printing_character_in_url():146    with pytest.raises(httpx.InvalidURL) as exc:147        urlparse("https://www.example.com/\n")148    assert str(exc.value) == "Invalid non-printable ASCII character in URL"149150151def test_urlparse_non_printing_character_in_component():152    with pytest.raises(httpx.InvalidURL) as exc:153        urlparse("https://www.example.com", path="/\n")154    assert (155        str(exc.value)156        == "Invalid non-printable ASCII character in URL component 'path'"157    )158159160# Test for urlparse components161162163def test_urlparse_with_components():164    url = urlparse(scheme="https", host="www.example.com", path="/")165166    assert url.scheme == "https"167    assert url.userinfo == ""168    assert url.host == "www.example.com"169    assert url.port is None170    assert url.path == "/"171    assert url.query is None172    assert url.fragment is None173174    assert str(url) == "https://www.example.com/"175176177def test_urlparse_with_invalid_component():178    with pytest.raises(TypeError) as exc:179        urlparse(scheme="https", host="www.example.com", incorrect="/")180    assert str(exc.value) == "'incorrect' is an invalid keyword argument for urlparse()"181182183def test_urlparse_with_invalid_scheme():184    with pytest.raises(httpx.InvalidURL) as exc:185        urlparse(scheme="~", host="www.example.com", path="/")186    assert str(exc.value) == "Invalid URL component 'scheme'"187188189def test_urlparse_with_invalid_path():190    with pytest.raises(httpx.InvalidURL) as exc:191        urlparse(scheme="https", host="www.example.com", path="abc")192    assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"193194    with pytest.raises(httpx.InvalidURL) as exc:195        urlparse(path="//abc")196    assert (197        str(exc.value)198        == "URLs with no authority component cannot have a path starting with '//'"199    )200201    with pytest.raises(httpx.InvalidURL) as exc:202        urlparse(path=":abc")203    assert (204        str(exc.value)205        == "URLs with no scheme component cannot have a path starting with ':'"206    )207208209def test_urlparse_with_relative_path():210    # This path would be invalid for an absolute URL, but is valid as a relative URL.211    url = urlparse(path="abc")212    assert url.path == "abc"213214215# Tests for accessing and modifying `urlparse` results.216217218def test_copy_with():219    url = urlparse("https://www.example.com/")220    assert str(url) == "https://www.example.com/"221222    url = url.copy_with()223    assert str(url) == "https://www.example.com/"224225    url = url.copy_with(scheme="http")226    assert str(url) == "http://www.example.com/"227228    url = url.copy_with(netloc="example.com")229    assert str(url) == "http://example.com/"230231    url = url.copy_with(path="/abc")232    assert str(url) == "http://example.com/abc"