transfer_test_20.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. def testStreamInChunks(self):
  2. """Test StreamInChunks."""
  3. resume_incomplete_responses = [http_wrapper.Response(
  4. info={'status': http_wrapper.RESUME_INCOMPLETE,
  5. 'location': 'http://www.uploads.com',
  6. 'range': '0-{}'.format(end)},
  7. content='',
  8. request_url='http://www.uploads.com',) for end in [199, 399, 599]]
  9. responses = [
  10. self.response # Initial request in InitializeUpload().
  11. ] + resume_incomplete_responses + [
  12. self.response, # Successful request.
  13. ]
  14. # Create and configure the upload object.
  15. bytes_http = httplib2.Http()
  16. upload = transfer.Upload(
  17. stream=self.sample_stream,
  18. mime_type='text/plain',
  19. total_size=len(self.sample_data),
  20. close_stream=False,
  21. http=bytes_http)
  22. upload.strategy = transfer.RESUMABLE_UPLOAD
  23. # Set the chunk size so the entire stream is uploaded.
  24. upload.chunksize = 200
  25. # Mock the upload to return the sample response.
  26. with mock.patch.object(bytes_http,
  27. 'request') as make_request:
  28. # This side effect also checks the request body.
  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))