@@ -310,7 +310,7 @@ def send_head(self):
310310 self .send_response (response_code )
311311
312312 for (header , value ) in headers :
313- self .send_header (header , value % self .port )
313+ self .send_header (header , value % { 'port' : self .port } )
314314 if body :
315315 self .send_header ("Content-type" , "text/plain" )
316316 self .end_headers ()
@@ -341,10 +341,17 @@ def tearDown(self):
341341 self .server .stop ()
342342
343343 def urlopen (self , url , data = None ):
344+ l = []
344345 f = urllib .request .urlopen (url , data )
345- result = f .read ()
346- f .close ()
347- return result
346+ try :
347+ # Exercise various methods
348+ l .extend (f .readlines (200 ))
349+ l .append (f .readline ())
350+ l .append (f .read (1024 ))
351+ l .append (f .read ())
352+ finally :
353+ f .close ()
354+ return b"" .join (l )
348355
349356 def start_server (self , responses = None ):
350357 if responses is None :
@@ -361,7 +368,8 @@ def start_server(self, responses=None):
361368 def test_redirection (self ):
362369 expected_response = b"We got here..."
363370 responses = [
364- (302 , [("Location" , "http://localhost:%s/somewhere_else" )], "" ),
371+ (302 , [("Location" , "http://localhost:%(port)s/somewhere_else" )],
372+ "" ),
365373 (200 , [], expected_response )
366374 ]
367375
@@ -370,6 +378,20 @@ def test_redirection(self):
370378 self .assertEquals (data , expected_response )
371379 self .assertEquals (handler .requests , ["/" , "/somewhere_else" ])
372380
381+ def test_chunked (self ):
382+ expected_response = b"hello world"
383+ chunked_start = (
384+ b'a\r \n '
385+ b'hello worl\r \n '
386+ b'1\r \n '
387+ b'd\r \n '
388+ b'0\r \n '
389+ )
390+ response = [(200 , [("Transfer-Encoding" , "chunked" )], chunked_start )]
391+ handler = self .start_server (response )
392+ data = self .urlopen ("http://localhost:%s/" % handler .port )
393+ self .assertEquals (data , expected_response )
394+
373395 def test_404 (self ):
374396 expected_response = b"Bad bad bad..."
375397 handler = self .start_server ([(404 , [], expected_response )])
0 commit comments