read_chunked() method on closed Response no longer
raises AttributeError, instead returns no chunks.
MODIFY
test/with_dummyserver/test_connectionpool.py
test/with_dummyserver/test_connectionpool.py
@@ -5,6 +5,7 @@ import sys
import unittest
import time
import warnings
+import pytest
import mock
@@ -695,6 +696,16 @@ class TestConnectionPool(HTTPDummyServerTestCase):
self.assertEqual(self.pool.num_connections, 1)
self.assertEqual(self.pool.num_requests, x)
+ def test_read_chunked_short_circuit(self):
+ response = self.pool.request(
+ 'GET',
+ '/chunked',
+ preload_content=False
+ )
+ response.read()
+ with pytest.raises(StopIteration):
+ next(response.read_chunked())
+
def test_chunked_gzip(self):
response = self.pool.request(
'GET',

MODIFY
urllib3/response.py
urllib3/response.py
@@ -600,6 +600,11 @@ class HTTPResponse(io.IOBase):
Similar to :meth:`HTTPResponse.read`, but with an additional
parameter: ``decode_content``.
+ :param amt:
+ How much of the content to read. If specified, caching is skipped
+ because it doesn't make sense to cache partial content as the full
+ response.
+
:param decode_content:
If True, will attempt to decode the body based on the
'content-encoding' header.
@@ -621,6 +626,11 @@ class HTTPResponse(io.IOBase):
self._original_response.close()
return
+ # If a response is already read and closed
+ # then return immediately.
+ if self._fp.fp is None:
+ return
+
while True:
self._update_chunk_length()
if self.chunk_left == 0:

read_chunked() method on closed Response no longer
raises AttributeError, instead returns no chunks.