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

fix mocked HEAD response when content-length header is present #712

Merged
merged 1 commit into from
May 6, 2024
Merged
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
6 changes: 4 additions & 2 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def _form_response(
body: Union[BufferedReader, BytesIO],
headers: Optional[Mapping[str, str]],
status: int,
request_method: Optional[str],
) -> HTTPResponse:
"""
Function to generate `urllib3.response.HTTPResponse` object.
Expand Down Expand Up @@ -566,6 +567,7 @@ def _form_response(
headers=headers,
original_response=orig_response, # type: ignore[arg-type] # See comment above
preload_content=False,
request_method=request_method,
)


Expand Down Expand Up @@ -632,7 +634,7 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse:
content_length = len(body.getvalue())
headers["Content-Length"] = str(content_length)

return _form_response(body, headers, status)
return _form_response(body, headers, status, request.method)

def __repr__(self) -> str:
return (
Expand Down Expand Up @@ -695,7 +697,7 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse:
body = _handle_body(body)
headers.extend(r_headers)

return _form_response(body, headers, status)
return _form_response(body, headers, status, request.method)


class PassthroughResponse(BaseResponse):
Expand Down
12 changes: 12 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2416,6 +2416,18 @@ def run():
run()
assert_reset()

def test_head_with_content_length(self):
@responses.activate
def run():
headers = {"content-length": "1000"}
responses.head("http://example.com/1", status=200, headers=headers)
resp = requests.head("http://example.com/1")
assert resp.status_code == 200
assert resp.headers["Content-Length"] == "1000"

run()
assert_reset()

def test_options(self):
@responses.activate
def run():
Expand Down
Loading