]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add SyncResponse.stream
authorTom Christie <tom@tomchristie.com>
Tue, 23 Apr 2019 10:31:51 +0000 (11:31 +0100)
committerTom Christie <tom@tomchristie.com>
Tue, 23 Apr 2019 10:31:51 +0000 (11:31 +0100)
httpcore/sync.py
tests/test_sync.py

index ac2295c90d764d5af2250280b1ac3a89ec7af393..477edda3ab0dbaa86f0b12c3689db1ccec55419e 100644 (file)
@@ -30,6 +30,14 @@ class SyncResponse:
     def read(self) -> bytes:
         return asyncio_run(self._response.read())
 
+    def stream(self) -> typing.Iterator[bytes]:
+        inner = self._response.stream()
+        while True:
+            try:
+                yield asyncio_run(inner.__anext__())
+            except StopAsyncIteration as exc:
+                break
+
 
 class SyncClient:
     def __init__(self, client: Client):
index 7768cbc124a3e4560de34234ecbdf5ee36f7fd50..372bb92042df181fec6b701cf3a1cd93073a57a1 100644 (file)
@@ -36,3 +36,24 @@ def test_post(server):
     with httpcore.SyncConnectionPool() as http:
         response = http.request("POST", "http://127.0.0.1:8000/", body=b"Hello, world!")
     assert response.status_code == 200
+
+
+@threadpool
+def test_stream_response(server):
+    with httpcore.SyncConnectionPool() as http:
+        response = http.request("GET", "http://127.0.0.1:8000/", stream=True)
+    assert response.status_code == 200
+    assert not hasattr(response, "body")
+    body = response.read()
+    assert body == b"Hello, world!"
+
+
+@threadpool
+def test_stream_iterator(server):
+    with httpcore.SyncConnectionPool() as http:
+        response = http.request("GET", "http://127.0.0.1:8000/", stream=True)
+    assert response.status_code == 200
+    body = b''
+    for chunk in response.stream():
+        body += chunk
+    assert body == b"Hello, world!"