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 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);
}
}
@ -364,11 +367,15 @@ 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 newScript = "";
if(StringUtils.isNotEmpty(script)){
String [] scriptRowArr = StringUtils.split(script,"\n");
for (String scriptRow : scriptRowArr) {
for (String scriptItemRows : scriptRowArr) {
String [] scriptItemArr = scriptItemRows.split(";");
for (String scriptRow :scriptItemArr) {
scriptRow = scriptRow.trim();
if(StringUtils.startsWith(scriptRow,"returnMsg.add(") && StringUtils.endsWith(scriptRow,")")){
scriptRow = scriptRow.substring(14,scriptRow.length()-1).trim();
@ -407,11 +414,18 @@ public class MockApiUtils {
}else {
returnMsg += scriptRow;
}
}
}else {
newScript += scriptRow +";";
}
}
return returnMsg;
if(StringUtils.isNotEmpty(newScript)){
newScript += "\n";
}
}
}
returnMap.put("script",newScript);
returnMap.put("returnMsg",returnMsg);
return returnMap;
}
private static void runScript(String script, String scriptLanguage) {

View File

@ -46,27 +46,27 @@ export default {
children: [
{
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'),
value: '\nreturnMsg.add(@header(${param}))\n',
value: 'returnMsg.add(@header(${param}));',
},
{
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)",
value: '\nreturnMsg.add(@bodyRaw)\n',
value: 'returnMsg.add(@bodyRaw);',
},
{
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'),
value: '\nreturnMsg.add(@rest(${param}))\n',
value: 'returnMsg.add(@rest(${param}));',
},
]