From f641f6f6b7ef35fe5d21a24cf7b0da5e5ef677f3 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 10 Mar 2022 18:59:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8DMock=E6=9C=9F=E6=9C=9B=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8JMeter=E5=87=BD=E6=95=B0=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E5=AF=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复Mock期望可以使用JMeter函数提示对问题 --- .../api/mock/utils/MockApiUtils.java | 17 ++- .../api/mock/utils/MockScriptEngineUtils.java | 2 +- .../api/service/MockConfigService.java | 133 ------------------ .../scenario/component/ComponentConfig.vue | 82 ++++++----- .../schema/editor/mock/MockComplete.vue | 7 +- frontend/src/business/components/xpack | 2 +- frontend/src/i18n/en-US.js | 1 + frontend/src/i18n/zh-CN.js | 1 + frontend/src/i18n/zh-TW.js | 1 + 9 files changed, 60 insertions(+), 186 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java index ac5d7c7848..8fc7115838 100644 --- a/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java @@ -146,7 +146,7 @@ public class MockApiUtils { return returnJson; } - public static JSONObject parseJsonSchema(JSONObject bodyReturnObj) { + public static JSONObject parseJsonSchema(JSONObject bodyReturnObj,boolean useJMeterFunc) { JSONObject returnObj = new JSONObject(); if (bodyReturnObj == null) { return returnObj; @@ -157,7 +157,7 @@ public class MockApiUtils { try { JsonSchemaReturnObj obj = bodyReturnObj.getObject(key, JsonSchemaReturnObj.class); if (StringUtils.equals("object", obj.getType())) { - JSONObject itemObj = parseJsonSchema(obj.getProperties()); + JSONObject itemObj = parseJsonSchema(obj.getProperties(),useJMeterFunc); if (!itemObj.isEmpty()) { returnObj.put(key, itemObj); } @@ -167,13 +167,16 @@ public class MockApiUtils { if (itemObj.containsKey("type")) { if (StringUtils.equals("object", itemObj.getString("type")) && itemObj.containsKey("properties")) { JSONObject arrayObj = itemObj.getJSONObject("properties"); - JSONObject parseObj = parseJsonSchema(arrayObj); + JSONObject parseObj = parseJsonSchema(arrayObj,useJMeterFunc); JSONArray array = new JSONArray(); array.add(parseObj); returnObj.put(key, array); } else if (StringUtils.equals("string", itemObj.getString("type")) && itemObj.containsKey("mock")) { JsonSchemaReturnObj arrayObj = JSONObject.toJavaObject(itemObj, JsonSchemaReturnObj.class); - String value = getMockValues(arrayObj.getMockValue()); + String value = arrayObj.getMockValue(); + if(useJMeterFunc){ + value = getMockValues(arrayObj.getMockValue()); + } JSONArray array = new JSONArray(); array.add(value); returnObj.put(key, array); @@ -184,7 +187,7 @@ public class MockApiUtils { String values = obj.getMockValue(); if (StringUtils.isEmpty(values)) { values = ""; - } else { + } else if(useJMeterFunc){ try { values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values; } catch (Exception e) { @@ -254,7 +257,7 @@ public class MockApiUtils { if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) { String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString(); JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr); - JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj); + JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj,false); returnStr = returnObj.toJSONString(); } } else { @@ -379,7 +382,7 @@ public class MockApiUtils { if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) { String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString(); JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr); - JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj); + JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj,false); returnStr = returnObj.toJSONString(); } } else { 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 a34e2fa09a..941a05a26b 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 @@ -257,7 +257,7 @@ public class MockScriptEngineUtils { LogUtil.error(e); } } - if (StringUtils.isEmpty(value)) { + if (StringUtils.isEmpty(value) || StringUtils.equals(value,"null")) { value = paramKey; } return value; diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 4b20ccb71c..7be70b524b 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -782,139 +782,6 @@ public class MockConfigService { return returnStr; } - public String updateHttpServletResponse(List apis, HttpServletResponse response) { - String returnStr = ""; - try { - if (CollectionUtils.isEmpty(apis)) { - response.setStatus(404); - } else { - for (ApiDefinitionWithBLOBs api : apis) { - int status = 404; - if (api.getResponse() != null) { - JSONObject respObj = JSONObject.parseObject(api.getResponse()); - if (respObj.containsKey("headers")) { - JSONArray headersArr = respObj.getJSONArray("headers"); - for (int i = 0; i < headersArr.size(); i++) { - JSONObject obj = headersArr.getJSONObject(i); - if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) { - response.setHeader(obj.getString("name"), obj.getString("value")); - } - } - } - if (respObj.containsKey("statusCode")) { - JSONArray statusCodeArr = respObj.getJSONArray("statusCode"); - for (int i = 0; i < statusCodeArr.size(); i++) { - JSONObject obj = statusCodeArr.getJSONObject(i); - if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) { -// response.setHeader(obj.getString("name"), obj.getString("value")); - try { - status = Integer.parseInt(obj.getString("name")); - } catch (Exception e) { - } - } - } - } - if (respObj.containsKey("body")) { - JSONObject bodyObj = respObj.getJSONObject("body"); - if (bodyObj.containsKey("type")) { - String type = bodyObj.getString("type"); - if (StringUtils.equals(type, "JSON")) { - //判断是否是JsonSchema - boolean isJsonSchema = false; - if (bodyObj.containsKey("format")) { - String foramtValue = String.valueOf(bodyObj.get("format")); - if (StringUtils.equals("JSON-SCHEMA", foramtValue)) { - isJsonSchema = true; - } - } - if (isJsonSchema) { - if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) { - String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString(); - JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr); - JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj); - returnStr = returnObj.toJSONString(); - } - } else { - if (bodyObj.containsKey("raw")) { - returnStr = bodyObj.getString("raw"); - } - } - } else if (StringUtils.equalsAny(type, "XML", "Raw")) { - if (bodyObj.containsKey("raw")) { - String raw = bodyObj.getString("raw"); - returnStr = raw; - } - } else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) { - Map paramMap = new LinkedHashMap<>(); - if (bodyObj.containsKey("kvs")) { - JSONArray bodyParamArr = new JSONArray(); - JSONArray kvsArr = bodyObj.getJSONArray("kvs"); - for (int i = 0; i < kvsArr.size(); i++) { - JSONObject kv = kvsArr.getJSONObject(i); - if (kv.containsKey("name")) { - String values = kv.getString("value"); - if (StringUtils.isEmpty(values)) { - values = ""; - } else { - try { - values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values; - } catch (Exception e) { - } - } - paramMap.put(kv.getString("name"), values); - } - } - } - returnStr = JSONObject.toJSONString(paramMap); - //Binary的先不处理 -// } else if (StringUtils.equals(type, "BINARY")) { -// Map paramMap = new LinkedHashMap<>(); -// if (bodyObj.containsKey("binary")) { -// JSONArray kvsArr = bodyObj.getJSONArray("kvs"); -// for (int i = 0; i < kvsArr.size(); i++) { -// JSONObject kv = kvsArr.getJSONObject(i); -// if (kv.containsKey("description") && kv.containsKey("files")) { -// String name = kv.getString("description"); -// JSONArray fileArr = kv.getJSONArray("files"); -// String allValue = ""; -// for (int j = 0; j < fileArr.size(); j++) { -// JSONObject fileObj = fileArr.getJSONObject(j); -// if (fileObj.containsKey("name")) { -// String values = fileObj.getString("name"); -// if (StringUtils.isEmpty(values)) { -// values = ""; -// } else { -// try { -// values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values; -// } catch (Exception e) { -// } -// } -// -// allValue += values + " ;"; -// } -// } -// paramMap.put(name, allValue); -// } -// } -// } -// returnStr = JSONObject.toJSONString(paramMap); - } - } - } - } - if (StringUtils.isNotEmpty(returnStr) && status == 404) { - status = 200; - } - response.setStatus(status); - } - } - - } catch (Exception e) { - LogUtil.error(e); - } - return returnStr; - } - public MockExpectConfigWithBLOBs findMockExpectConfigById(String id) { return mockExpectConfigMapper.selectByPrimaryKey(id); } diff --git a/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue b/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue index 0539cc3d7e..a108b8b571 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue @@ -1,47 +1,45 @@ diff --git a/frontend/src/business/components/common/json-schema/schema/editor/mock/MockComplete.vue b/frontend/src/business/components/common/json-schema/schema/editor/mock/MockComplete.vue index 369af43a39..23c8609d41 100644 --- a/frontend/src/business/components/common/json-schema/schema/editor/mock/MockComplete.vue +++ b/frontend/src/business/components/common/json-schema/schema/editor/mock/MockComplete.vue @@ -67,8 +67,11 @@ }, methods: { funcSearch(queryString, cb) { - let funcs = MOCKJS_FUNC.concat(JMETER_FUNC); - let results = queryString ? funcs.filter(this.funcFilter(queryString)) : funcs; + let results = []; + if(!this.showMockVars){ + let funcs = MOCKJS_FUNC.concat(JMETER_FUNC); + results = queryString ? funcs.filter(this.funcFilter(queryString)) : funcs; + } // 调用 callback 返回建议列表的数据 cb(results); }, diff --git a/frontend/src/business/components/xpack b/frontend/src/business/components/xpack index f12e04064d..fe4593657e 160000 --- a/frontend/src/business/components/xpack +++ b/frontend/src/business/components/xpack @@ -1 +1 @@ -Subproject commit f12e04064dc1288becfa63c48c4db59dc5322292 +Subproject commit fe4593657ec9d2733ad31057fa636a3562e38230 diff --git a/frontend/src/i18n/en-US.js b/frontend/src/i18n/en-US.js index a3ac32eb32..fd363d9576 100644 --- a/frontend/src/i18n/en-US.js +++ b/frontend/src/i18n/en-US.js @@ -3056,6 +3056,7 @@ export default { error_report_library: { name: "Error report", assertion: "Error code assertion", + tips:"Tips: Use error report in \"Project Settings - Project environment-Assertions\"", use_error_report: "Use error code", use_desc: "In case of conflict between error code and error assertions, it is treated as error code", option: { diff --git a/frontend/src/i18n/zh-CN.js b/frontend/src/i18n/zh-CN.js index f15968114f..80e829a88b 100644 --- a/frontend/src/i18n/zh-CN.js +++ b/frontend/src/i18n/zh-CN.js @@ -3060,6 +3060,7 @@ export default { error_report_library: { name: "误报库", assertion: "误报断言", + tips:"注:在\"项目设置-设置环境-全局断言\"中启用误报将进行以下规则匹配", use_error_report: "启用误报", use_desc: "失败断言与误报规则冲突时统一处理为误报", option: { diff --git a/frontend/src/i18n/zh-TW.js b/frontend/src/i18n/zh-TW.js index f9ed917cd8..3c0ed90d81 100644 --- a/frontend/src/i18n/zh-TW.js +++ b/frontend/src/i18n/zh-TW.js @@ -3059,6 +3059,7 @@ export default { error_report_library: { name: "誤報庫", assertion: "誤報斷言", + tips:"注:在\"項目設置-設置環境-全局斷言\"中啟用誤報將進行以下規則匹配", use_error_report: "啟用誤報", use_desc: "失敗斷言與誤報規則衝突時統一處理為誤報", option: {