lundberg / respx

Mock HTTPX with awesome request patterns and response side effects 🦋
https://lundberg.github.io/respx
BSD 3-Clause "New" or "Revised" License
580 stars 38 forks source link

respx.mock() fails when supplied with a pickled response #272

Open Ravencentric opened 1 week ago

Ravencentric commented 1 week ago

Simple reproduction:

import pickle

import httpx
import respx

pickle.dump(httpx.get("https://www.example.org/"), open("response.pickle", "wb"))

@respx.mock
def test_example() -> None:
    my_route = respx.get("https://www.example.org/").mock(return_value=pickle.load(open("response.pickle", "rb")))
    response = httpx.get("https://www.example.org/")
    assert my_route.called
    assert response.status_code == 200
test.py::test_example FAILED                                                                                                                                                                           [100%]

================================================================================================= FAILURES ==================================================================================================
_______________________________________________________________________________________________ test_example ________________________________________________________________________________________________

    @respx.mock
    def test_example() -> None:
        my_route = respx.get("https://www.example.org/").mock(return_value=pickle.load(open("response.pickle", "rb")))
>       response = httpx.get("https://www.example.org/")

test.py:11: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv\Lib\site-packages\httpx\_api.py:198: in get
    return request(
.venv\Lib\site-packages\httpx\_api.py:106: in request
    return client.request(
.venv\Lib\site-packages\httpx\_client.py:827: in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
.venv\Lib\site-packages\httpx\_client.py:928: in send
    raise exc
.venv\Lib\site-packages\httpx\_client.py:922: in send
    response.read()
.venv\Lib\site-packages\httpx\_models.py:813: in read
    self._content = b"".join(self.iter_bytes())
.venv\Lib\site-packages\httpx\_models.py:829: in iter_bytes
    for raw_bytes in self.iter_raw():
.venv\Lib\site-packages\httpx\_models.py:883: in iter_raw
    for raw_stream_bytes in self.stream:
.venv\Lib\site-packages\httpx\_client.py:126: in __iter__
    for chunk in self._stream:
.venv\Lib\site-packages\httpx\_transports\default.py:113: in __iter__
    for part in self._httpcore_stream:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <httpx._content.UnattachedStream object at 0x00000237AA8F86E0>

    def __iter__(self) -> Iterator[bytes]:
>       raise StreamClosed()
E       httpx.StreamClosed: Attempted to read or stream content, but the stream has been closed.

.venv\Lib\site-packages\httpx\_content.py:98: StreamClosed
========================================================================================== short test summary info ========================================================================================== 
FAILED test.py::test_example - httpx.StreamClosed: Attempted to read or stream content, but the stream has been closed.
============================================================================================= 1 failed in 1.81s ============================================================================================= 
Ravencentric commented 1 week ago

My current workaround for this is loading the pickled response and recasting it:

with open("pickled_response", "rb") as file:
    response: Response = pickle.load(file)
    return Response(status_code=response.status_code, content=response.content)