diff --git a/CHANGES/1442.bugfix.rst b/CHANGES/1442.bugfix.rst new file mode 100644 index 00000000..a6284439 --- /dev/null +++ b/CHANGES/1442.bugfix.rst @@ -0,0 +1 @@ +Fixed uppercase ASCII hosts being rejected by :meth:`URL.build() ` and :py:meth:`~yarl.URL.with_host` -- by :user:`bdraco`. diff --git a/CHANGES/954.bugfix.rst b/CHANGES/954.bugfix.rst new file mode 120000 index 00000000..671f0b1a --- /dev/null +++ b/CHANGES/954.bugfix.rst @@ -0,0 +1 @@ +1442.bugfix.rst \ No newline at end of file diff --git a/tests/test_url_build.py b/tests/test_url_build.py index 43f13835..b41d2691 100644 --- a/tests/test_url_build.py +++ b/tests/test_url_build.py @@ -382,3 +382,11 @@ def test_build_with_none_query_string(): def test_build_with_none_fragment(): with pytest.raises(TypeError): URL.build(scheme="http", host="example.com", fragment=None) + + +def test_build_uppercase_host(): + u = URL.build( + host="UPPER.case", + encoded=False, + ) + assert u.host == "upper.case" diff --git a/yarl/_url.py b/yarl/_url.py index e85e0d65..7118b7a7 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -1499,6 +1499,7 @@ def _encode_host(host: str, validate_host: bool) -> str: if host.isascii(): # Check for invalid characters explicitly; _idna_encode() does this # for non-ascii host names. + host = host.lower() if validate_host and (invalid := NOT_REG_NAME.search(host)): value, pos, extra = invalid.group(), invalid.start(), "" if value == "@" or (value == ":" and "@" in host[pos:]): @@ -1510,7 +1511,7 @@ def _encode_host(host: str, validate_host: bool) -> str: raise ValueError( f"Host {host!r} cannot contain {value!r} (at position {pos}){extra}" ) from None - return host.lower() + return host return _idna_encode(host)