8000 fix #122: httpx streaming via `iter_raw` raises `httpx.StreamConsumed… · h2non/pook@c07c496 · GitHub
[go: up one dir, main page]

Skip to content

Commit c07c496

Browse files
authored
fix #122: httpx streaming via iter_raw raises httpx.StreamConsumed (#123)
* Implemented automated regression tests for #122 * Implemented fix for the #122 regression
1 parent ab8e8ee commit c07c496

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/pook/interceptors/_httpx.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def _get_httpx_response(self, httpx_request, mock_response):
7575
request=httpx_request,
7676
)
7777

78+
# Allow to read the response on client side
79+
res.is_stream_consumed = False
80+
res.is_closed = False
81+
if hasattr(res, "_content"):
82+
del res._content
83+
7884
return res
7985

8086

tests/unit/interceptors/httpx_test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,25 @@ def test_json():
4444
assert response.json() == {"title": "123abc title"}
4545

4646

47-
def test_streaming():
47+
def _check_streaming_via(response_method):
4848
streamed_response = b"streamed response"
4949
pook.get(URL).times(1).reply(200).body(streamed_response).mock
5050

5151
with httpx.stream("GET", URL) as r:
52-
read_bytes = list(r.iter_bytes(chunk_size=1))
52+
read_bytes = list(getattr(r, response_method)(chunk_size=1))
5353

5454
assert len(read_bytes) == len(streamed_response)
5555
assert bytes().join(read_bytes) == streamed_response
5656

5757

58+
def test_streaming_via_iter_bytes():
59+
_check_streaming_via("iter_bytes")
60+
61+
62+
def test_streaming_via_iter_raw():
63+
_check_streaming_via("iter_raw")
64+
65+
5866
def test_redirect_following():
5967
urls = [URL, f"{URL}/redirected", f"{URL}/redirected_again"]
6068
for req, dest in zip_longest(urls, urls[1:], fillvalue=None):

0 commit comments

Comments
 (0)
0