Skip to content

Commit

Permalink
Fix issue where the stream was being read in StreamMedia() where it
Browse files Browse the repository at this point in the history
shouldn't have.
  • Loading branch information
kevinli7 authored and vilasj committed Dec 8, 2017
1 parent 6836d47 commit 7333bbf
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
7 changes: 1 addition & 6 deletions apitools/base/py/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,13 +982,8 @@ def __SendMediaBody(self, start, additional_headers=None):
if self.total_size is None:
raise exceptions.TransferInvalidError(
'Total size must be known for SendMediaBody')
# Change body_stream from a stream to a string object. This is
# because httpwrapper.MakeRequest doesn't handle the case where
# request.body is a stream. In the case that the body is a stream,
# if a request has to be retried, then the stream will be exhausted
# and the request will hang.
body_stream = stream_slice.StreamSlice(
self.stream, self.total_size - start).read()
self.stream, self.total_size - start)

request = http_wrapper.Request(url=self.url, http_method='PUT',
body=body_stream)
Expand Down
32 changes: 0 additions & 32 deletions apitools/base/py/transfer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,35 +531,3 @@ def testRetryRequestChunks(self):

# Ensure the mock was called the correct number of times.
self.assertEquals(make_request.call_count, len(responses))

def testRetryRequestMedia(self):
"""Test that StreamMedia will retry correctly."""
# Create and configure the upload object.
bytes_http = httplib2.Http()
upload = transfer.Upload(
stream=self.sample_stream,
mime_type='text/plain',
total_size=len(self.sample_data),
close_stream=False,
http=bytes_http)

upload.strategy = transfer.RESUMABLE_UPLOAD
# Set the chunk size so the entire stream is uploaded.
upload.chunksize = len(self.sample_data)
# Mock the upload to return the sample response.
with mock.patch.object(bytes_http,
'request') as make_request:
# This side effect also checks the request body.
responses = [
self.response, # Initial request in InitializeUpload().
self.fail_response, # 503 status code from server.
self.response # Successful request.
]
make_request.side_effect = self.HttpRequestSideEffect(responses)

# Initialization.
upload.InitializeUpload(self.request, bytes_http)
upload.StreamMedia()

# Ensure the mock was called the correct number of times..
self.assertEquals(make_request.call_count, len(responses))

0 comments on commit 7333bbf

Please sign in to comment.