mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Issue #13464: Add a readinto() method to http.client.HTTPResponse.
Patch by Jon Kuhn.
This commit is contained in:
parent
0f6492250c
commit
38d9643d5e
5 changed files with 215 additions and 48 deletions
|
@ -158,6 +158,23 @@ class BasicTest(TestCase):
|
|||
self.assertEqual(resp.read(2), b'xt')
|
||||
self.assertTrue(resp.isclosed())
|
||||
|
||||
def test_partial_readintos(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 = 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')
|
||||
self.assertTrue(resp.isclosed())
|
||||
|
||||
def test_host_port(self):
|
||||
# Check invalid host_port
|
||||
|
||||
|
@ -206,6 +223,21 @@ class BasicTest(TestCase):
|
|||
if resp.read():
|
||||
self.fail("Did not expect response from HEAD request")
|
||||
|
||||
def test_readinto_head(self):
|
||||
# Test that the library doesn't attempt to read any data
|
||||
# from a HEAD request. (Tickles SF bug #622042.)
|
||||
sock = FakeSocket(
|
||||
'HTTP/1.1 200 OK\r\n'
|
||||
'Content-Length: 14432\r\n'
|
||||
'\r\n',
|
||||
NoEOFStringIO)
|
||||
resp = client.HTTPResponse(sock, method="HEAD")
|
||||
resp.begin()
|
||||
b = bytearray(5)
|
||||
if resp.readinto(b) != 0:
|
||||
self.fail("Did not expect response from HEAD request")
|
||||
self.assertEqual(bytes(b), b'\x00'*5)
|
||||
|
||||
def test_send_file(self):
|
||||
expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n'
|
||||
b'Accept-Encoding: identity\r\nContent-Length:')
|
||||
|
@ -285,6 +317,40 @@ class BasicTest(TestCase):
|
|||
finally:
|
||||
resp.close()
|
||||
|
||||
def test_readinto_chunked(self):
|
||||
chunked_start = (
|
||||
'HTTP/1.1 200 OK\r\n'
|
||||
'Transfer-Encoding: chunked\r\n\r\n'
|
||||
'a\r\n'
|
||||
'hello worl\r\n'
|
||||
'1\r\n'
|
||||
'd\r\n'
|
||||
)
|
||||
sock = FakeSocket(chunked_start + '0\r\n')
|
||||
resp = client.HTTPResponse(sock, method="GET")
|
||||
resp.begin()
|
||||
b = bytearray(16)
|
||||
n = resp.readinto(b)
|
||||
self.assertEqual(b[:11], b'hello world')
|
||||
self.assertEqual(n, 11)
|
||||
resp.close()
|
||||
|
||||
for x in ('', 'foo\r\n'):
|
||||
sock = FakeSocket(chunked_start + x)
|
||||
resp = client.HTTPResponse(sock, method="GET")
|
||||
resp.begin()
|
||||
try:
|
||||
b = bytearray(16)
|
||||
n = resp.readinto(b)
|
||||
except client.IncompleteRead as i:
|
||||
self.assertEqual(i.partial, b'hello world')
|
||||
self.assertEqual(repr(i),'IncompleteRead(11 bytes read)')
|
||||
self.assertEqual(str(i),'IncompleteRead(11 bytes read)')
|
||||
else:
|
||||
self.fail('IncompleteRead expected')
|
||||
finally:
|
||||
resp.close()
|
||||
|
||||
def test_chunked_head(self):
|
||||
chunked_start = (
|
||||
'HTTP/1.1 200 OK\r\n'
|
||||
|
@ -302,6 +368,26 @@ class BasicTest(TestCase):
|
|||
self.assertEqual(resp.reason, 'OK')
|
||||
self.assertTrue(resp.isclosed())
|
||||
|
||||
def test_readinto_chunked_head(self):
|
||||
chunked_start = (
|
||||
'HTTP/1.1 200 OK\r\n'
|
||||
'Transfer-Encoding: chunked\r\n\r\n'
|
||||
'a\r\n'
|
||||
'hello world\r\n'
|
||||
'1\r\n'
|
||||
'd\r\n'
|
||||
)
|
||||
sock = FakeSocket(chunked_start + '0\r\n')
|
||||
resp = client.HTTPResponse(sock, method="HEAD")
|
||||
resp.begin()
|
||||
b = bytearray(5)
|
||||
n = resp.readinto(b)
|
||||
self.assertEqual(n, 0)
|
||||
self.assertEqual(bytes(b), b'\x00'*5)
|
||||
self.assertEqual(resp.status, 200)
|
||||
self.assertEqual(resp.reason, 'OK')
|
||||
self.assertTrue(resp.isclosed())
|
||||
|
||||
def test_negative_content_length(self):
|
||||
sock = FakeSocket(
|
||||
'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue