Skip to content

Commit 8d2bfe7

Browse files
committed
recorder: set content_type for Response in Recorder._on_request
Recorder._on_request does not set the content_type parameter when creating a new Response instance. When content_type is not set the Response.__init__ will always set the value to "text/plain". This means the recording file is always written with that content type and when responses are replayed they may not have their original content-type header value. For example, the python-gitlab package expects the response header content type to be 'application/json' but this information is in the recording and playing it back will fail. Fix this by having _on_request() pass in the content_type of the requests_response.header. Signed-off-by: Patrick Talbert <[email protected]>
1 parent 7b9b8cf commit 8d2bfe7

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

responses/_recorder.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,20 @@ def _on_request(
144144
headers_values = {
145145
key: value for key, value in requests_response.headers.items()
146146
}
147-
responses_response = Response(
148-
method=str(request.method),
149-
url=str(requests_response.request.url),
150-
status=requests_response.status_code,
151-
body=requests_response.text,
152-
headers=headers_values,
153-
)
147+
148+
response_params = {
149+
"method": str(request.method),
150+
"url": str(requests_response.request.url),
151+
"status": requests_response.status_code,
152+
"body": requests_response.text,
153+
"headers": headers_values,
154+
}
155+
156+
# If the header has a content type then pass it in.
157+
if content_type := requests_response.headers.get("content-type"):
158+
response_params["content_type"] = content_type
159+
160+
responses_response = Response(**response_params)
154161
self._registry.add(responses_response)
155162
return requests_response
156163

0 commit comments

Comments
 (0)