From: Yeray Diaz Diaz Date: Sat, 27 Apr 2019 16:16:41 +0000 (+0100) Subject: Add `raw` method to SyncResponse X-Git-Tag: 0.3.0~53^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99dc1a99d2d93f21f850a23c569b72a878029ccb;p=thirdparty%2Fhttpx.git Add `raw` method to SyncResponse --- diff --git a/httpcore/sync.py b/httpcore/sync.py index 1d560faf..e23fd68b 100644 --- a/httpcore/sync.py +++ b/httpcore/sync.py @@ -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()) diff --git a/tests/test_sync.py b/tests/test_sync.py index 5879d650..28dbc328 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -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!"