summaryrefslogtreecommitdiffstats
path: root/contrib/python/yarl/tests
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/python/yarl/tests')
-rw-r--r--contrib/python/yarl/tests/test_cache.py12
-rw-r--r--contrib/python/yarl/tests/test_url.py29
-rw-r--r--contrib/python/yarl/tests/test_url_build.py62
-rw-r--r--contrib/python/yarl/tests/test_url_parsing.py25
-rw-r--r--contrib/python/yarl/tests/test_url_update_netloc.py20
5 files changed, 137 insertions, 11 deletions
diff --git a/contrib/python/yarl/tests/test_cache.py b/contrib/python/yarl/tests/test_cache.py
index 46d5b01f159..9b0e75a3551 100644
--- a/contrib/python/yarl/tests/test_cache.py
+++ b/contrib/python/yarl/tests/test_cache.py
@@ -13,7 +13,7 @@ def test_cache_clear() -> None:
def test_cache_info() -> None:
info = yarl.cache_info()
- assert info.keys() == {"idna_encode", "idna_decode", "ip_address"}
+ assert info.keys() == {"idna_encode", "idna_decode", "ip_address", "host_validate"}
def test_cache_configure_default() -> None:
@@ -22,11 +22,17 @@ def test_cache_configure_default() -> None:
def test_cache_configure_None() -> None:
yarl.cache_configure(
- idna_encode_size=None, idna_decode_size=None, ip_address_size=None
+ idna_encode_size=None,
+ idna_decode_size=None,
+ ip_address_size=None,
+ host_validate_size=None,
)
def test_cache_configure_explicit() -> None:
yarl.cache_configure(
- idna_encode_size=128, idna_decode_size=128, ip_address_size=128
+ idna_encode_size=128,
+ idna_decode_size=128,
+ ip_address_size=128,
+ host_validate_size=128,
)
diff --git a/contrib/python/yarl/tests/test_url.py b/contrib/python/yarl/tests/test_url.py
index 1dd98197fe1..44018c73bce 100644
--- a/contrib/python/yarl/tests/test_url.py
+++ b/contrib/python/yarl/tests/test_url.py
@@ -176,6 +176,24 @@ def test_raw_host():
assert url.raw_host == url._val.hostname
+ ("host"),
+ [
+ ("example.com"),
+ ("[::1]"),
+ ("xn--gnter-4ya.com"),
+ ],
+)
+def test_host_subcomponent(host: str):
+ url = URL(f"http://{host}")
+ assert url.host_subcomponent == host
+
+
+def test_host_subcomponent_return_idna_encoded_host():
+ url = URL("http://оун-упа.укр")
+ assert url.host_subcomponent == "xn----8sb1bdhvc.xn--j1amh"
+
+
def test_raw_host_non_ascii():
url = URL("http://оун-упа.укр")
assert "xn----8sb1bdhvc.xn--j1amh" == url.raw_host
@@ -2041,3 +2059,14 @@ def test_parsing_populates_cache():
assert url._cache["raw_query_string"] == "a=b"
assert url._cache["raw_fragment"] == "frag"
assert url._cache["scheme"] == "http"
+
+
+ ("host", "is_authority"),
+ [
+ *(("other_gen_delim_" + c, False) for c in "[]"),
+ ],
+)
+def test_build_with_invalid_ipv6_host(host: str, is_authority: bool):
+ with pytest.raises(ValueError, match="Invalid IPv6 URL"):
+ URL(f"http://{host}/")
diff --git a/contrib/python/yarl/tests/test_url_build.py b/contrib/python/yarl/tests/test_url_build.py
index a0ab77b2d4e..cc9b15f7e4f 100644
--- a/contrib/python/yarl/tests/test_url_build.py
+++ b/contrib/python/yarl/tests/test_url_build.py
@@ -15,22 +15,24 @@ def test_build_simple():
assert str(u) == "http://127.0.0.1"
def test_url_build_ipv6():
u = URL.build(scheme="http", host="::1")
- assert str(u) == "http://::1"
+ assert str(u) == "http://[::1]"
-def test_url_build_ipv6_brackets():
- u = URL.build(scheme="http", host="[::1]")
- assert str(u) == "http://::1"
+def test_url_build_ipv6_brackets_encoded():
+ u = URL.build(scheme="http", host="[::1]", encoded=True)
+ assert str(u) == "http://[::1]"
+
+
+def test_url_build_ipv6_brackets_not_encoded():
+ u = URL.build(scheme="http", host="::1", encoded=False)
+ assert str(u) == "http://[::1]"
def test_url_ipv4_in_ipv6():
u = URL.build(scheme="http", host="2001:db8:122:344::192.0.2.33")
- assert str(u) == "http://2001:db8:122:344::c000:221"
+ assert str(u) == "http://[2001:db8:122:344::c000:221]"
def test_build_with_scheme():
@@ -118,6 +120,25 @@ def test_build_with_authority_and_host():
URL.build(authority="host.com", host="example.com")
+ ("host", "is_authority"),
+ [
+ ("user:[email protected]", True),
+ ("[email protected]", True),
+ ("host:com", False),
+ ("not_percent_encoded%Zf", False),
+ ("still_not_percent_encoded%fZ", False),
+ *(("other_gen_delim_" + c, False) for c in "/?#[]"),
+ ],
+)
+def test_build_with_invalid_host(host: str, is_authority: bool):
+ match = r"Host '[^']+' cannot contain '[^']+' \(at position \d+\)"
+ if is_authority:
+ match += ", if .* use 'authority' instead of 'host'"
+ with pytest.raises(ValueError, match=f"{match}$"):
+ URL.build(host=host)
+
+
def test_build_with_authority():
url = URL.build(scheme="http", authority="степан:[email protected]:8000", path="path")
assert (
@@ -132,6 +153,31 @@ def test_build_with_authority_without_encoding():
assert str(url) == "http://foo:[email protected]:8000/path"
+def test_build_with_authority_empty_host_no_scheme():
+ url = URL.build(authority="", path="path")
+ assert str(url) == "path"
+
+
+def test_build_with_authority_and_only_user():
+ url = URL.build(scheme="https", authority="user:@foo.com", path="/path")
+ assert str(url) == "https://user:@foo.com/path"
+
+
+def test_build_with_authority_with_port():
+ url = URL.build(scheme="https", authority="foo.com:8080", path="/path")
+ assert str(url) == "https://foo.com:8080/path"
+
+
+def test_build_with_authority_with_ipv6():
+ url = URL.build(scheme="https", authority="[::1]", path="/path")
+ assert str(url) == "https://[::1]/path"
+
+
+def test_build_with_authority_with_ipv6_and_port():
+ url = URL.build(scheme="https", authority="[::1]:81", path="/path")
+ assert str(url) == "https://[::1]:81/path"
+
+
def test_query_str():
u = URL.build(scheme="http", host="127.0.0.1", path="/", query_string="arg=value1")
assert str(u) == "http://127.0.0.1/?arg=value1"
diff --git a/contrib/python/yarl/tests/test_url_parsing.py b/contrib/python/yarl/tests/test_url_parsing.py
index 4fe95185e71..0d58c9d09bb 100644
--- a/contrib/python/yarl/tests/test_url_parsing.py
+++ b/contrib/python/yarl/tests/test_url_parsing.py
@@ -604,3 +604,28 @@ def test_schemes_that_require_host(scheme: str) -> None:
)
with pytest.raises(ValueError, match=expect):
URL(f"{scheme}://:1")
+
+
+ ("url", "hostname", "hostname_without_brackets"),
+ [
+ ("http://[::1]", "[::1]", "::1"),
+ ("http://[::1]:8080", "[::1]", "::1"),
+ ("http://127.0.0.1:8080", "127.0.0.1", "127.0.0.1"),
+ (
+ "http://xn--jxagkqfkduily1i.eu",
+ "xn--jxagkqfkduily1i.eu",
+ "xn--jxagkqfkduily1i.eu",
+ ),
+ ],
+)
+def test_url_round_trips(
+ url: str, hostname: str, hostname_without_brackets: str
+) -> None:
+ """Verify that URLs round-trip correctly."""
+ parsed = URL(url)
+ assert parsed._val.hostname == hostname_without_brackets
+ assert parsed.raw_host == hostname_without_brackets
+ assert parsed.host_subcomponent == hostname
+ assert str(parsed) == url
+ assert str(URL(str(parsed))) == url
diff --git a/contrib/python/yarl/tests/test_url_update_netloc.py b/contrib/python/yarl/tests/test_url_update_netloc.py
index cfe6ca7d457..a8ff1f9f8b3 100644
--- a/contrib/python/yarl/tests/test_url_update_netloc.py
+++ b/contrib/python/yarl/tests/test_url_update_netloc.py
@@ -171,6 +171,26 @@ def test_with_host_non_ascii():
assert url2.authority == "оун-упа.укр:123"
+ ("host", "is_authority"),
+ [
+ ("user:[email protected]", True),
+ ("[email protected]", True),
+ ("host:com", False),
+ ("not_percent_encoded%Zf", False),
+ ("still_not_percent_encoded%fZ", False),
+ *(("other_gen_delim_" + c, False) for c in "/?#[]"),
+ ],
+)
+def test_with_invalid_host(host: str, is_authority: bool):
+ url = URL("http://example.com:123")
+ match = r"Host '[^']+' cannot contain '[^']+' \(at position \d+\)"
+ if is_authority:
+ match += ", if .* use 'authority' instead of 'host'"
+ with pytest.raises(ValueError, match=f"{match}$"):
+ url.with_host(host=host)
+
+
def test_with_host_percent_encoded():
url = URL("http://%25cf%2580%cf%80:%25cf%2580%cf%[email protected]:123")
url2 = url.with_host("%cf%80.org")