def GetConfigMockClass(sections=None): """Mocks a configuration file for use by the API handler. Args: sections: A dict containing one key per config section with a value of a dict containing one key per config parameter name and a value of config parameter value. (default {}) Returns: A class to be used as a config mock. """ if sections is None: sections = {} missing = object() type_infos = [] values = {} raw_values = {} default_values = {} for section_name, section in sections.iteritems(): for parameter_name, parameter_data in section.iteritems(): name = "%s.%s" % (section_name, parameter_name) descriptor = utils.DataObject(section=section_name, name=name) type_infos.append(descriptor) if "value" in parameter_data: values[name] = parameter_data["value"] if "raw_value" in parameter_data: raw_values[name] = parameter_data["raw_value"] if "default_value" in parameter_data: default_values[name] = parameter_data["default_value"] def Get(parameter, default=missing): try: return values[parameter] except KeyError: if default is missing: return default_values[parameter] return default def GetRaw(parameter, default=missing): try: return raw_values[parameter] except KeyError: if default is missing: return default_values[parameter] return default return {"Get": Get, "GetRaw": GetRaw, "type_infos": type_infos}
def _CreateRequest(self, method, path, query_parameters=None): request = utils.DataObject() request.method = method request.path = path request.scheme = "http" request.environ = {"SERVER_NAME": "foo.bar", "SERVER_PORT": 1234} request.user = "******" request.args = query_parameters or {} request.headers = {} request.get_data = lambda as_text=False: "" return request
def _CreateRequest(self, method, path, username="******", query_parameters=None): request = utils.DataObject() request.method = method request.path = path request.scheme = "http" request.environ = {"SERVER_NAME": "foo.bar", "SERVER_PORT": 1234} request.user = username request.args = query_parameters or {} request.content_type = "application/json; charset=utf-8" request.headers = {} request.get_data = lambda as_text=False: "" return request
def _HandleConfig(self, sections): with self._ConfigStub(sections): mock_request = utils.DataObject() result = self.handler.Handle(mock_request) return result