feat(接口测试): TCP-Mock期望添加后置脚本功能

TCP-Mock期望添加后置脚本功能
This commit is contained in:
song-tianyang 2022-02-16 16:33:58 +08:00 committed by CountryBuilder
parent 47fb30ad1d
commit 3fc8708c51
3 changed files with 102 additions and 98 deletions

View File

@ -15,28 +15,28 @@ import java.util.regex.Pattern;
public class MockScriptEngineUtils { public class MockScriptEngineUtils {
public JSONObject getVars(ScriptEngine engine){ public JSONObject getVars(ScriptEngine engine) {
try { try {
return JSONObject.parseObject(JSONObject.toJSONString(engine.get("vars"))); return JSONObject.parseObject(JSONObject.toJSONString(engine.get("vars")));
}catch (Exception e){ } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }
return new JSONObject(); return new JSONObject();
} }
public String get(ScriptEngine engine, String key){ public String get(ScriptEngine engine, String key) {
return String.valueOf(engine.get(key)); return String.valueOf(engine.get(key));
} }
public void runScript(ScriptEngine engine, String script){ public void runScript(ScriptEngine engine, String script) {
try { try {
engine.eval(script); engine.eval(script);
}catch (Exception e){ } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }
} }
public ScriptEngine getBaseScriptEngine(String scriptLanguage, String url, Map<String, String> headerMap, RequestMockParams requestMockParams){ public ScriptEngine getBaseScriptEngine(String scriptLanguage, String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
ScriptEngine engine = null; ScriptEngine engine = null;
try { try {
if (StringUtils.isEmpty(scriptLanguage)) { if (StringUtils.isEmpty(scriptLanguage)) {
@ -53,7 +53,7 @@ public class MockScriptEngineUtils {
String preScript = this.genPythonPreScript(url, headerMap, requestMockParams); String preScript = this.genPythonPreScript(url, headerMap, requestMockParams);
engine.eval(preScript); engine.eval(preScript);
} }
}catch (Exception e){ } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }
return engine; return engine;
@ -64,50 +64,55 @@ public class MockScriptEngineUtils {
preScriptBuffer.append("Map vars = new HashMap();\n"); preScriptBuffer.append("Map vars = new HashMap();\n");
preScriptBuffer.append("vars.put(\"address\",\"" + url + "\");\n"); preScriptBuffer.append("vars.put(\"address\",\"" + url + "\");\n");
//写入请求头 //写入请求头
for (Map.Entry<String, String> headEntry : headerMap.entrySet()) { if (headerMap != null) {
String headerKey = headEntry.getKey(); for (Map.Entry<String, String> headEntry : headerMap.entrySet()) {
String headerValue = headEntry.getValue(); String headerKey = headEntry.getKey();
preScriptBuffer.append("vars.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n"); String headerValue = headEntry.getValue();
preScriptBuffer.append("vars.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n");
}
} }
//写入body参数 //写入body参数
if (requestMockParams.getBodyParams() != null) { if (requestMockParams != null) {
if (requestMockParams.getBodyParams().size() == 1) { if (requestMockParams.getBodyParams() != null) {
//参数是jsonObject if (requestMockParams.getBodyParams().size() == 1) {
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); //参数是jsonObject
for (String key : bodyParamObj.keySet()) { JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
String value = String.valueOf(bodyParamObj.get(key)); for (String key : bodyParamObj.keySet()) {
String value = String.valueOf(bodyParamObj.get(key));
value = StringUtils.replace(value, "\\", "\\\\");
value = StringUtils.replace(value, "\"", "\\\"");
preScriptBuffer.append("vars.put(\"body." + key + "\",\"" + value + "\");\n");
if (StringUtils.equalsIgnoreCase(key, "raw")) {
preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + value + "\");\n");
}
}
String jsonBody = bodyParamObj.toJSONString();
jsonBody = StringUtils.replace(jsonBody, "\\", "\\\\");
jsonBody = StringUtils.replace(jsonBody, "\"", "\\\"");
preScriptBuffer.append("vars.put(\"body.json\",\"" + jsonBody + "\");\n");
} else {
preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + requestMockParams.getBodyParams().toJSONString() + "\");\n");
}
}
//写入query参数
if (requestMockParams.getQueryParamsObj() != null) {
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
for (String key : queryParamsObj.keySet()) {
String value = String.valueOf(queryParamsObj.get(key));
value = StringUtils.replace(value, "\\", "\\\\"); value = StringUtils.replace(value, "\\", "\\\\");
value = StringUtils.replace(value, "\"", "\\\""); value = StringUtils.replace(value, "\"", "\\\"");
preScriptBuffer.append("vars.put(\"body." + key + "\",\"" + value + "\");\n"); preScriptBuffer.append("vars.put(\"query." + key + "\",\"" + value + "\");\n");
if (StringUtils.equalsIgnoreCase(key, "raw")) {
preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + value + "\");\n");
}
} }
String jsonBody = bodyParamObj.toJSONString();
jsonBody = StringUtils.replace(jsonBody, "\\", "\\\\");
jsonBody = StringUtils.replace(jsonBody, "\"", "\\\"");
preScriptBuffer.append("vars.put(\"body.json\",\"" + jsonBody + "\");\n");
} else {
preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + requestMockParams.getBodyParams().toJSONString() + "\");\n");
} }
//写入rest参数
} if (requestMockParams.getRestParamsObj() != null) {
//写入query参数 JSONObject restParamsObj = requestMockParams.getRestParamsObj();
if (requestMockParams.getQueryParamsObj() != null) { for (String key : restParamsObj.keySet()) {
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj(); String value = String.valueOf(restParamsObj.get(key));
for (String key : queryParamsObj.keySet()) { preScriptBuffer.append("vars.put(\"rest." + key + "\",\"" + value + "\");\n");
String value = String.valueOf(queryParamsObj.get(key)); }
value = StringUtils.replace(value, "\\", "\\\\");
value = StringUtils.replace(value, "\"", "\\\"");
preScriptBuffer.append("vars.put(\"query." + key + "\",\"" + value + "\");\n");
}
}
//写入rest参数
if (requestMockParams.getRestParamsObj() != null) {
JSONObject restParamsObj = requestMockParams.getRestParamsObj();
for (String key : restParamsObj.keySet()) {
String value = String.valueOf(restParamsObj.get(key));
preScriptBuffer.append("vars.put(\"rest." + key + "\",\"" + value + "\");\n");
} }
} }
return preScriptBuffer.toString(); return preScriptBuffer.toString();
@ -172,37 +177,37 @@ public class MockScriptEngineUtils {
Pattern pattern = Pattern.compile(regStr); Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(reportString); Matcher matcher = pattern.matcher(reportString);
List<String> paramKeys = new ArrayList<>(); List<String> paramKeys = new ArrayList<>();
while (matcher.find()){ while (matcher.find()) {
String paramKey = matcher.group(0); String paramKey = matcher.group(0);
if(!paramKeys.contains(paramKey)){ if (!paramKeys.contains(paramKey)) {
paramKeys.add(paramKey); paramKeys.add(paramKey);
} }
} }
JSONObject varsObject = this.getVars(scriptEngine); JSONObject varsObject = this.getVars(scriptEngine);
for (String paramKey : paramKeys) { for (String paramKey : paramKeys) {
String value = this.getValue(scriptEngine,varsObject,paramKey); String value = this.getValue(scriptEngine, varsObject, paramKey);
reportString = StringUtils.replace(reportString,paramKey,value); reportString = StringUtils.replace(reportString, paramKey, value);
} }
return reportString; return reportString;
} }
private String getValue(ScriptEngine scriptEngine, JSONObject varsObject, String paramKey) { private String getValue(ScriptEngine scriptEngine, JSONObject varsObject, String paramKey) {
String key = paramKey; String key = paramKey;
if(key.startsWith("${") && key.endsWith("}")){ if (key.startsWith("${") && key.endsWith("}")) {
key = paramKey.substring(2,key.length()-1); key = paramKey.substring(2, key.length() - 1);
} }
String value = null; String value = null;
if(varsObject != null && varsObject.containsKey(key)){ if (varsObject != null && varsObject.containsKey(key)) {
value = varsObject.getString(key); value = varsObject.getString(key);
} }
if(StringUtils.isEmpty(value)){ if (StringUtils.isEmpty(value)) {
try { try {
value = JSONObject.toJSONString(scriptEngine.get(key)); value = JSONObject.toJSONString(scriptEngine.get(key));
}catch (Exception e){ } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }
} }
if(StringUtils.isEmpty(value)){ if (StringUtils.isEmpty(value)) {
value = paramKey; value = paramKey;
} }
return value; return value;

View File

@ -60,7 +60,7 @@ public class TCPServicer {
MockApiUtils mockApiUtils = new MockApiUtils(); MockApiUtils mockApiUtils = new MockApiUtils();
boolean useScript = false; boolean useScript = false;
if(respResultObj.containsKey("usePostScript")){ if(respResultObj.containsKey("usePostScript")){
useScript = responseObj.getBoolean("usePostScript"); useScript = respResultObj.getBoolean("usePostScript");
} }
returnMsg = mockApiUtils.getResultByResponseResult(respResultObj.getJSONObject("body"),"",null,null,useScript); returnMsg = mockApiUtils.getResultByResponseResult(respResultObj.getJSONObject("body"),"",null,null,useScript);
} }

View File

@ -80,48 +80,48 @@ export default {
}, },
computed: { computed: {
httpCodeTemplates(){ httpCodeTemplates() {
let returnData = [ let returnData = [
{ {
title: "API" + this.$t('api_test.definition.document.request_info'), title: "API" + this.$t('api_test.definition.document.request_info'),
children: [ children: [
{ {
title: this.$t('api_test.request.address'), title: this.$t('api_test.request.address'),
value: this.getScript("address"), value: this.getScript("address"),
}, },
{ {
title: "Header " + this.$t('api_test.definition.document.request_param'), title: "Header " + this.$t('api_test.definition.document.request_param'),
value: this.getScript("header"), value: this.getScript("header"),
}, },
{ {
title: this.$t('api_test.request.body') + this.$t('api_test.variable'), title: this.$t('api_test.request.body') + this.$t('api_test.variable'),
value: this.getScript("body"), value: this.getScript("body"),
}, },
{ {
title: this.$t('api_test.request.body') + this.$t('api_test.variable') + " (Raw)", title: this.$t('api_test.request.body') + this.$t('api_test.variable') + " (Raw)",
value: this.getScript("bodyRaw"), value: this.getScript("bodyRaw"),
}, },
{ {
title: "Query " + this.$t('api_test.definition.document.request_param'), title: "Query " + this.$t('api_test.definition.document.request_param'),
value: this.getScript("query"), value: this.getScript("query"),
}, },
{ {
title: "Rest " + this.$t('api_test.definition.document.request_param'), title: "Rest " + this.$t('api_test.definition.document.request_param'),
value: this.getScript("rest"), value: this.getScript("rest"),
}, },
] ]
}, },
{ {
title: this.$t('project.code_segment.code_segment'), title: this.$t('project.code_segment.code_segment'),
children: [ children: [
{ {
title: this.$t('project.code_segment.insert_segment'), title: this.$t('project.code_segment.insert_segment'),
command: "custom_function", command: "custom_function",
} }
] ]
}, },
]; ];
return returnData; return returnData;
} }
}, },
@ -144,13 +144,12 @@ export default {
jsr223Processor() { jsr223Processor() {
this.reload(); this.reload();
}, },
'jsr223Processor.scriptLanguage'(){ 'jsr223Processor.scriptLanguage'() {
if (this.showApi) { if (this.showApi) {
this.baseCodeTemplates = this.httpCodeTemplates; this.baseCodeTemplates = this.httpCodeTemplates;
} else { } else {
this.baseCodeTemplates = this.tcpCodeTemplates; this.baseCodeTemplates = this.tcpCodeTemplates;
} }
alert(JSON.stringify(this.baseCodeTemplates));
} }
} }
, ,