From: James Braza Date: Mon, 11 Dec 2023 22:34:25 +0000 (-0500) Subject: Fixed `iter_text` adding an empty string (#2998) X-Git-Tag: 0.26.0~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1e110964736a95d822144390b99d7ecf17fca527;p=thirdparty%2Fhttpx.git Fixed `iter_text` adding an empty string (#2998) --- diff --git a/httpx/_decoders.py b/httpx/_decoders.py index 57c64929..3f507c8e 100644 --- a/httpx/_decoders.py +++ b/httpx/_decoders.py @@ -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() diff --git a/httpx/_models.py b/httpx/_models.py index dac177c4..b8617cda 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -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 diff --git a/tests/test_decoders.py b/tests/test_decoders.py index 61c9a4ac..170a9345 100644 --- a/tests/test_decoders.py +++ b/tests/test_decoders.py @@ -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()) == []