fix(Mock自定义脚本): 修复Mock响应中自定义脚本执行时未排除ms特定脚本的问题

修复Mock响应中自定义脚本执行时未排除ms特定脚本的问题
This commit is contained in:
song-tianyang 2021-10-21 14:31:14 +08:00 committed by song-tianyang
parent 2bb5cbdc93
commit be404b1e27
2 changed files with 61 additions and 47 deletions

View File

@ -351,10 +351,13 @@ public class MockApiUtils {
String script = scriptObj.getString("script"); String script = scriptObj.getString("script");
String scriptLanguage =scriptObj.getString("scriptLanguage"); String scriptLanguage =scriptObj.getString("scriptLanguage");
returnStr = parseScript(script,url,headerMap,requestMockParams); Map<String,String> scrpitParseMap = parseScript(script,url,headerMap,requestMockParams);
if(scrpitParseMap.containsKey("returnMsg")){
returnStr = scrpitParseMap.get("returnMsg");
}
if(scrpitParseMap.containsKey("script")){
script = scrpitParseMap.get("script");
}
runScript(script,scriptLanguage); runScript(script,scriptLanguage);
} }
} }
@ -364,54 +367,65 @@ public class MockApiUtils {
} }
} }
private static String parseScript(String script,String url,Map<String,String> headerMap,RequestMockParams requestMockParams) { private static Map<String,String> parseScript(String script,String url,Map<String,String> headerMap,RequestMockParams requestMockParams) {
Map<String,String> returnMap = new HashMap<>();
String returnMsg = ""; String returnMsg = "";
String newScript = "";
if(StringUtils.isNotEmpty(script)){ if(StringUtils.isNotEmpty(script)){
String [] scriptRowArr = StringUtils.split(script,"\n"); String [] scriptRowArr = StringUtils.split(script,"\n");
for (String scriptRow : scriptRowArr) { for (String scriptItemRows : scriptRowArr) {
scriptRow = scriptRow.trim(); String [] scriptItemArr = scriptItemRows.split(";");
if(StringUtils.startsWith(scriptRow,"returnMsg.add(") && StringUtils.endsWith(scriptRow,")")){ for (String scriptRow :scriptItemArr) {
scriptRow = scriptRow.substring(14,scriptRow.length()-1).trim(); scriptRow = scriptRow.trim();
if(StringUtils.equalsIgnoreCase(scriptRow,"@address")){ if(StringUtils.startsWith(scriptRow,"returnMsg.add(") && StringUtils.endsWith(scriptRow,")")){
returnMsg += url; scriptRow = scriptRow.substring(14,scriptRow.length()-1).trim();
}else if(StringUtils.startsWith(scriptRow,"@header(${") && StringUtils.endsWith(scriptRow,"})")){ if(StringUtils.equalsIgnoreCase(scriptRow,"@address")){
String paramName = scriptRow.substring(10,scriptRow.length()-2); returnMsg += url;
if(headerMap.containsKey(paramName)){ }else if(StringUtils.startsWith(scriptRow,"@header(${") && StringUtils.endsWith(scriptRow,"})")){
returnMsg += headerMap.get(paramName); String paramName = scriptRow.substring(10,scriptRow.length()-2);
} if(headerMap.containsKey(paramName)){
}else if(StringUtils.startsWith(scriptRow,"@body(${") && StringUtils.endsWith(scriptRow,"})")){ returnMsg += headerMap.get(paramName);
String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey(paramName)){
returnMsg += String.valueOf(bodyParamObj.get(paramName));
} }
} }else if(StringUtils.startsWith(scriptRow,"@body(${") && StringUtils.endsWith(scriptRow,"})")){
}else if(StringUtils.equalsIgnoreCase(scriptRow,"@bodyRaw")){ String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){ if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey("raw")){ if(bodyParamObj.containsKey(paramName)){
returnMsg += String.valueOf(bodyParamObj.get("raw")); returnMsg += String.valueOf(bodyParamObj.get(paramName));
}
} }
} }else if(StringUtils.equalsIgnoreCase(scriptRow,"@bodyRaw")){
}else if(StringUtils.startsWith(scriptRow,"@query(${") && StringUtils.endsWith(scriptRow,"})")){ if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
String paramName = scriptRow.substring(9,scriptRow.length()-2); JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(requestMockParams.getQueryParamsObj() != null && requestMockParams.getQueryParamsObj().containsKey(paramName)){ if(bodyParamObj.containsKey("raw")){
returnMsg += String.valueOf(requestMockParams.getQueryParamsObj().get(paramName)); returnMsg += String.valueOf(bodyParamObj.get("raw"));
} }
}else if(StringUtils.startsWith(scriptRow,"@rest(${") && StringUtils.endsWith(scriptRow,"})")){ }
String paramName = scriptRow.substring(8,scriptRow.length()-2); }else if(StringUtils.startsWith(scriptRow,"@query(${") && StringUtils.endsWith(scriptRow,"})")){
if(requestMockParams.getRestParamsObj() != null && requestMockParams.getRestParamsObj().containsKey(paramName)){ String paramName = scriptRow.substring(9,scriptRow.length()-2);
returnMsg += String.valueOf(requestMockParams.getRestParamsObj().get(paramName)); if(requestMockParams.getQueryParamsObj() != null && requestMockParams.getQueryParamsObj().containsKey(paramName)){
returnMsg += String.valueOf(requestMockParams.getQueryParamsObj().get(paramName));
}
}else if(StringUtils.startsWith(scriptRow,"@rest(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(8,scriptRow.length()-2);
if(requestMockParams.getRestParamsObj() != null && requestMockParams.getRestParamsObj().containsKey(paramName)){
returnMsg += String.valueOf(requestMockParams.getRestParamsObj().get(paramName));
}
}else {
returnMsg += scriptRow;
} }
}else { }else {
returnMsg += scriptRow; newScript += scriptRow +";";
} }
} }
if(StringUtils.isNotEmpty(newScript)){
newScript += "\n";
}
} }
} }
return returnMsg; returnMap.put("script",newScript);
returnMap.put("returnMsg",returnMsg);
return returnMap;
} }
private static void runScript(String script, String scriptLanguage) { private static void runScript(String script, String scriptLanguage) {

View File

@ -46,27 +46,27 @@ export default {
children: [ children: [
{ {
title: this.$t('api_test.request.address'), title: this.$t('api_test.request.address'),
value: '\nreturnMsg.add(@address)\n', value: 'returnMsg.add(@address);',
}, },
{ {
title: "Header "+this.$t('api_test.definition.document.request_param'), title: "Header "+this.$t('api_test.definition.document.request_param'),
value: '\nreturnMsg.add(@header(${param}))\n', value: 'returnMsg.add(@header(${param}));',
}, },
{ {
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: '\nreturnMsg.add(@body(${param}))\n', value: 'returnMsg.add(@body(${param}));',
}, },
{ {
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: '\nreturnMsg.add(@bodyRaw)\n', value: 'returnMsg.add(@bodyRaw);',
}, },
{ {
title: "Query "+this.$t('api_test.definition.document.request_param'), title: "Query "+this.$t('api_test.definition.document.request_param'),
value: '\nreturnMsg.add(@query(${param}))\n', value: 'returnMsg.add(@query(${param}));',
}, },
{ {
title: "Rest "+this.$t('api_test.definition.document.request_param'), title: "Rest "+this.$t('api_test.definition.document.request_param'),
value: '\nreturnMsg.add(@rest(${param}))\n', value: 'returnMsg.add(@rest(${param}));',
}, },
] ]