Skip to content
Open
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
7 changes: 5 additions & 2 deletions responses/_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,19 @@ def _on_request(
request.params = self._parse_request_params(request.path_url) # type: ignore[attr-defined]
request.req_kwargs = kwargs # type: ignore[attr-defined]
requests_response = _real_send(adapter, request, **kwargs)
content_type = requests_response.headers.get("Content-Type", _UNSET)
headers_values = {
key: value for key, value in requests_response.headers.items()
key: value
for key, value in requests_response.headers.items()
if key.lower() != "content-type"
}
responses_response = Response(
method=str(request.method),
url=str(requests_response.request.url),
status=requests_response.status_code,
body=requests_response.text,
headers=headers_values,
content_type=requests_response.headers.get("Content-Type", _UNSET),
content_type=content_type,
)
self._registry.add(responses_response)
return requests_response
Expand Down
25 changes: 25 additions & 0 deletions responses/tests/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ def run():

assert data == get_data(httpserver.host, httpserver.port)

def test_recorder_excludes_content_type_from_headers(self, httpserver):
"""Content-Type should be in content_type, not duplicated in headers."""
httpserver.expect_request("/json").respond_with_data(
'{"message": "hello"}',
status=200,
content_type="application/json; charset=UTF-8",
)
url = httpserver.url_for("/json")

@_recorder.record(file_path=self.out_file)
def record():
requests.get(url)

record()

with open(self.out_file) as file:
data = yaml.safe_load(file)

rsp = data["responses"][0]["response"]
assert rsp["content_type"] == "application/json; charset=UTF-8"
headers = rsp.get("headers") or {}
assert "Content-Type" not in headers
assert "content-type" not in headers

def prepare_server(self, httpserver):
httpserver.expect_request("/500").respond_with_data(
"500 Internal Server Error",
Expand Down Expand Up @@ -238,3 +262,4 @@ def _parse_resp_f(file_path):
assert responses.registered()[3].content_type == "text/plain"

run()