diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/dns/MsDNSCacheManager.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/dns/MsDNSCacheManager.java index 38166128b8..00fb73cec0 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/dns/MsDNSCacheManager.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/dns/MsDNSCacheManager.java @@ -3,6 +3,7 @@ package io.metersphere.api.dto.definition.request.dns; import com.alibaba.fastjson.annotation.JSONType; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.ParameterConfig; +import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.environment.Host; @@ -40,9 +41,9 @@ public class MsDNSCacheManager extends MsTestElement { samplerHashTree.add(arguments(name, config.getCommonConfig().getVariables())); } - public static void addEnvironmentDNS(HashTree samplerHashTree, String name, EnvironmentConfig config) { - if (config.getCommonConfig().isEnableHost() && CollectionUtils.isNotEmpty(config.getCommonConfig().getHosts())) { - String domain = config.getHttpConfig().getDomain().trim(); + public static void addEnvironmentDNS(HashTree samplerHashTree, String name, EnvironmentConfig config, HttpConfig httpConfig) { + if (config.getCommonConfig().isEnableHost() && CollectionUtils.isNotEmpty(config.getCommonConfig().getHosts()) && httpConfig != null) { + String domain = httpConfig.getDomain().trim(); List hosts = new ArrayList<>(); config.getCommonConfig().getHosts().forEach(host -> { if (StringUtils.isNotBlank(host.getDomain())) { 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 369b192bb5..7c10a7a095 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 @@ -197,10 +197,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { } compatible(config); - + HttpConfig httpConfig = null; try { if (config.isEffective(this.getProjectId())) { - HttpConfig httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig(), tree); + httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig()); if (httpConfig == null && !isURL(this.getUrl())) { MSException.throwException("未匹配到环境,请检查环境配置"); } @@ -333,13 +333,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (CollectionUtils.isNotEmpty(this.headers)) { setHeader(httpSamplerTree, this.headers); } - - // 通用请求Headers - if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null - && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) { + // 新版本符合条件 HTTP 请求头 + if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) { if (!this.isCustomizeReq() || this.isRefEnvironment) { // 如果不是自定义请求,或者引用环境则添加环境请求头 - setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders()); + setHeader(httpSamplerTree, httpConfig.getHeaders()); } } @@ -352,7 +350,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null && config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) { MsDNSCacheManager.addEnvironmentVariables(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId())); - MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId())); + MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()), httpConfig); } if (this.authManager != null) { @@ -559,7 +557,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { * @param httpConfig * @return */ - private HttpConfig getHttpConfig(HttpConfig httpConfig, HashTree tree) { + private HttpConfig getHttpConfig(HttpConfig httpConfig) { boolean isNext = true; if (CollectionUtils.isNotEmpty(httpConfig.getConditions())) { for (HttpConfigCondition item : httpConfig.getConditions()) { @@ -609,13 +607,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { } } } - // HTTP 环境中请求头 - if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) { - if (!this.isCustomizeReq() || this.isRefEnvironment) { - // 如果不是自定义请求,或者引用环境则添加环境请求头 - setHeader(tree, httpConfig.getHeaders()); - } - } return httpConfig; } 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 c168de1cd9..8a4e5a1dd1 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 @@ -22,6 +22,7 @@ public class HttpConfig { HttpConfig config = new HttpConfig(); config.isMock = this.isMock; BeanUtils.copyBean(config, configCondition); + config.setHeaders(configCondition.getHeaders()); return config; }