From 69b18e97c946e74d4ebbca5dcec6e8e2f8b14dae Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 25 Oct 2021 14:09:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(Mock):=20#1007385=20#1007387=20#1007366=20?= =?UTF-8?q?=20=E6=B5=8B=E8=AF=95mock=E6=9C=9F=E6=9C=9B=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E6=9C=AA=E7=94=9F=E6=95=88=E3=80=81=E5=93=8D=E5=BA=94=E4=BD=93?= =?UTF-8?q?=E6=98=AF=E8=87=AA=E5=AE=9A=E4=B9=89=E8=84=9A=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?test=E6=89=A7=E8=A1=8C=E7=BB=93=E6=9E=9C=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1007385 --user=宋天阳 [接口定义]-编辑get接口-添加MOCK期望-修改rest参数-执行test测试mock期望结果未生效 https://www.tapd.cn/55049933/s/1058299;--bug=1007387 --user=宋天阳 [接口定义]-编辑接口-添加mock期望-响应体是自定义脚本,test执行结果返回格式错误 https://www.tapd.cn/55049933/s/1058224;--bug=1007366 --user=宋天阳 【接口定义】-PATCH请求使用MOCK环境测试期望结果失败 https://www.tapd.cn/55049933/s/1058406 --- .../api/dto/automation/EsbDataStruct.java | 2 - .../automation/TcpTreeTableDataStruct.java | 2 - .../api/dto/mock/MockApiUtils.java | 16 +++- .../api/service/ApiDefinitionService.java | 86 ++++++++----------- .../api/service/ApiScenarioReportService.java | 1 - .../api/service/MockConfigService.java | 4 +- .../commons/utils/JsonStructUtils.java | 10 ++- 7 files changed, 60 insertions(+), 61 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java b/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java index b9aab28d66..09ee8b0f70 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/EsbDataStruct.java @@ -123,7 +123,6 @@ public class EsbDataStruct { element.addAttribute("attr", attrString); } } catch (Exception e) { - System.out.println(this.name); e.printStackTrace(); } @@ -163,7 +162,6 @@ public class EsbDataStruct { element.addAttribute("attr", attrString); } } catch (Exception e) { - System.out.println(this.name); e.printStackTrace(); } diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/TcpTreeTableDataStruct.java b/backend/src/main/java/io/metersphere/api/dto/automation/TcpTreeTableDataStruct.java index 7d4ddf1db9..14cc42971e 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/TcpTreeTableDataStruct.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/TcpTreeTableDataStruct.java @@ -123,7 +123,6 @@ public class TcpTreeTableDataStruct { element.addAttribute("attr", attrString); } } catch (Exception e) { - System.out.println(this.name); e.printStackTrace(); } @@ -163,7 +162,6 @@ public class TcpTreeTableDataStruct { element.addAttribute("attr", attrString); } } catch (Exception e) { - System.out.println(this.name); e.printStackTrace(); } diff --git a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java index fd095f69db..0e024fb545 100644 --- a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java @@ -424,6 +424,9 @@ public class MockApiUtils { if(StringUtils.isNotEmpty(newScript)){ newScript += "\n"; } + if(StringUtils.isNotEmpty(returnMsg)){ + returnMsg += "\n"; + } } } returnMap.put("script",newScript); @@ -435,8 +438,8 @@ public class MockApiUtils { JSR223Sampler jmeterScriptSampler = new JSR223Sampler(); jmeterScriptSampler.setScriptLanguage(scriptLanguage); jmeterScriptSampler.setScript(script); - SampleResult result = jmeterScriptSampler.sample(null); - System.out.println(result.getResponseData()); + jmeterScriptSampler.sample(null); + } public static RequestMockParams getParams(String urlParams, String apiPath, JSONObject queryParamsObject,JSON paramJson){ @@ -492,7 +495,6 @@ public class MockApiUtils { return returnJson; } else if (StringUtils.equalsIgnoreCase("text/xml", request.getContentType())) { String xmlString = readXml(request); - System.out.println(xmlString); org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString); String jsonStr = xmlJSONObj.toString(); @@ -511,6 +513,14 @@ public class MockApiUtils { object.put(key, value); } return object; + } else if (StringUtils.equalsIgnoreCase("text/plain", request.getContentType())) { + JSONObject object = new JSONObject(); + String bodyParam = readBody(request); + if(StringUtils.isNotEmpty(bodyParam)){ + object.put("raw",bodyParam); + } + return object; + } else { JSONObject object = new JSONObject(); String bodyParam = readBody(request); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 75c1e04d65..1f756accc4 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -1443,67 +1443,55 @@ public class ApiDefinitionService { // return apiDefinitionMapper.selectByExample(example); // } - public List preparedUrl(String projectId, String method, String url, String urlSuffix) { + public List preparedUrl(String projectId, String method, String urlSuffix) { if (StringUtils.isEmpty(urlSuffix)) { return new ArrayList<>(); } else { - if (StringUtils.equalsAnyIgnoreCase(method, "GET", "DELETE")) { - ApiDefinitionExample example = new ApiDefinitionExample(); - ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId); - if (StringUtils.isNotEmpty(url)) { - criteria.andPathEqualTo(url); - } - List apiList = apiDefinitionMapper.selectByExample(example); + ApiDefinitionExample example = new ApiDefinitionExample(); + example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId); + List apiList = apiDefinitionMapper.selectByExample(example); - List apiIdList = new ArrayList<>(); - boolean urlSuffixEndEmpty = false; - if (urlSuffix.endsWith("/")) { - urlSuffixEndEmpty = true; - urlSuffix = urlSuffix + "testMock"; + List apiIdList = new ArrayList<>(); + boolean urlSuffixEndEmpty = false; + if (urlSuffix.endsWith("/")) { + urlSuffixEndEmpty = true; + urlSuffix = urlSuffix + "testMock"; + } + String[] urlParams = urlSuffix.split("/"); + if (urlSuffixEndEmpty) { + urlParams[urlParams.length - 1] = ""; + } + for (ApiDefinition api : apiList) { + String path = api.getPath(); + if (path.startsWith("/")) { + path = path.substring(1); } - String[] urlParams = urlSuffix.split("/"); - if (urlSuffixEndEmpty) { - urlParams[urlParams.length - 1] = ""; - } - for (ApiDefinition api : apiList) { - String path = api.getPath(); - if (path.startsWith("/")) { - path = path.substring(1); - } - if (StringUtils.isNotEmpty(path)) { - String[] pathArr = path.split("/"); - if (pathArr.length == urlParams.length) { - boolean isFetch = true; - for (int i = 0; i < urlParams.length; i++) { - String pathItem = pathArr[i]; - if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) { - if (!StringUtils.equals(pathArr[i], urlParams[i])) { - isFetch = false; - break; - } + if (StringUtils.isNotEmpty(path)) { + String[] pathArr = path.split("/"); + if (pathArr.length == urlParams.length) { + boolean isFetch = true; + for (int i = 0; i < urlParams.length; i++) { + String pathItem = pathArr[i]; + if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) { + if (!StringUtils.equals(pathArr[i], urlParams[i])) { + isFetch = false; + break; } + } - } - if (isFetch) { - apiIdList.add(api.getId()); - } + } + if (isFetch) { + apiIdList.add(api.getId()); } } } - if (apiIdList.isEmpty()) { - return new ArrayList<>(); - } else { - example.clear(); - example.createCriteria().andIdIn(apiIdList); - return apiDefinitionMapper.selectByExampleWithBLOBs(example); - } + } + if (apiIdList.isEmpty()) { + return new ArrayList<>(); } else { - if (!url.startsWith("/")) { - url = "/" + url; - } - ApiDefinitionExample example = new ApiDefinitionExample(); - ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId).andPathEqualTo(url); + example.clear(); + example.createCriteria().andIdIn(apiIdList); return apiDefinitionMapper.selectByExampleWithBLOBs(example); } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index 11bfd9d106..cbc5e69c12 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -605,7 +605,6 @@ public class ApiScenarioReportService { if (obj != null) { ReportCounter counter = (ReportCounter) obj; counter.setNumber(counter.getNumber() + 1); - System.out.println("得到统计数量:" + counter.getNumber()); MessageCache.cache.put(report.getScenarioId(), counter); } } 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 e2ed305bec..bbf42009e8 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -1032,7 +1032,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, urlSuffix); + aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix); JSON paramJson = MockApiUtils.getPostParamMap(request); JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request); @@ -1094,7 +1094,7 @@ public class MockConfigService { List aualifiedApiList = new ArrayList<>(); if (project != null) { String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); - aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, null, urlSuffix); + aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix); /** * GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况: diff --git a/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java b/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java index 4f0bb742e3..7b877743ec 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/JsonStructUtils.java @@ -59,9 +59,15 @@ public class JsonStructUtils { } public static boolean checkJsonArrayCompliance(JSONArray sourceArray, JSONArray matchArray) { - if (sourceArray == null && matchArray == null) { + if(sourceArray == null){ + sourceArray = new JSONArray(); + } + if(matchArray == null){ + matchArray = new JSONArray(); + } + if (sourceArray.isEmpty() && matchArray.isEmpty()) { return true; - } else if (sourceArray != null && matchArray != null && sourceArray.size() >= matchArray.size()) { + } else if (!sourceArray.isEmpty() && !matchArray.isEmpty() && sourceArray.size() >= matchArray.size()) { try { for (int i = 0; i < matchArray.size(); i++) { Object obj = matchArray.get(i);