test_urlparse.py
This file
Path
tests/test_urlparse.py
285 lines · 8.2 KiB · Python
Last changed by
Fix encode host (#2886)robinalybefore adoptione63b659
History of this fileTake it
1import pytest23import httpx456def test_urlparse():7 url = httpx.URL("https://www.example.com/")89 assert url.scheme == "https"10 assert url.userinfo == b""11 assert url.netloc == b"www.example.com"12 assert url.host == "www.example.com"13 assert url.port is None14 assert url.path == "/"15 assert url.query == b""16 assert url.fragment == ""1718 assert str(url) == "https://www.example.com/"192021def test_urlparse_no_scheme():22 url = httpx.URL("://example.com")23 assert url.scheme == ""24 assert url.host == "example.com"25 assert url.path == "/"262728def test_urlparse_no_authority():29 url = httpx.URL("http://")30 assert url.scheme == "http"31 assert url.host == ""32 assert url.path == "/"333435# Tests for different host types363738def test_urlparse_valid_host():39 url = httpx.URL("https://example.com/")40 assert url.host == "example.com"414243def test_urlparse_normalized_host():44 url = httpx.URL("https://EXAMPLE.com/")45 assert url.host == "example.com"464748def test_urlparse_ipv4_like_host():49 """rare host names used to quality as IPv4"""50 url = httpx.URL("https://023b76x43144/")51 assert url.host == "023b76x43144"525354def test_urlparse_valid_ipv4():55 url = httpx.URL("https://1.2.3.4/")56 assert url.host == "1.2.3.4"575859def test_urlparse_invalid_ipv4():60 with pytest.raises(httpx.InvalidURL) as exc:61 httpx.URL("https://999.999.999.999/")62 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"636465def test_urlparse_valid_ipv6():66 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")67 assert url.host == "2001:db8::ff00:42:8329"686970def test_urlparse_invalid_ipv6():71 with pytest.raises(httpx.InvalidURL) as exc:72 httpx.URL("https://[2001]/")73 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"747576def test_urlparse_unescaped_idna_host():77 url = httpx.URL("https://中国.icom.museum/")78 assert url.raw_host == b"xn--fiqs8s.icom.museum"798081def test_urlparse_escaped_idna_host():82 url = httpx.URL("https://xn--fiqs8s.icom.museum/")83 assert url.raw_host == b"xn--fiqs8s.icom.museum"848586def test_urlparse_invalid_idna_host():87 with pytest.raises(httpx.InvalidURL) as exc:88 httpx.URL("https://☃.com/")89 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"909192# Tests for different port types939495def test_urlparse_valid_port():96 url = httpx.URL("https://example.com:123/")97 assert url.port == 1239899100def test_urlparse_normalized_port():101 # If the port matches the scheme default it is normalized to None.102 url = httpx.URL("https://example.com:443/")103 assert url.port is None104105106def test_urlparse_invalid_port():107 with pytest.raises(httpx.InvalidURL) as exc:108 httpx.URL("https://example.com:abc/")109 assert str(exc.value) == "Invalid port: 'abc'"110111112# Tests for path handling113114115def test_urlparse_normalized_path():116 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")117 assert url.path == "/abc/ghi/jkl"118119120def test_urlparse_escaped_path():121 url = httpx.URL("https://example.com/ /🌟/")122 assert url.raw_path == b"/%20/%F0%9F%8C%9F/"123124125def test_urlparse_leading_dot_prefix_on_absolute_url():126 url = httpx.URL("https://example.com/../abc")127 assert url.path == "/abc"128129130def test_urlparse_leading_dot_prefix_on_relative_url():131 url = httpx.URL("../abc")132 assert url.path == "../abc"133134135# Tests for optional percent encoding136137138def test_param_requires_encoding():139 url = httpx.URL("http://webservice", params={"u": "with spaces"})140 assert str(url) == "http://webservice?u=with%20spaces"141142143def test_param_does_not_require_encoding():144 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})145 assert str(url) == "http://webservice?u=with%20spaces"146147148def test_param_with_existing_escape_requires_encoding():149 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})150 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"151152153# Tests for invalid URLs154155156def test_urlparse_excessively_long_url():157 with pytest.raises(httpx.InvalidURL) as exc:158 httpx.URL("https://www.example.com/" + "x" * 100_000)159 assert str(exc.value) == "URL too long"160161162def test_urlparse_excessively_long_component():163 with pytest.raises(httpx.InvalidURL) as exc:164 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)165 assert str(exc.value) == "URL component 'path' too long"166167168def test_urlparse_non_printing_character_in_url():169 with pytest.raises(httpx.InvalidURL) as exc:170 httpx.URL("https://www.example.com/\n")171 assert str(exc.value) == "Invalid non-printable ASCII character in URL"172173174def test_urlparse_non_printing_character_in_component():175 with pytest.raises(httpx.InvalidURL) as exc:176 httpx.URL("https://www.example.com", path="/\n")177 assert (178 str(exc.value)179 == "Invalid non-printable ASCII character in URL component 'path'"180 )181182183# Test for urlparse components184185186def test_urlparse_with_components():187 url = httpx.URL(scheme="https", host="www.example.com", path="/")188189 assert url.scheme == "https"190 assert url.userinfo == b""191 assert url.host == "www.example.com"192 assert url.port is None193 assert url.path == "/"194 assert url.query == b""195 assert url.fragment == ""196197 assert str(url) == "https://www.example.com/"198199200def test_urlparse_with_invalid_component():201 with pytest.raises(TypeError) as exc:202 httpx.URL(scheme="https", host="www.example.com", incorrect="/")203 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"204205206def test_urlparse_with_invalid_scheme():207 with pytest.raises(httpx.InvalidURL) as exc:208 httpx.URL(scheme="~", host="www.example.com", path="/")209 assert str(exc.value) == "Invalid URL component 'scheme'"210211212def test_urlparse_with_invalid_path():213 with pytest.raises(httpx.InvalidURL) as exc:214 httpx.URL(scheme="https", host="www.example.com", path="abc")215 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"216217 with pytest.raises(httpx.InvalidURL) as exc:218 httpx.URL(path="//abc")219 assert (220 str(exc.value)221 == "URLs with no authority component cannot have a path starting with '//'"222 )223224 with pytest.raises(httpx.InvalidURL) as exc:225 httpx.URL(path=":abc")226 assert (227 str(exc.value)228 == "URLs with no scheme component cannot have a path starting with ':'"229 )230231232def test_urlparse_with_relative_path():233 # This path would be invalid for an absolute URL, but is valid as a relative URL.234 url = httpx.URL(path="abc")235 assert url.path == "abc"236237238# Tests for accessing and modifying `urlparse` results.239240241def test_copy_with():242 url = httpx.URL("https://www.example.com/")243 assert str(url) == "https://www.example.com/"244245 url = url.copy_with()246 assert str(url) == "https://www.example.com/"247248 url = url.copy_with(scheme="http")249 assert str(url) == "http://www.example.com/"250251 url = url.copy_with(netloc=b"example.com")252 assert str(url) == "http://example.com/"253254 url = url.copy_with(path="/abc")255 assert str(url) == "http://example.com/abc"256257258# Tests for percent encoding across path, query, and fragement...259260261def test_path_percent_encoding():262 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS263 url = httpx.URL("https://example.com/!$&'()*+,;= abc ABC 123 :/[]@")264 assert url.raw_path == b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@"265 assert url.path == "/!$&'()*+,;= abc ABC 123 :/[]@"266 assert url.query == b""267 assert url.fragment == ""268269270def test_query_percent_encoding():271 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS272 url = httpx.URL("https://example.com/?!$&'()*+,;= abc ABC 123 :/[]@" + "?")273 assert url.raw_path == b"/?!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"274 assert url.path == "/"275 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"276 assert url.fragment == ""277278279def test_fragment_percent_encoding():280 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS281 url = httpx.URL("https://example.com/#!$&'()*+,;= abc ABC 123 :/[]@" + "?#")282 assert url.raw_path == b"/"283 assert url.path == "/"284 assert url.query == b""285 assert url.fragment == "!$&'()*+,;= abc ABC 123 :/[]@?#"