transfer_test_19.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. def testRetryRequestChunks(self):
  2. """Test that StreamInChunks will retry correctly."""
  3. refresh_response = http_wrapper.Response(
  4. info={'status': http_wrapper.RESUME_INCOMPLETE,
  5. 'location': 'http://www.uploads.com'},
  6. content='',
  7. request_url='http://www.uploads.com',)
  8. # Create and configure the upload object.
  9. bytes_http = httplib2.Http()
  10. upload = transfer.Upload(
  11. stream=self.sample_stream,
  12. mime_type='text/plain',
  13. total_size=len(self.sample_data),
  14. close_stream=False,
  15. http=bytes_http)
  16. upload.strategy = transfer.RESUMABLE_UPLOAD
  17. # Set the chunk size so the entire stream is uploaded.
  18. upload.chunksize = len(self.sample_data)
  19. # Mock the upload to return the sample response.
  20. with mock.patch.object(bytes_http,
  21. 'request') as make_request:
  22. # This side effect also checks the request body.
  23. responses = [
  24. self.response, # Initial request in InitializeUpload().
  25. self.fail_response, # 503 status code from server.
  26. refresh_response, # Refresh upload progress.
  27. self.response, # Successful request.
  28. ]
  29. make_request.side_effect = self.HttpRequestSideEffect(responses)
  30. # Initialization.
  31. upload.InitializeUpload(self.request, bytes_http)
  32. upload.StreamInChunks()
  33. # Ensure the mock was called the correct number of times.
  34. self.assertEquals(make_request.call_count, len(responses))