fix(接口测试): 修复mock测试由于更换JSON工具包导致匹配不到期望等问题

--bug=1017962 --user=宋天阳 【接口测试】有一个mock匹配不到对应的期望
https://www.tapd.cn/55049933/s/1260589;--bug=1017962 --user=宋天阳
【接口测试】有一个mock匹配不到对应的期望 https://www.tapd.cn/55049933/s/1260727
This commit is contained in:
song-tianyang 2022-10-13 16:33:14 +08:00 committed by 建国
parent 03550f2c39
commit 9c6a06a3b8
4 changed files with 35 additions and 17 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -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<Object> paramsArray = new LinkedList<>();
paramsArray.add(paramJson);
returnParams.setBodyParams(new JSONArray(paramsArray));
if (!((JSONObject) paramJson).keySet().isEmpty()) {
JSONArray bodyParams = returnParams.getBodyParams();
if (bodyParams == null) {
List<Object> 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;

View File

@ -113,6 +113,8 @@ public class MockScriptEngineUtils {
for (Map.Entry<String, String> 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<String, String> 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");
}
}