Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed iter_text adding an empty string #2998

Merged
merged 7 commits into from
Dec 11, 2023
6 changes: 4 additions & 2 deletions httpx/_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def __init__(self, chunk_size: typing.Optional[int] = None) -> None:

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:
Expand Down Expand Up @@ -280,7 +280,9 @@ def decode(self, text: str) -> typing.List[str]:
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()
Expand Down
4 changes: 2 additions & 2 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ def iter_text(
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

Expand Down Expand Up @@ -957,7 +957,7 @@ async def aiter_text(
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

Expand Down
11 changes: 11 additions & 0 deletions tests/test_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()) == []
Expand Down