From a934c36a859cd5cf6eac19cd8b76d60aaac7109a Mon Sep 17 00:00:00 2001 From: Florimond Manca Date: Wed, 15 Feb 2023 15:43:19 +0100 Subject: [PATCH] Drop private imports from test_urlparse.py (#2572) * Drop private imports from test_urlparse.py * Coverage * Drop ._uri_reference --- httpx/_urlparse.py | 12 ------ tests/test_urlparse.py | 91 +++++++++++++++++++++--------------------- 2 files changed, 45 insertions(+), 58 deletions(-) diff --git a/httpx/_urlparse.py b/httpx/_urlparse.py index ac1c6271..0fbec358 100644 --- a/httpx/_urlparse.py +++ b/httpx/_urlparse.py @@ -195,18 +195,6 @@ def urlparse(url: str = "", **kwargs: typing.Optional[str]) -> ParseResult: # ------------------------------------------------------------- for key, value in kwargs.items(): - if key not in ( - "scheme", - "authority", - "path", - "query", - "fragment", - "userinfo", - "host", - "port", - ): - raise TypeError(f"'{key}' is an invalid keyword argument for urlparse()") - if value is not None: if len(value) > MAX_URL_LENGTH: raise InvalidURL(f"URL component '{key}' too long") diff --git a/tests/test_urlparse.py b/tests/test_urlparse.py index e48ffa64..ec1fd20d 100644 --- a/tests/test_urlparse.py +++ b/tests/test_urlparse.py @@ -1,86 +1,85 @@ import pytest import httpx -from httpx._urlparse import urlparse def test_urlparse(): - url = urlparse("https://www.example.com/") + url = httpx.URL("https://www.example.com/") assert url.scheme == "https" - assert url.userinfo == "" - assert url.netloc == "www.example.com" + assert url.userinfo == b"" + assert url.netloc == b"www.example.com" assert url.host == "www.example.com" assert url.port is None assert url.path == "/" - assert url.query is None - assert url.fragment is None + assert url.query == b"" + assert url.fragment == "" assert str(url) == "https://www.example.com/" def test_urlparse_no_scheme(): - url = urlparse("://example.com") + url = httpx.URL("://example.com") assert url.scheme == "" assert url.host == "example.com" - assert url.path == "" + assert url.path == "/" def test_urlparse_no_authority(): - url = urlparse("http://") + url = httpx.URL("http://") assert url.scheme == "http" assert url.host == "" - assert url.path == "" + assert url.path == "/" # Tests for different host types def test_urlparse_valid_host(): - url = urlparse("https://example.com/") + url = httpx.URL("https://example.com/") assert url.host == "example.com" def test_urlparse_normalized_host(): - url = urlparse("https://EXAMPLE.com/") + url = httpx.URL("https://EXAMPLE.com/") assert url.host == "example.com" def test_urlparse_valid_ipv4(): - url = urlparse("https://1.2.3.4/") + url = httpx.URL("https://1.2.3.4/") assert url.host == "1.2.3.4" def test_urlparse_invalid_ipv4(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://999.999.999.999/") + httpx.URL("https://999.999.999.999/") assert str(exc.value) == "Invalid IPv4 address" def test_urlparse_valid_ipv6(): - url = urlparse("https://[2001:db8::ff00:42:8329]/") + url = httpx.URL("https://[2001:db8::ff00:42:8329]/") assert url.host == "2001:db8::ff00:42:8329" def test_urlparse_invalid_ipv6(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://[2001]/") + httpx.URL("https://[2001]/") assert str(exc.value) == "Invalid IPv6 address" def test_urlparse_unescaped_idna_host(): - url = urlparse("https://中国.icom.museum/") - assert url.host == "xn--fiqs8s.icom.museum" + url = httpx.URL("https://中国.icom.museum/") + assert url.raw_host == b"xn--fiqs8s.icom.museum" def test_urlparse_escaped_idna_host(): - url = urlparse("https://xn--fiqs8s.icom.museum/") - assert url.host == "xn--fiqs8s.icom.museum" + url = httpx.URL("https://xn--fiqs8s.icom.museum/") + assert url.raw_host == b"xn--fiqs8s.icom.museum" def test_urlparse_invalid_idna_host(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://☃.com/") + httpx.URL("https://☃.com/") assert str(exc.value) == "Invalid IDNA hostname" @@ -88,19 +87,19 @@ def test_urlparse_invalid_idna_host(): def test_urlparse_valid_port(): - url = urlparse("https://example.com:123/") + url = httpx.URL("https://example.com:123/") assert url.port == 123 def test_urlparse_normalized_port(): # If the port matches the scheme default it is normalized to None. - url = urlparse("https://example.com:443/") + url = httpx.URL("https://example.com:443/") assert url.port is None def test_urlparse_invalid_port(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://example.com:abc/") + httpx.URL("https://example.com:abc/") assert str(exc.value) == "Invalid port" @@ -108,22 +107,22 @@ def test_urlparse_invalid_port(): def test_urlparse_normalized_path(): - url = urlparse("https://example.com/abc/def/../ghi/./jkl") + url = httpx.URL("https://example.com/abc/def/../ghi/./jkl") assert url.path == "/abc/ghi/jkl" def test_urlparse_escaped_path(): - url = urlparse("https://example.com/ /🌟/") - assert url.path == "/%20/%F0%9F%8C%9F/" + url = httpx.URL("https://example.com/ /🌟/") + assert url.raw_path == b"/%20/%F0%9F%8C%9F/" def test_urlparse_leading_dot_prefix_on_absolute_url(): - url = urlparse("https://example.com/../abc") + url = httpx.URL("https://example.com/../abc") assert url.path == "/abc" def test_urlparse_leading_dot_prefix_on_relative_url(): - url = urlparse("../abc") + url = httpx.URL("../abc") assert url.path == "../abc" @@ -132,25 +131,25 @@ def test_urlparse_leading_dot_prefix_on_relative_url(): def test_urlparse_excessively_long_url(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://www.example.com/" + "x" * 100_000) + httpx.URL("https://www.example.com/" + "x" * 100_000) assert str(exc.value) == "URL too long" def test_urlparse_excessively_long_component(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://www.example.com", path="/" + "x" * 100_000) + httpx.URL("https://www.example.com", path="/" + "x" * 100_000) assert str(exc.value) == "URL component 'path' too long" def test_urlparse_non_printing_character_in_url(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://www.example.com/\n") + httpx.URL("https://www.example.com/\n") assert str(exc.value) == "Invalid non-printable ASCII character in URL" def test_urlparse_non_printing_character_in_component(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse("https://www.example.com", path="/\n") + httpx.URL("https://www.example.com", path="/\n") assert ( str(exc.value) == "Invalid non-printable ASCII character in URL component 'path'" @@ -161,45 +160,45 @@ def test_urlparse_non_printing_character_in_component(): def test_urlparse_with_components(): - url = urlparse(scheme="https", host="www.example.com", path="/") + url = httpx.URL(scheme="https", host="www.example.com", path="/") assert url.scheme == "https" - assert url.userinfo == "" + assert url.userinfo == b"" assert url.host == "www.example.com" assert url.port is None assert url.path == "/" - assert url.query is None - assert url.fragment is None + assert url.query == b"" + assert url.fragment == "" assert str(url) == "https://www.example.com/" def test_urlparse_with_invalid_component(): with pytest.raises(TypeError) as exc: - urlparse(scheme="https", host="www.example.com", incorrect="/") - assert str(exc.value) == "'incorrect' is an invalid keyword argument for urlparse()" + httpx.URL(scheme="https", host="www.example.com", incorrect="/") + assert str(exc.value) == "'incorrect' is an invalid keyword argument for URL()" def test_urlparse_with_invalid_scheme(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse(scheme="~", host="www.example.com", path="/") + httpx.URL(scheme="~", host="www.example.com", path="/") assert str(exc.value) == "Invalid URL component 'scheme'" def test_urlparse_with_invalid_path(): with pytest.raises(httpx.InvalidURL) as exc: - urlparse(scheme="https", host="www.example.com", path="abc") + httpx.URL(scheme="https", host="www.example.com", path="abc") assert str(exc.value) == "For absolute URLs, path must be empty or begin with '/'" with pytest.raises(httpx.InvalidURL) as exc: - urlparse(path="//abc") + httpx.URL(path="//abc") assert ( str(exc.value) == "URLs with no authority component cannot have a path starting with '//'" ) with pytest.raises(httpx.InvalidURL) as exc: - urlparse(path=":abc") + httpx.URL(path=":abc") assert ( str(exc.value) == "URLs with no scheme component cannot have a path starting with ':'" @@ -208,7 +207,7 @@ def test_urlparse_with_invalid_path(): def test_urlparse_with_relative_path(): # This path would be invalid for an absolute URL, but is valid as a relative URL. - url = urlparse(path="abc") + url = httpx.URL(path="abc") assert url.path == "abc" @@ -216,7 +215,7 @@ def test_urlparse_with_relative_path(): def test_copy_with(): - url = urlparse("https://www.example.com/") + url = httpx.URL("https://www.example.com/") assert str(url) == "https://www.example.com/" url = url.copy_with() @@ -225,7 +224,7 @@ def test_copy_with(): url = url.copy_with(scheme="http") assert str(url) == "http://www.example.com/" - url = url.copy_with(netloc="example.com") + url = url.copy_with(netloc=b"example.com") assert str(url) == "http://example.com/" url = url.copy_with(path="/abc") -- 2.47.3