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,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 newScript = "";
if(StringUtils.isNotEmpty(script)){
String [] scriptRowArr = StringUtils.split(script,"\n");
for (String scriptRow : scriptRowArr) {
scriptRow = scriptRow.trim();
if(StringUtils.startsWith(scriptRow,"returnMsg.add(") && StringUtils.endsWith(scriptRow,")")){
scriptRow = scriptRow.substring(14,scriptRow.length()-1).trim();
if(StringUtils.equalsIgnoreCase(scriptRow,"@address")){
returnMsg += url;
}else if(StringUtils.startsWith(scriptRow,"@header(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(10,scriptRow.length()-2);
if(headerMap.containsKey(paramName)){
returnMsg += headerMap.get(paramName);
}
}else if(StringUtils.startsWith(scriptRow,"@body(${") && StringUtils.endsWith(scriptRow,"})")){
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));
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();
if(StringUtils.equalsIgnoreCase(scriptRow,"@address")){
returnMsg += url;
}else if(StringUtils.startsWith(scriptRow,"@header(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(10,scriptRow.length()-2);
if(headerMap.containsKey(paramName)){
returnMsg += headerMap.get(paramName);
}
}
}else if(StringUtils.equalsIgnoreCase(scriptRow,"@bodyRaw")){
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey("raw")){
returnMsg += String.valueOf(bodyParamObj.get("raw"));
}else if(StringUtils.startsWith(scriptRow,"@body(${") && StringUtils.endsWith(scriptRow,"})")){
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,"@query(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(9,scriptRow.length()-2);
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 if(StringUtils.equalsIgnoreCase(scriptRow,"@bodyRaw")){
if(requestMockParams.getBodyParams() != null && requestMockParams.getBodyParams().size() > 0){
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
if(bodyParamObj.containsKey("raw")){
returnMsg += String.valueOf(bodyParamObj.get("raw"));
}
}
}else if(StringUtils.startsWith(scriptRow,"@query(${") && StringUtils.endsWith(scriptRow,"})")){
String paramName = scriptRow.substring(9,scriptRow.length()-2);
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 {
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) {

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}));',
},
]