]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Clarified error when header value is None (#3312)
authorPolina Beskorovainaia <54182599+nomilkinmyhome@users.noreply.github.com>
Thu, 26 Sep 2024 17:01:47 +0000 (20:01 +0300)
committerGitHub <noreply@github.com>
Thu, 26 Sep 2024 17:01:47 +0000 (18:01 +0100)
Co-authored-by: Zanie Blue <contact@zanie.dev>
httpx/_utils.py
tests/client/test_headers.py

index a9ece1943878d0182d87ca77569fe3bf47e3bba8..46a4d63b1d4d3d0389f0ec97d1b9031500ff530a 100644 (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")
 
 
index c51e40c3356103708706ea1cc90d9fb13df964d7..b8d29767007d48fd2f3ba82ed0f4c0b6f4eb52bc 100755 (executable)
@@ -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