From: Polina Beskorovainaia <54182599+nomilkinmyhome@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:01:47 +0000 (+0300) Subject: Clarified error when header value is None (#3312) X-Git-Tag: 0.28.0~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49d74a2e7f773f88a84dd4af900d7a8543636ae2;p=thirdparty%2Fhttpx.git Clarified error when header value is None (#3312) Co-authored-by: Zanie Blue --- 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