Skip to content

Commit

Permalink
Merge pull request #36 from brandonsalmon22/FixOneByte
Browse files Browse the repository at this point in the history
Fixed off by one error in GetRange.
  • Loading branch information
thobrla committed Jun 27, 2015
2 parents 77662a2 + db5efc2 commit c941c70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
20 changes: 14 additions & 6 deletions apitools/base/py/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,18 @@ def __ComputeEndByte(self, start, end=None, use_chunks=True):
end_byte = end
if use_chunks:
alternate = start + self.chunksize - 1
end_byte = min(end_byte, alternate) if end_byte else alternate
if end_byte is not None:
end_byte = min(end_byte, alternate)
else:
end_byte = alternate

if self.total_size:
alternate = self.total_size - 1
end_byte = min(end_byte, alternate) if end_byte else alternate
if end_byte is not None:
end_byte = min(end_byte, alternate)
else:
end_byte = alternate

return end_byte

def __GetChunk(self, start, end, additional_headers=None):
Expand Down Expand Up @@ -434,18 +442,18 @@ def GetRange(self, start, end=None, additional_headers=None,
self.EnsureInitialized()
progress_end_normalized = False
if self.total_size is not None:
progress, end = self.__NormalizeStartEnd(start, end)
progress, end_byte = self.__NormalizeStartEnd(start, end)
progress_end_normalized = True
else:
progress = start
while not progress_end_normalized or progress < end:
end_byte = self.__ComputeEndByte(progress, end=end,
while not progress_end_normalized or progress <= end_byte:
end_byte = self.__ComputeEndByte(progress, end=end_byte,
use_chunks=use_chunks)
response = self.__GetChunk(progress, end_byte,
additional_headers=additional_headers)
if not progress_end_normalized:
self.__SetTotal(response.info)
progress, end = self.__NormalizeStartEnd(start, end)
progress, end_byte = self.__NormalizeStartEnd(start, end)
progress_end_normalized = True
response = self.__ProcessResponse(response)
progress += response.length
Expand Down
30 changes: 30 additions & 0 deletions apitools/base/py/transfer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@ def testComputeEndByteSmallTotal(self):
download._Download__ComputeEndByte(start),
msg='Failed on start={0}'.format(start))

def testGetRange(self):
for (start_byte, end_byte) in [(0, 25), (5, 15), (0, 0), (25, 25)]:
bytes_http = object()
http = object()
download_stream = six.StringIO()
download = transfer.Download.FromStream(download_stream,
total_size=26,
auto_transfer=False)
download.bytes_http = bytes_http
base_url = 'https://part.one/'
with mock.patch.object(http_wrapper, 'MakeRequest',
autospec=True) as make_request:
make_request.return_value = http_wrapper.Response(
info={
'content-range': 'bytes %d-%d/26' %
(start_byte, end_byte),
'status': http_client.OK,
},
content=string.ascii_lowercase[start_byte:end_byte+1],
request_url=base_url,
)
request = http_wrapper.Request(url='https://part.one/')
download.InitializeDownload(request, http=http)
download.GetRange(start_byte, end_byte)
self.assertEqual(1, make_request.call_count)
received_request = make_request.call_args[0][1]
self.assertEqual(base_url, received_request.url)
self.assertRangeAndContentRangeCompatible(
received_request, make_request.return_value)

def testNonChunkedDownload(self):
bytes_http = object()
http = object()
Expand Down

0 comments on commit c941c70

Please sign in to comment.