Fixed iter_text adding an empty string (#2998)

This commit is contained in:
James Braza 2023-12-11 17:34:25 -05:00 committed by GitHub
parent 90538a3b46
commit 1e11096473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View 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()

View 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

View 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()) == []