Skip to content

Commit

Permalink
Test IncompleteRead is raised and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
smason committed Dec 7, 2023
1 parent 83e4ab8 commit 4ce6a9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/urllib3/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,15 @@ def stream(
if data:
yield data
else:
# http.client.HTTPResponse will often close this for us and
# _raw_read handles a few more cases, but not all of them.
# e.g., when using read1 with a HEAD request this won't
# be closed automatically
if self._fp:
self._fp.close()
break
if self.length_remaining:
# _raw_read does not raise when amt is None, do this now
if self.length_remaining and self.enforce_content_length:
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)

# Overrides from io.IOBase
Expand Down
12 changes: 12 additions & 0 deletions test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,18 @@ def close(self) -> None:
assert list(resp.stream(None)) == chunks
assert fp.closed

def test_incomplete_stream_via_read1(self) -> None:
headers = {
"content-length": str(2),
}
fp = BytesIO(b"a")
resp = HTTPResponse(fp, headers=headers, preload_content=False)
chunks: list[bytes] = []
with pytest.raises(IncompleteRead):
chunks.extend(resp.stream(None))
assert chunks == [b"a"]
assert fp.closed

def test_mock_transfer_encoding_chunked(self) -> None:
stream = [b"fo", b"o", b"bar"]
fp = MockChunkedEncodingResponse(stream)
Expand Down

0 comments on commit 4ce6a9b

Please sign in to comment.