]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add `raw` method to SyncResponse
authorYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Sat, 27 Apr 2019 16:16:41 +0000 (17:16 +0100)
committerYeray Diaz Diaz <yeraydiazdiaz@gmail.com>
Sat, 27 Apr 2019 16:16:41 +0000 (17:16 +0100)
httpcore/sync.py
tests/test_sync.py

index 1d560faf91b2c31498d13071221d5b989f73e4a6..e23fd68b3e39ec4a14a6de8c83f178381bb7822b 100644 (file)
@@ -39,6 +39,14 @@ class SyncResponse:
             except StopAsyncIteration as exc:
                 break
 
+    def raw(self) -> typing.Iterator[bytes]:
+        inner = self._response.raw()
+        while True:
+            try:
+                yield self._loop.run_until_complete(inner.__anext__())
+            except StopAsyncIteration as exc:
+                break
+
     def close(self) -> None:
         return self._loop.run_until_complete(self._response.close())
 
index 5879d6507858d6a0f6973e2e03b714b76c2c5c81..28dbc328306118259be5c9248ba3851efc1f19bd 100644 (file)
@@ -57,3 +57,14 @@ def test_stream_iterator(server):
     for chunk in response.stream():
         body += chunk
     assert body == b"Hello, world!"
+
+
+@threadpool
+def test_raw_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.raw():
+        body += chunk
+    assert body == b"Hello, world!"