Skip to content
Myr

Add SyncResponse.stream

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent 4a36ec7 · Browse files · 2 files+29 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−0 removed

  • tests:+21 added−0 removed
  • other:+8 added−0 removed

2 files+29 added−0 removed

modified:httpcore/sync.py+8 added
28 unmodified lines
2930 def read(self) -> bytes:31 return asyncio_run(self._response.read())3233 def stream(self) -> typing.Iterator[bytes]:34 inner = self._response.stream()35 while True:36 try:37 yield asyncio_run(inner.__anext__())38 except StopAsyncIteration as exc:39 break404142class SyncClient:43 def __init__(self, client: Client):44 self._client = client45
42 unmodified lines
modified:tests/test_sync.py+21 added
34 unmodified lines
35def test_post(server):36 with httpcore.SyncConnectionPool() as http:37 response = http.request("POST", "http://127.0.0.1:8000/", body=b"Hello, world!")38 assert response.status_code == 200394041@threadpool42def test_stream_response(server):43 with httpcore.SyncConnectionPool() as http:44 response = http.request("GET", "http://127.0.0.1:8000/", stream=True)45 assert response.status_code == 20046 assert not hasattr(response, "body")47 body = response.read()48 assert body == b"Hello, world!"495051@threadpool52def test_stream_iterator(server):53 with httpcore.SyncConnectionPool() as http:54 response = http.request("GET", "http://127.0.0.1:8000/", stream=True)55 assert response.status_code == 20056 body = b''57 for chunk in response.stream():58 body += chunk59 assert body == b"Hello, world!"