transfer_test_17.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. def testMediaCompressed(self):
  2. """Test that media uploads are compressed."""
  3. # Create the media 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. # Configure the request.
  17. upload.ConfigureRequest(upload_config, self.request, self.url_builder)
  18. # Ensure the request is a media request now.
  19. self.assertEqual(self.url_builder.query_params['uploadType'], 'media')
  20. # Ensure the request is gzip encoded.
  21. self.assertEqual(self.request.headers['Content-Encoding'], 'gzip')
  22. # Ensure data is compressed
  23. self.assertLess(len(self.request.body), len(self.sample_data))
  24. # Ensure uncompressed data includes the sample data.
  25. with gzip.GzipFile(fileobj=self.request.body) as f:
  26. original = f.read()
  27. self.assertTrue(self.sample_data in original)