Skip to content
Myr

Moving test cases into 'test_url.py' (#2982)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Moving test cases into 'test_url.py' * Move test_url to test_basic_url * Linting * Move TypeError test case. Move basic httpx.URL cases. * Linting * Merge invalid URL cases * Move percent encoding test cases * Move remaining test cases * Linting * Add missing test cases

authored by Tom Christie · committed by GitHub · parent 724eced · Browse files · 2 files+325 added−313 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+325 added−313 removed

  • tests:+325 added−313 removed

2 files+325 added−313 removed

modified:tests/models/test_url.py+325 added-28 removed
1import pytest23import httpx45# Tests for `httpx.URL` instantiation and property accessors.676def test_url():8def test_basic_url():9 url = httpx.URL("https://www.example.com/")1011 assert url.scheme == "https"12 assert url.userinfo == b""13 assert url.netloc == b"www.example.com"14 assert url.host == "www.example.com"15 assert url.port is None16 assert url.path == "/"17 assert url.query == b""18 assert url.fragment == ""1920 assert str(url) == "https://www.example.com/"21 assert repr(url) == "URL('https://www.example.com/')"222324def test_complete_url():25 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")26 assert url.scheme == "https"27 assert url.host == "example.org"28 assert url.port == 12329 assert url.path == "/path/to/somewhere"30 assert url.query == b"abc=123"31 assert url.raw_path == b"/path/to/somewhere?abc=123"32 assert url.fragment == "anchor"3334 assert str(url) == "https://example.org:123/path/to/somewhere?abc=123#anchor"35 assert (36 repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"37 )3819 new = url.copy_with(scheme="http", port=None)20 assert new == httpx.URL("http://example.org/path/to/somewhere?abc=123#anchor")21 assert new.scheme == "http"392340def test_url_with_empty_query():41 """42 URLs with and without a trailing `?` but an empty query component43 should preserve the information on the raw path.
8 unmodified lines
52 assert url.query == b""53 assert url.raw_path == b"/path?"545556def test_url_no_scheme():57 url = httpx.URL("://example.com")58 assert url.scheme == ""59 assert url.host == "example.com"60 assert url.path == "/"616263def test_url_no_authority():64 url = httpx.URL("http://")65 assert url.scheme == "http"66 assert url.host == ""67 assert url.path == "/"686970# Tests for percent encoding across path, query, and fragment...717273def test_path_percent_encoding():74 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS75 url = httpx.URL("https://example.com/!$&'()*+,;= abc ABC 123 :/[]@")76 assert url.raw_path == b"/!$&'()*+,;=%20abc%20ABC%20123%20:/[]@"77 assert url.path == "/!$&'()*+,;= abc ABC 123 :/[]@"78 assert url.query == b""79 assert url.fragment == ""808182def test_query_percent_encoding():83 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS84 url = httpx.URL("https://example.com/?!$&'()*+,;= abc ABC 123 :/[]@" + "?")85 assert url.raw_path == b"/?!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"86 assert url.path == "/"87 assert url.query == b"!$&'()*+,;=%20abc%20ABC%20123%20:%2F[]@?"88 assert url.fragment == ""899091def test_fragment_percent_encoding():92 # Test percent encoding for SUB_DELIMS ALPHA NUM and allowable GEN_DELIMS93 url = httpx.URL("https://example.com/#!$&'()*+,;= abc ABC 123 :/[]@" + "?#")94 assert url.raw_path == b"/"95 assert url.path == "/"96 assert url.query == b""97 assert url.fragment == "!$&'()*+,;= abc ABC 123 :/[]@?#"9899100def test_url_query_encoding():101 """102 URL query parameters should use '%20' to encoding spaces,103 and should treat '/' as a safe character. This behaviour differs
15 unmodified lines
119 assert url.query == b""120 assert url.raw_path == b"/path%20to%20somewhere"12112263def test_url_raw_compatibility():64 url = httpx.URL("https://www.example.com/path")65 scheme, host, port, raw_path = url.raw123def test_url_params():124 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})125 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"126 assert url.params == httpx.QueryParams({"a": "123"})12767 assert scheme == b"https"68 assert host == b"www.example.com"69 assert port is None70 assert raw_path == b"/path"128 url = httpx.URL(129 "https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}130 )131 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"132 assert url.params == httpx.QueryParams({"a": "123"})133134135# Tests for different host types136137138def test_url_valid_host():139 url = httpx.URL("https://example.com/")140 assert url.host == "example.com"141142143def test_url_normalized_host():144 url = httpx.URL("https://EXAMPLE.com/")145 assert url.host == "example.com"146147148def test_url_ipv4_like_host():149 """rare host names used to quality as IPv4"""150 url = httpx.URL("https://023b76x43144/")151 assert url.host == "023b76x43144"152153154# Tests for different port types155156157def test_url_valid_port():158 url = httpx.URL("https://example.com:123/")159 assert url.port == 123160161162def test_url_normalized_port():163 # If the port matches the scheme default it is normalized to None.164 url = httpx.URL("https://example.com:443/")165 assert url.port is None16616773def test_url_invalid():168def test_url_invalid_port():169 with pytest.raises(httpx.InvalidURL) as exc:170 httpx.URL("https://example.com:abc/")171 assert str(exc.value) == "Invalid port: 'abc'"172173174# Tests for path handling175176177def test_url_normalized_path():178 url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")179 assert url.path == "/abc/ghi/jkl"180181182def test_url_escaped_path():183 url = httpx.URL("https://example.com/ /🌟/")184 assert url.raw_path == b"/%20/%F0%9F%8C%9F/"185186187def test_url_leading_dot_prefix_on_absolute_url():188 url = httpx.URL("https://example.com/../abc")189 assert url.path == "/abc"190191192def test_url_leading_dot_prefix_on_relative_url():193 url = httpx.URL("../abc")194 assert url.path == "../abc"195196197# Tests for optional percent encoding198199200def test_param_requires_encoding():201 url = httpx.URL("http://webservice", params={"u": "with spaces"})202 assert str(url) == "http://webservice?u=with%20spaces"203204205def test_param_does_not_require_encoding():206 url = httpx.URL("http://webservice", params={"u": "with%20spaces"})207 assert str(url) == "http://webservice?u=with%20spaces"208209210def test_param_with_existing_escape_requires_encoding():211 url = httpx.URL("http://webservice", params={"u": "http://example.com?q=foo%2Fa"})212 assert str(url) == "http://webservice?u=http%3A%2F%2Fexample.com%3Fq%3Dfoo%252Fa"213214215# Tests for invalid URLs216217218def test_url_invalid_hostname():219 """220 Ensure that invalid URLs raise an `httpx.InvalidURL` exception.221 """222 with pytest.raises(httpx.InvalidURL):223 httpx.URL("https://😇/")224225226def test_url_excessively_long_url():227 with pytest.raises(httpx.InvalidURL) as exc:228 httpx.URL("https://www.example.com/" + "x" * 100_000)229 assert str(exc.value) == "URL too long"230231232def test_url_excessively_long_component():233 with pytest.raises(httpx.InvalidURL) as exc:234 httpx.URL("https://www.example.com", path="/" + "x" * 100_000)235 assert str(exc.value) == "URL component 'path' too long"236237238def test_url_non_printing_character_in_url():239 with pytest.raises(httpx.InvalidURL) as exc:240 httpx.URL("https://www.example.com/\n")241 assert str(exc.value) == "Invalid non-printable ASCII character in URL"242243244def test_url_non_printing_character_in_component():245 with pytest.raises(httpx.InvalidURL) as exc:246 httpx.URL("https://www.example.com", path="/\n")247 assert (248 str(exc.value)249 == "Invalid non-printable ASCII character in URL component 'path'"250 )251252253# Test for url components254255256def test_url_with_components():257 url = httpx.URL(scheme="https", host="www.example.com", path="/")258259 assert url.scheme == "https"260 assert url.userinfo == b""261 assert url.host == "www.example.com"262 assert url.port is None263 assert url.path == "/"264 assert url.query == b""265 assert url.fragment == ""266267 assert str(url) == "https://www.example.com/"268269270def test_urlparse_with_invalid_component():271 with pytest.raises(TypeError) as exc:272 httpx.URL(scheme="https", host="www.example.com", incorrect="/")273 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"274275276def test_urlparse_with_invalid_scheme():277 with pytest.raises(httpx.InvalidURL) as exc:278 httpx.URL(scheme="~", host="www.example.com", path="/")279 assert str(exc.value) == "Invalid URL component 'scheme'"280281282def test_urlparse_with_invalid_path():283 with pytest.raises(httpx.InvalidURL) as exc:284 httpx.URL(scheme="https", host="www.example.com", path="abc")285 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"286287 with pytest.raises(httpx.InvalidURL) as exc:288 httpx.URL(path="//abc")289 assert (290 str(exc.value)291 == "URLs with no authority component cannot have a path starting with '//'"292 )293294 with pytest.raises(httpx.InvalidURL) as exc:295 httpx.URL(path=":abc")296 assert (297 str(exc.value)298 == "URLs with no scheme component cannot have a path starting with ':'"299 )300301302def test_url_with_relative_path():303 # This path would be invalid for an absolute URL, but is valid as a relative URL.304 url = httpx.URL(path="abc")305 assert url.path == "abc"306307308# Tests for `httpx.URL` python built-in operators.309310311def test_url_eq_str():312 """83 Ensure that `httpx.URL` supports the equality operator,84 and can be compared against plain strings.313 Ensure that `httpx.URL` supports the equality operator.314 """315 url = httpx.URL("https://example.org:123/path/to/somewhere?abc=123#anchor")316 assert url == "https://example.org:123/path/to/somewhere?abc=123#anchor"317 assert str(url) == url
12 unmodified lines
330331 assert all(url in urls for url in url_set)332333334# Tests for TypeErrors when instantiating `httpx.URL`.335336337def test_url_invalid_type():338 """339 Ensure that invalid types on `httpx.URL()` raise a `TypeError`.340 """
4 unmodified lines
345 with pytest.raises(TypeError):346 httpx.URL(ExternalURLClass()) # type: ignore347348117# Tests for `QueryParams`.349def test_url_with_invalid_component():350 with pytest.raises(TypeError) as exc:351 httpx.URL(scheme="https", host="www.example.com", incorrect="/")352 assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"353354120def test_url_params():121 url = httpx.URL("https://example.org:123/path/to/somewhere", params={"a": "123"})122 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"123 assert url.params == httpx.QueryParams({"a": "123"})124125 url = httpx.URL(126 "https://example.org:123/path/to/somewhere?b=456", params={"a": "123"}127 )128 assert str(url) == "https://example.org:123/path/to/somewhere?a=123"129 assert url.params == httpx.QueryParams({"a": "123"})130131355# Tests for `URL.join()`.356357358def test_url_join():
84 unmodified lines
443444# Tests for `URL.copy_with()`.445446447def test_copy_with():448 url = httpx.URL("https://www.example.com/")449 assert str(url) == "https://www.example.com/"450451 url = url.copy_with()452 assert str(url) == "https://www.example.com/"453454 url = url.copy_with(scheme="http")455 assert str(url) == "http://www.example.com/"456457 url = url.copy_with(netloc=b"example.com")458 assert str(url) == "http://example.com/"459460 url = url.copy_with(path="/abc")461 assert str(url) == "http://example.com/abc"462463464def test_url_copywith_authority_subcomponents():465 copy_with_kwargs = {466 "username": "username",467 "password": "password",
196 unmodified lines
664 assert url.scheme == scheme665 assert url.port == port666667668def test_url_unescaped_idna_host():669 url = httpx.URL("https://中国.icom.museum/")670 assert url.raw_host == b"xn--fiqs8s.icom.museum"671672673def test_url_escaped_idna_host():674 url = httpx.URL("https://xn--fiqs8s.icom.museum/")675 assert url.raw_host == b"xn--fiqs8s.icom.museum"676677678def test_url_invalid_idna_host():679 with pytest.raises(httpx.InvalidURL) as exc:680 httpx.URL("https://☃.com/")681 assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"682683684# Tests for IPv4 hostname support.685686687def test_url_valid_ipv4():688 url = httpx.URL("https://1.2.3.4/")689 assert url.host == "1.2.3.4"690691692def test_url_invalid_ipv4():693 with pytest.raises(httpx.InvalidURL) as exc:694 httpx.URL("https://999.999.999.999/")695 assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"696697698# Tests for IPv6 hostname support.699700701def test_ipv6_url():
2 unmodified lines
704 assert url.host == "::ffff:192.168.0.1"705 assert url.netloc == b"[::ffff:192.168.0.1]:5678"706707708def test_url_valid_ipv6():709 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")710 assert url.host == "2001:db8::ff00:42:8329"711712713def test_url_invalid_ipv6():714 with pytest.raises(httpx.InvalidURL) as exc:715 httpx.URL("https://[2001]/")716 assert str(exc.value) == "Invalid IPv6 address: '[2001]'"717718719@pytest.mark.parametrize("host", ["[::ffff:192.168.0.1]", "::ffff:192.168.0.1"])720def test_ipv6_url_from_raw_url(host):721 url = httpx.URL(scheme="https", host=host, port=443, path="/")722
16 unmodified lines
739740 assert url.host == "::ffff:192.168.0.1"741 assert url.netloc == b"[::ffff:192.168.0.1]:1234"742 assert str(url) == "http://[::ffff:192.168.0.1]:1234"743744745# Test for deprecated API746747748def test_url_raw_compatibility():749 """750 Test case for the (to-be-deprecated) `url.raw` accessor.751 """752 url = httpx.URL("https://www.example.com/path")753 scheme, host, port, raw_path = url.raw754755 assert scheme == b"https"756 assert host == b"www.example.com"757 assert port is None758 assert raw_path == b"/path"
deleted:tests/test_urlparse.py-285 removed
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 :/[]@?#"