Add "strict_mode" to the storage driver. When this mode is enabled (disabled by default), it will default to an old behavior when no content type is provided and none can't be automatically detected.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/ec90fcb8 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/ec90fcb8 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/ec90fcb8 Branch: refs/heads/trunk Commit: ec90fcb8a32f5e4a0330ffa6d7b932b65bc82077 Parents: 1f249f5 Author: Tomaz Muraus <[email protected]> Authored: Mon Dec 9 15:05:56 2013 +0100 Committer: Tomaz Muraus <[email protected]> Committed: Mon Dec 9 15:05:56 2013 +0100 ---------------------------------------------------------------------- libcloud/storage/base.py | 18 +++++++++++++----- libcloud/test/storage/test_base.py | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/ec90fcb8/libcloud/storage/base.py ---------------------------------------------------------------------- diff --git a/libcloud/storage/base.py b/libcloud/storage/base.py index 5b441a6..cbfff9c 100644 --- a/libcloud/storage/base.py +++ b/libcloud/storage/base.py @@ -44,8 +44,8 @@ __all__ = [ CHUNK_SIZE = 8096 -# Default Content-Type which is sent when upload an object if one is not -# supplied and can't be detected +# Default Content-Type which is sent when uploading an object if one is not +# supplied and can't be detected when using non-strict mode. DEFAULT_CONTENT_TYPE = 'application/octet-stream' @@ -186,6 +186,10 @@ class StorageDriver(BaseDriver): hash_type = 'md5' supports_chunked_encoding = False + # When strict mode is used, exception will be thrown if no content type is + # provided and none can be detected when uploading an object + strict_mode = False + def __init__(self, key, secret=None, secure=True, host=None, port=None, **kwargs): super(StorageDriver, self).__init__(key=key, secret=secret, @@ -605,9 +609,13 @@ class StorageDriver(BaseDriver): content_type, _ = libcloud.utils.files.guess_file_mime_type(name) if not content_type: - # Fallback to a content-type that will cause most browsers to - # download it again as a binary file. - content_type = DEFAULT_CONTENT_TYPE + if self.strict_mode: + raise AttributeError('File content-type could not be ' + 'guessed and no content_type value ' + 'is provided') + else: + # Fallback to a content-type + content_type = DEFAULT_CONTENT_TYPE file_size = None http://git-wip-us.apache.org/repos/asf/libcloud/blob/ec90fcb8/libcloud/test/storage/test_base.py ---------------------------------------------------------------------- diff --git a/libcloud/test/storage/test_base.py b/libcloud/test/storage/test_base.py index 9e4bd2d..28ad528 100644 --- a/libcloud/test/storage/test_base.py +++ b/libcloud/test/storage/test_base.py @@ -43,6 +43,9 @@ class BaseStorageTests(unittest.TestCase): self.driver2 = StorageDriver('username', 'key', host='localhost') self.driver2.supports_chunked_encoding = False + self.driver1.strict_mode = False + self.driver1.strict_mode = False + def test__upload_object_iterator_must_have_next_method(self): class Iterator(object): @@ -152,12 +155,13 @@ class BaseStorageTests(unittest.TestCase): else: self.fail('Invalid hash type but exception was not thrown') - def test_upload_default_content_type_is_specified_when_not_supplied(self): + def test_upload_no_content_type_supplied_or_detected(self): iterator = StringIO() upload_func = Mock() upload_func.return_value = True, '', 0 + # strict_mode is disabled, default content type should be used self.driver1.connection = Mock() self.driver1._upload_object(object_name='test', @@ -170,5 +174,20 @@ class BaseStorageTests(unittest.TestCase): headers = self.driver1.connection.request.call_args[-1]['headers'] self.assertEqual(headers['Content-Type'], DEFAULT_CONTENT_TYPE) + # strict_mode is enabled, exception should be thrown + + self.driver1.strict_mode = True + expected_msg = ('File content-type could not be guessed and no' + ' content_type value is provided') + self.assertRaisesRegexp(AttributeError, expected_msg, + self.driver1._upload_object, + object_name='test', + content_type=None, + upload_func=upload_func, + upload_func_kwargs={}, + request_path='/', + iterator=iterator) + + if __name__ == '__main__': sys.exit(unittest.main())
