def dispatch(method, headers, url, payload): """Dispatches incoming request and returns response. In dev appserver or unittest environment, this method is called instead of urlfetch. Args: method: urlfetch method. headers: urlfetch headers. url: urlfetch url. payload: urlfecth payload. Returns: A _FakeUrlFetchResult. Raises: ValueError: invalid request method. """ method, headers, filename, param_dict = _preprocess(method, headers, url) gcs_stub = cloudstorage_stub.CloudStorageStub( apiproxy_stub_map.apiproxy.GetStub('blobstore').storage) if method == 'POST': return _handle_post(gcs_stub, filename, headers) elif method == 'PUT': return _handle_put(gcs_stub, filename, param_dict, headers, payload) elif method == 'GET': return _handle_get(gcs_stub, filename, param_dict, headers) elif method == 'HEAD': return _handle_head(gcs_stub, filename) elif method == 'DELETE': return _handle_delete(gcs_stub, filename) raise ValueError('Unrecognized request method %r.' % method)
def setUp(self): super(BaseTest, self).setUp() root_path = '.' application_id = 'graphene-gae-test' # First, create an instance of the Testbed class. self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.setup_env(app_id=application_id, overwrite=True) policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy( probability=self.datastore_probability) self.testbed.init_datastore_v3_stub(root_path=root_path, consistency_policy=policy, require_indexes=True) self.testbed.init_app_identity_stub() self.testbed.init_blobstore_stub() self.testbed.init_memcache_stub() self.testbed.init_taskqueue_stub(root_path=root_path) self.testbed.init_urlfetch_stub() self.storage = cloudstorage_stub.CloudStorageStub( self.testbed.get_stub('blobstore').storage) self.testbed.init_mail_stub() self.testbed.init_user_stub() self.taskqueue_stub = self.testbed.get_stub( testbed.TASKQUEUE_SERVICE_NAME) ndb.get_context().clear_cache() ndb.get_context().set_cache_policy(lambda x: True)
def __init__(self, blob_storage): """Constructor. Args: blob_storage: apphosting.api.blobstore.blobstore_stub.BlobStorage instance. """ self.blob_storage = blob_storage self.gs_stub = cloudstorage_stub.CloudStorageStub(self.blob_storage) self.uploads = {} self.finalized = set() self.sequence_keys = {}
def create_blob(self, content_type='image/png'): """Create a GS object in the datastore and on disk. Overrides the superclass create_blob method. Returns: The BlobKey of the new object." """ data = 'a blob' filename = '/some_bucket/some_object' stub = cloudstorage_stub.CloudStorageStub(self.blob_storage) options = {} if content_type: options['content-type'] = content_type blob_key = stub.post_start_creation(filename, options) stub.put_continue_creation(blob_key, data, (0, len(data) - 1), True) self.blob_storage.StoreBlob(blob_key, cStringIO.StringIO(data)) return blob_key
def store_gs_file(self, content_type, gs_filename, blob_file, filename): """Store a supplied form-data item to GS. Delegate all the work of gs file creation to CloudStorageStub. Args: content_type: The MIME content type of the uploaded file. gs_filename: The gs filename to create of format bucket/filename. blob_file: A file-like object containing the contents of the file. filename: user provided filename. Returns: datastore.Entity('__GsFileInfo__') associated with the upload. """ gs_stub = cloudstorage_stub.CloudStorageStub(self._blob_storage) blobkey = gs_stub.post_start_creation('/' + gs_filename, {'content-type': content_type}) content = blob_file.read() return gs_stub.put_continue_creation(blobkey, content, (0, len(content) - 1), True, filename)
def dispatch(method, headers, url, payload): """Dispatches incoming request and returns response. In dev appserver GCS requests are forwarded to this method via the /_ah/gcs endpoint. In unittest environment, this method is called instead of urlfetch. See https://developers.google.com/storage/docs/xml-api-overview for the exepected format for the request. Args: method: A string represneting the HTTP request method. headers: A dict mapping HTTP header names to values. url: A string representing the request URL in the form of http://<host>/_ah/gcs/<bucket>/<object>. payload: A string containing the payload for the request. Returns: A _FakeUrlFetchResult containing the HTTP status code, headers, and body of the response. Raises: ValueError: invalid request method. """ method, headers, filename, param_dict = _preprocess(method, headers, url) gcs_stub = cloudstorage_stub.CloudStorageStub( apiproxy_stub_map.apiproxy.GetStub('blobstore').storage) with GCS_STUB_LOCK: if method == 'POST': return _handle_post(gcs_stub, filename, headers) elif method == 'PUT': return _handle_put(gcs_stub, filename, param_dict, headers, payload) elif method == 'GET': return _handle_get(gcs_stub, filename, param_dict, headers) elif method == 'HEAD': return _handle_head(gcs_stub, filename) elif method == 'DELETE': return _handle_delete(gcs_stub, filename) raise ValueError('Unrecognized request method %r.' % method, httplib.METHOD_NOT_ALLOWED)