59
62
def makefile(self, mode='r', bufsize=None):
60
63
return self.readfile
62
66
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
64
68
def __init__(self, sock):
753
757
out.read() # Read the whole range
754
758
# Fail to find the boundary line
755
759
self.assertRaises(errors.InvalidHttpResponse, out.seek, 1, 1)
762
class TestRangeFileSizeReadLimited(tests.TestCase):
763
"""Test RangeFile _max_read_size functionality which limits the size of
764
read blocks to prevent MemoryError messages in socket.recv.
768
# create a test datablock larger than _max_read_size.
769
chunk_size = response.RangeFile._max_read_size
770
test_pattern = '0123456789ABCDEF'
771
self.test_data = test_pattern * (3 * chunk_size / len(test_pattern))
772
self.test_data_len = len(self.test_data)
774
def test_max_read_size(self):
775
"""Read data in blocks and verify that the reads are not larger than
776
the maximum read size.
778
# retrieve data in large blocks from response.RangeFile object
779
mock_read_file = FakeReadFile(self.test_data)
780
range_file = response.RangeFile('test_max_read_size', mock_read_file)
781
response_data = range_file.read(self.test_data_len)
783
# verify read size was equal to the maximum read size
784
self.assertTrue(mock_read_file.get_max_read_size() > 0)
785
self.assertEqual(mock_read_file.get_max_read_size(),
786
response.RangeFile._max_read_size)
787
self.assertEqual(mock_read_file.get_read_count(), 3)
789
# report error if the data wasn't equal (we only report the size due
790
# to the length of the data)
791
if response_data != self.test_data:
792
message = "Data not equal. Expected %d bytes, received %d."
793
self.fail(message % (len(response_data), self.test_data_len))