From 8855e4954b834d33325f9991a164f8667614be93 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 21 Apr 2021 15:19:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/definition/ApiTestCaseDTO.java | 1 + .../dto/definition/request/MsTestElement.java | 13 ++ .../request/sampler/MsDubboSampler.java | 35 +++--- .../request/sampler/MsHTTPSamplerProxy.java | 46 +++---- .../request/sampler/MsJDBCSampler.java | 33 +++-- .../request/sampler/MsTCPSampler.java | 26 ++-- .../api/dto/scenario/HttpConfig.java | 27 ++-- .../api/service/ApiAutomationService.java | 26 +--- .../base/mapper/ext/ExtApiTestCaseMapper.xml | 2 +- .../scenario/component/LoopController.vue | 118 ++++++++---------- .../scenario/variable/VariableList.vue | 9 +- .../components/case/ApiCaseList.vue | 32 ++--- .../components/list/ApiCaseSimpleList.vue | 13 +- .../environment/EnvironmentHttpConfig.vue | 4 +- .../api/test/model/EnvironmentModel.js | 2 +- .../common/json-schema/JsonSchemaEditor.vue | 1 + .../components/common/model/JsonData.js | 3 +- frontend/src/i18n/en-US.js | 5 + frontend/src/i18n/zh-CN.js | 5 + frontend/src/i18n/zh-TW.js | 5 + 20 files changed, 196 insertions(+), 210 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java b/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java index efd86e961..f22c26431 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/ApiTestCaseDTO.java @@ -10,6 +10,7 @@ public class ApiTestCaseDTO extends ApiTestCase { private String moduleId; private String path; private String protocol; + private String casePath; private String updateUser; private String createUser; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java index 65678ddc3..bb11fb075 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java @@ -46,6 +46,7 @@ import org.apache.jorphan.collections.ListedHashTree; import java.io.ByteArrayOutputStream; import java.io.File; +import java.net.URL; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -286,6 +287,18 @@ public abstract class MsTestElement { } return ""; } + public boolean isURL(String str) { + try { + new URL(str); + return true; + } catch (Exception e) { + // 支持包含变量的url + if (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")) { + return true; + } + return false; + } + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java index d84e50461..8c04584a1 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java @@ -38,7 +38,9 @@ import java.util.stream.Collectors; @EqualsAndHashCode(callSuper = true) @JSONType(typeName = "DubboSampler") public class MsDubboSampler extends MsTestElement { - /** type 必须放最前面,以便能够转换正确的类 */ + /** + * type 必须放最前面,以便能够转换正确的类 + */ private String type = "DubboSampler"; @JSONField(ordinal = 52) @@ -90,40 +92,35 @@ public class MsDubboSampler extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsDubboSampler proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { this.setProjectId(bloBs.getProjectId()); - MsDubboSampler proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(bloBs.getName()); - this.setMethod(proxy.getMethod()); - this.set_interface(proxy.get_interface()); - this.setAttachmentArgs(proxy.getAttachmentArgs()); - this.setArgs(proxy.getArgs()); - this.setConsumerAndService(proxy.getConsumerAndService()); - this.setRegistryCenter(proxy.getRegistryCenter()); - this.setConfigCenter(proxy.getConfigCenter()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { this.setProjectId(apiDefinition.getProjectId()); - MsDubboSampler proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(apiDefinition.getName()); - this.setMethod(proxy.getMethod()); - this.set_interface(proxy.get_interface()); - this.setAttachmentArgs(proxy.getAttachmentArgs()); - this.setArgs(proxy.getArgs()); - this.setConsumerAndService(proxy.getConsumerAndService()); - this.setRegistryCenter(proxy.getRegistryCenter()); - this.setConfigCenter(proxy.getConfigCenter()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setMethod(proxy.getMethod()); + this.set_interface(proxy.get_interface()); + this.setAttachmentArgs(proxy.getAttachmentArgs()); + this.setArgs(proxy.getArgs()); + this.setConsumerAndService(proxy.getConsumerAndService()); + this.setRegistryCenter(proxy.getRegistryCenter()); + this.setConfigCenter(proxy.getConfigCenter()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 93bd87b00..b97272971 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -110,36 +110,33 @@ public class MsHTTPSamplerProxy extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsHTTPSamplerProxy proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { this.setProjectId(bloBs.getProjectId()); - MsHTTPSamplerProxy proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(bloBs.getName()); - this.setMethod(proxy.getMethod()); - this.setBody(proxy.getBody()); - this.setRest(proxy.getRest()); - this.setArguments(proxy.getArguments()); - this.setHeaders(proxy.getHeaders()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { + this.setName(apiDefinition.getName()); this.setProjectId(apiDefinition.getProjectId()); - MsHTTPSamplerProxy proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(apiDefinition.getName()); - this.setMethod(proxy.getMethod()); - this.setBody(proxy.getBody()); - this.setRest(proxy.getRest()); - this.setArguments(proxy.getArguments()); - this.setHeaders(proxy.getHeaders()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setMethod(proxy.getMethod()); + this.setBody(proxy.getBody()); + this.setRest(proxy.getRest()); + this.setArguments(proxy.getArguments()); + this.setHeaders(proxy.getHeaders()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); @@ -202,7 +199,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { } } } - try { if (config.isEffective(this.getProjectId())) { HttpConfig httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig(), tree); @@ -428,20 +424,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { tree.add(headerManager); } } - - private boolean isURL(String str) { - try { - new URL(str); - return true; - } catch (Exception e) { - // 支持包含变量的url - if (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")) { - return true; - } - return false; - } - } - /** * 按照环境规则匹配环境 * @@ -453,7 +435,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (CollectionUtils.isNotEmpty(httpConfig.getConditions())) { for (HttpConfigCondition item : httpConfig.getConditions()) { if (item.getType().equals(ConditionType.PATH.name())) { - HttpConfig config = httpConfig.getPathCondition(this.getPath()); + HttpConfig config = httpConfig.getPathCondition(this.getPath(), item); if (config != null) { isNext = false; httpConfig = config; @@ -470,7 +452,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { apiDefinition = apiDefinitionService.get(this.getId()); } if (apiDefinition != null) { - HttpConfig config = httpConfig.getModuleCondition(apiDefinition.getModuleId()); + HttpConfig config = httpConfig.getModuleCondition(apiDefinition.getModuleId(), item); if (config != null) { isNext = false; httpConfig = config; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index 1969f1d5c..cd2692135 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -151,40 +151,35 @@ public class MsJDBCSampler extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsJDBCSampler proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { + this.setName(bloBs.getName()); this.setProjectId(bloBs.getProjectId()); - MsJDBCSampler proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(bloBs.getName()); - this.setDataSource(proxy.getDataSource()); - this.setDataSourceId(proxy.getDataSourceId()); - this.setQuery(proxy.getQuery()); - this.setVariables(proxy.getVariables()); - this.setVariableNames(proxy.getVariableNames()); - this.setResultVariable(proxy.getResultVariable()); - this.setQueryTimeout(proxy.getQueryTimeout()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { this.setProjectId(apiDefinition.getProjectId()); - MsJDBCSampler proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); this.setName(apiDefinition.getName()); - this.setDataSource(proxy.getDataSource()); - this.setDataSourceId(proxy.getDataSourceId()); - this.setQuery(proxy.getQuery()); - this.setVariables(proxy.getVariables()); - this.setVariableNames(proxy.getVariableNames()); - this.setResultVariable(proxy.getResultVariable()); - this.setQueryTimeout(proxy.getQueryTimeout()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setDataSource(proxy.getDataSource()); + this.setDataSourceId(proxy.getDataSourceId()); + this.setQuery(proxy.getQuery()); + this.setVariables(proxy.getVariables()); + this.setVariableNames(proxy.getVariableNames()); + this.setResultVariable(proxy.getResultVariable()); + this.setQueryTimeout(proxy.getQueryTimeout()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index b94a325d2..36bc12d12 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -129,34 +129,32 @@ public class MsTCPSampler extends MsTestElement { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MsTCPSampler proxy = null; if (StringUtils.equals(this.getRefType(), "CASE")) { ApiTestCaseService apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiTestCaseWithBLOBs bloBs = apiTestCaseService.get(this.getId()); if (bloBs != null) { + this.setName(bloBs.getName()); this.setProjectId(bloBs.getProjectId()); - MsTCPSampler proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { + proxy = mapper.readValue(bloBs.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(bloBs.getName()); - this.setClassname(proxy.getClassname()); - this.setServer(proxy.getServer()); - this.setPort(proxy.getPort()); - this.setRequest(proxy.getRequest()); } } else { ApiDefinitionWithBLOBs apiDefinition = apiDefinitionService.getBLOBs(this.getId()); if (apiDefinition != null) { + this.setName(apiDefinition.getName()); this.setProjectId(apiDefinition.getProjectId()); - MsTCPSampler proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { + proxy = mapper.readValue(apiDefinition.getRequest(), new TypeReference() { }); - this.setHashTree(proxy.getHashTree()); - this.setName(apiDefinition.getName()); - this.setClassname(proxy.getClassname()); - this.setServer(proxy.getServer()); - this.setPort(proxy.getPort()); - this.setRequest(proxy.getRequest()); } } + if (proxy != null) { + this.setHashTree(proxy.getHashTree()); + this.setClassname(proxy.getClassname()); + this.setServer(proxy.getServer()); + this.setPort(proxy.getPort()); + this.setRequest(proxy.getRequest()); + } } catch (Exception ex) { ex.printStackTrace(); LogUtil.error(ex.getMessage()); diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java b/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java index 747ab1355..bd96e1f1d 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/HttpConfig.java @@ -1,6 +1,5 @@ package io.metersphere.api.dto.scenario; -import io.metersphere.commons.constants.ConditionType; import io.metersphere.commons.utils.BeanUtils; import lombok.Data; import org.apache.commons.collections.CollectionUtils; @@ -31,27 +30,21 @@ public class HttpConfig { return config; } - public HttpConfig getPathCondition(String path) { - List conditions = this.getConditions().stream().filter(condition -> ConditionType.PATH.name().equals(condition.getType()) && CollectionUtils.isNotEmpty(condition.getDetails())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(conditions)) { - for (HttpConfigCondition item : conditions) { - List details = item.getDetails().stream().filter(detail -> (detail.getValue().equals("contains") && StringUtils.contains(path, detail.getName())) || (detail.getValue().equals("equals") && StringUtils.equals(path, detail.getName()))).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(details)) { - return initHttpConfig(item); - } + public HttpConfig getPathCondition(String path, HttpConfigCondition configCondition) { + if (CollectionUtils.isNotEmpty(configCondition.getDetails())) { + List details = configCondition.getDetails().stream().filter(detail -> (detail.getValue().equals("contains") && StringUtils.contains(path, detail.getName())) || (detail.getValue().equals("equals") && StringUtils.equals(path, detail.getName()))).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(details)) { + return initHttpConfig(configCondition); } } return null; } - public HttpConfig getModuleCondition(String moduleId) { - List conditions = this.getConditions().stream().filter(condition -> ConditionType.MODULE.name().equals(condition.getType()) && CollectionUtils.isNotEmpty(condition.getDetails())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(conditions)) { - for (HttpConfigCondition item : conditions) { - List details = item.getDetails().stream().filter(detail -> StringUtils.contains(detail.getValue(), moduleId)).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(details)) { - return initHttpConfig(item); - } + public HttpConfig getModuleCondition(String moduleId, HttpConfigCondition configCondition) { + if (CollectionUtils.isNotEmpty(configCondition.getDetails())) { + List details = configCondition.getDetails().stream().filter(detail -> StringUtils.contains(detail.getValue(), moduleId)).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(details)) { + return initHttpConfig(configCondition); } } return null; diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 1f75f4395..5506fdda5 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -399,7 +399,7 @@ public class ApiAutomationService { http.setUrl(apiDefinition.getPath()); } if (http.isEnable()) { - if (StringUtils.isBlank(http.getUrl()) || !isURL(http.getUrl())) { + if (StringUtils.isBlank(http.getUrl()) || !tr.isURL(http.getUrl())) { env.getProjectIds().add(http.getProjectId()); env.setFullUrl(false); } @@ -430,7 +430,7 @@ public class ApiAutomationService { // 校验是否是全路径 MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr; if (httpSamplerProxy.isEnable()) { - if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !isURL(httpSamplerProxy.getUrl())) { + if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !tr.isURL(httpSamplerProxy.getUrl())) { env.getProjectIds().add(httpSamplerProxy.getProjectId()); env.setFullUrl(false); } @@ -473,7 +473,7 @@ public class ApiAutomationService { http.setUrl(apiDefinition.getPath()); } if (http.isEnable()) { - if (StringUtils.isBlank(http.getUrl()) || !this.isURL(http.getUrl())) { + if (StringUtils.isBlank(http.getUrl()) || !tr.isURL(http.getUrl())) { env.setFullUrl(false); env.getProjectIds().add(http.getProjectId()); } @@ -504,7 +504,7 @@ public class ApiAutomationService { // 校验是否是全路径 MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr; if (httpSamplerProxy.isEnable()) { - if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !isURL(httpSamplerProxy.getUrl())) { + if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !tr.isURL(httpSamplerProxy.getUrl())) { env.setFullUrl(false); env.getProjectIds().add(httpSamplerProxy.getProjectId()); } @@ -528,21 +528,7 @@ public class ApiAutomationService { } } - private boolean isURL(String str) { - try { - String regex = "^((https|http|ftp|rtsp|mms)?://)" - + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" - + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" + "|" + "([0-9a-z_!~*'()-]+\\.)*" - + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." - + "[a-z]{2,6})" - + "(:[0-9]{1,5})?" - + "((/?)|" - + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; - return str.matches(regex) || (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")); - } catch (Exception e) { - return false; - } - } + public List getApiScenarios(List ids) { if (CollectionUtils.isNotEmpty(ids)) { @@ -857,7 +843,7 @@ public class ApiAutomationService { } /** - * 场景串行 + * 场景执行 * * @param request * @return diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml index 5b9992c74..3757db57d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml @@ -265,7 +265,7 @@