From 55a610a1f1e440402261e34ea6b6a432e19e9ec7 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 17 Apr 2023 17:00:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20TC?= =?UTF-8?q?PMock=E5=90=8E=E7=BD=AE=E8=84=9A=E6=9C=AC=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=8E=B7=E5=8F=96tcp=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1025471 --user=宋天阳 【接口测试】gtihub#23671,【接口测试】接口定义-tcp mock 后置脚本获取不到求内容 https://www.tapd.cn/55049933/s/1363471 --- .../api/dto/mock/RequestMockParams.java | 3 ++- .../api/tcp/server/TCPService.java | 11 +++++++---- .../commons/utils/mock/MockApiUtils.java | 1 - .../utils/mock/MockScriptEngineUtils.java | 19 +++++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/mock/RequestMockParams.java b/api-test/backend/src/main/java/io/metersphere/api/dto/mock/RequestMockParams.java index 3551c818e2..0675c24eed 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/mock/RequestMockParams.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/mock/RequestMockParams.java @@ -25,9 +25,10 @@ public class RequestMockParams { private JSONObject xmlToJsonParam; - private String raw; + private String tcpParam; + public boolean isEmpty() { boolean isJsonParamEmpty = false; diff --git a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java index a6e9602b70..5061d0d24f 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java @@ -1,11 +1,12 @@ package io.metersphere.api.tcp.server; import io.metersphere.api.dto.mock.MockExpectConfigDTO; +import io.metersphere.api.dto.mock.RequestMockParams; +import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.commons.utils.JSONUtil; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.mock.MockApiUtils; import io.metersphere.service.MockConfigService; -import io.metersphere.commons.utils.CommonBeanFactory; -import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.JSONUtil; import org.apache.commons.lang3.StringUtils; import org.json.JSONObject; @@ -66,7 +67,9 @@ public class TCPService { if (respResultObj.has("usePostScript")) { useScript = respResultObj.getBoolean("usePostScript"); } - returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.optJSONObject("body"), StringUtils.EMPTY, null, null, useScript); + RequestMockParams requestMockParams = new RequestMockParams(); + requestMockParams.setTcpParam(message); + returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.optJSONObject("body"), StringUtils.EMPTY, null, requestMockParams, useScript); } try { if (respResultObj.has("delayed")) { 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 48d1eac183..f439276034 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 @@ -9,7 +9,6 @@ import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.enums.MockParamConditionEnums; import io.metersphere.commons.enums.MockRequestType; -import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.*; import io.metersphere.jmeter.utils.ScriptEngineUtils; import jakarta.servlet.http.HttpServletRequest; 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 12581c9aef..2d98dced5a 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 @@ -89,6 +89,14 @@ public class MockScriptEngineUtils { return engine; } + private String escapeString(String str) { + if (str == null) { + return str; + } else { + return StringUtils.replace(str, "\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n"); + } + } + private String genBeanshellPreScript(String url, Map headerMap, RequestMockParams requestMockParams) { StringBuilder preScriptBuffer = new StringBuilder(); preScriptBuffer.append("Map vars = new HashMap();\n"); @@ -105,6 +113,11 @@ public class MockScriptEngineUtils { } if (requestMockParams != null) { + //判断是否含有tcpParam + if (StringUtils.isNotEmpty(requestMockParams.getTcpParam())) { + String value = this.escapeString(requestMockParams.getTcpParam()); + preScriptBuffer.append("vars.put(\"tcpParam\",\"").append(value).append("\");\n"); + } //写入body参数 if (requestMockParams.isPost()) { if (requestMockParams.getQueryParamsObj() != null) { @@ -175,6 +188,12 @@ public class MockScriptEngineUtils { preScriptBuffer.append("vars[\"header.").append(headerKey).append("\"]=\"").append(headerValue).append("\";\n"); } if (requestMockParams != null) { + //判断是否含有tcpParam + if (StringUtils.isNotEmpty(requestMockParams.getTcpParam())) { + String value = requestMockParams.getTcpParam(); + value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\""); + preScriptBuffer.append("vars[\"bodyRaw\"]=\"").append(value).append("\";\n"); + } //写入body参数 if (requestMockParams.isPost()) { if (requestMockParams.getQueryParamsObj() != null) {