def test_range_syntax(self): """Test the Content-Range scanning.""" f = response.RangeFile('foo', StringIO()) def ok(expected, header_value): f.set_range_from_header(header_value) # Slightly peek under the covers to get the size self.assertEquals(expected, (f.tell(), f._size)) ok((1, 10), 'bytes 1-10/11') ok((1, 10), 'bytes 1-10/*') ok((12, 2), '\tbytes 12-13/*') ok((28, 1), ' bytes 28-28/*') ok((2123, 2120), 'bytes 2123-4242/12310') ok((1, 10), 'bytes 1-10/ttt') # We don't check total (ttt) def nok(header_value): self.assertRaises(errors.InvalidHttpRange, f.set_range_from_header, header_value) nok('bytes 10-2/3') nok('chars 1-2/3') nok('bytes xx-yyy/zzz') nok('bytes xx-12/zzz') nok('bytes 11-yy/zzz') nok('bytes10-2/3')
def test_seek_whence(self): """Test the seek whence parameter values.""" f = response.RangeFile('foo', StringIO('abc')) f.set_range(0, 3) f.seek(0) f.seek(1, 1) f.seek(-1, 2) self.assertRaises(ValueError, f.seek, 0, 14)
def test_max_read_size(self): """Read data in blocks and verify that the reads are not larger than the maximum read size. """ # retrieve data in large blocks from response.RangeFile object mock_read_file = FakeReadFile(self.test_data) range_file = response.RangeFile('test_max_read_size', mock_read_file) response_data = range_file.read(self.test_data_len) # verify read size was equal to the maximum read size self.assertTrue(mock_read_file.get_max_read_size() > 0) self.assertEqual(mock_read_file.get_max_read_size(), response.RangeFile._max_read_size) self.assertEqual(mock_read_file.get_read_count(), 3) # report error if the data wasn't equal (we only report the size due # to the length of the data) if response_data != self.test_data: message = "Data not equal. Expected %d bytes, received %d." self.fail(message % (len(response_data), self.test_data_len))
def setUp(self): super(TestRangeFileMultipleRanges, self).setUp() boundary = self.boundary content = '' self.first_range_start = 25 file_size = 200 # big enough to encompass all ranges for (start, part) in [ (self.first_range_start, self.alpha), # Two contiguous ranges (100, self.alpha), (126, self.alpha.upper()) ]: content += self._multipart_byterange(part, start, boundary, file_size) # Final boundary content += self._boundary_line() self._file = response.RangeFile('Multiple_ranges_file', StringIO(content)) self.set_file_boundary()
def setUp(self): super(TestRangeFileSingleRange, self).setUp() self._file = response.RangeFile('Single_range_file', StringIO(self.alpha)) self.first_range_start = 15 self._file.set_range(self.first_range_start, len(self.alpha))
def setUp(self): super(TestRangeFileSizeKnown, self).setUp() self._file = response.RangeFile('Whole_file_size_known', StringIO(self.alpha)) self._file.set_range(0, len(self.alpha)) self.first_range_start = 0 # It's the whole file
def setUp(self): super(TestRangeFileSizeUnknown, self).setUp() self._file = response.RangeFile('Whole_file_size_known', StringIO(self.alpha)) # We define no range, relying on RangeFile to provide default values self.first_range_start = 0 # It's the whole file