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

recorder: use a case-insensitive HTTP headers check #728

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
38 changes: 23 additions & 15 deletions responses/_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ def _remove_default_headers(data: "Any") -> "Any":
record functionality.
"""
if isinstance(data, dict):
# Items in this keys_to_remove list must be lower case!
keys_to_remove = [
"Content-Length",
"Content-Type",
"Date",
"Server",
"Connection",
"Content-Encoding",
"content-length",
"content-type",
"date",
"server",
"connection",
"content-encoding",
]
for i, response in enumerate(data["responses"]):
for key in keys_to_remove:
if key in response["response"]["headers"]:
for key in [key for key in response["response"]["headers"]]:
if key.lower() in keys_to_remove:
del data["responses"][i]["response"]["headers"][key]
if not response["response"]["headers"]:
del data["responses"][i]["response"]["headers"]
Expand Down Expand Up @@ -143,13 +144,20 @@ def _on_request(
headers_values = {
key: value for key, value in requests_response.headers.items()
}
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,
)

response_params = {
"method": str(request.method),
"url": str(requests_response.request.url),
"status": requests_response.status_code,
"body": requests_response.text,
"headers": headers_values,
}

# If the header has a content type then pass it in.
if content_type := requests_response.headers.get("content-type"):
response_params["content_type"] = content_type
Comment on lines +157 to +158
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to have a test covering this change and validating that the Response that is created from the recording has the right parameters/headers.


responses_response = Response(**response_params)
self._registry.add(responses_response)
return requests_response

Expand Down
Loading