Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for stream uploads that subclass SyncByteStream/AsyncByteStream #2016

Merged
merged 2 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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