Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37d3d9a

Browse files
committed
Add test case for the HTTPResponse being an iterable. Follow-up of issue #4608.
1 parent e3f7616 commit 37d3d9a

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

Lib/test/test_urllib2_localnet.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,9 @@ class FakeHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
308308

309309
def do_GET(self):
310310
body = self.send_head()
311-
if body:
312-
self.wfile.write(body)
311+
while body:
312+
done = self.wfile.write(body)
313+
body = body[done:]
313314

314315
def do_POST(self):
315316
content_length = self.headers["Content-Length"]
@@ -501,6 +502,25 @@ def test_bad_address(self):
501502
urllib.request.urlopen,
502503
"http://sadflkjsasf.i.nvali.d./")
503504

505+
def test_iteration(self):
506+
expected_response = b"pycon 2008..."
507+
handler = self.start_server([(200, [], expected_response)])
508+
data = urllib.request.urlopen("http://localhost:%s" % handler.port)
509+
for line in data:
510+
self.assertEqual(line, expected_response)
511+
512+
def test_line_iteration(self):
513+
lines = [b"We\n", b"got\n", b"here\n", b"verylong " * 8192 + b"\n"]
514+
expected_response = b"".join(lines)
515+
handler = self.start_server([(200, [], expected_response)])
516+
data = urllib.request.urlopen("http://localhost:%s" % handler.port)
517+
for index, line in enumerate(data):
518+
self.assertEqual(line, lines[index],
519+
"Fetched line number %s doesn't match expected:\n"
520+
" Expected length was %s, got %s" %
521+
(index, len(lines[index]), len(line)))
522+
self.assertEqual(index + 1, len(lines))
523+
504524
def test_main():
505525
support.run_unittest(ProxyAuthTests, TestUrlopen)
506526

0 commit comments

Comments
 (0)