fix(接口测试): TCPMock后置脚本支持获取tcp请求参数

--bug=1025471 --user=宋天阳 【接口测试】gtihub#23671,【接口测试】接口定义-tcp mock
后置脚本获取不到求内容 https://www.tapd.cn/55049933/s/1363471
This commit is contained in:
song-tianyang 2023-04-17 17:00:34 +08:00 committed by 建国
parent e5267db35c
commit 55a610a1f1
4 changed files with 28 additions and 6 deletions

View File

@ -25,9 +25,10 @@ public class RequestMockParams {
private JSONObject xmlToJsonParam; private JSONObject xmlToJsonParam;
private String raw; private String raw;
private String tcpParam;
public boolean isEmpty() { public boolean isEmpty() {
boolean isJsonParamEmpty = false; boolean isJsonParamEmpty = false;

View File

@ -1,11 +1,12 @@
package io.metersphere.api.tcp.server; package io.metersphere.api.tcp.server;
import io.metersphere.api.dto.mock.MockExpectConfigDTO; 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.commons.utils.mock.MockApiUtils;
import io.metersphere.service.MockConfigService; 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.apache.commons.lang3.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
@ -66,7 +67,9 @@ public class TCPService {
if (respResultObj.has("usePostScript")) { if (respResultObj.has("usePostScript")) {
useScript = respResultObj.getBoolean("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 { try {
if (respResultObj.has("delayed")) { if (respResultObj.has("delayed")) {

View File

@ -9,7 +9,6 @@ import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.enums.MockParamConditionEnums; import io.metersphere.commons.enums.MockParamConditionEnums;
import io.metersphere.commons.enums.MockRequestType; import io.metersphere.commons.enums.MockRequestType;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;

View File

@ -89,6 +89,14 @@ public class MockScriptEngineUtils {
return engine; 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<String, String> headerMap, RequestMockParams requestMockParams) { private String genBeanshellPreScript(String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
StringBuilder preScriptBuffer = new StringBuilder(); StringBuilder preScriptBuffer = new StringBuilder();
preScriptBuffer.append("Map vars = new HashMap();\n"); preScriptBuffer.append("Map vars = new HashMap();\n");
@ -105,6 +113,11 @@ public class MockScriptEngineUtils {
} }
if (requestMockParams != null) { 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参数 //写入body参数
if (requestMockParams.isPost()) { if (requestMockParams.isPost()) {
if (requestMockParams.getQueryParamsObj() != null) { if (requestMockParams.getQueryParamsObj() != null) {
@ -175,6 +188,12 @@ public class MockScriptEngineUtils {
preScriptBuffer.append("vars[\"header.").append(headerKey).append("\"]=\"").append(headerValue).append("\";\n"); preScriptBuffer.append("vars[\"header.").append(headerKey).append("\"]=\"").append(headerValue).append("\";\n");
} }
if (requestMockParams != null) { 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参数 //写入body参数
if (requestMockParams.isPost()) { if (requestMockParams.isPost()) {
if (requestMockParams.getQueryParamsObj() != null) { if (requestMockParams.getQueryParamsObj() != null) {