summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_httplib.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-02-02 22:06:22 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-02-02 22:06:22 (GMT)
commitb5032c85af4b76c9e10b179efa841a2109793f8e (patch)
tree8c67e81c89cab8d74b3c794d7be64fad63bdde57 /Lib/test/test_httplib.py
parent3a9ae7fd984ea9e5c9d3e22c87c61c02c03dd1c9 (diff)
parent6a35e18161c4d06015aeb79c951df04bcea9ca9a (diff)
downloadcpython-b5032c85af4b76c9e10b179efa841a2109793f8e.zip
cpython-b5032c85af4b76c9e10b179efa841a2109793f8e.tar.gz
cpython-b5032c85af4b76c9e10b179efa841a2109793f8e.tar.bz2
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
Diffstat (limited to 'Lib/test/test_httplib.py')
-rw-r--r--Lib/test/test_httplib.py37
1 files changed, 34 insertions, 3 deletions
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index e2d644d..0ebd091 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -237,6 +237,38 @@ class BasicTest(TestCase):
self.assertEqual(n, 0)
self.assertTrue(resp.isclosed())
+ def test_partial_reads_incomplete_body(self):
+ # if the server shuts down the connection before the whole
+ # content-length is delivered, the socket is gracefully closed
+ body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText"
+ sock = FakeSocket(body)
+ resp = client.HTTPResponse(sock)
+ resp.begin()
+ self.assertEqual(resp.read(2), b'Te')
+ self.assertFalse(resp.isclosed())
+ self.assertEqual(resp.read(2), b'xt')
+ self.assertEqual(resp.read(1), b'')
+ self.assertTrue(resp.isclosed())
+
+ def test_partial_readintos_incomplete_body(self):
+ # if the server shuts down the connection before the whole
+ # content-length is delivered, the socket is gracefully closed
+ body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText"
+ sock = FakeSocket(body)
+ resp = client.HTTPResponse(sock)
+ resp.begin()
+ b = bytearray(2)
+ n = resp.readinto(b)
+ self.assertEqual(n, 2)
+ self.assertEqual(bytes(b), b'Te')
+ self.assertFalse(resp.isclosed())
+ n = resp.readinto(b)
+ self.assertEqual(n, 2)
+ self.assertEqual(bytes(b), b'xt')
+ n = resp.readinto(b)
+ self.assertEqual(n, 0)
+ self.assertTrue(resp.isclosed())
+
def test_host_port(self):
# Check invalid host_port
@@ -490,7 +522,7 @@ class BasicTest(TestCase):
resp = client.HTTPResponse(sock, method="GET")
resp.begin()
self.assertEqual(resp.read(), b'Hello\r\n')
- resp.close()
+ self.assertTrue(resp.isclosed())
def test_incomplete_read(self):
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
@@ -504,10 +536,9 @@ class BasicTest(TestCase):
"IncompleteRead(7 bytes read, 3 more expected)")
self.assertEqual(str(i),
"IncompleteRead(7 bytes read, 3 more expected)")
+ self.assertTrue(resp.isclosed())
else:
self.fail('IncompleteRead expected')
- finally:
- resp.close()
def test_epipe(self):
sock = EPipeSocket(