Skip to content

Commit

Permalink
Fix for stream uploads that subclass SyncByteStream/AsyncByteStream (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Jan 6, 2022
1 parent 99ba25c commit b7dc0c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions httpx/_content.py
Expand Up @@ -50,7 +50,9 @@ def __iter__(self) -> Iterator[bytes]:
raise StreamConsumed()

self._is_stream_consumed = True
if hasattr(self._stream, "read"):
if hasattr(self._stream, "read") and not isinstance(
self._stream, SyncByteStream
):
# File-like interfaces should use 'read' directly.
chunk = self._stream.read(self.CHUNK_SIZE) # type: ignore
while chunk:
Expand All @@ -75,7 +77,9 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
raise StreamConsumed()

self._is_stream_consumed = True
if hasattr(self._stream, "aread"):
if hasattr(self._stream, "aread") and not isinstance(
self._stream, AsyncByteStream
):
# File-like interfaces should use 'aread' directly.
chunk = await self._stream.aread(self.CHUNK_SIZE) # type: ignore
while chunk:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_api.py
Expand Up @@ -28,6 +28,18 @@ def data():
assert response.reason_phrase == "OK"


def test_post_byte_stream(server):
class Data(httpx.SyncByteStream):
def __iter__(self):
yield b"Hello"
yield b", "
yield b"world!"

response = httpx.post(server.url, content=Data())
assert response.status_code == 200
assert response.reason_phrase == "OK"


def test_options(server):
response = httpx.options(server.url)
assert response.status_code == 200
Expand Down

0 comments on commit b7dc0c3

Please sign in to comment.