]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix iter_bytes with empty content (#1827)
authorTom Christie <tom@tomchristie.com>
Tue, 31 Aug 2021 10:52:52 +0000 (11:52 +0100)
committerGitHub <noreply@github.com>
Tue, 31 Aug 2021 10:52:52 +0000 (11:52 +0100)
httpx/_models.py
tests/models/test_responses.py

index c86d37d93d0ab24802302fb5593b99d319e4f6e5..f8dd7731563c3cafe700e0579f6a9864de2488b3 100644 (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()
index adcf2f65c7acd9817a8dfd3c17c4a2298ba5caa6..6af06c3c5367eb67710d0bd4cf669b230a67b79a 100644 (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(