Ensure that iter_bytes does not ever yield any zero-length chunks (#2068)

This commit is contained in:
Tom Christie 2022-02-07 09:19:26 +00:00 committed by GitHub
parent 0088253b32
commit 420911bc69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -172,7 +172,7 @@ class ByteChunker:
def decode(self, content: bytes) -> typing.List[bytes]:
if self._chunk_size is None:
return [content]
return [content] if content else []
self._buffer.write(content)
if self._buffer.tell() >= self._chunk_size:

View File

@ -1585,7 +1585,7 @@ class Response:
yield chunk
decoded = decoder.flush()
for chunk in chunker.decode(decoded):
yield chunk
yield chunk # pragma: nocover
for chunk in chunker.flush():
yield chunk
@ -1683,7 +1683,7 @@ class Response:
yield chunk
decoded = decoder.flush()
for chunk in chunker.decode(decoded):
yield chunk
yield chunk # pragma: nocover
for chunk in chunker.flush():
yield chunk

View File

@ -396,6 +396,19 @@ def test_iter_raw_with_chunksize():
assert parts == [b"Hello, world!"]
def test_iter_raw_doesnt_return_empty_chunks():
def streaming_body_with_empty_chunks():
yield b"Hello, "
yield b""
yield b"world!"
yield b""
response = httpx.Response(200, content=streaming_body_with_empty_chunks())
parts = [part for part in response.iter_raw()]
assert parts == [b"Hello, ", b"world!"]
def test_iter_raw_on_iterable():
response = httpx.Response(
200,
@ -526,6 +539,19 @@ def test_iter_bytes_with_empty_response():
assert parts == []
def test_iter_bytes_doesnt_return_empty_chunks():
def streaming_body_with_empty_chunks():
yield b"Hello, "
yield b""
yield b"world!"
yield b""
response = httpx.Response(200, content=streaming_body_with_empty_chunks())
parts = [part for part in response.iter_bytes()]
assert parts == [b"Hello, ", b"world!"]
@pytest.mark.asyncio
async def test_aiter_bytes():
response = httpx.Response(