Ensure that iter_bytes does not ever yield any zero-length chunks (#2068)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 0088253 · Browse files · 3 files·+29 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−3 removed
- tests:+26 added−0 removed
- other:+3 added−3 removed
Provenance
- Commit
420911b- Tree
b85112f- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
0088253
3 files changed+29 added−3 removed
modified:httpx/_decoders.py+1 added-1 removed
170 unmodified lines171 self._chunk_size = chunk_size172 173 def decode(self, content: bytes) -> typing.List[bytes]:174 if self._chunk_size is None:175- return [content]175+ return [content] if content else []176 177 self._buffer.write(content)178 if self._buffer.tell() >= self._chunk_size:179 value = self._buffer.getvalue()154 unmodified linesmodified:httpx/_models.py+2 added-2 removed
1583 unmodified lines1584 for chunk in chunker.decode(decoded):1585 yield chunk1586 decoded = decoder.flush()1587 for chunk in chunker.decode(decoded):1588- yield chunk1588+ yield chunk # pragma: nocover1589 for chunk in chunker.flush():1590 yield chunk1591 1592 def iter_text(self, chunk_size: int = None) -> typing.Iterator[str]:89 unmodified lines1682 for chunk in chunker.decode(decoded):1683 yield chunk1684 decoded = decoder.flush()1685 for chunk in chunker.decode(decoded):1686- yield chunk1686+ yield chunk # pragma: nocover1687 for chunk in chunker.flush():1688 yield chunk1689 1690 async def aiter_text(self, chunk_size: int = None) -> typing.AsyncIterator[str]:257 unmodified linesmodified:tests/models/test_responses.py+26 added
394 unmodified lines395 parts = [part for part in response.iter_raw(chunk_size=20)]396 assert parts == [b"Hello, world!"]397 398 399+def test_iter_raw_doesnt_return_empty_chunks():400+ def streaming_body_with_empty_chunks():401+ yield b"Hello, "402+ yield b""403+ yield b"world!"404+ yield b""405+406+ response = httpx.Response(200, content=streaming_body_with_empty_chunks())407+408+ parts = [part for part in response.iter_raw()]409+ assert parts == [b"Hello, ", b"world!"]410+411+412 def test_iter_raw_on_iterable():413 response = httpx.Response(414 200,415 content=StreamingBody(),122 unmodified lines538 parts = [part for part in response.iter_bytes()]539 assert parts == []540 541 542+def test_iter_bytes_doesnt_return_empty_chunks():543+ def streaming_body_with_empty_chunks():544+ yield b"Hello, "545+ yield b""546+ yield b"world!"547+ yield b""548+549+ response = httpx.Response(200, content=streaming_body_with_empty_chunks())550+551+ parts = [part for part in response.iter_bytes()]552+ assert parts == [b"Hello, ", b"world!"]553+554+555 @pytest.mark.asyncio556 async def test_aiter_bytes():557 response = httpx.Response(558 200,412 unmodified lines