From 49d74a2e7f773f88a84dd4af900d7a8543636ae2 Mon Sep 17 00:00:00 2001 From: Polina Beskorovainaia <54182599+nomilkinmyhome@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:01:47 +0300 Subject: [PATCH] Clarified error when header value is None (#3312) Co-authored-by: Zanie Blue --- httpx/_utils.py | 2 ++ tests/client/test_headers.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/httpx/_utils.py b/httpx/_utils.py index a9ece194..46a4d63b 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -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") diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py index c51e40c3..b8d29767 100755 --- a/tests/client/test_headers.py +++ b/tests/client/test_headers.py @@ -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