From 9c6a06a3b886ce4b0f6ebe1af6d62acb7d3b7acb Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 13 Oct 2022 16:33:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmock=E6=B5=8B=E8=AF=95=E7=94=B1=E4=BA=8E?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2JSON=E5=B7=A5=E5=85=B7=E5=8C=85=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=8C=B9=E9=85=8D=E4=B8=8D=E5=88=B0=E6=9C=9F=E6=9C=9B?= =?UTF-8?q?=E7=AD=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1017962 --user=宋天阳 【接口测试】有一个mock匹配不到对应的期望 https://www.tapd.cn/55049933/s/1260589;--bug=1017962 --user=宋天阳 【接口测试】有一个mock匹配不到对应的期望 https://www.tapd.cn/55049933/s/1260727 --- .../request/sampler/MsTCPSampler.java | 10 +++++----- .../api/dto/scenario/TCPConfig.java | 2 +- .../commons/utils/mock/MockApiUtils.java | 20 ++++++++++--------- .../utils/mock/MockScriptEngineUtils.java | 20 +++++++++++++++++-- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index 0bf8f44275..8a2ac71cde 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -14,19 +14,19 @@ import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.environment.GlobalScriptFilterRequest; import io.metersphere.api.parse.api.JMeterScriptUtil; import io.metersphere.api.parse.scenario.TcpTreeTableDataParser; -import io.metersphere.service.definition.ApiDefinitionService; -import io.metersphere.service.definition.ApiTestCaseService; import io.metersphere.base.domain.ApiDefinitionWithBLOBs; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.commons.utils.HashTreeUtil; +import io.metersphere.commons.utils.JSONUtil; import io.metersphere.commons.utils.LogUtil; import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; -import io.metersphere.commons.utils.HashTreeUtil; -import io.metersphere.commons.utils.JSONUtil; +import io.metersphere.service.definition.ApiDefinitionService; +import io.metersphere.service.definition.ApiTestCaseService; import io.metersphere.utils.LoggerUtil; import lombok.Data; import lombok.EqualsAndHashCode; @@ -225,7 +225,7 @@ public class MsTCPSampler extends MsTestElement { if (!isCustomizeReq() && config != null && config.getTcpConfig() != null) { if (!isCustomizeReq() && config != null) { this.server = config.getTcpConfig().getServer(); - this.port = config.getTcpConfig().getPort(); + this.port = config.getTcpConfig().getPort() + ""; if (StringUtils.equals(this.eolByte, " ")) { this.eolByte = ""; } else { diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/TCPConfig.java b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/TCPConfig.java index 47d74d5d6d..a549c0afaf 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/TCPConfig.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/scenario/TCPConfig.java @@ -6,7 +6,7 @@ import lombok.Data; public class TCPConfig { private String classname = ""; private String server = ""; - private String port = ""; + private int port = 0; private String ctimeout = ""; private String timeout = ""; private boolean reUseConnection = true; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java index 335d13e44d..56ec9f3b34 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java @@ -8,11 +8,7 @@ import io.metersphere.api.exec.generator.JSONSchemaGenerator; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.enums.MockParamConditionEnums; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.JSON; -import io.metersphere.commons.utils.JSONUtil; -import io.metersphere.commons.utils.JSONValidator; -import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.XMLUtil; +import io.metersphere.commons.utils.*; import io.metersphere.jmeter.utils.ScriptEngineUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -355,10 +351,16 @@ public class MockApiUtils { RequestMockParams returnParams = getGetParamMap(urlParams, apiPath, queryParamsObject, isPostRequest); if (paramJson != null) { if (paramJson instanceof JSONObject) { - if (((JSONObject) paramJson) != null) { - List paramsArray = new LinkedList<>(); - paramsArray.add(paramJson); - returnParams.setBodyParams(new JSONArray(paramsArray)); + if (!((JSONObject) paramJson).keySet().isEmpty()) { + JSONArray bodyParams = returnParams.getBodyParams(); + if (bodyParams == null) { + List paramsArray = new LinkedList<>(); + paramsArray.add(paramJson); + bodyParams = new JSONArray(paramsArray); + } else { + bodyParams.put(((JSONObject) paramJson)); + } + returnParams.setBodyParams(bodyParams); } } else if (paramJson instanceof JSONArray) { JSONArray paramArray = (JSONArray) paramJson; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java index 1913665b03..85ca57393b 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockScriptEngineUtils.java @@ -113,6 +113,8 @@ public class MockScriptEngineUtils { for (Map.Entry headEntry : headerMap.entrySet()) { String headerKey = headEntry.getKey(); String headerValue = headEntry.getValue(); + headerKey = StringUtils.replace(headerKey, "\\", "\\\\").replace("\"", "\\\""); + headerValue = StringUtils.replace(headerValue, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n"); } } @@ -127,6 +129,7 @@ public class MockScriptEngineUtils { String value = String.valueOf(bodyParamObj.get(key)); value = StringUtils.replace(value, "\\", "\\\\"); value = StringUtils.replace(value, "\"", "\\\""); + key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars.put(\"body." + key + "\",\"" + value + "\");\n"); if (StringUtils.equalsIgnoreCase(key, "raw")) { preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + value + "\");\n"); @@ -137,7 +140,9 @@ public class MockScriptEngineUtils { jsonBody = StringUtils.replace(jsonBody, "\"", "\\\""); preScriptBuffer.append("vars.put(\"body.json\",\"" + jsonBody + "\");\n"); } else { - preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + JSON.toJSONString(requestMockParams.getBodyParams()) + "\");\n"); + String bodyRowString = requestMockParams.getBodyParams().toString(); + bodyRowString = StringUtils.replace(bodyRowString, "\\", "\\\\").replace("\"", "\\\""); + preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + bodyRowString + "\");\n"); } } @@ -148,6 +153,7 @@ public class MockScriptEngineUtils { String value = String.valueOf(queryParamsObj.get(key)); value = StringUtils.replace(value, "\\", "\\\\"); value = StringUtils.replace(value, "\"", "\\\""); + key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars.put(\"query." + key + "\",\"" + value + "\");\n"); } } @@ -156,6 +162,9 @@ public class MockScriptEngineUtils { JSONObject restParamsObj = requestMockParams.getRestParamsObj(); for (String key : restParamsObj.keySet()) { String value = String.valueOf(restParamsObj.get(key)); + key = StringUtils.replace(key, "\"", "\\\""); + value = StringUtils.replace(value, "\"", "\\\""); + key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars.put(\"rest." + key + "\",\"" + value + "\");\n"); } } @@ -171,6 +180,8 @@ public class MockScriptEngineUtils { for (Map.Entry headEntry : headerMap.entrySet()) { String headerKey = headEntry.getKey(); String headerValue = headEntry.getValue(); + headerKey = StringUtils.replace(headerKey, "\\", "\\\\").replace("\"", "\\\""); + headerValue = StringUtils.replace(headerValue, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars[\"header." + headerKey + "\"]=\"" + headerValue + "\";\n"); } //写入body参数 @@ -182,6 +193,7 @@ public class MockScriptEngineUtils { String value = String.valueOf(bodyParamObj.get(key)); value = StringUtils.replace(value, "\\", "\\\\"); value = StringUtils.replace(value, "\"", "\\\""); + key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars[\"body." + key + "\"]=\"" + value + "\";\n"); if (StringUtils.equalsIgnoreCase(key, "raw")) { preScriptBuffer.append("vars[\"bodyRaw\"]=\"" + value + "\";\n"); @@ -192,7 +204,8 @@ public class MockScriptEngineUtils { jsonBody = StringUtils.replace(jsonBody, "\"", "\\\""); preScriptBuffer.append("vars[\"body.json\"]=\"" + jsonBody + "\";\n"); } else { - preScriptBuffer.append("vars[\"bodyRaw\"]=\"" + JSON.toJSONString(requestMockParams.getBodyParams()) + "\";\n"); + String bodyRaw = StringUtils.replace(requestMockParams.getBodyParams().toString(), "\\", "\\\\").replace("\"", "\\\""); + preScriptBuffer.append("vars[\"bodyRaw\"]=\"" + bodyRaw + "\";\n"); } } @@ -203,6 +216,7 @@ public class MockScriptEngineUtils { String value = String.valueOf(queryParamsObj.get(key)); value = StringUtils.replace(value, "\\", "\\\\"); value = StringUtils.replace(value, "\"", "\\\""); + key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars[\"query." + key + "\"]=\"" + value + "\";\n"); } } @@ -211,6 +225,8 @@ public class MockScriptEngineUtils { JSONObject restParamsObj = requestMockParams.getRestParamsObj(); for (String key : restParamsObj.keySet()) { String value = String.valueOf(restParamsObj.get(key)); + key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\""); + value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\""); preScriptBuffer.append("vars[\"rest." + key + "\"]=\"" + value + "\";\n"); } }