Drop private imports from test_urlparse.py (#2572)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Drop private imports from test_urlparse.py * Coverage * Drop ._uri_reference
authored by Florimond Manca · committed by GitHub · parent f0fd919 · Browse files · 2 files·+45 added−58 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+45 added−58 removed
- tests:+45 added−46 removed
- other:+0 added−12 removed
Provenance
- Commit
a934c36- Tree
9976f38- Author
Florimond Manca <florimond.manca@protonmail.com>- Committer
GitHub <noreply@github.com>- Parent
f0fd919
2 files changed+45 added−58 removed
modified:httpx/_urlparse.py-12 removed
193 unmodified lines194 # If any keyword arguments are provided, ensure they are valid.195 # -------------------------------------------------------------196 197 for key, value in kwargs.items():198- if key not in (199- "scheme",200- "authority",201- "path",202- "query",203- "fragment",204- "userinfo",205- "host",206- "port",207- ):208- raise TypeError(f"'{key}' is an invalid keyword argument for urlparse()")209-198 if value is not None:199 if len(value) > MAX_URL_LENGTH:200 raise InvalidURL(f"URL component '{key}' too long")201 238 unmodified linesmodified:tests/test_urlparse.py+45 added-46 removed
1 import pytest2 3 import httpx4-from httpx._urlparse import urlparse4 5 6 def test_urlparse():8- url = urlparse("https://www.example.com/")7+ url = httpx.URL("https://www.example.com/")8 9 assert url.scheme == "https"11- assert url.userinfo == ""12- assert url.netloc == "www.example.com"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 == "/"16- assert url.query is None17- assert url.fragment is None15+ assert url.query == b""16+ assert url.fragment == ""17 18 assert str(url) == "https://www.example.com/"19 20 21 def test_urlparse_no_scheme():23- url = urlparse("://example.com")22+ url = httpx.URL("://example.com")23 assert url.scheme == ""24 assert url.host == "example.com"26- assert url.path == ""25+ assert url.path == "/"26 27 28 def test_urlparse_no_authority():30- url = urlparse("http://")29+ url = httpx.URL("http://")30 assert url.scheme == "http"31 assert url.host == ""33- assert url.path == ""32+ assert url.path == "/"33 34 35 # Tests for different host types36 37 38 def test_urlparse_valid_host():40- url = urlparse("https://example.com/")39+ url = httpx.URL("https://example.com/")40 assert url.host == "example.com"41 42 43 def test_urlparse_normalized_host():45- url = urlparse("https://EXAMPLE.com/")44+ url = httpx.URL("https://EXAMPLE.com/")45 assert url.host == "example.com"46 47 48 def test_urlparse_valid_ipv4():50- url = urlparse("https://1.2.3.4/")49+ url = httpx.URL("https://1.2.3.4/")50 assert url.host == "1.2.3.4"51 52 53 def test_urlparse_invalid_ipv4():54 with pytest.raises(httpx.InvalidURL) as exc:56- urlparse("https://999.999.999.999/")55+ httpx.URL("https://999.999.999.999/")56 assert str(exc.value) == "Invalid IPv4 address"57 58 59 def test_urlparse_valid_ipv6():61- url = urlparse("https://[2001:db8::ff00:42:8329]/")60+ url = httpx.URL("https://[2001:db8::ff00:42:8329]/")61 assert url.host == "2001:db8::ff00:42:8329"62 63 64 def test_urlparse_invalid_ipv6():65 with pytest.raises(httpx.InvalidURL) as exc:67- urlparse("https://[2001]/")66+ httpx.URL("https://[2001]/")67 assert str(exc.value) == "Invalid IPv6 address"68 69 70 def test_urlparse_unescaped_idna_host():72- url = urlparse("https://中国.icom.museum/")73- assert url.host == "xn--fiqs8s.icom.museum"71+ url = httpx.URL("https://中国.icom.museum/")72+ assert url.raw_host == b"xn--fiqs8s.icom.museum"73 74 75 def test_urlparse_escaped_idna_host():77- url = urlparse("https://xn--fiqs8s.icom.museum/")78- assert url.host == "xn--fiqs8s.icom.museum"76+ url = httpx.URL("https://xn--fiqs8s.icom.museum/")77+ assert url.raw_host == b"xn--fiqs8s.icom.museum"78 79 80 def test_urlparse_invalid_idna_host():81 with pytest.raises(httpx.InvalidURL) as exc:83- urlparse("https://☃.com/")82+ httpx.URL("https://☃.com/")83 assert str(exc.value) == "Invalid IDNA hostname"84 85 86 # Tests for different port types87 88 89 def test_urlparse_valid_port():91- url = urlparse("https://example.com:123/")90+ url = httpx.URL("https://example.com:123/")91 assert url.port == 12392 93 94 def test_urlparse_normalized_port():95 # If the port matches the scheme default it is normalized to None.97- url = urlparse("https://example.com:443/")96+ url = httpx.URL("https://example.com:443/")97 assert url.port is None98 99 100 def test_urlparse_invalid_port():101 with pytest.raises(httpx.InvalidURL) as exc:103- urlparse("https://example.com:abc/")102+ httpx.URL("https://example.com:abc/")103 assert str(exc.value) == "Invalid port"104 105 106 # Tests for path handling107 108 109 def test_urlparse_normalized_path():111- url = urlparse("https://example.com/abc/def/../ghi/./jkl")110+ url = httpx.URL("https://example.com/abc/def/../ghi/./jkl")111 assert url.path == "/abc/ghi/jkl"112 113 114 def test_urlparse_escaped_path():116- url = urlparse("https://example.com/ /🌟/")117- assert url.path == "/%20/%F0%9F%8C%9F/"115+ url = httpx.URL("https://example.com/ /🌟/")116+ assert url.raw_path == b"/%20/%F0%9F%8C%9F/"117 118 119 def test_urlparse_leading_dot_prefix_on_absolute_url():121- url = urlparse("https://example.com/../abc")120+ url = httpx.URL("https://example.com/../abc")121 assert url.path == "/abc"122 123 124 def test_urlparse_leading_dot_prefix_on_relative_url():126- url = urlparse("../abc")125+ url = httpx.URL("../abc")126 assert url.path == "../abc"127 128 129 # Tests for invalid URLs130 131 132 def test_urlparse_excessively_long_url():133 with pytest.raises(httpx.InvalidURL) as exc:135- urlparse("https://www.example.com/" + "x" * 100_000)134+ httpx.URL("https://www.example.com/" + "x" * 100_000)135 assert str(exc.value) == "URL too long"136 137 138 def test_urlparse_excessively_long_component():139 with pytest.raises(httpx.InvalidURL) as exc:141- urlparse("https://www.example.com", path="/" + "x" * 100_000)140+ httpx.URL("https://www.example.com", path="/" + "x" * 100_000)141 assert str(exc.value) == "URL component 'path' too long"142 143 144 def test_urlparse_non_printing_character_in_url():145 with pytest.raises(httpx.InvalidURL) as exc:147- urlparse("https://www.example.com/\n")146+ httpx.URL("https://www.example.com/\n")147 assert str(exc.value) == "Invalid non-printable ASCII character in URL"148 149 150 def test_urlparse_non_printing_character_in_component():151 with pytest.raises(httpx.InvalidURL) as exc:153- urlparse("https://www.example.com", path="/\n")152+ httpx.URL("https://www.example.com", path="/\n")153 assert (154 str(exc.value)155 == "Invalid non-printable ASCII character in URL component 'path'"156 )2 unmodified lines159 # Test for urlparse components160 161 162 def test_urlparse_with_components():164- url = urlparse(scheme="https", host="www.example.com", path="/")163+ url = httpx.URL(scheme="https", host="www.example.com", path="/")164 165 assert url.scheme == "https"167- assert url.userinfo == ""166+ assert url.userinfo == b""167 assert url.host == "www.example.com"168 assert url.port is None169 assert url.path == "/"171- assert url.query is None172- assert url.fragment is None170+ assert url.query == b""171+ assert url.fragment == ""172 173 assert str(url) == "https://www.example.com/"174 175 176 def test_urlparse_with_invalid_component():177 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()"178+ httpx.URL(scheme="https", host="www.example.com", incorrect="/")179+ assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()"180 181 182 def test_urlparse_with_invalid_scheme():183 with pytest.raises(httpx.InvalidURL) as exc:185- urlparse(scheme="~", host="www.example.com", path="/")184+ httpx.URL(scheme="~", host="www.example.com", path="/")185 assert str(exc.value) == "Invalid URL component 'scheme'"186 187 188 def test_urlparse_with_invalid_path():189 with pytest.raises(httpx.InvalidURL) as exc:191- urlparse(scheme="https", host="www.example.com", path="abc")190+ httpx.URL(scheme="https", host="www.example.com", path="abc")191 assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'"192 193 with pytest.raises(httpx.InvalidURL) as exc:195- urlparse(path="//abc")194+ httpx.URL(path="//abc")195 assert (196 str(exc.value)197 == "URLs with no authority component cannot have a path starting with '//'"198 )199 200 with pytest.raises(httpx.InvalidURL) as exc:202- urlparse(path=":abc")201+ httpx.URL(path=":abc")202 assert (203 str(exc.value)204 == "URLs with no scheme component cannot have a path starting with ':'"205 )206 207 208 def test_urlparse_with_relative_path():209 # This path would be invalid for an absolute URL, but is valid as a relative URL.211- url = urlparse(path="abc")210+ url = httpx.URL(path="abc")211 assert url.path == "abc"212 213 214 # Tests for accessing and modifying `urlparse` results.215 216 217 def test_copy_with():219- url = urlparse("https://www.example.com/")218+ url = httpx.URL("https://www.example.com/")219 assert str(url) == "https://www.example.com/"220 221 url = url.copy_with()222 assert str(url) == "https://www.example.com/"223 224 url = url.copy_with(scheme="http")225 assert str(url) == "http://www.example.com/"226 228- url = url.copy_with(netloc="example.com")227+ url = url.copy_with(netloc=b"example.com")228 assert str(url) == "http://example.com/"229 230 url = url.copy_with(path="/abc")231 assert str(url) == "http://example.com/abc"