Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't send 'User-Agent' twice if header is binary #2047

Merged
merged 1 commit into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/urllib3/connection.py
Expand Up @@ -228,7 +228,7 @@ def request(self, method, url, body=None, headers=None):
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)

Expand Down
23 changes: 23 additions & 0 deletions test/with_dummyserver/test_connectionpool.py
Expand Up @@ -829,6 +829,29 @@ def test_default_user_agent_header(self):
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?"
Expand Down