Ensure that iter_bytes does not ever yield any zero-length chunks (#2068)
This commit is contained in:
parent
0088253b32
commit
420911bc69
@ -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:
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user