]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fixed `iter_text` adding an empty string (#2998)
authorJames Braza <jamesbraza@gmail.com>
Mon, 11 Dec 2023 22:34:25 +0000 (17:34 -0500)
committerGitHub <noreply@github.com>
Mon, 11 Dec 2023 22:34:25 +0000 (22:34 +0000)
httpx/_decoders.py
httpx/_models.py
tests/test_decoders.py

index 57c649297af9984fe946cb77be7d3c73aab22e7a..3f507c8e040c6e6d192bbd798556315fb0fcff68 100644 (file)
@@ -212,7 +212,7 @@ class TextChunker:
 
     def decode(self, content: str) -> typing.List[str]:
         if self._chunk_size is None:
-            return [content]
+            return [content] if content else []
 
         self._buffer.write(content)
         if self._buffer.tell() >= self._chunk_size:
@@ -280,7 +280,9 @@ class LineDecoder:
             text = text[:-1]
 
         if not text:
-            return []
+            # NOTE: the edge case input of empty text doesn't occur in practice,
+            # because other httpx internals filter out this value
+            return []  # pragma: no cover
 
         trailing_newline = text[-1] in NEWLINE_CHARS
         lines = text.splitlines()
index dac177c4f6c4ad9a741c25508bf65689a0fcefa4..b8617cdab56594c49904efdc666d8cd60ccf4459 100644 (file)
@@ -853,7 +853,7 @@ class Response:
                     yield chunk
             text_content = decoder.flush()
             for chunk in chunker.decode(text_content):
-                yield chunk
+                yield chunk  # pragma: no cover
             for chunk in chunker.flush():
                 yield chunk
 
@@ -957,7 +957,7 @@ class Response:
                     yield chunk
             text_content = decoder.flush()
             for chunk in chunker.decode(text_content):
-                yield chunk
+                yield chunk  # pragma: no cover
             for chunk in chunker.flush():
                 yield chunk
 
index 61c9a4accace997370f933e7139750d325f02bf7..170a93453cfafd6e6ae1130c95f0b4f8d19bac61 100644 (file)
@@ -219,6 +219,17 @@ def test_text_decoder_empty_cases():
     assert response.text == ""
 
 
+@pytest.mark.parametrize(
+    ["data", "expected"],
+    [((b"Hello,", b" world!"), ["Hello,", " world!"])],
+)
+def test_streaming_text_decoder(
+    data: typing.Iterable[bytes], expected: typing.List[str]
+) -> None:
+    response = httpx.Response(200, content=iter(data))
+    assert list(response.iter_text()) == expected
+
+
 def test_line_decoder_nl():
     response = httpx.Response(200, content=[b""])
     assert list(response.iter_lines()) == []