@@ -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+
504524def test_main ():
505525 support .run_unittest (ProxyAuthTests , TestUrlopen )
506526
0 commit comments