summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Michael Larson <sethmichaellarson@gmail.com>2020-11-11 13:30:59 -0600
committerGitHub <noreply@github.com>2020-11-11 13:30:59 -0600
commitf4464f7056d4bfc2440e1670fb67765752190968 (patch)
tree4b1f752c4d61db2a35bb5f09aa2cdcef022eef21
parentd2e3d03368afe9f5c820cd23a487d015b1e347c9 (diff)
downloadurllib3-f4464f7056d4bfc2440e1670fb67765752190968.tar.gz
[1.26] Don't send 'User-Agent' twice if header is binary
-rw-r--r--src/urllib3/connection.py2
-rw-r--r--test/with_dummyserver/test_connectionpool.py23
2 files changed, 24 insertions, 1 deletions
diff --git a/src/urllib3/connection.py b/src/urllib3/connection.py
index 986cd241..660d679c 100644
--- a/src/urllib3/connection.py
+++ b/src/urllib3/connection.py
@@ -229,7 +229,7 @@ class HTTPConnection(_HTTPConnection, object):
else:
# Avoid modifying the headers passed into .request()
headers = headers.copy()
- if "user-agent" not in (k.lower() for k in headers):
+ if "user-agent" not in (six.ensure_str(k.lower()) for k in headers):
headers["User-Agent"] = _get_default_user_agent()
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
diff --git a/test/with_dummyserver/test_connectionpool.py b/test/with_dummyserver/test_connectionpool.py
index 408a2380..f6a66182 100644
--- a/test/with_dummyserver/test_connectionpool.py
+++ b/test/with_dummyserver/test_connectionpool.py
@@ -829,6 +829,29 @@ class TestConnectionPool(HTTPDummyServerTestCase):
request_headers = json.loads(r.data.decode("utf8"))
assert request_headers.get("User-Agent") == custom_ua2
+ @pytest.mark.parametrize(
+ "headers",
+ [
+ None,
+ {},
+ {"User-Agent": "key"},
+ {"user-agent": "key"},
+ {b"uSeR-AgEnT": b"key"},
+ {b"user-agent": "key"},
+ ],
+ )
+ @pytest.mark.parametrize("chunked", [True, False])
+ def test_user_agent_header_not_sent_twice(self, headers, chunked):
+ with HTTPConnectionPool(self.host, self.port) as pool:
+ r = pool.request("GET", "/headers", headers=headers, chunked=chunked)
+ request_headers = json.loads(r.data.decode("utf8"))
+
+ if not headers:
+ assert request_headers["User-Agent"].startswith("python-urllib3/")
+ assert "key" not in request_headers["User-Agent"]
+ else:
+ assert request_headers["User-Agent"] == "key"
+
def test_no_user_agent_header(self):
""" ConnectionPool can suppress sending a user agent header """
custom_ua = "I'm not a web scraper, what are you talking about?"