diff --git a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java index c940235591..e0c6c185f5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java @@ -351,10 +351,13 @@ public class MockApiUtils { String script = scriptObj.getString("script"); String scriptLanguage =scriptObj.getString("scriptLanguage"); - returnStr = parseScript(script,url,headerMap,requestMockParams); - - - + Map 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 headerMap,RequestMockParams requestMockParams) { + private static Map parseScript(String script,String url,Map headerMap,RequestMockParams requestMockParams) { + Map 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) { diff --git a/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue b/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue index e02d4f2789..46af689ba5 100644 --- a/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue +++ b/frontend/src/business/components/api/definition/components/mock/Components/MockApiScriptEditor.vue @@ -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}));', }, ]