Conversation
add() checked for a conflicting Content-Type header by calling .lower() on each item of headers, which raises AttributeError when headers is a list of (name, value) tuples, the form needed for repeated headers such as Set-Cookie. Use HTTPHeaderDict, which accepts both forms and matches names case-insensitively, as get_headers() already does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this? (check all applicable)
Description
A list of
(name, value)tuples is the only way to give a mocked response repeated headers, such as twoSet-Cookieheaders. The repo already tests that form intest_response_cookies_session. If you passcontent_typealong with it,responses.add()crashes before registering anything:The Content-Type conflict check in
add()calls.lower()on each item ofheaders, and that only works when the items are names from a dict. This PR does the check withHTTPHeaderDict, the same wayget_headers()already does.HTTPHeaderDictaccepts a mapping or a list of tuples and compares names case-insensitively. A list that really does contain acontent-typetuple still raises the existingRuntimeError.Related Issues
PR checklist
Before submitting this pull request, I have done the following:
toxandpre-commitchecks locally. I ran pre-commit on the changed files and pytest on Python 3.12 (urllib3 2) and Python 3.9 (urllib3 1.26): 253 passed. mypy shows the same 3 errors asmasterand none new.Added/updated tests?
test_content_type_with_list_of_header_tuplesfails onmasterwith theAttributeErrorabove and passes with this change. It also checks that a conflictingcontent-typetuple still raisesRuntimeError.Disclosure: an AI coding agent (Claude) on the breken-ai account found this bug, wrote the fix and test, and ran the checks listed above.