From 7496801454ef7d355580439ea3345382deb126af Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 21 Feb 2022 18:31:18 +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=8Dapi=E5=AE=9A=E4=B9=89=E4=B8=8D=E5=A1=AB?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E4=BD=93=EF=BC=8Cmock=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=9C=AA=E6=89=BE=E5=88=B0mock=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1010453 --user=宋天阳 【接口测试】api定义不填响应体,mock匹配不到期望应该显示api定义的响应体 https://www.tapd.cn/55049933/s/1106962 --- .../api/service/MockConfigService.java | 60 +++++++++---------- .../api/tcp/server/TCPServicer.java | 1 + 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index d3b84ab8a1..5f65b9ed10 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -1116,7 +1116,7 @@ public class MockConfigService { public String checkReturnWithMockExpectByBodyParam(String method, Map requestHeaderMap, Project project, HttpServletRequest request, HttpServletResponse response) { String returnStr = ""; - boolean isMatch = false; + boolean matchApi = false; String url = request.getRequestURL().toString(); if (project != null) { String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); @@ -1124,24 +1124,23 @@ public class MockConfigService { JSON paramJson = MockApiUtils.getPostParamMap(request); JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request); for (ApiDefinitionWithBLOBs api : aualifiedApiList) { - RequestMockParams mockParams = MockApiUtils.getParams(urlSuffix, api.getPath(), parameterObject, paramJson, true); - MockConfigResponse mockConfigData = this.findByApiId(api.getId()); - MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), mockParams); - if (finalExpectConfig != null) { - isMatch = true; - returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, mockParams, response); - break; - } - if (!isMatch) { - returnStr = this.getApiDefinitionResponse(api, response); - if (StringUtils.isNotEmpty(returnStr)) { - isMatch = true; + if(StringUtils.isEmpty(returnStr)){ + RequestMockParams mockParams = MockApiUtils.getParams(urlSuffix, api.getPath(), parameterObject, paramJson, true); + MockConfigResponse mockConfigData = this.findByApiId(api.getId()); + MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), mockParams); + if (finalExpectConfig != null) { + returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, mockParams, response); + }else { + returnStr = this.getApiDefinitionResponse(api, response); } } } + if(CollectionUtils.isNotEmpty(aualifiedApiList)){ + matchApi = true; + } } - if (!isMatch) { + if (!matchApi) { response.setStatus(404); returnStr = Translator.get("mock_warning"); } @@ -1151,7 +1150,7 @@ public class MockConfigService { public String checkReturnWithMockExpectByUrlParam(String method, Map requestHeaderMap, Project project, HttpServletRequest request, HttpServletResponse response) { String returnStr = ""; - boolean isMatch = false; + boolean matchApi = false; String url = request.getRequestURL().toString(); List aualifiedApiList = new ArrayList<>(); if (project != null) { @@ -1169,27 +1168,26 @@ public class MockConfigService { JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request); for (ApiDefinitionWithBLOBs api : aualifiedApiList) { - RequestMockParams paramMap = MockApiUtils.getParams(urlSuffix, api.getPath(), parameterObject, paramJson, false); - - MockConfigResponse mockConfigData = this.findByApiId(api.getId()); - if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) { - MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), paramMap); - if (finalExpectConfig != null) { - returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, paramMap, response); - isMatch = true; - break; - } - } - if (!isMatch) { - returnStr = this.getApiDefinitionResponse(api, response); - if (StringUtils.isNotEmpty(returnStr)) { - isMatch = true; + if(StringUtils.isEmpty(returnStr)){ + RequestMockParams paramMap = MockApiUtils.getParams(urlSuffix, api.getPath(), parameterObject, paramJson, false); + MockConfigResponse mockConfigData = this.findByApiId(api.getId()); + if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) { + MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), paramMap); + if (finalExpectConfig != null) { + returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, paramMap, response); + }else { + returnStr = this.getApiDefinitionResponse(api, response); + } } } } + + if(CollectionUtils.isNotEmpty(aualifiedApiList)){ + matchApi = true; + } } - if (!isMatch) { + if (!matchApi) { response.setStatus(404); returnStr = Translator.get("mock_warning"); } diff --git a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java index f56893726d..41236c569e 100644 --- a/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java +++ b/backend/src/main/java/io/metersphere/api/tcp/server/TCPServicer.java @@ -83,6 +83,7 @@ public class TCPServicer { LogUtil.error(e); } } + LogUtil.info("TCP-Mock start. port: " + this.port + "; Message:" + message + "; response:" + returnMsg); return returnMsg; }