Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,19 @@ def _add_from_file(self, file_path: "Union[str, bytes, os.PathLike[Any]]") -> No

for rsp in data["responses"]:
rsp = rsp["response"]
headers = rsp["headers"] if "headers" in rsp else None

if headers is not None and "content_type" in rsp:
headers = {k: v for k, v in headers.items() if k.lower() != "content-type"}
if not headers:
headers = None

self.add(
method=rsp["method"],
url=rsp["url"],
body=rsp["body"],
status=rsp["status"],
headers=rsp["headers"] if "headers" in rsp else None,
headers=headers,
content_type=rsp["content_type"],
auto_calculate_content_length=rsp["auto_calculate_content_length"],
)
Expand Down
68 changes: 68 additions & 0 deletions responses/tests/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,71 @@ def _parse_resp_f(file_path):
assert responses.registered()[3].content_type == "text/plain"

run()

def test_add_from_file_content_type_in_headers(self):
"""Fixture files may contain Content-Type in both headers and content_type.

The recorder captures ``Content-Type`` inside the ``headers`` dict *and*
as the dedicated ``content_type`` field. Passing both to ``add()``
raises a ``RuntimeError`` because ``content_type`` and a ``Content-Type``
header conflict. ``_add_from_file`` should strip the duplicate header
entry so that the dedicated ``content_type`` kwarg wins.

Using mismatched values (``text/html`` in headers vs ``application/json``
in ``content_type``) ensures the assertion is non-trivial and confirms
that ``content_type`` takes precedence over the header value.
"""
data = {
"responses": [
{
"response": {
"method": "GET",
"url": "http://example.com/api",
"body": '{"status": "ok"}',
"status": 200,
# headers has a *different* Content-Type than content_type
# to verify that content_type wins (not just that both happen
# to be the same value).
"headers": {"Content-Type": "text/html"},
"content_type": "application/json",
"auto_calculate_content_length": False,
}
},
{
"response": {
"method": "POST",
"url": "http://example.com/submit",
"body": "created",
"status": 201,
"headers": {
"Content-Type": "text/html",
"X-Request-Id": "abc123",
},
"content_type": "text/plain",
"auto_calculate_content_length": False,
}
},
]
}

with open(self.out_file, "w") as f:
yaml.dump(data, f)

@responses.activate
def run():
responses._add_from_file(file_path=self.out_file)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll either need to save your file as toml, or switch the loader to yaml for this test.


# Verify responses were registered without RuntimeError
assert len(responses.registered()) == 2

# content_type must win over the conflicting Content-Type header
assert responses.registered()[0].url == "http://example.com/api"
assert responses.registered()[0].content_type == "application/json"

assert responses.registered()[1].url == "http://example.com/submit"
assert responses.registered()[1].content_type == "text/plain"
# Non-content-type headers should be preserved
resp = requests.post("http://example.com/submit")
assert resp.headers["X-Request-Id"] == "abc123"

run()
Loading