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"); } }