def SetResponse(self, module, response): ''' set data returned by mock server Two args: The first arg is the module instance of the api template The second arg is response we'd like to set if success, return true. else, return false ''' uri = "/set/response" #assamble body body = { "url": module.url, "method": module.method, "response": response } print body try: request = Request(url=uri, body=body, method="post", base_url=self.baseurl) print request result = request.send().result print 'this is result' print result if result['status'] == '200': print("Set response success!") return True else: raise Exception("Set response failed!") except: print("Error response returned!") return False
def SetResponse(self, module, response): ''' set data returned by mock server Two args: The first arg is the module instance of the api template The second arg is response we'd like to set if success, return true. else, return false ''' uri = "/set/response" #assamble body body = { "url": module.url, "method": module.method, "response": response } print body try: request = Request(url = uri, body = body, method = "post", base_url = self.baseurl) print request result = request.send().result print 'this is result' print result if result['status'] == '200': print("Set response success!") return True else: raise Exception("Set response failed!") except: print("Error response returned!") return False
def SetMode(self, mode = Mode.Mock, is_global = True): ''' by default set mode will change mode permenently. If you don't want this, set global to false ''' uri = "/set/mode" body = { 'mode': mode, 'is_global': is_global} try: request = Request(url = uri, body = body, method = "post", base_url = self.baseurl) result = request.send().result if result['status'] == '200': print("Set mode success!") return True else: raise Exception("Set mode failed!") except: print("Error response returned!") return False
def ForwardRequest(): Utils.AssertConfig('MOCK_SERVER', 'serveraddress') base_url = web.mockserver.config['MOCK_SERVER']['serveraddress'] url = web.ctx.fullpath headers = web.ctx.env['REQUEST_HEADERS'] # headers = Utils.ConvertHeaderToDict(web.ctx.headers) body = web.data() method = web.ctx.method Log.Mockserver.debug("remote server url is %s, headers are %s" % (base_url, str(headers))) request = Request(url = url, headers = None, method = method, body = body, base_url = base_url) req = request.send() if isinstance(req.response.data, dict) or isinstance(req.response.data, list) or isinstance(req.response.data, tuple): req.response.data = json.dumps(req.response.data) for k in req.response.headers.keys(): if k in ignore_headers: del(req.response.headers[k]) return req.response
def SetDelay(self, delay = 0, is_global = False): ''' by default the delay is 0. set value to get a delayed response. The value must be int type. ''' if not isinstance(delay, int): raise Exception("delay must be integer!") uri = "/set/delay" body = { 'delay': delay , 'is_global': is_global} try: request = Request(url = uri, body = body, method = "post", base_url = self.baseurl) result = request.send().result if result['status'] == '200': print("Set delay success!") return True else: raise Exception("Set delay failed!") except: print("Error response returned!") return False
def SetMode(self, mode=Mode.Mock, is_global=True): ''' by default set mode will change mode permenently. If you don't want this, set global to false ''' uri = "/set/mode" body = {'mode': mode, 'is_global': is_global} try: request = Request(url=uri, body=body, method="post", base_url=self.baseurl) result = request.send().result if result['status'] == '200': print("Set mode success!") return True else: raise Exception("Set mode failed!") except: print("Error response returned!") return False
def SetDelay(self, delay=0, is_global=False): ''' by default the delay is 0. set value to get a delayed response. The value must be int type. ''' if not isinstance(delay, int): raise Exception("delay must be integer!") uri = "/set/delay" body = {'delay': delay, 'is_global': is_global} try: request = Request(url=uri, body=body, method="post", base_url=self.baseurl) result = request.send().result if result['status'] == '200': print("Set delay success!") return True else: raise Exception("Set delay failed!") except: print("Error response returned!") return False