transfer_test_16.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. def testMultipartCompressed(self):
  2. """Test that multipart uploads are compressed."""
  3. # Create the multipart configuration.
  4. upload_config = base_api.ApiUploadInfo(
  5. accept=['*/*'],
  6. max_size=None,
  7. simple_multipart=True,
  8. simple_path=u'/upload',)
  9. # Create the upload object.
  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. gzip_encoded=True)
  16. # Set a body to trigger multipart configuration.
  17. self.request.body = '{"body_field_one": 7}'
  18. # Configure the request.
  19. upload.ConfigureRequest(upload_config, self.request, self.url_builder)
  20. # Ensure the request is a multipart request now.
  21. self.assertEqual(
  22. self.url_builder.query_params['uploadType'], 'multipart')
  23. # Ensure the request is gzip encoded.
  24. self.assertEqual(self.request.headers['Content-Encoding'], 'gzip')
  25. # Ensure data is compressed
  26. self.assertLess(len(self.request.body), len(self.sample_data))
  27. # Ensure uncompressed data includes the sample data.
  28. with gzip.GzipFile(fileobj=self.request.body) as f:
  29. original = f.read()
  30. self.assertTrue(self.sample_data in original)