From 3fc8708c51a95130a6269ad0c4581c9f419bced2 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 16 Feb 2022 16:33:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20T?= =?UTF-8?q?CP-Mock=E6=9C=9F=E6=9C=9B=E6=B7=BB=E5=8A=A0=E5=90=8E=E7=BD=AE?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TCP-Mock期望添加后置脚本功能 --- .../api/mock/utils/MockScriptEngineUtils.java | 115 +++++++++--------- .../api/tcp/server/TCPServicer.java | 2 +- .../mock/Components/MockApiScriptEditor.vue | 83 +++++++------ 3 files changed, 102 insertions(+), 98 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java b/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java index 764521e85e..25e0d12db5 100644 --- a/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java +++ b/backend/src/main/java/io/metersphere/api/mock/utils/MockScriptEngineUtils.java @@ -15,28 +15,28 @@ import java.util.regex.Pattern; public class MockScriptEngineUtils { - public JSONObject getVars(ScriptEngine engine){ + public JSONObject getVars(ScriptEngine engine) { try { return JSONObject.parseObject(JSONObject.toJSONString(engine.get("vars"))); - }catch (Exception e){ + } catch (Exception e) { LogUtil.error(e); } return new JSONObject(); } - public String get(ScriptEngine engine, String key){ + public String get(ScriptEngine engine, String key) { return String.valueOf(engine.get(key)); } - public void runScript(ScriptEngine engine, String script){ + public void runScript(ScriptEngine engine, String script) { try { engine.eval(script); - }catch (Exception e){ + } catch (Exception e) { LogUtil.error(e); } } - public ScriptEngine getBaseScriptEngine(String scriptLanguage, String url, Map headerMap, RequestMockParams requestMockParams){ + public ScriptEngine getBaseScriptEngine(String scriptLanguage, String url, Map headerMap, RequestMockParams requestMockParams) { ScriptEngine engine = null; try { if (StringUtils.isEmpty(scriptLanguage)) { @@ -53,7 +53,7 @@ public class MockScriptEngineUtils { String preScript = this.genPythonPreScript(url, headerMap, requestMockParams); engine.eval(preScript); } - }catch (Exception e){ + } catch (Exception e) { LogUtil.error(e); } return engine; @@ -64,50 +64,55 @@ public class MockScriptEngineUtils { preScriptBuffer.append("Map vars = new HashMap();\n"); preScriptBuffer.append("vars.put(\"address\",\"" + url + "\");\n"); //写入请求头 - for (Map.Entry headEntry : headerMap.entrySet()) { - String headerKey = headEntry.getKey(); - String headerValue = headEntry.getValue(); - preScriptBuffer.append("vars.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n"); + if (headerMap != null) { + for (Map.Entry headEntry : headerMap.entrySet()) { + String headerKey = headEntry.getKey(); + String headerValue = headEntry.getValue(); + preScriptBuffer.append("vars.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n"); + } } + //写入body参数 - if (requestMockParams.getBodyParams() != null) { - if (requestMockParams.getBodyParams().size() == 1) { - //参数是jsonObject - JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); - for (String key : bodyParamObj.keySet()) { - String value = String.valueOf(bodyParamObj.get(key)); + if (requestMockParams != null) { + if (requestMockParams.getBodyParams() != null) { + if (requestMockParams.getBodyParams().size() == 1) { + //参数是jsonObject + JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0); + 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, "\"", "\\\""); - preScriptBuffer.append("vars.put(\"body." + key + "\",\"" + value + "\");\n"); - if (StringUtils.equalsIgnoreCase(key, "raw")) { - preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + value + "\");\n"); - } + preScriptBuffer.append("vars.put(\"query." + key + "\",\"" + 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, "\"", "\\\""); - 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"); + //写入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(); @@ -172,37 +177,37 @@ public class MockScriptEngineUtils { Pattern pattern = Pattern.compile(regStr); Matcher matcher = pattern.matcher(reportString); List paramKeys = new ArrayList<>(); - while (matcher.find()){ + while (matcher.find()) { String paramKey = matcher.group(0); - if(!paramKeys.contains(paramKey)){ + if (!paramKeys.contains(paramKey)) { paramKeys.add(paramKey); } } JSONObject varsObject = this.getVars(scriptEngine); for (String paramKey : paramKeys) { - String value = this.getValue(scriptEngine,varsObject,paramKey); - reportString = StringUtils.replace(reportString,paramKey,value); + String value = this.getValue(scriptEngine, varsObject, paramKey); + reportString = StringUtils.replace(reportString, paramKey, value); } return reportString; } private String getValue(ScriptEngine scriptEngine, JSONObject varsObject, String paramKey) { String key = paramKey; - if(key.startsWith("${") && key.endsWith("}")){ - key = paramKey.substring(2,key.length()-1); + if (key.startsWith("${") && key.endsWith("}")) { + key = paramKey.substring(2, key.length() - 1); } String value = null; - if(varsObject != null && varsObject.containsKey(key)){ + if (varsObject != null && varsObject.containsKey(key)) { value = varsObject.getString(key); } - if(StringUtils.isEmpty(value)){ + if (StringUtils.isEmpty(value)) { try { value = JSONObject.toJSONString(scriptEngine.get(key)); - }catch (Exception e){ + } catch (Exception e) { LogUtil.error(e); } } - if(StringUtils.isEmpty(value)){ + if (StringUtils.isEmpty(value)) { value = paramKey; } return value; diff --git a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java index d77192dc6b..2c0a1b7b8f 100644 --- a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java +++ b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java @@ -60,7 +60,7 @@ public class TCPServicer { MockApiUtils mockApiUtils = new MockApiUtils(); boolean useScript = false; if(respResultObj.containsKey("usePostScript")){ - useScript = responseObj.getBoolean("usePostScript"); + useScript = respResultObj.getBoolean("usePostScript"); } returnMsg = mockApiUtils.getResultByResponseResult(respResultObj.getJSONObject("body"),"",null,null,useScript); } 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 22f0f046e4..a380dbdf14 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 @@ -80,48 +80,48 @@ export default { }, computed: { - httpCodeTemplates(){ + httpCodeTemplates() { let returnData = [ - { - title: "API" + this.$t('api_test.definition.document.request_info'), - children: [ - { - title: this.$t('api_test.request.address'), - value: this.getScript("address"), - }, - { - title: "Header " + this.$t('api_test.definition.document.request_param'), - value: this.getScript("header"), - }, - { - title: this.$t('api_test.request.body') + this.$t('api_test.variable'), - value: this.getScript("body"), - }, - { - title: this.$t('api_test.request.body') + this.$t('api_test.variable') + " (Raw)", - value: this.getScript("bodyRaw"), - }, - { - title: "Query " + this.$t('api_test.definition.document.request_param'), - value: this.getScript("query"), - }, - { - title: "Rest " + this.$t('api_test.definition.document.request_param'), - value: this.getScript("rest"), - }, + { + title: "API" + this.$t('api_test.definition.document.request_info'), + children: [ + { + title: this.$t('api_test.request.address'), + value: this.getScript("address"), + }, + { + title: "Header " + this.$t('api_test.definition.document.request_param'), + value: this.getScript("header"), + }, + { + title: this.$t('api_test.request.body') + this.$t('api_test.variable'), + value: this.getScript("body"), + }, + { + title: this.$t('api_test.request.body') + this.$t('api_test.variable') + " (Raw)", + value: this.getScript("bodyRaw"), + }, + { + title: "Query " + this.$t('api_test.definition.document.request_param'), + value: this.getScript("query"), + }, + { + title: "Rest " + this.$t('api_test.definition.document.request_param'), + value: this.getScript("rest"), + }, - ] - }, - { - title: this.$t('project.code_segment.code_segment'), - children: [ - { - title: this.$t('project.code_segment.insert_segment'), - command: "custom_function", - } - ] - }, - ]; + ] + }, + { + title: this.$t('project.code_segment.code_segment'), + children: [ + { + title: this.$t('project.code_segment.insert_segment'), + command: "custom_function", + } + ] + }, + ]; return returnData; } }, @@ -144,13 +144,12 @@ export default { jsr223Processor() { this.reload(); }, - 'jsr223Processor.scriptLanguage'(){ + 'jsr223Processor.scriptLanguage'() { if (this.showApi) { this.baseCodeTemplates = this.httpCodeTemplates; } else { this.baseCodeTemplates = this.tcpCodeTemplates; } - alert(JSON.stringify(this.baseCodeTemplates)); } } ,