diff --git a/responses/_recorder.py b/responses/_recorder.py index 73e0b431..8edecc83 100644 --- a/responses/_recorder.py +++ b/responses/_recorder.py @@ -145,8 +145,11 @@ 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), @@ -154,7 +157,7 @@ def _on_request( 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 diff --git a/responses/tests/test_recorder.py b/responses/tests/test_recorder.py index fece12fc..86d0b337 100644 --- a/responses/tests/test_recorder.py +++ b/responses/tests/test_recorder.py @@ -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", @@ -238,3 +262,4 @@ def _parse_resp_f(file_path): assert responses.registered()[3].content_type == "text/plain" run() +