mirror of https://github.com/python/cpython
Issue #1580738. When HTTPConnection reads the whole stream with read(),
it closes itself. When the stream is read in several calls to read(n), it should behave in the same way if HTTPConnection knows where the end of the stream is (through self.length). Added a test case for this behaviour.
This commit is contained in:
parent
a1e42e11d5
commit
7066590736
|
@ -530,7 +530,8 @@ class HTTPResponse:
|
||||||
s = self.fp.read(amt)
|
s = self.fp.read(amt)
|
||||||
if self.length is not None:
|
if self.length is not None:
|
||||||
self.length -= len(s)
|
self.length -= len(s)
|
||||||
|
if not self.length:
|
||||||
|
self.close()
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def _read_chunked(self, amt):
|
def _read_chunked(self, amt):
|
||||||
|
|
|
@ -81,13 +81,25 @@ class BasicTest(TestCase):
|
||||||
resp = httplib.HTTPResponse(sock)
|
resp = httplib.HTTPResponse(sock)
|
||||||
resp.begin()
|
resp.begin()
|
||||||
self.assertEqual(resp.read(), 'Text')
|
self.assertEqual(resp.read(), 'Text')
|
||||||
resp.close()
|
self.assertTrue(resp.isclosed())
|
||||||
|
|
||||||
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
|
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
|
||||||
sock = FakeSocket(body)
|
sock = FakeSocket(body)
|
||||||
resp = httplib.HTTPResponse(sock)
|
resp = httplib.HTTPResponse(sock)
|
||||||
self.assertRaises(httplib.BadStatusLine, resp.begin)
|
self.assertRaises(httplib.BadStatusLine, resp.begin)
|
||||||
|
|
||||||
|
def test_partial_reads(self):
|
||||||
|
# if we have a lenght, the system knows when to close itself
|
||||||
|
# same behaviour than when we read the whole thing with read()
|
||||||
|
body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
|
||||||
|
sock = FakeSocket(body)
|
||||||
|
resp = httplib.HTTPResponse(sock)
|
||||||
|
resp.begin()
|
||||||
|
self.assertEqual(resp.read(2), 'Te')
|
||||||
|
self.assertFalse(resp.isclosed())
|
||||||
|
self.assertEqual(resp.read(2), 'xt')
|
||||||
|
self.assertTrue(resp.isclosed())
|
||||||
|
|
||||||
def test_host_port(self):
|
def test_host_port(self):
|
||||||
# Check invalid host_port
|
# Check invalid host_port
|
||||||
|
|
||||||
|
@ -133,7 +145,6 @@ class BasicTest(TestCase):
|
||||||
resp.begin()
|
resp.begin()
|
||||||
if resp.read() != "":
|
if resp.read() != "":
|
||||||
self.fail("Did not expect response from HEAD request")
|
self.fail("Did not expect response from HEAD request")
|
||||||
resp.close()
|
|
||||||
|
|
||||||
def test_send_file(self):
|
def test_send_file(self):
|
||||||
expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
|
expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
|
||||||
|
|
Loading…
Reference in New Issue