Skip to content

Commit

Permalink
fix mocked HEAD response when content-length header is present (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddarricau committed May 6, 2024
1 parent 3478dcf commit bfd3a0d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit bfd3a0d

Please sign in to comment.