From 00b62ec3072c32a60de4357a0de986f5b0cc9146 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 21 Oct 2022 17:01:43 +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=E8=AF=B7=E6=B1=82=E5=A4=B4=E6=9C=89?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC=E6=97=B6=E5=8C=B9=E9=85=8D=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1018616 --user=宋天阳 【接口测试】github#18704,mock请求头值以空格开头,匹配的响应不对 https://www.tapd.cn/55049933/s/1271170 --- .../service/MockConfigService.java | 42 ++++--------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java index 2c119128a6..bf83c736fe 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java @@ -11,20 +11,8 @@ import io.metersphere.api.dto.mock.config.response.MockConfigResponse; import io.metersphere.api.dto.mock.config.response.MockExpectConfigResponse; import io.metersphere.api.parse.api.ApiDefinitionImport; import io.metersphere.api.parse.scenario.TcpTreeTableDataParser; -import io.metersphere.service.definition.ApiDefinitionService; -import io.metersphere.service.ext.ExtProjectApplicationService; import io.metersphere.api.tcp.TCPPool; -import io.metersphere.base.domain.ApiDefinitionWithBLOBs; -import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; -import io.metersphere.base.domain.MockConfig; -import io.metersphere.base.domain.MockConfigExample; -import io.metersphere.base.domain.MockExpectConfig; -import io.metersphere.base.domain.MockExpectConfigExample; -import io.metersphere.base.domain.MockExpectConfigWithBLOBs; -import io.metersphere.base.domain.Project; -import io.metersphere.base.domain.ProjectApplication; -import io.metersphere.base.domain.ProjectApplicationExample; -import io.metersphere.base.domain.ProjectExample; +import io.metersphere.base.domain.*; import io.metersphere.base.mapper.MockConfigMapper; import io.metersphere.base.mapper.MockExpectConfigMapper; import io.metersphere.base.mapper.ProjectApplicationMapper; @@ -33,19 +21,13 @@ import io.metersphere.base.mapper.ext.ExtMockExpectConfigMapper; import io.metersphere.commons.constants.ProjectApplicationType; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.BeanUtils; -import io.metersphere.commons.utils.FileUtils; -import io.metersphere.commons.utils.JSON; -import io.metersphere.commons.utils.LogUtil; -import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.commons.utils.*; +import io.metersphere.commons.utils.mock.MockApiUtils; import io.metersphere.dto.ProjectConfig; import io.metersphere.environment.service.BaseEnvironmentService; import io.metersphere.i18n.Translator; -import io.metersphere.commons.utils.JSONUtil; -import io.metersphere.commons.utils.JSONValidator; -import io.metersphere.commons.utils.JsonStructUtils; -import io.metersphere.commons.utils.XMLUtil; -import io.metersphere.commons.utils.mock.MockApiUtils; +import io.metersphere.service.definition.ApiDefinitionService; +import io.metersphere.service.ext.ExtProjectApplicationService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -65,11 +47,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -341,7 +319,7 @@ public class MockConfigService { JSONObject jsonObject = headerArr.optJSONObject(i); if (jsonObject.has("name") && jsonObject.has("value")) { String headerName = jsonObject.optString("name"); - String headerValue = jsonObject.optString("value"); + String headerValue = StringUtils.trim(jsonObject.optString("value")); if (StringUtils.isNotEmpty(headerName)) { if (!requestHeaderMap.containsKey(headerName) || !StringUtils.equals(requestHeaderMap.get(headerName), headerValue)) { return false; @@ -461,8 +439,6 @@ public class MockConfigService { } } } - -// boolean isMatching = JsonStructUtils.checkJsonObjCompliance(reqJsonObj, mockExpectJson); return matchRest || matchQuery || matchBody; } @@ -857,7 +833,7 @@ public class MockConfigService { String url = request.getRequestURL().toString(); if (project != null) { String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); - List aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix,requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID)); + List aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID)); Object paramJson = MockApiUtils.getPostParamMap(request); JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request); for (ApiDefinitionWithBLOBs api : aualifiedApiList) { @@ -891,7 +867,7 @@ public class MockConfigService { List aualifiedApiList = new ArrayList<>(); if (project != null) { String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); - aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix,requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID)); + aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID)); /** * GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况: