上一篇,写了Mock Server的基础实现与被测系统的对接
当我们mock的接口信息、返回值等时不时维护时,都要在代码中编辑,那体验就不太好了,如果这些可以直接在浏览器编辑就好了。
因此对后端部分做下调整,再加个前端,前端页面大致如下:
新增、修改接口:
接口返回值管理:
一、后端path参数化
对于path部分做下参数化:
1 @db_response_app.route('/<path:url_path>', methods=['GET', 'POST']) 2 def db_response(url_path): 3 check_data = CheckData(url_path,request.method) 4 return check_data.check_data_list()
对获取到的返回值做下检查及处理后返回:
1 from mysql_operate.mock_config_opt import MockConfigOperate 2 from data_manage.resp_param_replace import RespParamReplace 3 4 5 class CheckData(): 6 def __init__(self,url, method): 7 self.mock_config_operate = MockConfigOperate() 8 self.resp_param_replace = RespParamReplace(url, method) 9 self.data_list = self.mock_config_operate.get_resp(url, method) 10 11 def check_data_list(self): 12 if len(self.data_list) == 1: 13 response_data = self.resp_param_replace.param_replace() 14 return response_data 15 elif len(self.data_list) > 1: 16 return '相同的路径和请求方法存在多条,请检查!' 17 else: 18 return 'not defined'
返回值从数据库获取:
1 from common.db_common import DBCommon 2 3 4 class MockConfigOperate(): 5 6 def __init__(self): 7 self.dbCommon = DBCommon() 8 9 def get_resp(self, url_path, method): 10 11 resp_sql = "SELECT md.resparams from mock_detail md,mock_config mc WHERE mc.respid=md.id and " 12 "mc.domain='{url_path}' and mc.methods='{method}';".format(method=method, url_path=url_path) 13 resp_list = self.dbCommon.query_list(resp_sql) 14 return resp_list
表设计:
mock_config:respid决定启用的resp
mock_detail:
获取到的返回值处理:
1 from mysql_operate.mock_config_opt import MockConfigOperate 2 from common.get_time import GetTime 3 from data_manage.update_param import UpdateParam 4 import re 5 6 7 class RespParamReplace(): 8 def __init__(self,url,method): 9 self.time_stamp = GetTime() 10 self.mock_config_operate = MockConfigOperate() 11 self.resp_list = self.mock_config_operate.get_resp(url, method) 12 self.update_param = UpdateParam() 13 14 self.param_dict = {'@{param_uuid}': self.update_param.update_uuid(), 15 '@{param_time_stamp}': self.update_param.update_time_stamp(), 16 '@{param_id}':self.update_param.update_id()} 17 18 def __get_param(self): 19 param_key = re.findall(r'@{.*?}', self.resp_list[0]['resparams']) 20 return param_key 21 22 def param_replace(self): 23 resp_value = self.resp_list[0]['resparams'] 24 param_list = self.__get_param() 25 if param_list: 26 for param in param_list: 27 if self.param_dict.has_key(param): 28 resp_value = re.sub(param,str(self.param_dict[param]),resp_value) 29 return resp_value 30 else: 31 return resp_value