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

deduplicate content-type headers #673

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
through which function they would like to send real requests
* Added support for re.Pattern based header matching.
* Added support for gzipped response bodies to `json_params_matcher`.
* Fix `Content-Type` headers issue when the header was duplicated. See #644
* Moved types-pyyaml dependency to `tests_requires`

0.23.3
Expand Down
9 changes: 8 additions & 1 deletion responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,17 @@ def _req_attr_matches(

def get_headers(self) -> HTTPHeaderDict:
headers = HTTPHeaderDict() # Duplicate headers are legal
if self.content_type is not None:

# Add Content-Type if it exists and is not already in headers
if self.content_type and (
not self.headers or "Content-Type" not in self.headers
):
headers["Content-Type"] = self.content_type

# Extend headers if they exist
if self.headers:
headers.extend(self.headers)

return headers

def get_response(self, request: "PreparedRequest") -> HTTPResponse:
Expand Down
23 changes: 23 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,29 @@ def run():
assert_reset()


def test_headers_deduplicated_content_type():
"""Test to ensure that we do not have two values for `content-type`.

For more details see https://github.com/getsentry/responses/issues/644
"""

@responses.activate
def run():
responses.get(
"https://example.org/",
json={},
headers={"Content-Type": "application/json"},
)
responses.start()

resp = requests.get("https://example.org/")

assert resp.headers["Content-Type"] == "application/json"

run()
assert_reset()


def test_content_length_error(monkeypatch):
"""
Currently 'requests' does not enforce content length validation,
Expand Down