Skip to content

httpx.Response.iter_text has empty string for iterator's last item #2995

Discussion options

You must be logged in to vote

Thanks! Adding this test case to tests/test_decoders.py will expose the bug:

@pytest.mark.parametrize(
    ["data", "expected"],
    [((b"Hello,", b" world!"), ["Hello,", " world!"])],
)
def test_streaming_iter_text(data: typing.Iterable[bytes], expected: typing.List[str]):
    response = httpx.Response(200, content=iter(data))
    assert list(response.iter_text()) == expected

The issue seems to come from TextChunker.decode:

  1. Decoder (both IdentityDecoder and TextDecoder) emits a empty value
  2. ByteChunker().decode(b"") returns [], but TextChunker().decode("") returns [""]

I traced the cause to some missing logic in TextChunker.decode that ByteChunker.decode has, so I opened #2998

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jamesbraza
Comment options

Answer selected by tomchristie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants