Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(38869)

Side by Side Diff: Lib/test/test_httplib.py

Issue 16723: io.TextIOWrapper on urllib.request.urlopen terminates prematurely
Patch Set: Created 6 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/http/client.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import errno 1 import errno
2 from http import client 2 from http import client
3 import io 3 import io
4 import os 4 import os
5 import array 5 import array
6 import socket 6 import socket
7 7
8 import unittest 8 import unittest
9 TestCase = unittest.TestCase 9 TestCase = unittest.TestCase
10 10
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 class BasicTest(TestCase): 159 class BasicTest(TestCase):
160 def test_status_lines(self): 160 def test_status_lines(self):
161 # Test HTTP status lines 161 # Test HTTP status lines
162 162
163 body = "HTTP/1.1 200 Ok\r\n\r\nText" 163 body = "HTTP/1.1 200 Ok\r\n\r\nText"
164 sock = FakeSocket(body) 164 sock = FakeSocket(body)
165 resp = client.HTTPResponse(sock) 165 resp = client.HTTPResponse(sock)
166 resp.begin() 166 resp.begin()
167 self.assertEqual(resp.read(), b"Text") 167 self.assertEqual(resp.read(), b"Text")
168 self.assertTrue(resp.isclosed()) 168 self.assertTrue(resp.isclosed())
169 self.assertFalse(resp.closed)
170 resp.close()
171 self.assertTrue(resp.closed)
169 172
170 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" 173 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
171 sock = FakeSocket(body) 174 sock = FakeSocket(body)
172 resp = client.HTTPResponse(sock) 175 resp = client.HTTPResponse(sock)
173 self.assertRaises(client.BadStatusLine, resp.begin) 176 self.assertRaises(client.BadStatusLine, resp.begin)
174 177
175 def test_bad_status_repr(self): 178 def test_bad_status_repr(self):
176 exc = client.BadStatusLine('') 179 exc = client.BadStatusLine('')
177 self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''') 180 self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''')
178 181
179 def test_partial_reads(self): 182 def test_partial_reads(self):
180 # if we have a length, the system knows when to close itself 183 # if we have a length, the system knows when to close itself
181 # same behaviour than when we read the whole thing with read() 184 # same behaviour than when we read the whole thing with read()
182 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" 185 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
183 sock = FakeSocket(body) 186 sock = FakeSocket(body)
184 resp = client.HTTPResponse(sock) 187 resp = client.HTTPResponse(sock)
185 resp.begin() 188 resp.begin()
186 self.assertEqual(resp.read(2), b'Te') 189 self.assertEqual(resp.read(2), b'Te')
187 self.assertFalse(resp.isclosed()) 190 self.assertFalse(resp.isclosed())
188 self.assertEqual(resp.read(2), b'xt') 191 self.assertEqual(resp.read(2), b'xt')
189 self.assertTrue(resp.isclosed()) 192 self.assertTrue(resp.isclosed())
193 self.assertFalse(resp.closed)
194 resp.close()
195 self.assertTrue(resp.closed)
190 196
191 def test_partial_readintos(self): 197 def test_partial_readintos(self):
192 # if we have a length, the system knows when to close itself 198 # if we have a length, the system knows when to close itself
193 # same behaviour than when we read the whole thing with read() 199 # same behaviour than when we read the whole thing with read()
194 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" 200 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
195 sock = FakeSocket(body) 201 sock = FakeSocket(body)
196 resp = client.HTTPResponse(sock) 202 resp = client.HTTPResponse(sock)
197 resp.begin() 203 resp.begin()
198 b = bytearray(2) 204 b = bytearray(2)
199 n = resp.readinto(b) 205 n = resp.readinto(b)
200 self.assertEqual(n, 2) 206 self.assertEqual(n, 2)
201 self.assertEqual(bytes(b), b'Te') 207 self.assertEqual(bytes(b), b'Te')
202 self.assertFalse(resp.isclosed()) 208 self.assertFalse(resp.isclosed())
203 n = resp.readinto(b) 209 n = resp.readinto(b)
204 self.assertEqual(n, 2) 210 self.assertEqual(n, 2)
205 self.assertEqual(bytes(b), b'xt') 211 self.assertEqual(bytes(b), b'xt')
206 self.assertTrue(resp.isclosed()) 212 self.assertTrue(resp.isclosed())
213 self.assertFalse(resp.closed)
214 resp.close()
215 self.assertTrue(resp.closed)
207 216
208 def test_partial_reads_no_content_length(self): 217 def test_partial_reads_no_content_length(self):
209 # when no length is present, the socket should be gracefully closed when 218 # when no length is present, the socket should be gracefully closed when
210 # all data was read 219 # all data was read
211 body = "HTTP/1.1 200 Ok\r\n\r\nText" 220 body = "HTTP/1.1 200 Ok\r\n\r\nText"
212 sock = FakeSocket(body) 221 sock = FakeSocket(body)
213 resp = client.HTTPResponse(sock) 222 resp = client.HTTPResponse(sock)
214 resp.begin() 223 resp.begin()
215 self.assertEqual(resp.read(2), b'Te') 224 self.assertEqual(resp.read(2), b'Te')
216 self.assertFalse(resp.isclosed()) 225 self.assertFalse(resp.isclosed())
217 self.assertEqual(resp.read(2), b'xt') 226 self.assertEqual(resp.read(2), b'xt')
218 self.assertEqual(resp.read(1), b'') 227 self.assertEqual(resp.read(1), b'')
219 self.assertTrue(resp.isclosed()) 228 self.assertTrue(resp.isclosed())
229 self.assertFalse(resp.closed)
230 resp.close()
231 self.assertTrue(resp.closed)
220 232
221 def test_partial_readintos_no_content_length(self): 233 def test_partial_readintos_no_content_length(self):
222 # when no length is present, the socket should be gracefully closed when 234 # when no length is present, the socket should be gracefully closed when
223 # all data was read 235 # all data was read
224 body = "HTTP/1.1 200 Ok\r\n\r\nText" 236 body = "HTTP/1.1 200 Ok\r\n\r\nText"
225 sock = FakeSocket(body) 237 sock = FakeSocket(body)
226 resp = client.HTTPResponse(sock) 238 resp = client.HTTPResponse(sock)
227 resp.begin() 239 resp.begin()
228 b = bytearray(2) 240 b = bytearray(2)
229 n = resp.readinto(b) 241 n = resp.readinto(b)
230 self.assertEqual(n, 2) 242 self.assertEqual(n, 2)
231 self.assertEqual(bytes(b), b'Te') 243 self.assertEqual(bytes(b), b'Te')
232 self.assertFalse(resp.isclosed()) 244 self.assertFalse(resp.isclosed())
233 n = resp.readinto(b) 245 n = resp.readinto(b)
234 self.assertEqual(n, 2) 246 self.assertEqual(n, 2)
235 self.assertEqual(bytes(b), b'xt') 247 self.assertEqual(bytes(b), b'xt')
236 n = resp.readinto(b) 248 n = resp.readinto(b)
237 self.assertEqual(n, 0) 249 self.assertEqual(n, 0)
238 self.assertTrue(resp.isclosed()) 250 self.assertTrue(resp.isclosed())
251 self.assertFalse(resp.closed)
252 resp.close()
253 self.assertTrue(resp.closed)
239 254
240 def test_host_port(self): 255 def test_host_port(self):
241 # Check invalid host_port 256 # Check invalid host_port
242 257
243 for hp in ("www.python.org:abc", "user:password@www.python.org"): 258 for hp in ("www.python.org:abc", "user:password@www.python.org"):
244 self.assertRaises(client.InvalidURL, client.HTTPConnection, hp) 259 self.assertRaises(client.InvalidURL, client.HTTPConnection, hp)
245 260
246 for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", 261 for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000",
247 "fe80::207:e9ff:fe9b", 8000), 262 "fe80::207:e9ff:fe9b", 8000),
248 ("www.python.org:80", "www.python.org", 80), 263 ("www.python.org:80", "www.python.org", 80),
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 '1\r\n' 471 '1\r\n'
457 'd\r\n' 472 'd\r\n'
458 ) 473 )
459 sock = FakeSocket(chunked_start + '0\r\n') 474 sock = FakeSocket(chunked_start + '0\r\n')
460 resp = client.HTTPResponse(sock, method="HEAD") 475 resp = client.HTTPResponse(sock, method="HEAD")
461 resp.begin() 476 resp.begin()
462 self.assertEqual(resp.read(), b'') 477 self.assertEqual(resp.read(), b'')
463 self.assertEqual(resp.status, 200) 478 self.assertEqual(resp.status, 200)
464 self.assertEqual(resp.reason, 'OK') 479 self.assertEqual(resp.reason, 'OK')
465 self.assertTrue(resp.isclosed()) 480 self.assertTrue(resp.isclosed())
481 self.assertFalse(resp.closed)
482 resp.close()
483 self.assertTrue(resp.closed)
466 484
467 def test_readinto_chunked_head(self): 485 def test_readinto_chunked_head(self):
468 chunked_start = ( 486 chunked_start = (
469 'HTTP/1.1 200 OK\r\n' 487 'HTTP/1.1 200 OK\r\n'
470 'Transfer-Encoding: chunked\r\n\r\n' 488 'Transfer-Encoding: chunked\r\n\r\n'
471 'a\r\n' 489 'a\r\n'
472 'hello world\r\n' 490 'hello world\r\n'
473 '1\r\n' 491 '1\r\n'
474 'd\r\n' 492 'd\r\n'
475 ) 493 )
476 sock = FakeSocket(chunked_start + '0\r\n') 494 sock = FakeSocket(chunked_start + '0\r\n')
477 resp = client.HTTPResponse(sock, method="HEAD") 495 resp = client.HTTPResponse(sock, method="HEAD")
478 resp.begin() 496 resp.begin()
479 b = bytearray(5) 497 b = bytearray(5)
480 n = resp.readinto(b) 498 n = resp.readinto(b)
481 self.assertEqual(n, 0) 499 self.assertEqual(n, 0)
482 self.assertEqual(bytes(b), b'\x00'*5) 500 self.assertEqual(bytes(b), b'\x00'*5)
483 self.assertEqual(resp.status, 200) 501 self.assertEqual(resp.status, 200)
484 self.assertEqual(resp.reason, 'OK') 502 self.assertEqual(resp.reason, 'OK')
485 self.assertTrue(resp.isclosed()) 503 self.assertTrue(resp.isclosed())
504 self.assertFalse(resp.closed)
505 resp.close()
506 self.assertTrue(resp.closed)
486 507
487 def test_negative_content_length(self): 508 def test_negative_content_length(self):
488 sock = FakeSocket( 509 sock = FakeSocket(
489 'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n') 510 'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n')
490 resp = client.HTTPResponse(sock, method="GET") 511 resp = client.HTTPResponse(sock, method="GET")
491 resp.begin() 512 resp.begin()
492 self.assertEqual(resp.read(), b'Hello\r\n') 513 self.assertEqual(resp.read(), b'Hello\r\n')
493 resp.close() 514 resp.close()
494 515
495 def test_incomplete_read(self): 516 def test_incomplete_read(self):
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 self.assertRaises(client.LineTooLong, resp.read) 573 self.assertRaises(client.LineTooLong, resp.read)
553 574
554 def test_early_eof(self): 575 def test_early_eof(self):
555 # Test httpresponse with no \r\n termination, 576 # Test httpresponse with no \r\n termination,
556 body = "HTTP/1.1 200 Ok" 577 body = "HTTP/1.1 200 Ok"
557 sock = FakeSocket(body) 578 sock = FakeSocket(body)
558 resp = client.HTTPResponse(sock) 579 resp = client.HTTPResponse(sock)
559 resp.begin() 580 resp.begin()
560 self.assertEqual(resp.read(), b'') 581 self.assertEqual(resp.read(), b'')
561 self.assertTrue(resp.isclosed()) 582 self.assertTrue(resp.isclosed())
583 self.assertFalse(resp.closed)
584 resp.close()
585 self.assertTrue(resp.closed)
562 586
563 def test_delayed_ack_opt(self): 587 def test_delayed_ack_opt(self):
564 # Test that Nagle/delayed_ack optimistaion works correctly. 588 # Test that Nagle/delayed_ack optimistaion works correctly.
565 589
566 # For small payloads, it should coalesce the body with 590 # For small payloads, it should coalesce the body with
567 # headers, resulting in a single sendall() call 591 # headers, resulting in a single sendall() call
568 conn = client.HTTPConnection('example.com') 592 conn = client.HTTPConnection('example.com')
569 sock = FakeSocket(None) 593 sock = FakeSocket(None)
570 conn.sock = sock 594 conn.sock = sock
571 body = b'x' * (conn.mss - 1) 595 body = b'x' * (conn.mss - 1)
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 header = self.resp.getheader('No-Such-Header',default=42) 906 header = self.resp.getheader('No-Such-Header',default=42)
883 self.assertEqual(header, 42) 907 self.assertEqual(header, 42)
884 908
885 def test_main(verbose=None): 909 def test_main(verbose=None):
886 support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, 910 support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
887 HTTPSTest, RequestBodyTest, SourceAddressTest, 911 HTTPSTest, RequestBodyTest, SourceAddressTest,
888 HTTPResponseTest) 912 HTTPResponseTest)
889 913
890 if __name__ == '__main__': 914 if __name__ == '__main__':
891 test_main() 915 test_main()
OLDNEW
« no previous file with comments | « Lib/http/client.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+