Skip to content

Commit

Permalink
Fixed iter_text adding an empty string (#2998)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza authored Dec 11, 2023
1 parent 90538a3 commit 1e11096
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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

0 comments on commit 1e11096

Please sign in to comment.