transfer_test_13.py 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. def testStreamInChunksCompressed(self):
  2. """Test that StreamInChunks will handle compression correctly."""
  3. # Create and configure the upload object.
  4. upload = transfer.Upload(
  5. stream=self.sample_stream,
  6. mime_type='text/plain',
  7. total_size=len(self.sample_data),
  8. close_stream=False,
  9. gzip_encoded=True)
  10. upload.strategy = transfer.RESUMABLE_UPLOAD
  11. # Set the chunk size so the entire stream is uploaded.
  12. upload.chunksize = len(self.sample_data)
  13. # Mock the upload to return the sample response.
  14. with mock.patch.object(transfer.Upload,
  15. '_Upload__SendMediaRequest') as mock_result, \
  16. mock.patch.object(http_wrapper,
  17. 'MakeRequest') as make_request:
  18. mock_result.return_value = self.response
  19. make_request.return_value = self.response
  20. # Initialization.
  21. upload.InitializeUpload(self.request, 'http')
  22. upload.StreamInChunks()
  23. # Get the uploaded request and end position of the stream.
  24. (request, _), _ = mock_result.call_args_list[0]
  25. # Ensure the mock was called.
  26. self.assertTrue(mock_result.called)
  27. # Ensure the correct content encoding was set.
  28. self.assertEqual(request.headers['Content-Encoding'], 'gzip')
  29. # Ensure the stream was compresed.
  30. self.assertLess(len(request.body), len(self.sample_data))