Clarified error when header value is None (#3312)

Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
Polina Beskorovainaia 2024-09-26 20:01:47 +03:00 committed by GitHub
parent 2e01aa0075
commit 49d74a2e7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -50,6 +50,8 @@ def normalize_header_value(value: str | bytes, encoding: str | None = None) -> b
"""
if isinstance(value, bytes):
return value
if not isinstance(value, str):
raise TypeError(f"Header value must be str or bytes, not {type(value)}")
return value.encode(encoding or "ascii")

View File

@ -177,6 +177,14 @@ def test_header_does_not_exist():
del headers["baz"]
def test_header_with_incorrect_value():
with pytest.raises(
TypeError,
match=f"Header value must be str or bytes, not {type(None)}",
):
httpx.Headers({"foo": None}) # type: ignore
def test_host_with_auth_and_port_in_url():
"""
The Host header should only include the hostname, or hostname:port