class AzureBlobsMockRawResponse(MockRawResponse): fixtures = StorageFileFixtures('azure_blobs') def _foo_bar_container_foo_test_upload_INVALID_HASH( self, method, url, body, headers): body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['content-md5'] = 'd4fe4c9829f7ca1cc89db7ad670d2bbd' # test_upload_object_invalid_hash1 return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload(self, method, url, body, headers): # test_upload_object_success body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['content-md5'] = 'd4fe4c9829f7ca1cc89db7ad670d2bbd' return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_upload_object_invalid_file_size body = self._generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_INVALID_SIZE(self, method, url, body, headers): # test_upload_object_invalid_file_size body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class GoogleStorageMockHttp(S3MockHttp): fixtures = StorageFileFixtures('google_storage') def _test2_test_get_object(self, method, url, body, headers): # test_get_object # Google uses a different HTTP header prefix for meta data body = self.fixtures.load('list_containers.xml') headers = { 'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-goog-meta-rabbits': 'monkeys', 'content-length': '12345', 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return httplib.OK, body, headers, httplib.responses[httplib.OK] def _test2_test_cont_length_get_object(self, method, url, body, headers): # test_get_object_object_size_not_in_content_length_header # Google uses a different HTTP header prefix for meta data body = self.fixtures.load('list_containers.xml') headers = { 'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-goog-meta-rabbits': 'monkeys', 'x-goog-stored-content-length': '9587', 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return httplib.OK, body, headers, httplib.responses[httplib.OK] def _container_path_UNAUTHORIZED(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK])
class CloudFilesMockRawResponse(MockRawResponse): fixtures = StorageFileFixtures('cloudfiles') base_headers = {'content-type': 'application/json; charset=UTF-8'} def _v1_MossoCloudFS_foo_bar_container_foo_test_upload( self, method, url, body, headers): # test_object_upload_success body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_test_upload_INVALID_HASH( self, method, url, body, headers): # test_object_upload_invalid_hash body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'foobar' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object( self, method, url, body, headers): # test_download_object_success body = 'test' self._data = self._generate_random_data(1000) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_INVALID_SIZE( self, method, url, body, headers): # test_download_object_invalid_file_size body = 'test' self._data = self._generate_random_data(100) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_NOT_FOUND( self, method, url, body, headers): body = '' return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_test_stream_data( self, method, url, body, headers): # test_upload_object_via_stream_success headers = {} headers.update(self.base_headers) headers['etag'] = '577ef1154f3240ad5b9b413aa7346a1e' body = 'test' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK])
class AzuriteBlobsMockHttp(AzureBlobsMockHttp): fixtures = StorageFileFixtures('azurite_blobs') def _get_method_name(self, *args, **kwargs): method_name = super(AzuriteBlobsMockHttp, self).\ _get_method_name(*args, **kwargs) if method_name.startswith('_account'): method_name = method_name[8:] return method_name
class AtmosMockRawResponse(MockRawResponse): fixtures = StorageFileFixtures('atmos') def _rest_namespace_foo_bar_container_foo_bar_object(self, method, url, body, headers): body = 'test' self._data = self._generate_random_data(1000) return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _rest_namespace_foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) def _rest_namespace_fbc_ftu(self, method, url, body, headers): return (httplib.CREATED, '', {}, httplib.responses[httplib.CREATED])
class GoogleStorageMockHttp(S3MockHttp): fixtures = StorageFileFixtures("google_storage") def _test2_test_get_object(self, method, url, body, headers): # test_get_object # Google uses a different HTTP header prefix for meta data body = self.fixtures.load("list_containers.xml") headers = { "content-type": "application/zip", "etag": '"e31208wqsdoj329jd"', "x-goog-meta-rabbits": "monkeys", "content-length": "12345", "last-modified": "Thu, 13 Sep 2012 07:13:22 GMT", } return httplib.OK, body, headers, httplib.responses[httplib.OK] def _test2_test_cont_length_get_object(self, method, url, body, headers): # test_get_object_object_size_not_in_content_length_header # Google uses a different HTTP header prefix for meta data body = self.fixtures.load("list_containers.xml") headers = { "content-type": "application/zip", "etag": '"e31208wqsdoj329jd"', "x-goog-meta-rabbits": "monkeys", "x-goog-stored-content-length": "9587", "last-modified": "Thu, 13 Sep 2012 07:13:22 GMT", } return httplib.OK, body, headers, httplib.responses[httplib.OK] def _container_path_UNAUTHORIZED(self, method, url, body, headers): return ( httplib.UNAUTHORIZED, "", self.base_headers, httplib.responses[httplib.OK], )
class S3MockHttp(MockHttp): fixtures = StorageFileFixtures('s3') base_headers = {} def _UNAUTHORIZED(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK]) def _DIFFERENT_REGION(self, method, url, body, headers): return (httplib.MOVED_PERMANENTLY, '', self.base_headers, httplib.responses[httplib.OK]) def _list_containers_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers_TOKEN(self, method, url, body, headers): if 'x-amz-security-token' in headers: assert headers['x-amz-security-token'] == 'asdf' body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_ITERATOR(self, method, url, body, headers): if url.find('3.zip') == -1: # First part of the response (first 3 objects) file_name = 'list_container_objects_not_exhausted1.xml' else: file_name = 'list_container_objects_not_exhausted2.xml' body = self.fixtures.load(file_name) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test2_get_object(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test2_test_get_object(self, method, url, body, headers): # test_get_object body = self.fixtures.load('list_containers.xml') headers = {'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-amz-meta-rabbits': 'monkeys', 'content-length': '12345', 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _new_container_INVALID_NAME(self, method, url, body, headers): # test_create_container return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _new_container_ALREADY_EXISTS(self, method, url, body, headers): # test_create_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _new_container(self, method, url, body, headers): # test_create_container, test_delete_container if method == 'PUT': status = httplib.OK elif method == 'DELETE': status = httplib.NO_CONTENT return (status, body, headers, httplib.responses[httplib.OK]) def _new_container_DOESNT_EXIST(self, method, url, body, headers): # test_delete_container return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _new_container_NOT_EMPTY(self, method, url, body, headers): # test_delete_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _test1_get_container(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _container1_get_container(self, method, url, body, headers): return (httplib.NOT_FOUND, '', self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _test_inexistent_get_object(self, method, url, body, headers): return (httplib.NOT_FOUND, '', self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container(self, method, url, body, headers): # test_delete_container return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_NOT_FOUND(self, method, url, body, headers): # test_delete_container_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_DELETE(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data(self, method, url, body, headers): # test_upload_object_via_stream body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data_MULTIPART(self, method, url, body, headers): if method == 'POST': if 'uploadId' in url: # Complete multipart request body = self.fixtures.load('complete_multipart.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) else: # Initiate multipart request body = self.fixtures.load('initiate_multipart.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) elif method == 'DELETE': # Abort multipart request return (httplib.NO_CONTENT, '', headers, httplib.responses[httplib.NO_CONTENT]) else: # Upload chunk multipart request headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _foo_bar_container_LIST_MULTIPART(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'key-marker' not in query: body = self.fixtures.load('list_multipart_1.xml') else: body = self.fixtures.load('list_multipart_2.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_my_divisor_LIST_MULTIPART(self, method, url, body, headers): body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT]) def _foo_bar_container_my_movie_m2ts_LIST_MULTIPART(self, method, url, body, headers): body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT]) def parse_body(self): if len(self.body) == 0 and not self.parse_zero_length_body: return self.body try: try: body = ET.XML(self.body) except ValueError: # lxml wants a bytes and tests are basically hard-coded to str body = ET.XML(self.body.encode('utf-8')) except: raise MalformedResponseError("Failed to parse XML", body=self.body, driver=self.connection.driver) return body def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_download_object_success body = generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_NO_BUFFER(self, method, url, body, headers): # test_download_object_data_is_not_buffered_in_memory body = generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload_INVALID_HASH1(self, method, url, body, headers): body = '' headers = {} headers['etag'] = '"foobar"' # test_upload_object_invalid_hash1 return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload_INVALID_HASH2(self, method, url, body, headers): # test_upload_object_invalid_hash2 body = '' headers = {'etag': '"hash343hhash89h932439jsaa89"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload(self, method, url, body, headers): # test_upload_object_success body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_INVALID_SIZE(self, method, url, body, headers): # test_upload_object_invalid_file_size body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class BackblazeB2MockHttp(MockHttp): fixtures = StorageFileFixtures('backblaze_b2') def _b2api_v1_b2_authorize_account(self, method, url, body, headers): if method == 'GET': body = json.dumps({ 'accountId': 'test', 'apiUrl': 'test', 'downloadUrl': 'test', 'authorizationToken': 'test' }) else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_buckets(self, method, url, body, headers): if method == 'GET': body = self.fixtures.load('b2_list_buckets.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_file_names(self, method, url, body, headers): if method == 'GET': body = self.fixtures.load('b2_list_file_names.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_create_bucket(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_create_bucket.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_delete_bucket(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_delete_bucket.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_delete_file_version(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_delete_file_version.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_get_upload_url(self, method, url, body, headers): # test_upload_object if method == 'GET': body = self.fixtures.load('b2_get_upload_url.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_upload_file_abcd_defg(self, method, url, body, headers): # test_upload_object if method == 'POST': body = self.fixtures.load('b2_upload_file.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_file_versions(self, method, url, body, headers): if method == 'GET': body = self.fixtures.load('b2_list_file_versions.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_hide_file(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_hide_file.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _file_test00001_2_txt(self, method, url, body, headers): # test_download_object if method == 'GET': body = 'ab' else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK])
class CloudFilesMockHttp(StorageMockHttp, MockHttpTestCase): fixtures = StorageFileFixtures('cloudfiles') base_headers = {'content-type': 'application/json; charset=UTF-8'} # fake auth token response def _v2_0_tokens(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('_v2_0__auth.json') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_MALFORMED_JSON(self, method, url, body, headers): # test_invalid_json_throws_exception body = 'broken: json /*"' return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_EMPTY(self, method, url, body, headers): return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'GET': # list_containers body = self.fixtures.load('list_containers.json') status_code = httplib.OK elif method == 'HEAD': # get_meta_data body = self.fixtures.load('meta_data.json') status_code = httplib.NO_CONTENT headers.update({ 'x-account-container-count': 10, 'x-account-object-count': 400, 'x-account-bytes-used': 1234567 }) elif method == 'POST': body = '' status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_not_found(self, method, url, body, headers): # test_get_object_not_found if method == 'HEAD': body = '' else: raise ValueError('Invalid method') return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.json') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'GET': # list_container_objects if url.find('marker') == -1: body = self.fixtures.load('list_container_objects.json') status_code = httplib.OK else: body = '' status_code = httplib.NO_CONTENT elif method == 'HEAD': # get_container body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({ 'x-container-object-count': 800, 'x-container-bytes-used': 1234568 }) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_ITERATOR(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) # list_container_objects if url.find('foo-test-3') != -1: body = self.fixtures.load( 'list_container_objects_not_exhausted2.json') status_code = httplib.OK elif url.find('foo-test-5') != -1: body = '' status_code = httplib.NO_CONTENT else: # First request body = self.fixtures.load( 'list_container_objects_not_exhausted1.json') status_code = httplib.OK return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_not_found(self, method, url, body, headers): # test_get_container_not_found if method == 'HEAD': body = '' else: raise ValueError('Invalid method') return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_test_object(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'HEAD': # get_object body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({ 'content-length': 555, 'last-modified': 'Tue, 25 Jan 2011 22:01:49 GMT', 'etag': '6b21c4a111ac178feacf9ec9d0c71f17', 'x-object-meta-foo-bar': 'test 1', 'x-object-meta-bar-foo': 'test 2', 'content-type': 'application/zip' }) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container__7E_test_object( self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'HEAD': # get_object_name_encoding body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({ 'content-length': 555, 'last-modified': 'Tue, 25 Jan 2011 22:01:49 GMT', 'etag': '6b21c4a111ac178feacf9ec9d0c71f17', 'x-object-meta-foo-bar': 'test 1', 'x-object-meta-bar-foo': 'test 2', 'content-type': 'application/zip' }) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_create_container(self, method, url, body, headers): # test_create_container_success headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers = copy.deepcopy(self.base_headers) headers.update({ 'content-length': 18, 'date': 'Mon, 28 Feb 2011 07:52:57 GMT' }) status_code = httplib.CREATED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_speci_40l_name(self, method, url, body, headers): # test_create_container_put_object_name_encoding # Verify that the name is properly url encoded container_name = 'speci@l_name' encoded_container_name = urlquote(container_name) self.assertTrue(encoded_container_name in url) headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers = copy.deepcopy(self.base_headers) headers.update({ 'content-length': 18, 'date': 'Mon, 28 Feb 2011 07:52:57 GMT' }) status_code = httplib.CREATED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_create_container_ALREADY_EXISTS( self, method, url, body, headers): # test_create_container_already_exists headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers.update({'content-type': 'text/plain'}) status_code = httplib.ACCEPTED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NO_CONTENT elif method == 'POST': # test_ex_enable_static_website body = '' headers = self.base_headers status_code = httplib.ACCEPTED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_object_PURGE_SUCCESS( self, method, url, body, headers): if method == 'DELETE': # test_ex_purge_from_cdn headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_object_PURGE_SUCCESS_EMAIL( self, method, url, body, headers): if method == 'DELETE': # test_ex_purge_from_cdn_with_email self.assertEqual(headers['X-Purge-Email'], '*****@*****.**') headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_NOT_FOUND(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_not_found body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NOT_FOUND return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_NOT_EMPTY(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_not_empty body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.CONFLICT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object( self, method, url, body, headers): if method == 'DELETE': # test_delete_object_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_NOT_FOUND( self, method, url, body, headers): if method == 'DELETE': # test_delete_object_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NOT_FOUND return (status_code, body, headers, httplib.responses[httplib.OK])
class OSSMockHttp(MockHttp, unittest.TestCase): fixtures = StorageFileFixtures("oss") base_headers = {} def _unauthorized(self, method, url, body, headers): return ( httplib.UNAUTHORIZED, "", self.base_headers, httplib.responses[httplib.OK], ) def _list_containers_empty(self, method, url, body, headers): body = self.fixtures.load("list_containers_empty.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load("list_containers.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_empty(self, method, url, body, headers): body = self.fixtures.load("list_container_objects_empty.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects(self, method, url, body, headers): body = self.fixtures.load("list_container_objects.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_chinese(self, method, url, body, headers): body = self.fixtures.load("list_container_objects_chinese.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_prefix(self, method, url, body, headers): params = {"prefix": self.test.prefix} self.assertUrlContainsQueryParams(url, params) body = self.fixtures.load("list_container_objects_prefix.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _get_container(self, method, url, body, headers): return self._list_containers(method, url, body, headers) def _get_object(self, method, url, body, headers): return self._list_containers(method, url, body, headers) def _notexisted_get_object(self, method, url, body, headers): return ( httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.NOT_FOUND], ) def _test_get_object(self, method, url, body, headers): self.base_headers.update({ "accept-ranges": "bytes", "connection": "keep-alive", "content-length": "0", "content-type": "application/octet-stream", "date": "Sat, 16 Jan 2016 15:38:14 GMT", "etag": '"D41D8CD98F00B204E9800998ECF8427E"', "last-modified": "Fri, 15 Jan 2016 14:43:15 GMT", "server": "AliyunOSS", "x-oss-object-type": "Normal", "x-oss-request-id": "569A63E6257784731E3D877F", "x-oss-meta-rabbits": "monkeys", }) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _invalid_name(self, method, url, body, headers): # test_create_container_bad_request return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _already_exists(self, method, url, body, headers): # test_create_container_already_existed return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _create_container(self, method, url, body, headers): # test_create_container_success self.assertEqual("PUT", method) self.assertEqual("", body) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _create_container_location(self, method, url, body, headers): # test_create_container_success self.assertEqual("PUT", method) location_constraint = ("<CreateBucketConfiguration>" "<LocationConstraint>%s</LocationConstraint>" "</CreateBucketConfiguration>" % self.test.ex_location) self.assertEqual(location_constraint, body) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _delete_container_doesnt_exist(self, method, url, body, headers): # test_delete_container_doesnt_exist return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _delete_container_not_empty(self, method, url, body, headers): # test_delete_container_not_empty return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _delete_container(self, method, url, body, headers): return ( httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.NO_CONTENT], ) def _foo_bar_object_not_found(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object_delete(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _list_multipart(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if "key-marker" not in query: body = self.fixtures.load("ex_iterate_multipart_uploads_p1.xml") else: body = self.fixtures.load("ex_iterate_multipart_uploads_p2.xml") return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object(self, method, url, body, headers): # test_download_object_success body = generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object_invalid_size(self, method, url, body, headers): # test_upload_object_invalid_file_size body = "" return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_test_stream_data_multipart(self, method, url, body, headers): headers = {} body = "" headers = {"etag": '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class S3MockHttp(StorageMockHttp): fixtures = StorageFileFixtures('s3') base_headers = {} def _UNAUTHORIZED(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK]) def _DIFFERENT_REGION(self, method, url, body, headers): return (httplib.MOVED_PERMANENTLY, '', self.base_headers, httplib.responses[httplib.OK]) def _list_containers_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_ITERATOR(self, method, url, body, headers): if url.find('3.zip') == -1: # First part of the response (first 3 objects) file_name = 'list_container_objects_not_exhausted1.xml' else: file_name = 'list_container_objects_not_exhausted2.xml' body = self.fixtures.load(file_name) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test2_test_list_containers(self, method, url, body, headers): # test_get_object body = self.fixtures.load('list_containers.xml') headers = {'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-amz-meta-rabbits': 'monkeys', 'content-length': 12345, 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _new_container_INVALID_NAME(self, method, url, body, headers): # test_create_container return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _new_container_ALREADY_EXISTS(self, method, url, body, headers): # test_create_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _new_container(self, method, url, body, headers): # test_create_container, test_delete_container if method == 'PUT': status = httplib.OK elif method == 'DELETE': status = httplib.NO_CONTENT return (status, body, headers, httplib.responses[httplib.OK]) def _new_container_DOESNT_EXIST(self, method, url, body, headers): # test_delete_container return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _new_container_NOT_EMPTY(self, method, url, body, headers): # test_delete_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container(self, method, url, body, headers): # test_delete_container return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_NOT_FOUND(self, method, url, body, headers): # test_delete_container_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK])
class S3MockRawResponse(MockRawResponse): fixtures = StorageFileFixtures('s3') def parse_body(self): if len(self.body) == 0 and not self.parse_zero_length_body: return self.body try: body = ET.XML(self.body) except: raise MalformedResponseError("Failed to parse XML", body=self.body, driver=self.connection.driver) return body def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_download_object_success body = self._generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload_INVALID_HASH1( self, method, url, body, headers): body = '' headers = {} headers['etag'] = '"foobar"' # test_upload_object_invalid_hash1 return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload_INVALID_HASH2( self, method, url, body, headers): # test_upload_object_invalid_hash2 body = '' headers = {'etag': '"hash343hhash89h932439jsaa89"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload(self, method, url, body, headers): # test_upload_object_success body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_upload_object_invalid_file_size body = self._generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_INVALID_SIZE(self, method, url, body, headers): # test_upload_object_invalid_file_size body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data(self, method, url, body, headers): # test_upload_object_via_stream body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data_MULTIPART( self, method, url, body, headers): headers = {} # POST is done for initiating multipart upload if method == 'POST': body = self.fixtures.load('initiate_multipart.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) else: body = '' return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.BAD_REQUEST])
class GoogleStorageJSONMockHttp(StorageMockHttp): """ Extracts bucket and object out of requests and routes to methods of the forms (bucket, object, entity, and type are sanitized values {'-', '.', '/' are replaced with '_'}): _<bucket>[_<type>] _<bucket>_acl[_entity][_<type>] _<bucket>_defaultObjectAcl[_<entity>][_<type>] _<bucket>_<object>[_<type>] _<bucket>_<object>_acl[_<entity>][_<type>] Ugly example: /storage/v1/b/test-bucket/o/test-object/acl/test-entity with type='FOO' yields _test_bucket_test_object_acl_test_entity_FOO """ fixtures = StorageFileFixtures("google_storage") base_headers = {} # Path regex captures bucket, object, defaultObjectAcl, and acl values. path_rgx = re.compile(r"/storage/[^/]+/b/([^/]+)" r"(?:/(defaultObjectAcl(?:/[^/]+)?$)|" r"(?:/o/(.+?))?(?:/(acl(?:/[^/]+)?))?$)") # Permissions to use when handling requests. bucket_perms = google_storage.ContainerPermissions.NONE object_perms = google_storage.ObjectPermissions.NONE _FORBIDDEN = _error_helper(httplib.FORBIDDEN, base_headers) _NOT_FOUND = _error_helper(httplib.NOT_FOUND, base_headers) _PRECONDITION_FAILED = _error_helper(httplib.PRECONDITION_FAILED, base_headers) def _get_method_name(self, type, use_param, qs, path): match = self.path_rgx.match(path) if not match: raise ValueError("%s is not a valid path." % path) joined_groups = "_".join([g for g in match.groups() if g]) if type: meth_name = "_%s_%s" % (joined_groups, type) else: meth_name = "_%s" % joined_groups # Return sanitized method name. return meth_name.replace("/", "_").replace(".", "_").replace("-", "_") def _response_helper(self, fixture): body = self.fixtures.load(fixture) return httplib.OK, body, {}, httplib.responses[httplib.OK] #################### # Request handlers # #################### def _test_bucket(self, method, url, body, headers): """Bucket request.""" if method != "GET": raise NotImplementedError("%s is not implemented." % method) if self.bucket_perms < google_storage.ContainerPermissions.READER: return self._FORBIDDEN else: return self._response_helper("get_container.json") def _test_bucket_acl(self, method, url, body, headers): """Bucket list ACL request.""" if method != "GET": raise NotImplementedError("%s is not implemented." % method) if self.bucket_perms < google_storage.ContainerPermissions.OWNER: return self._FORBIDDEN else: return self._response_helper("list_container_acl.json") def _test_bucket_test_object(self, method, url, body, headers): """Object request.""" if method != "GET": raise NotImplementedError("%s is not implemented." % method) if self.object_perms < google_storage.ObjectPermissions.READER: return self._FORBIDDEN else: return self._response_helper("get_object.json") def _test_bucket_test_object_acl(self, method, url, body, headers): """Object list ACL request.""" if method != "GET": raise NotImplementedError("%s is not implemented." % method) if self.object_perms < google_storage.ObjectPermissions.OWNER: return self._FORBIDDEN else: return self._response_helper("list_object_acl.json") def _test_bucket_writecheck(self, method, url, body, headers): gen_match = headers.get("x-goog-if-generation-match") if method != "DELETE" or gen_match != "0": msg = "Improper write check delete strategy. method: %s, " "headers: %s" % ( method, headers, ) raise ValueError(msg) if self.bucket_perms < google_storage.ContainerPermissions.WRITER: return self._FORBIDDEN else: return self._PRECONDITION_FAILED
class BackblazeB2MockHttp(StorageMockHttp, MockHttpTestCase): fixtures = StorageFileFixtures('backblaze_b2') def _b2api_v1_b2_list_buckets(self, method, url, body, headers): if method == 'GET': body = self.fixtures.load('b2_list_buckets.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_file_names(self, method, url, body, headers): if method == 'GET': body = self.fixtures.load('b2_list_file_names.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_create_bucket(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_create_bucket.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_delete_bucket(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_delete_bucket.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_delete_file_version(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_delete_file_version.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_get_upload_url(self, method, url, body, headers): # test_upload_object if method == 'GET': body = self.fixtures.load('b2_get_upload_url.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_upload_file_abcd_defg(self, method, url, body, headers): # test_upload_object if method == 'POST': body = self.fixtures.load('b2_upload_file.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_file_versions(self, method, url, body, headers): if method == 'GET': body = self.fixtures.load('b2_list_file_versions.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_hide_file(self, method, url, body, headers): if method == 'POST': body = self.fixtures.load('b2_hide_file.json') else: raise AssertionError('Unsupported method') return (httplib.OK, body, {}, httplib.responses[httplib.OK])
class OSSMockHttp(StorageMockHttp, MockHttpTestCase): fixtures = StorageFileFixtures('oss') base_headers = {} def _unauthorized(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK]) def _list_containers_empty(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_empty(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_chinese(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_chinese.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_prefix(self, method, url, body, headers): params = {'prefix': self.test.prefix} self.assertUrlContainsQueryParams(url, params) body = self.fixtures.load('list_container_objects_prefix.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _get_container(self, method, url, body, headers): return self._list_containers(method, url, body, headers) def _get_object(self, method, url, body, headers): return self._list_containers(method, url, body, headers) def _notexisted_get_object(self, method, url, body, headers): return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _test_get_object(self, method, url, body, headers): self.base_headers.update({ 'accept-ranges': 'bytes', 'connection': 'keep-alive', 'content-length': '0', 'content-type': 'application/octet-stream', 'date': 'Sat, 16 Jan 2016 15:38:14 GMT', 'etag': '"D41D8CD98F00B204E9800998ECF8427E"', 'last-modified': 'Fri, 15 Jan 2016 14:43:15 GMT', 'server': 'AliyunOSS', 'x-oss-object-type': 'Normal', 'x-oss-request-id': '569A63E6257784731E3D877F', 'x-oss-meta-rabbits': 'monkeys' }) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _invalid_name(self, method, url, body, headers): # test_create_container_bad_request return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _already_exists(self, method, url, body, headers): # test_create_container_already_existed return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _create_container(self, method, url, body, headers): # test_create_container_success self.assertEqual('PUT', method) self.assertEqual('', body) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _create_container_location(self, method, url, body, headers): # test_create_container_success self.assertEqual('PUT', method) location_constraint = ('<CreateBucketConfiguration>' '<LocationConstraint>%s</LocationConstraint>' '</CreateBucketConfiguration>' % self.test.ex_location) self.assertEqual(location_constraint, body) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _delete_container_doesnt_exist(self, method, url, body, headers): # test_delete_container_doesnt_exist return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _delete_container_not_empty(self, method, url, body, headers): # test_delete_container_not_empty return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _delete_container(self, method, url, body, headers): return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.NO_CONTENT]) def _foo_bar_object_not_found(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_test_stream_data_multipart(self, method, url, body, headers): headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} TEST_UPLOAD_ID = '0004B9894A22E5B1888A1E29F8236E2D' query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if not query.get('uploadId', False): self.fail('Request doesnt contain uploadId query parameter') upload_id = query['uploadId'][0] if upload_id != TEST_UPLOAD_ID: self.fail('first uploadId doesnt match') if method == 'PUT': # PUT is used for uploading the part. part number is mandatory if not query.get('partNumber', False): self.fail('Request is missing partNumber query parameter') body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) elif method == 'DELETE': # DELETE is done for aborting the upload body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT]) else: commit = ET.fromstring(body) count = 0 for part in commit.findall('Part'): count += 1 part_no = part.find('PartNumber').text etag = part.find('ETag').text self.assertEqual(part_no, str(count)) self.assertEqual(etag, headers['etag']) # Make sure that manifest contains at least one part self.assertTrue(count >= 1) body = self.fixtures.load('complete_multipart_upload.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _list_multipart(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'key-marker' not in query: body = self.fixtures.load('ex_iterate_multipart_uploads_p1.xml') else: body = self.fixtures.load('ex_iterate_multipart_uploads_p2.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class OSSMockRawResponse(MockRawResponse, MockHttpTestCase): fixtures = StorageFileFixtures('oss') def parse_body(self): if len(self.body) == 0 and not self.parse_zero_length_body: return self.body try: if PY3: parser = ET.XMLParser(encoding='utf-8') body = ET.XML(self.body.encode('utf-8'), parser=parser) else: body = ET.XML(self.body) except: raise MalformedResponseError("Failed to parse XML", body=self.body, driver=self.connection.driver) return body def _foo_bar_object(self, method, url, body, headers): # test_download_object_success body = self._generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object_invalid_size(self, method, url, body, headers): # test_upload_object_invalid_file_size body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object_not_found(self, method, url, body, headers): # test_upload_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_test_upload_invalid_hash1(self, method, url, body, headers): body = '' headers = {} headers['etag'] = '"foobar"' # test_upload_object_invalid_hash1 return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_test_upload(self, method, url, body, headers): # test_upload_object_success body = '' headers = {'etag': '"0CC175B9C0F1B6A831C399E269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_test_upload_acl(self, method, url, body, headers): # test_upload_object_with_acl body = '' headers = {'etag': '"0CC175B9C0F1B6A831C399E269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_test_stream_data(self, method, url, body, headers): # test_upload_object_via_stream body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_test_stream_data_multipart(self, method, url, body, headers): headers = {} # POST is done for initiating multipart upload if method == 'POST': body = self.fixtures.load('initiate_multipart_upload.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) else: body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class GoogleStorageMockHttp(S3MockHttp): fixtures = StorageFileFixtures('google_storage')
class OSSMockHttp(MockHttp, unittest.TestCase): fixtures = StorageFileFixtures('oss') base_headers = {} def _unauthorized(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK]) def _list_containers_empty(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_empty(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_chinese(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_chinese.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_container_objects_prefix(self, method, url, body, headers): params = {'prefix': self.test.prefix} self.assertUrlContainsQueryParams(url, params) body = self.fixtures.load('list_container_objects_prefix.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _get_container(self, method, url, body, headers): return self._list_containers(method, url, body, headers) def _get_object(self, method, url, body, headers): return self._list_containers(method, url, body, headers) def _notexisted_get_object(self, method, url, body, headers): return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _test_get_object(self, method, url, body, headers): self.base_headers.update( {'accept-ranges': 'bytes', 'connection': 'keep-alive', 'content-length': '0', 'content-type': 'application/octet-stream', 'date': 'Sat, 16 Jan 2016 15:38:14 GMT', 'etag': '"D41D8CD98F00B204E9800998ECF8427E"', 'last-modified': 'Fri, 15 Jan 2016 14:43:15 GMT', 'server': 'AliyunOSS', 'x-oss-object-type': 'Normal', 'x-oss-request-id': '569A63E6257784731E3D877F', 'x-oss-meta-rabbits': 'monkeys'}) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _invalid_name(self, method, url, body, headers): # test_create_container_bad_request return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _already_exists(self, method, url, body, headers): # test_create_container_already_existed return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _create_container(self, method, url, body, headers): # test_create_container_success self.assertEqual('PUT', method) self.assertEqual('', body) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _create_container_location(self, method, url, body, headers): # test_create_container_success self.assertEqual('PUT', method) location_constraint = ('<CreateBucketConfiguration>' '<LocationConstraint>%s</LocationConstraint>' '</CreateBucketConfiguration>' % self.test.ex_location) self.assertEqual(location_constraint, body) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _delete_container_doesnt_exist(self, method, url, body, headers): # test_delete_container_doesnt_exist return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _delete_container_not_empty(self, method, url, body, headers): # test_delete_container_not_empty return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _delete_container(self, method, url, body, headers): return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.NO_CONTENT]) def _foo_bar_object_not_found(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object_delete(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _list_multipart(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'key-marker' not in query: body = self.fixtures.load('ex_iterate_multipart_uploads_p1.xml') else: body = self.fixtures.load('ex_iterate_multipart_uploads_p2.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object(self, method, url, body, headers): # test_download_object_success body = generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_object_invalid_size(self, method, url, body, headers): # test_upload_object_invalid_file_size body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_test_stream_data_multipart(self, method, url, body, headers): headers = {} body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class CloudFilesMockHttp(MockHttp, unittest.TestCase): fixtures = StorageFileFixtures('cloudfiles') base_headers = {'content-type': 'application/json; charset=UTF-8'} # fake auth token response def _v2_0_tokens(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('_v2_0__auth.json') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_MALFORMED_JSON(self, method, url, body, headers): # test_invalid_json_throws_exception body = 'broken: json /*"' return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_EMPTY(self, method, url, body, headers): return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'GET': # list_containers body = self.fixtures.load('list_containers.json') status_code = httplib.OK elif method == 'HEAD': # get_meta_data body = self.fixtures.load('meta_data.json') status_code = httplib.NO_CONTENT headers.update({'x-account-container-count': '10', 'x-account-object-count': '400', 'x-account-bytes-used': '1234567' }) elif method == 'POST': body = '' status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_not_found(self, method, url, body, headers): # test_get_object_not_found if method == 'HEAD': body = '' else: raise ValueError('Invalid method') return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.json') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_20container_201_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.json') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'GET': # list_container_objects if url.find('marker') == -1: body = self.fixtures.load('list_container_objects.json') status_code = httplib.OK else: body = '' status_code = httplib.NO_CONTENT elif method == 'HEAD': # get_container body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({'x-container-object-count': '800', 'x-container-bytes-used': '1234568' }) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_ITERATOR(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) # list_container_objects if url.find('foo-test-3') != -1: body = self.fixtures.load( 'list_container_objects_not_exhausted2.json') status_code = httplib.OK elif url.find('foo-test-5') != -1: body = '' status_code = httplib.NO_CONTENT else: # First request body = self.fixtures.load( 'list_container_objects_not_exhausted1.json') status_code = httplib.OK return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_not_found( self, method, url, body, headers): # test_get_container_not_found if method == 'HEAD': body = '' else: raise ValueError('Invalid method') return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_test_object( self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'HEAD': # get_object body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({'content-length': '555', 'last-modified': 'Tue, 25 Jan 2011 22:01:49 GMT', 'etag': '6b21c4a111ac178feacf9ec9d0c71f17', 'x-object-meta-foo-bar': 'test 1', 'x-object-meta-bar-foo': 'test 2', 'content-type': 'application/zip'}) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container__7E_test_object( self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'HEAD': # get_object_name_encoding body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({'content-length': '555', 'last-modified': 'Tue, 25 Jan 2011 22:01:49 GMT', 'etag': '6b21c4a111ac178feacf9ec9d0c71f17', 'x-object-meta-foo-bar': 'test 1', 'x-object-meta-bar-foo': 'test 2', 'content-type': 'application/zip'}) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_create_container( self, method, url, body, headers): # test_create_container_success headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers = copy.deepcopy(self.base_headers) headers.update({'content-length': '18', 'date': 'Mon, 28 Feb 2011 07:52:57 GMT' }) status_code = httplib.CREATED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_speci_40l_name(self, method, url, body, headers): # test_create_container_put_object_name_encoding # Verify that the name is properly url encoded container_name = 'speci@l_name' encoded_container_name = urlquote(container_name) self.assertTrue(encoded_container_name in url) headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers = copy.deepcopy(self.base_headers) headers.update({'content-length': '18', 'date': 'Mon, 28 Feb 2011 07:52:57 GMT' }) status_code = httplib.CREATED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_create_container_ALREADY_EXISTS( self, method, url, body, headers): # test_create_container_already_exists headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers.update({'content-type': 'text/plain'}) status_code = httplib.ACCEPTED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NO_CONTENT elif method == 'POST': # test_ex_enable_static_website body = '' headers = self.base_headers status_code = httplib.ACCEPTED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_object_PURGE_SUCCESS( self, method, url, body, headers): if method == 'DELETE': # test_ex_purge_from_cdn headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_object_PURGE_SUCCESS_EMAIL( self, method, url, body, headers): if method == 'DELETE': # test_ex_purge_from_cdn_with_email self.assertEqual(headers['X-Purge-Email'], '*****@*****.**') headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_NOT_FOUND( self, method, url, body, headers): if method == 'DELETE': # test_delete_container_not_found body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NOT_FOUND return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_NOT_EMPTY( self, method, url, body, headers): if method == 'DELETE': # test_delete_container_not_empty body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.CONFLICT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object( self, method, url, body, headers): if method == 'DELETE': # test_delete_object_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) elif method == 'GET': body = generate_random_data(1000) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_py3_img_or_vid(self, method, url, body, headers): headers = {'etag': 'e2378cace8712661ce7beec3d9362ef6'} headers.update(self.base_headers) return httplib.CREATED, '', headers, httplib.responses[httplib.CREATED] def _v1_MossoCloudFS_foo_bar_container_foo_test_upload( self, method, url, body, headers): # test_object_upload_success body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_speci_40l_name_m_40obj_E2_82_ACct(self, method, url, body, headers): # test_create_container_put_object_name_encoding # Verify that the name is properly url encoded object_name = 'm@obj€ct' urlquote(object_name) headers = copy.deepcopy(self.base_headers) body = '' headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_empty(self, method, url, body, headers): # test_upload_object_zero_size_object body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_test_upload_INVALID_HASH( self, method, url, body, headers): # test_object_upload_invalid_hash body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'foobar' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_INVALID_SIZE( self, method, url, body, headers): # test_download_object_invalid_file_size body = generate_random_data(100) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_NOT_FOUND( self, method, url, body, headers): body = '' return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_test_stream_data( self, method, url, body, headers): # test_upload_object_via_stream_success headers = {} headers.update(self.base_headers) headers['etag'] = '577ef1154f3240ad5b9b413aa7346a1e' body = 'test' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK])
class CloudFilesMockHttp(StorageMockHttp): fixtures = StorageFileFixtures('cloudfiles') auth_fixtures = OpenStackFixtures() base_headers = {'content-type': 'application/json; charset=UTF-8'} # fake auth token response def _v1_0(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) headers.update({ 'x-server-management-url': 'https://servers.api.rackspacecloud.com/v1.0/slug', 'x-auth-token': 'FE011C19', 'x-cdn-management-url': 'https://cdn.clouddrive.com/v1/MossoCloudFS', 'x-storage-token': 'FE011C19', 'x-storage-url': 'https://storage4.clouddrive.com/v1/MossoCloudFS' }) return (httplib.NO_CONTENT, "", headers, httplib.responses[httplib.NO_CONTENT]) def _v1_MossoCloudFS_MALFORMED_JSON(self, method, url, body, headers): # test_invalid_json_throws_exception body = 'broken: json /*"' return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_EMPTY(self, method, url, body, headers): return (httplib.NO_CONTENT, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'GET': # list_containers body = self.fixtures.load('list_containers.json') status_code = httplib.OK elif method == 'HEAD': # get_meta_data body = self.fixtures.load('meta_data.json') status_code = httplib.NO_CONTENT headers.update({ 'x-account-container-count': 10, 'x-account-object-count': 400, 'x-account-bytes-used': 1234567 }) elif method == 'POST': body = '' status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_not_found(self, method, url, body, headers): # test_get_object_not_found if method == 'HEAD': body = '' else: raise ValueError('Invalid method') return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.json') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'GET': # list_container_objects if url.find('marker') == -1: body = self.fixtures.load('list_container_objects.json') status_code = httplib.OK else: body = '' status_code = httplib.NO_CONTENT elif method == 'HEAD': # get_container body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({ 'x-container-object-count': 800, 'x-container-bytes-used': 1234568 }) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_ITERATOR(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) # list_container_objects if url.find('foo-test-3') != -1: body = self.fixtures.load( 'list_container_objects_not_exhausted2.json') status_code = httplib.OK elif url.find('foo-test-5') != -1: body = '' status_code = httplib.NO_CONTENT else: # First request body = self.fixtures.load( 'list_container_objects_not_exhausted1.json') status_code = httplib.OK return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_not_found(self, method, url, body, headers): # test_get_container_not_found if method == 'HEAD': body = '' else: raise ValueError('Invalid method') return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_container_test_object(self, method, url, body, headers): headers = copy.deepcopy(self.base_headers) if method == 'HEAD': # get_object body = self.fixtures.load('list_container_objects_empty.json') status_code = httplib.NO_CONTENT headers.update({ 'content-length': 555, 'last-modified': 'Tue, 25 Jan 2011 22:01:49 GMT', 'etag': '6b21c4a111ac178feacf9ec9d0c71f17', 'x-object-meta-foo-bar': 'test 1', 'x-object-meta-bar-foo': 'test 2', 'content-type': 'application/zip' }) return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_create_container(self, method, url, body, headers): # test_create_container_success headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers = copy.deepcopy(self.base_headers) headers.update({ 'content-length': 18, 'date': 'Mon, 28 Feb 2011 07:52:57 GMT' }) status_code = httplib.CREATED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_test_create_container_ALREADY_EXISTS( self, method, url, body, headers): # test_create_container_already_exists headers = copy.deepcopy(self.base_headers) body = self.fixtures.load('list_container_objects_empty.json') headers.update({'content-type': 'text/plain'}) status_code = httplib.ACCEPTED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NO_CONTENT elif method == 'POST': # test_ex_enable_static_website body = '' headers = self.base_headers status_code = httplib.ACCEPTED return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_NOT_FOUND(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_not_found body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NOT_FOUND return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_NOT_EMPTY(self, method, url, body, headers): if method == 'DELETE': # test_delete_container_not_empty body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.CONFLICT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object( self, method, url, body, headers): if method == 'DELETE': # test_delete_object_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NO_CONTENT return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_NOT_FOUND( self, method, url, body, headers): if method == 'DELETE': # test_delete_object_success body = self.fixtures.load('list_container_objects_empty.json') headers = self.base_headers status_code = httplib.NOT_FOUND return (status_code, body, headers, httplib.responses[httplib.OK]) def _v1_1_auth(self, method, url, body, headers): body = self.auth_fixtures.load('_v1_1__auth.json') return (httplib.OK, body, { 'content-type': 'application/json; charset=UTF-8' }, httplib.responses[httplib.OK])
class AtmosMockHttp(StorageMockHttp, unittest.TestCase): fixtures = StorageFileFixtures('atmos') upload_created = False upload_stream_created = False def __init__(self, *args, **kwargs): unittest.TestCase.__init__(self) if kwargs.get('host', None) and kwargs.get('port', None): StorageMockHttp.__init__(self, *args, **kwargs) self._upload_object_via_stream_first_request = True def runTest(self): pass def request(self, method, url, body=None, headers=None, raw=False): headers = headers or {} parsed = urlparse.urlparse(url) if parsed.query.startswith('metadata/'): parsed = list(parsed) parsed[2] = parsed[2] + '/' + parsed[4] parsed[4] = '' url = urlparse.urlunparse(parsed) return super(AtmosMockHttp, self).request(method, url, body, headers, raw) def _rest_namespace_EMPTY(self, method, url, body, headers): body = self.fixtures.load('empty_directory_listing.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _rest_namespace(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _rest_namespace_test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('empty_directory_listing.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _rest_namespace_test_container(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _rest_namespace_test_container__metadata_system( self, method, url, body, headers): headers = { 'x-emc-meta': 'objectid=b21cb59a2ba339d1afdd4810010b0a5aba2ab6b9' } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_test_20_26_20container__metadata_system( self, method, url, body, headers): headers = { 'x-emc-meta': 'objectid=b21cb59a2ba339d1afdd4810010b0a5aba2ab6b9' } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_not_found__metadata_system(self, method, url, body, headers): body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) def _rest_namespace_test_create_container(self, method, url, body, headers): return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_test_create_container__metadata_system( self, method, url, body, headers): headers = { 'x-emc-meta': 'objectid=31a27b593629a3fe59f887fd973fd953e80062ce' } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_test_create_container_ALREADY_EXISTS( self, method, url, body, headers): body = self.fixtures.load('already_exists.xml') return (httplib.BAD_REQUEST, body, {}, httplib.responses[httplib.BAD_REQUEST]) def _rest_namespace_foo_bar_container(self, method, url, body, headers): return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_foo_bar_container_NOT_FOUND(self, method, url, body, headers): body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) def _rest_namespace_foo_bar_container_NOT_EMPTY(self, method, url, body, headers): body = self.fixtures.load('not_empty.xml') return (httplib.BAD_REQUEST, body, {}, httplib.responses[httplib.BAD_REQUEST]) def _rest_namespace_test_container_test_object_metadata_system( self, method, url, body, headers): meta = { 'objectid': '322dce3763aadc41acc55ef47867b8d74e45c31d6643', 'size': '555', 'mtime': '2011-01-25T22:01:49Z' } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_test_20_26_20container_test_20_26_20object_metadata_system( self, method, url, body, headers): meta = { 'objectid': '322dce3763aadc41acc55ef47867b8d74e45c31d6643', 'size': '555', 'mtime': '2011-01-25T22:01:49Z' } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_test_container_test_object_metadata_user( self, method, url, body, headers): meta = { 'md5': '6b21c4a111ac178feacf9ec9d0c71f17', 'foo-bar': 'test 1', 'bar-foo': 'test 2', } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_test_20_26_20container_test_20_26_20object_metadata_user( self, method, url, body, headers): meta = { 'md5': '6b21c4a111ac178feacf9ec9d0c71f17', 'foo-bar': 'test 1', 'bar-foo': 'test 2', } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_test_container_not_found_metadata_system( self, method, url, body, headers): body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) def _rest_namespace_foo_bar_container_foo_bar_object( self, method, url, body, headers): return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_foo_20_26_20bar_container_foo_20_26_20bar_object( self, method, url, body, headers): return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_foo_bar_container_foo_bar_object_NOT_FOUND( self, method, url, body, headers): body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) def _rest_namespace_fbc_ftu_metadata_system(self, method, url, body, headers): if not self.upload_created: self.__class__.upload_created = True body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) self.__class__.upload_created = False meta = { 'objectid': '322dce3763aadc41acc55ef47867b8d74e45c31d6643', 'size': '555', 'mtime': '2011-01-25T22:01:49Z' } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftu_metadata_user(self, method, url, body, headers): self.assertTrue('x-emc-meta' in headers) return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsdn_metadata_system(self, method, url, body, headers): if not self.upload_stream_created: self.__class__.upload_stream_created = True body = self.fixtures.load('not_found.xml') return (httplib.NOT_FOUND, body, {}, httplib.responses[httplib.NOT_FOUND]) self.__class__.upload_stream_created = False meta = { 'objectid': '322dce3763aadc41acc55ef47867b8d74e45c31d6643', 'size': '555', 'mtime': '2011-01-25T22:01:49Z' } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsdn(self, method, url, body, headers): if self._upload_object_via_stream_first_request: self.assertTrue('Range' not in headers) self.assertEqual(method, 'POST') self._upload_object_via_stream_first_request = False else: self.assertTrue('Range' in headers) self.assertEqual(method, 'PUT') return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsdn_metadata_user(self, method, url, body, headers): self.assertTrue('x-emc-meta' in headers) return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsde_metadata_system(self, method, url, body, headers): meta = { 'objectid': '322dce3763aadc41acc55ef47867b8d74e45c31d6643', 'size': '555', 'mtime': '2011-01-25T22:01:49Z' } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsde(self, method, url, body, headers): if self._upload_object_via_stream_first_request: self.assertTrue('Range' not in headers) self._upload_object_via_stream_first_request = False else: self.assertTrue('Range' in headers) self.assertEqual(method, 'PUT') return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsde_metadata_user(self, method, url, body, headers): self.assertTrue('x-emc-meta' in headers) return (httplib.OK, '', {}, httplib.responses[httplib.OK]) def _rest_namespace_fbc_ftsd_metadata_system(self, method, url, body, headers): meta = { 'objectid': '322dce3763aadc41acc55ef47867b8d74e45c31d6643', 'size': '555', 'mtime': '2011-01-25T22:01:49Z' } headers = { 'x-emc-meta': ', '.join([k + '=' + v for k, v in list(meta.items())]) } return (httplib.OK, '', headers, httplib.responses[httplib.OK])
class S3MockHttp(StorageMockHttp, MockHttpTestCase): fixtures = StorageFileFixtures('s3') base_headers = {} def _UNAUTHORIZED(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK]) def _DIFFERENT_REGION(self, method, url, body, headers): return (httplib.MOVED_PERMANENTLY, '', self.base_headers, httplib.responses[httplib.OK]) def _list_containers_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers_TOKEN(self, method, url, body, headers): self.assertEqual(headers['x-amz-security-token'], 'asdf') body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_ITERATOR(self, method, url, body, headers): if url.find('3.zip') == -1: # First part of the response (first 3 objects) file_name = 'list_container_objects_not_exhausted1.xml' else: file_name = 'list_container_objects_not_exhausted2.xml' body = self.fixtures.load(file_name) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test2_get_object(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test2_test_get_object(self, method, url, body, headers): # test_get_object body = self.fixtures.load('list_containers.xml') headers = { 'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-amz-meta-rabbits': 'monkeys', 'content-length': 12345, 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _new_container_INVALID_NAME(self, method, url, body, headers): # test_create_container return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _new_container_ALREADY_EXISTS(self, method, url, body, headers): # test_create_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _new_container(self, method, url, body, headers): # test_create_container, test_delete_container if method == 'PUT': status = httplib.OK elif method == 'DELETE': status = httplib.NO_CONTENT return (status, body, headers, httplib.responses[httplib.OK]) def _new_container_DOESNT_EXIST(self, method, url, body, headers): # test_delete_container return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _new_container_NOT_EMPTY(self, method, url, body, headers): # test_delete_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _test1_get_container(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _container1_get_container(self, method, url, body, headers): return (httplib.NOT_FOUND, '', self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _test_inexistent_get_object(self, method, url, body, headers): return (httplib.NOT_FOUND, '', self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container(self, method, url, body, headers): # test_delete_container return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_NOT_FOUND(self, method, url, body, headers): # test_delete_container_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data(self, method, url, body, headers): # test_upload_object_via_stream body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data_MULTIPART( self, method, url, body, headers): headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_LIST_MULTIPART(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'key-marker' not in query: body = self.fixtures.load('list_multipart_1.xml') else: body = self.fixtures.load('list_multipart_2.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_my_divisor_LIST_MULTIPART(self, method, url, body, headers): body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT]) def _foo_bar_container_my_movie_m2ts_LIST_MULTIPART( self, method, url, body, headers): body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT])
class S3MockRawResponse(MockRawResponse): fixtures = StorageFileFixtures('s3') def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_download_object_success body = '' self._data = self._generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload_INVALID_HASH1(self, method, url, body, headers): body = '' headers = {} headers['etag'] = '"foobar"' # test_upload_object_invalid_hash1 return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload_INVALID_HASH2(self, method, url, body, headers): # test_upload_object_invalid_hash2 body = '' headers = {'etag': '"hash343hhash89h932439jsaa89"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_upload(self, method, url, body, headers): # test_upload_object_success body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_INVALID_SIZE(self, method, url, body, headers): # test_upload_object_invalid_file_size body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data(self, method, url, body, headers): # test_upload_object_via_stream body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK])
class AzureBlobsMockHttp(BaseRangeDownloadMockHttp, unittest.TestCase): fixtures = StorageFileFixtures("azure_blobs") base_headers = {} # Note: using this method to get the oauth key for azure ad authentication def __getattr__(self, n): def fn(method, url, body, headers): fixture = self.fixtures.load(n + ".json") if method in ("POST", "PUT"): try: body = json.loads(body) fixture_tmp = json.loads(fixture) fixture_tmp = self._update(fixture_tmp, body) fixture = json.dumps(fixture_tmp) except ValueError: pass return (httplib.OK, fixture, headers, httplib.responses[httplib.OK]) return fn def _UNAUTHORIZED(self, method, url, body, headers): return ( httplib.UNAUTHORIZED, "", self.base_headers, httplib.responses[httplib.UNAUTHORIZED], ) def _list_containers_EMPTY(self, method, url, body, headers): body = self.fixtures.load("list_containers_empty.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if "marker" not in query: body = self.fixtures.load("list_containers_1.xml") else: body = self.fixtures.load("list_containers_2.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_EMPTY(self, method, url, body, headers): if method == "DELETE": body = "" return ( httplib.ACCEPTED, body, self.base_headers, httplib.responses[httplib.ACCEPTED], ) else: body = self.fixtures.load("list_objects_empty.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _new__container_INVALID_NAME(self, method, url, body, headers): return ( httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST], ) def _test_container(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if "marker" not in query: body = self.fixtures.load("list_objects_1.xml") else: body = self.fixtures.load("list_objects_2.xml") return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container100(self, method, url, body, headers): body = "" if method != "HEAD": return ( httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST], ) return ( httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.NOT_FOUND], ) def _test_container200(self, method, url, body, headers): body = "" if method != "HEAD": return ( httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST], ) headers = {} headers["etag"] = "0x8CFB877BB56A6FB" headers["last-modified"] = "Fri, 04 Jan 2013 09:48:06 GMT" headers["x-ms-lease-status"] = "unlocked" headers["x-ms-lease-state"] = "available" headers["x-ms-meta-meta1"] = "value1" return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _test_container200_test(self, method, url, body, headers): body = "" if method != "HEAD": return ( httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST], ) headers = {} headers["etag"] = "0x8CFB877BB56A6FB" headers["last-modified"] = "Fri, 04 Jan 2013 09:48:06 GMT" headers["content-length"] = "12345" headers["content-type"] = "application/zip" headers["x-ms-blob-type"] = "Block" headers["x-ms-lease-status"] = "unlocked" headers["x-ms-lease-state"] = "available" headers["x-ms-meta-rabbits"] = "monkeys" return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _test2_test_list_containers(self, method, url, body, headers): # test_get_object body = self.fixtures.load("list_containers.xml") headers = { "content-type": "application/zip", "etag": '"e31208wqsdoj329jd"', "x-amz-meta-rabbits": "monkeys", "content-length": "12345", "last-modified": "Thu, 13 Sep 2012 07:13:22 GMT", } return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _new_container_ALREADY_EXISTS(self, method, url, body, headers): # test_create_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.CONFLICT]) def _new_container(self, method, url, body, headers): # test_create_container, test_delete_container headers = {} if method == "PUT": status = httplib.CREATED headers["etag"] = "0x8CFB877BB56A6FB" headers["last-modified"] = "Fri, 04 Jan 2013 09:48:06 GMT" headers["x-ms-lease-status"] = "unlocked" headers["x-ms-lease-state"] = "available" headers["x-ms-meta-meta1"] = "value1" elif method == "DELETE": status = httplib.NO_CONTENT return (status, body, headers, httplib.responses[status]) def _new_container_DOESNT_EXIST(self, method, url, body, headers): # test_delete_container return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container_NOT_FOUND(self, method, url, body, headers): # test_delete_container_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container_foo_bar_object_DELETE(self, method, url, body, headers): # test_delete_object return (httplib.ACCEPTED, body, headers, httplib.responses[httplib.ACCEPTED]) def _foo_bar_container_foo_test_upload(self, method, url, body, headers): self._assert_content_length_header_is_string(headers=headers) query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) comp = query.get("comp", []) headers = {} body = "" if "blocklist" in comp or not comp: headers["etag"] = '"0x8CFB877BB56A6FB"' headers["content-md5"] = "d4fe4c9829f7ca1cc89db7ad670d2bbd" elif "block" in comp: headers["content-md5"] = "lvcfx/bOJvndpRlrdKU1YQ==" else: raise NotImplementedError("Unknown request comp: {}".format(comp)) return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_block(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) body = "" headers = {} headers["etag"] = "0x8CFB877BB56A6FB" return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_blocklist(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) body = "" headers = {} headers["etag"] = "0x8CFB877BB56A6FB" headers["content-md5"] = "d4fe4c9829f7ca1cc89db7ad670d2bbd" return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_lease(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) action = headers["x-ms-lease-action"] rheaders = {"x-ms-lease-id": "someleaseid"} body = "" if action == "acquire": return (httplib.CREATED, body, rheaders, httplib.responses[httplib.CREATED]) else: if headers.get("x-ms-lease-id", None) != "someleaseid": return ( httplib.BAD_REQUEST, body, rheaders, httplib.responses[httplib.BAD_REQUEST], ) return (httplib.OK, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_INVALID_HASH( self, method, url, body, headers ): # test_upload_object_invalid_hash1 self._assert_content_length_header_is_string(headers=headers) body = "" headers = {} headers["etag"] = "0x8CFB877BB56A6FB" headers["content-md5"] = "d4fe4c9829f7ca1cc89db7ad670d2bbd" return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_upload_object_invalid_file_size self._assert_content_length_header_is_string(headers=headers) body = generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_range(self, method, url, body, headers): # test_download_object_range_success body = "0123456789123456789" self.assertTrue("x-ms-range" in headers) self.assertEqual(headers["x-ms-range"], "bytes=5-6") start_bytes, end_bytes = self._get_start_and_end_bytes_from_range_str( headers["x-ms-range"], body ) return ( httplib.PARTIAL_CONTENT, body[start_bytes : end_bytes + 1], headers, httplib.responses[httplib.PARTIAL_CONTENT], ) def _foo_bar_container_foo_bar_object_range_stream( self, method, url, body, headers ): # test_download_object_range_as_stream_success body = "0123456789123456789" self.assertTrue("x-ms-range" in headers) self.assertEqual(headers["x-ms-range"], "bytes=4-5") start_bytes, end_bytes = self._get_start_and_end_bytes_from_range_str( headers["x-ms-range"], body ) return ( httplib.PARTIAL_CONTENT, body[start_bytes : end_bytes + 1], headers, httplib.responses[httplib.PARTIAL_CONTENT], ) def _foo_bar_container_foo_bar_object_INVALID_SIZE( self, method, url, body, headers ): # test_upload_object_invalid_file_size self._assert_content_length_header_is_string(headers=headers) body = "" return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _assert_content_length_header_is_string(self, headers): if "Content-Length" in headers: self.assertTrue(isinstance(headers["Content-Length"], basestring))
class S3MockHttp(StorageMockHttp, MockHttpTestCase): fixtures = StorageFileFixtures('s3') base_headers = {} def _UNAUTHORIZED(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.OK]) def _DIFFERENT_REGION(self, method, url, body, headers): return (httplib.MOVED_PERMANENTLY, '', self.base_headers, httplib.responses[httplib.OK]) def _list_containers_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): body = self.fixtures.load('list_containers.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_container_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container(self, method, url, body, headers): body = self.fixtures.load('list_container_objects.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_ITERATOR(self, method, url, body, headers): if url.find('3.zip') == -1: # First part of the response (first 3 objects) file_name = 'list_container_objects_not_exhausted1.xml' else: file_name = 'list_container_objects_not_exhausted2.xml' body = self.fixtures.load(file_name) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test2_test_list_containers(self, method, url, body, headers): # test_get_object body = self.fixtures.load('list_containers.xml') headers = { 'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-amz-meta-rabbits': 'monkeys', 'content-length': 12345, 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _new_container_INVALID_NAME(self, method, url, body, headers): # test_create_container return (httplib.BAD_REQUEST, body, headers, httplib.responses[httplib.OK]) def _new_container_ALREADY_EXISTS(self, method, url, body, headers): # test_create_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _new_container(self, method, url, body, headers): # test_create_container, test_delete_container if method == 'PUT': status = httplib.OK elif method == 'DELETE': status = httplib.NO_CONTENT return (status, body, headers, httplib.responses[httplib.OK]) def _new_container_DOESNT_EXIST(self, method, url, body, headers): # test_delete_container return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _new_container_NOT_EMPTY(self, method, url, body, headers): # test_delete_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container(self, method, url, body, headers): # test_delete_container return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_NOT_FOUND(self, method, url, body, headers): # test_delete_container_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_delete_object return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data(self, method, url, body, headers): # test_upload_object_via_stream body = '' headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_test_stream_data_MULTIPART( self, method, url, body, headers): headers = {'etag': '"0cc175b9c0f1b6a831c399e269772661"'} TEST_ID = 'VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA' query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if not query.get('uploadId', False): self.fail('Request doesnt contain uploadId query parameter') upload_id = query['uploadId'][0] if upload_id != TEST_ID: self.fail('first uploadId doesnt match TEST_ID') if method == 'PUT': # PUT is used for uploading the part. part number is mandatory if not query.get('partNumber', False): self.fail('Request is missing partNumber query parameter') body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) elif method == 'DELETE': # DELETE is done for aborting the upload body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT]) else: # POST is done for committing the upload. Parse the XML and # check if the commit is proper (TODO: XML Schema based check?) commit = ET.fromstring(body) count = 0 for part in commit.findall('Part'): count += 1 part_no = part.find('PartNumber').text etag = part.find('ETag').text self.assertEqual(part_no, str(count)) self.assertEqual(etag, headers['etag']) self.assertEqual(count, 3) body = self.fixtures.load('complete_multipart.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_LIST_MULTIPART(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'key-marker' not in query: body = self.fixtures.load('list_multipart_1.xml') else: body = self.fixtures.load('list_multipart_2.xml') return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_my_divisor_LIST_MULTIPART(self, method, url, body, headers): body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT]) def _foo_bar_container_my_movie_m2ts_LIST_MULTIPART( self, method, url, body, headers): body = '' return (httplib.NO_CONTENT, body, headers, httplib.responses[httplib.NO_CONTENT])
class CloudFilesMockRawResponse(MockRawResponse): fixtures = StorageFileFixtures('cloudfiles') base_headers = {'content-type': 'application/json; charset=UTF-8'} def _v1_MossoCloudFS_foo_bar_container_foo_test_upload( self, method, url, body, headers): # test_object_upload_success body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_speci_40l_name_m_40obj_E2_82_ACct( self, method, url, body, headers): # test_create_container_put_object_name_encoding # Verify that the name is properly url encoded object_name = 'm@obj€ct' urlquote(object_name) headers = copy.deepcopy(self.base_headers) body = '' headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_empty(self, method, url, body, headers): # test_upload_object_zero_size_object body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'hash343hhash89h932439jsaa89' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_test_upload_INVALID_HASH( self, method, url, body, headers): # test_object_upload_invalid_hash body = '' headers = {} headers.update(self.base_headers) headers['etag'] = 'foobar' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object( self, method, url, body, headers): # test_download_object_success body = self._generate_random_data(1000) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_INVALID_SIZE( self, method, url, body, headers): # test_download_object_invalid_file_size body = self._generate_random_data(100) return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_bar_object_NOT_FOUND( self, method, url, body, headers): body = '' return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.OK]) def _v1_MossoCloudFS_foo_bar_container_foo_test_stream_data( self, method, url, body, headers): # test_upload_object_via_stream_success headers = {} headers.update(self.base_headers) headers['etag'] = '577ef1154f3240ad5b9b413aa7346a1e' body = 'test' return (httplib.CREATED, body, headers, httplib.responses[httplib.OK])
class AzureBlobsMockHttp(MockHttp, unittest.TestCase): fixtures = StorageFileFixtures('azure_blobs') base_headers = {} def _UNAUTHORIZED(self, method, url, body, headers): return (httplib.UNAUTHORIZED, '', self.base_headers, httplib.responses[httplib.UNAUTHORIZED]) def _list_containers_EMPTY(self, method, url, body, headers): body = self.fixtures.load('list_containers_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _list_containers(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'marker' not in query: body = self.fixtures.load('list_containers_1.xml') else: body = self.fixtures.load('list_containers_2.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container_EMPTY(self, method, url, body, headers): if method == 'DELETE': body = u'' return (httplib.ACCEPTED, body, self.base_headers, httplib.responses[httplib.ACCEPTED]) else: body = self.fixtures.load('list_objects_empty.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _new__container_INVALID_NAME(self, method, url, body, headers): return (httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST]) def _test_container(self, method, url, body, headers): query_string = urlparse.urlsplit(url).query query = parse_qs(query_string) if 'marker' not in query: body = self.fixtures.load('list_objects_1.xml') else: body = self.fixtures.load('list_objects_2.xml') return (httplib.OK, body, self.base_headers, httplib.responses[httplib.OK]) def _test_container100(self, method, url, body, headers): body = '' if method != 'HEAD': return (httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST]) return (httplib.NOT_FOUND, body, self.base_headers, httplib.responses[httplib.NOT_FOUND]) def _test_container200(self, method, url, body, headers): body = '' if method != 'HEAD': return (httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST]) headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['last-modified'] = 'Fri, 04 Jan 2013 09:48:06 GMT' headers['x-ms-lease-status'] = 'unlocked' headers['x-ms-lease-state'] = 'available' headers['x-ms-meta-meta1'] = 'value1' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _test_container200_test(self, method, url, body, headers): body = '' if method != 'HEAD': return (httplib.BAD_REQUEST, body, self.base_headers, httplib.responses[httplib.BAD_REQUEST]) headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['last-modified'] = 'Fri, 04 Jan 2013 09:48:06 GMT' headers['content-length'] = '12345' headers['content-type'] = 'application/zip' headers['x-ms-blob-type'] = 'Block' headers['x-ms-lease-status'] = 'unlocked' headers['x-ms-lease-state'] = 'available' headers['x-ms-meta-rabbits'] = 'monkeys' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _test2_test_list_containers(self, method, url, body, headers): # test_get_object body = self.fixtures.load('list_containers.xml') headers = { 'content-type': 'application/zip', 'etag': '"e31208wqsdoj329jd"', 'x-amz-meta-rabbits': 'monkeys', 'content-length': '12345', 'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT' } return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _new_container_ALREADY_EXISTS(self, method, url, body, headers): # test_create_container return (httplib.CONFLICT, body, headers, httplib.responses[httplib.CONFLICT]) def _new_container(self, method, url, body, headers): # test_create_container, test_delete_container headers = {} if method == 'PUT': status = httplib.CREATED headers['etag'] = '0x8CFB877BB56A6FB' headers['last-modified'] = 'Fri, 04 Jan 2013 09:48:06 GMT' headers['x-ms-lease-status'] = 'unlocked' headers['x-ms-lease-state'] = 'available' headers['x-ms-meta-meta1'] = 'value1' elif method == 'DELETE': status = httplib.NO_CONTENT return (status, body, headers, httplib.responses[status]) def _new_container_DOESNT_EXIST(self, method, url, body, headers): # test_delete_container return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container_NOT_FOUND(self, method, url, body, headers): # test_delete_container_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container_foo_bar_object_NOT_FOUND(self, method, url, body, headers): # test_delete_object_not_found return (httplib.NOT_FOUND, body, headers, httplib.responses[httplib.NOT_FOUND]) def _foo_bar_container_foo_bar_object_DELETE(self, method, url, body, headers): # test_delete_object return (httplib.ACCEPTED, body, headers, httplib.responses[httplib.ACCEPTED]) def _foo_bar_container_foo_test_upload(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['content-md5'] = 'd4fe4c9829f7ca1cc89db7ad670d2bbd' return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_block(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_page(self, method, url, body, headers): # test_upload_object_success body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_blocklist(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['content-md5'] = 'd4fe4c9829f7ca1cc89db7ad670d2bbd' return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_lease(self, method, url, body, headers): # test_upload_object_success self._assert_content_length_header_is_string(headers=headers) action = headers['x-ms-lease-action'] rheaders = {'x-ms-lease-id': 'someleaseid'} body = '' if action == 'acquire': return (httplib.CREATED, body, rheaders, httplib.responses[httplib.CREATED]) else: if headers.get('x-ms-lease-id', None) != 'someleaseid': return (httplib.BAD_REQUEST, body, rheaders, httplib.responses[httplib.BAD_REQUEST]) return (httplib.OK, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_test_upload_INVALID_HASH( self, method, url, body, headers): # test_upload_object_invalid_hash1 self._assert_content_length_header_is_string(headers=headers) body = '' headers = {} headers['etag'] = '0x8CFB877BB56A6FB' headers['content-md5'] = 'd4fe4c9829f7ca1cc89db7ad670d2bbd' return (httplib.CREATED, body, headers, httplib.responses[httplib.CREATED]) def _foo_bar_container_foo_bar_object(self, method, url, body, headers): # test_upload_object_invalid_file_size self._assert_content_length_header_is_string(headers=headers) body = generate_random_data(1000) return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _foo_bar_container_foo_bar_object_INVALID_SIZE(self, method, url, body, headers): # test_upload_object_invalid_file_size self._assert_content_length_header_is_string(headers=headers) body = '' return (httplib.OK, body, headers, httplib.responses[httplib.OK]) def _assert_content_length_header_is_string(self, headers): if 'Content-Length' in headers: self.assertTrue(isinstance(headers['Content-Length'], basestring))
class BackblazeB2MockHttp(MockHttp): fixtures = StorageFileFixtures("backblaze_b2") def _b2api_v1_b2_authorize_account(self, method, url, body, headers): if method == "GET": body = json.dumps( { "accountId": "test", "apiUrl": "https://apiNNN.backblazeb2.com", "downloadUrl": "https://f002.backblazeb2.com", "authorizationToken": "test", } ) else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_buckets(self, method, url, body, headers): if method == "GET": body = self.fixtures.load("b2_list_buckets.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_file_names(self, method, url, body, headers): if method == "GET": body = self.fixtures.load("b2_list_file_names.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_create_bucket(self, method, url, body, headers): if method == "POST": body = self.fixtures.load("b2_create_bucket.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_delete_bucket(self, method, url, body, headers): if method == "POST": body = self.fixtures.load("b2_delete_bucket.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_delete_file_version(self, method, url, body, headers): if method == "POST": body = self.fixtures.load("b2_delete_file_version.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_get_upload_url(self, method, url, body, headers): # test_upload_object if method == "GET": body = self.fixtures.load("b2_get_upload_url.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_upload_file_abcd_defg(self, method, url, body, headers): # test_upload_object if method == "POST": body = self.fixtures.load("b2_upload_file.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_list_file_versions(self, method, url, body, headers): if method == "GET": body = self.fixtures.load("b2_list_file_versions.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _b2api_v1_b2_hide_file(self, method, url, body, headers): if method == "POST": body = self.fixtures.load("b2_hide_file.json") else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK]) def _file_test00001_2_txt(self, method, url, body, headers): # test_download_object if method == "GET": body = "ab" else: raise AssertionError("Unsupported method") return (httplib.OK, body, {}, httplib.responses[httplib.OK])