Skip to content

Commit 2d6d039

Browse files
Fix socket issues (#693)
* Revert socket change (#685) * Added comment why this is the behaviour we have * Added run on the test to fix coverage
1 parent 939562f commit 2d6d039

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
* Reverted overloads removal
55
* Added typing to `Call` attributes.
6+
* Fix socket issues (see #693)
67

78

89
0.24.0

responses/__init__.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import http
21
import inspect
32
import json as json_module
43
import logging
5-
import socket
64
from functools import partialmethod
75
from functools import wraps
86
from http import client
@@ -535,25 +533,38 @@ def _form_response(
535533
headers: Optional[Mapping[str, str]],
536534
status: int,
537535
) -> HTTPResponse:
538-
dummy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
539-
orig_response = http.client.HTTPResponse(sock=dummy_socket)
540536
"""
537+
Function to generate `urllib3.response.HTTPResponse` object.
538+
541539
The cookie handling functionality of the `requests` library relies on the response object
542540
having an original response object with the headers stored in the `msg` attribute.
543541
Instead of supplying a file-like object of type `HTTPMessage` for the headers, we provide
544542
the headers directly. This approach eliminates the need to parse the headers into a file-like
545543
object and then rely on the library to unparse it back. These additional conversions can
546544
introduce potential errors.
547-
Therefore, we intentionally ignore type checking for this assignment.
548545
"""
549-
orig_response.msg = headers # type: ignore[assignment]
550546

547+
data = BytesIO()
548+
data.close()
549+
550+
"""
551+
The type `urllib3.response.HTTPResponse` is incorrect; we should
552+
use `http.client.HTTPResponse` instead. However, changing this requires opening
553+
a real socket to imitate the object. This may not be desired, as some users may
554+
want to completely restrict network access in their tests.
555+
See https://github.com/getsentry/responses/issues/691
556+
"""
557+
orig_response = HTTPResponse(
558+
body=data, # required to avoid "ValueError: Unable to determine whether fp is closed."
559+
msg=headers, # type: ignore[arg-type]
560+
preload_content=False,
561+
)
551562
return HTTPResponse(
552563
status=status,
553564
reason=client.responses.get(status, None),
554565
body=body,
555566
headers=headers,
556-
original_response=orig_response,
567+
original_response=orig_response, # type: ignore[arg-type] # See comment above
557568
preload_content=False,
558569
)
559570

responses/tests/test_responses.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ def run():
20542054
individual_call: Call = responses.calls[0]
20552055
call_slice: List[Call] = responses.calls[1:-1]
20562056

2057-
assert individual_call.request.url == "http://www.example.com"
2057+
assert individual_call.request.url == "http://www.example.com/"
20582058

20592059
assert call_slice == [
20602060
responses.calls[1],
@@ -2067,6 +2067,9 @@ def run():
20672067
"http://www.example.com/1",
20682068
]
20692069

2070+
run()
2071+
assert_reset()
2072+
20702073

20712074
def test_response_calls_and_registry_calls_are_equal():
20722075
@responses.activate

tox.ini

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
[tox]
22
envlist = py38,py39,py310,py311,mypy,precom
33

4+
[pytest]
5+
filterwarnings =
6+
error
7+
default::DeprecationWarning
8+
49
[testenv]
510
extras = tests
611
commands =

0 commit comments

Comments
 (0)