@@ -341,8 +341,8 @@ def test_bad_status_repr(self):
341341 self .assertEqual (repr (exc ), '''BadStatusLine("\' \' ",)''' )
342342
343343 def test_partial_reads (self ):
344- # if we have a length, the system knows when to close itself
345- # same behaviour than when we read the whole thing with read()
344+ # if we have Content-Length, HTTPResponse knows when to close itself,
345+ # the same behaviour as when we read the whole thing with read()
346346 body = "HTTP/1.1 200 Ok\r \n Content-Length: 4\r \n \r \n Text"
347347 sock = FakeSocket (body )
348348 resp = client .HTTPResponse (sock )
@@ -355,9 +355,24 @@ def test_partial_reads(self):
355355 resp .close ()
356356 self .assertTrue (resp .closed )
357357
358+ def test_mixed_reads (self ):
359+ # readline() should update the remaining length, so that read() knows
360+ # how much data is left and does not raise IncompleteRead
361+ body = "HTTP/1.1 200 Ok\r \n Content-Length: 13\r \n \r \n Text\r \n Another"
362+ sock = FakeSocket (body )
363+ resp = client .HTTPResponse (sock )
364+ resp .begin ()
365+ self .assertEqual (resp .readline (), b'Text\r \n ' )
366+ self .assertFalse (resp .isclosed ())
367+ self .assertEqual (resp .read (), b'Another' )
368+ self .assertTrue (resp .isclosed ())
369+ self .assertFalse (resp .closed )
370+ resp .close ()
371+ self .assertTrue (resp .closed )
372+
358373 def test_partial_readintos (self ):
359- # if we have a length, the system knows when to close itself
360- # same behaviour than when we read the whole thing with read()
374+ # if we have Content-Length, HTTPResponse knows when to close itself,
375+ # the same behaviour as when we read the whole thing with read()
361376 body = "HTTP/1.1 200 Ok\r \n Content-Length: 4\r \n \r \n Text"
362377 sock = FakeSocket (body )
363378 resp = client .HTTPResponse (sock )
@@ -827,7 +842,7 @@ def test_chunked_trailers(self):
827842 resp .begin ()
828843 self .assertEqual (resp .read (), expected )
829844 # we should have reached the end of the file
830- self .assertEqual (sock .file .read (100 ), b"" ) #we read to the end
845+ self .assertEqual (sock .file .read (), b"" ) #we read to the end
831846 resp .close ()
832847
833848 def test_chunked_sync (self ):
@@ -839,19 +854,65 @@ def test_chunked_sync(self):
839854 resp .begin ()
840855 self .assertEqual (resp .read (), expected )
841856 # the file should now have our extradata ready to be read
842- self .assertEqual (sock .file .read (100 ), extradata .encode ("ascii" )) #we read to the end
857+ self .assertEqual (sock .file .read (), extradata .encode ("ascii" )) #we read to the end
843858 resp .close ()
844859
845860 def test_content_length_sync (self ):
846861 """Check that we don't read past the end of the Content-Length stream"""
847- extradata = "extradata"
862+ extradata = b"extradata"
863+ expected = b"Hello123\r \n "
864+ sock = FakeSocket (b'HTTP/1.1 200 OK\r \n Content-Length: 10\r \n \r \n ' + expected + extradata )
865+ resp = client .HTTPResponse (sock , method = "GET" )
866+ resp .begin ()
867+ self .assertEqual (resp .read (), expected )
868+ # the file should now have our extradata ready to be read
869+ self .assertEqual (sock .file .read (), extradata ) #we read to the end
870+ resp .close ()
871+
872+ def test_readlines_content_length (self ):
873+ extradata = b"extradata"
874+ expected = b"Hello123\r \n "
875+ sock = FakeSocket (b'HTTP/1.1 200 OK\r \n Content-Length: 10\r \n \r \n ' + expected + extradata )
876+ resp = client .HTTPResponse (sock , method = "GET" )
877+ resp .begin ()
878+ self .assertEqual (resp .readlines (2000 ), [expected ])
879+ # the file should now have our extradata ready to be read
880+ self .assertEqual (sock .file .read (), extradata ) #we read to the end
881+ resp .close ()
882+
883+ def test_read1_content_length (self ):
884+ extradata = b"extradata"
885+ expected = b"Hello123\r \n "
886+ sock = FakeSocket (b'HTTP/1.1 200 OK\r \n Content-Length: 10\r \n \r \n ' + expected + extradata )
887+ resp = client .HTTPResponse (sock , method = "GET" )
888+ resp .begin ()
889+ self .assertEqual (resp .read1 (2000 ), expected )
890+ # the file should now have our extradata ready to be read
891+ self .assertEqual (sock .file .read (), extradata ) #we read to the end
892+ resp .close ()
893+
894+ def test_readline_bound_content_length (self ):
895+ extradata = b"extradata"
896+ expected = b"Hello123\r \n "
897+ sock = FakeSocket (b'HTTP/1.1 200 OK\r \n Content-Length: 10\r \n \r \n ' + expected + extradata )
898+ resp = client .HTTPResponse (sock , method = "GET" )
899+ resp .begin ()
900+ self .assertEqual (resp .readline (10 ), expected )
901+ self .assertEqual (resp .readline (10 ), b"" )
902+ # the file should now have our extradata ready to be read
903+ self .assertEqual (sock .file .read (), extradata ) #we read to the end
904+ resp .close ()
905+
906+ def test_read1_bound_content_length (self ):
907+ extradata = b"extradata"
848908 expected = b"Hello123\r \n "
849- sock = FakeSocket ('HTTP/1.1 200 OK\r \n Content-Length: 10 \r \n \r \n Hello123 \r \n ' + extradata )
909+ sock = FakeSocket (b 'HTTP/1.1 200 OK\r \n Content-Length: 30 \r \n \r \n ' + expected * 3 + extradata )
850910 resp = client .HTTPResponse (sock , method = "GET" )
851911 resp .begin ()
912+ self .assertEqual (resp .read1 (20 ), expected * 2 )
852913 self .assertEqual (resp .read (), expected )
853914 # the file should now have our extradata ready to be read
854- self .assertEqual (sock .file .read (100 ), extradata . encode ( "ascii" ) ) #we read to the end
915+ self .assertEqual (sock .file .read (), extradata ) #we read to the end
855916 resp .close ()
856917
857918class ExtendedReadTest (TestCase ):
0 commit comments