Fix iter_bytes with empty content (#1827)

This commit is contained in:
Tom Christie 2021-08-31 11:52:52 +01:00 committed by GitHub
parent 06498df528
commit 10b60d47c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -1488,7 +1488,7 @@ class Response:
"""
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()
@ -1586,7 +1586,7 @@ class Response:
"""
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()

View File

@ -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(