From cc6ac8f768bcf72a02d6b2f39772b597ea2c02c2 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Mon, 10 May 2021 22:34:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/sampler/MsHTTPSamplerProxy.java | 112 ++++++++++++------ .../api/automation/scenario/ApiCustomize.vue | 9 +- .../scenario/common/CustomizeReqInfo.vue | 32 ++++- .../scenario/component/ApiComponent.vue | 8 +- 4 files changed, 122 insertions(+), 39 deletions(-) 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 9c567a9eea..a06b3738f8 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 @@ -107,6 +107,9 @@ public class MsHTTPSamplerProxy extends MsTestElement { @JSONField(ordinal = 36) private MsAuthManager authManager; + @JSONField(ordinal = 37) + private Boolean isRefEnvironment; + private void setRefElement() { try { ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); @@ -185,26 +188,8 @@ public class MsHTTPSamplerProxy extends MsTestElement { config.setConfig(getEnvironmentConfig(useEnvironment)); } - // 数据兼容处理 - if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) { - // 1.8 前后 混合数据 - this.setProjectId(getParentProjectId()); - } else if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) { - // 1.8 之后 当前正常数据 - } else { - // 1.8 之前 数据 - if (config.getConfig() != null) { - if (!config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) { - // 测试计划执行 - Iterator it = config.getConfig().keySet().iterator(); - if (it.hasNext()) { - this.setProjectId(it.next()); - } - } else { - this.setProjectId(RunModeConstants.HIS_PRO_ID.toString()); - } - } - } + compatible(config); + try { if (config.isEffective(this.getProjectId())) { HttpConfig httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig(), tree); @@ -213,24 +198,33 @@ public class MsHTTPSamplerProxy extends MsTestElement { } String url = httpConfig.getProtocol() + "://" + httpConfig.getSocket(); // 补充如果是完整URL 则用自身URL - boolean isUrl; - if (isUrl = (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl()))) { + + if (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl())) { url = this.getUrl(); } - if (isUrl) { + + if (isUrl()) { + if (this.isCustomizeReq()) { + url = this.getUrl(); + sampler.setPath(url); + } if (StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { url.replaceAll(this.getPort(), "10990"); } - URL urlObject = new URL(url); - sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); + try { + URL urlObject = new URL(url); + sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); - if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { - sampler.setProperty("HTTPSampler.port", this.getPort()); - } else { - sampler.setPort(urlObject.getPort()); + if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { + sampler.setProperty("HTTPSampler.port", this.getPort()); + } else { + sampler.setPort(urlObject.getPort()); + } + sampler.setProtocol(urlObject.getProtocol()); + sampler.setPath(urlObject.getPath()); + } catch (Exception e) { + LogUtil.error(e.getMessage(), e); } - sampler.setProtocol(urlObject.getProtocol()); - sampler.setPath(urlObject.getPath()); } else { //1.9 增加对Mock环境的判断 if (this.isMockEnvironment()) { @@ -331,7 +325,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { // 通用请求Headers if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) { - setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders()); + if (!this.isCustomizeReq() || this.isRefEnvironment) { + // 如果不是自定义请求,或者引用环境则添加环境请求头 + setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders()); + } } // 环境通用请求头 @@ -355,6 +352,52 @@ public class MsHTTPSamplerProxy extends MsTestElement { } } + // 兼容旧数据 + private void compatible(ParameterConfig config) { + if (this.isCustomizeReq() && this.isRefEnvironment == null) { + if (StringUtils.isNotBlank(this.url)) { + this.isRefEnvironment = false; + } else { + this.isRefEnvironment = true; + } + } + + // 数据兼容处理 + if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) { + // 1.8 前后 混合数据 + this.setProjectId(getParentProjectId()); + } else if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) { + // 1.8 之后 当前正常数据 + } else { + // 1.8 之前 数据 + if (config.getConfig() != null) { + if (!config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) { + // 测试计划执行 + Iterator it = config.getConfig().keySet().iterator(); + if (it.hasNext()) { + this.setProjectId(it.next()); + } + } else { + this.setProjectId(RunModeConstants.HIS_PRO_ID.toString()); + } + } + } + } + + private boolean isUrl() { + // 自定义字段没有引用环境则非url + if (this.isCustomizeReq()) { + if (this.isRefEnvironment) { + return false; + } + return true; + } + if (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl())) { + return true; + } + return false; + } + private boolean isVariable(String path, String value) { Pattern p = Pattern.compile("(\\$\\{)([\\w]+)(\\})"); Matcher m = p.matcher(path); @@ -507,7 +550,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { } // HTTP 环境中请求头 if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) { - setHeader(tree, httpConfig.getHeaders()); + if (!this.isCustomizeReq() || this.isRefEnvironment) { + // 如果不是自定义请求,或者引用环境则添加环境请求头 + setHeader(tree, httpConfig.getHeaders()); + } } return httpConfig; } diff --git a/frontend/src/business/components/api/automation/scenario/ApiCustomize.vue b/frontend/src/business/components/api/automation/scenario/ApiCustomize.vue index cfed851d5e..9778032331 100644 --- a/frontend/src/business/components/api/automation/scenario/ApiCustomize.vue +++ b/frontend/src/business/components/api/automation/scenario/ApiCustomize.vue @@ -57,7 +57,14 @@ Object.assign(this.request, row.request); this.request.name = name; if (this.request.protocol === 'HTTP') { - this.request.url = row.url; + if (row.url) { + // 自定义请求根据是否勾选判断是否需要引用环境 + this.request.url = row.url; + this.request.path = row.url; + } else { + this.request.url = row.path; + this.request.path = row.path; + } this.request.method = row.method; } this.request.resourceId = getUUID(); diff --git a/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue b/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue index 2a5d3205eb..7335392200 100644 --- a/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue +++ b/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue @@ -1,9 +1,9 @@