]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Ensure that iter_bytes does not ever yield any zero-length chunks (#2068)
authorTom Christie <tom@tomchristie.com>
Mon, 7 Feb 2022 09:19:26 +0000 (09:19 +0000)
committerGitHub <noreply@github.com>
Mon, 7 Feb 2022 09:19:26 +0000 (09:19 +0000)
httpx/_decoders.py
httpx/_models.py
tests/models/test_responses.py

index 5081c86f9a200c3e1889df6bb72a36fe477fb57d..2d638b7d9930fd41412e9faad5aea53a6094af11 100644 (file)
@@ -172,7 +172,7 @@ class ByteChunker:
 
     def decode(self, content: bytes) -> typing.List[bytes]:
         if self._chunk_size is None:
-            return [content]
+            return [content] if content else []
 
         self._buffer.write(content)
         if self._buffer.tell() >= self._chunk_size:
index 3755c2548c633dc4441b71a55abedc5afb6c84fc..341b28ee012cc11d7fb709092e9e6985664d197b 100644 (file)
@@ -1585,7 +1585,7 @@ class Response:
                         yield chunk
                 decoded = decoder.flush()
                 for chunk in chunker.decode(decoded):
-                    yield chunk
+                    yield chunk  # pragma: nocover
                 for chunk in chunker.flush():
                     yield chunk
 
@@ -1683,7 +1683,7 @@ class Response:
                         yield chunk
                 decoded = decoder.flush()
                 for chunk in chunker.decode(decoded):
-                    yield chunk
+                    yield chunk  # pragma: nocover
                 for chunk in chunker.flush():
                     yield chunk
 
index 942231eed4dc554814dcee5cf4036925c4d90795..8a7e7e1aa85a5dd122f3a52624e97fad31f8fe91 100644 (file)
@@ -396,6 +396,19 @@ def test_iter_raw_with_chunksize():
     assert parts == [b"Hello, world!"]
 
 
+def test_iter_raw_doesnt_return_empty_chunks():
+    def streaming_body_with_empty_chunks():
+        yield b"Hello, "
+        yield b""
+        yield b"world!"
+        yield b""
+
+    response = httpx.Response(200, content=streaming_body_with_empty_chunks())
+
+    parts = [part for part in response.iter_raw()]
+    assert parts == [b"Hello, ", b"world!"]
+
+
 def test_iter_raw_on_iterable():
     response = httpx.Response(
         200,
@@ -526,6 +539,19 @@ def test_iter_bytes_with_empty_response():
     assert parts == []
 
 
+def test_iter_bytes_doesnt_return_empty_chunks():
+    def streaming_body_with_empty_chunks():
+        yield b"Hello, "
+        yield b""
+        yield b"world!"
+        yield b""
+
+    response = httpx.Response(200, content=streaming_body_with_empty_chunks())
+
+    parts = [part for part in response.iter_bytes()]
+    assert parts == [b"Hello, ", b"world!"]
+
+
 @pytest.mark.asyncio
 async def test_aiter_bytes():
     response = httpx.Response(