Skip to content

Commit

Permalink
Fix iter_bytes with empty content (#1827)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Aug 31, 2021
1 parent 06498df commit 10b60d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions httpx/_models.py
Expand Up @@ -1488,7 +1488,7 @@ def iter_bytes(self, chunk_size: int = None) -> typing.Iterator[bytes]:
"""
if hasattr(self, "_content"):
chunk_size = len(self._content) if chunk_size is None else chunk_size
for i in range(0, len(self._content), chunk_size):
for i in range(0, len(self._content), max(chunk_size, 1)):
yield self._content[i : i + chunk_size]
else:
decoder = self._get_content_decoder()
Expand Down Expand Up @@ -1586,7 +1586,7 @@ async def aiter_bytes(self, chunk_size: int = None) -> typing.AsyncIterator[byte
"""
if hasattr(self, "_content"):
chunk_size = len(self._content) if chunk_size is None else chunk_size
for i in range(0, len(self._content), chunk_size):
for i in range(0, len(self._content), max(chunk_size, 1)):
yield self._content[i : i + chunk_size]
else:
decoder = self._get_content_decoder()
Expand Down
6 changes: 6 additions & 0 deletions tests/models/test_responses.py
Expand Up @@ -486,6 +486,12 @@ def test_iter_bytes_with_chunk_size():
assert parts == [b"Hello, world!"]


def test_iter_bytes_with_empty_response():
response = httpx.Response(200, content=b"")
parts = [part for part in response.iter_bytes()]
assert parts == []


@pytest.mark.asyncio
async def test_aiter_bytes():
response = httpx.Response(
Expand Down

0 comments on commit 10b60d4

Please sign in to comment.