Coerce values to str in HTTPHeaderDict.__getitem__
MODIFY
src/urllib3/_collections.py
src/urllib3/_collections.py
@@ -18,7 +18,7 @@ except ImportError: # Platform-specific: No threads available
from collections import OrderedDict
from .exceptions import InvalidHeader
-from .packages.six import iterkeys, itervalues, PY3
+from .packages.six import ensure_str, iterkeys, itervalues, PY3
__all__ = ["RecentlyUsedContainer", "HTTPHeaderDict"]
@@ -154,7 +154,7 @@ class HTTPHeaderDict(MutableMapping):
def __getitem__(self, key):
val = self._container[key.lower()]
- return ", ".join(val[1:])
+ return ", ".join([ensure_str(v, "ascii") for v in val[1:]])
def __delitem__(self, key):
del self._container[key.lower()]

MODIFY
test/with_dummyserver/test_connectionpool.py
test/with_dummyserver/test_connectionpool.py
@@ -853,6 +853,14 @@ class TestConnectionPool(HTTPDummyServerTestCase):
assert no_ua_headers["User-Agent"] == SUPPRESS_USER_AGENT
assert pool_headers.get("User-Agent") == custom_ua
+ def test_bytes_header(self):
+ with HTTPConnectionPool(self.host, self.port) as pool:
+ headers = {"User-Agent": b"test header"}
+ r = pool.request("GET", "/headers", headers=headers)
+ request_headers = json.loads(r.data.decode("utf8"))
+ assert "User-Agent" in request_headers
+ assert request_headers["User-Agent"] == "test header"
+
class TestRetry(HTTPDummyServerTestCase):
def test_max_retry(self):

Coerce values to str in HTTPHeaderDict.__getitem__