From 75866a7054d298fd1f202251298aca40ea30a03a Mon Sep 17 00:00:00 2001 From: metersphere-bot <78466014+metersphere-bot@users.noreply.github.com> Date: Wed, 18 Aug 2021 14:11:26 +0800 Subject: [PATCH] =?UTF-8?q?fix=20(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E4=BF=AE=E5=A4=8D=E8=AF=B7=E6=B1=82=E5=A4=B4?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E6=89=BE=E4=B8=8D=E5=88=B0=E5=80=BC=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#5474)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: fit2-zhao --- .../dto/definition/request/MsScenario.java | 3 +- .../definition/request/ParameterConfig.java | 9 +++++- .../request/sampler/MsHTTPSamplerProxy.java | 29 ++++++++++++++----- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index bb23ba172b..bde7a7dcde 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -165,7 +165,8 @@ public class MsScenario extends MsTestElement { this.addCounter(tree, variables); this.addRandom(tree, variables); if (CollectionUtils.isNotEmpty(this.headers)) { - setHeader(tree, this.headers); + //setHeader(tree, this.headers); + config.setHeaders(this.headers); } if (CollectionUtils.isNotEmpty(hashTree)) { for (MsTestElement el : hashTree) { diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java index 6ee7992ab3..67625356c5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java @@ -1,6 +1,7 @@ package io.metersphere.api.dto.definition.request; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; +import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.ssl.MsKeyStore; import io.metersphere.jmeter.utils.ScriptEngineUtils; @@ -26,6 +27,12 @@ public class ParameterConfig { * 公共场景参数 */ private List variables; + + /** + * 公共场景参数 + */ + private List headers; + /** * 公共Cookie */ @@ -55,7 +62,7 @@ public class ParameterConfig { } static public Arguments valueSupposeMock(Arguments arguments) { - for(int i = 0; i < arguments.getArguments().size(); ++i) { + for (int i = 0; i < arguments.getArguments().size(); ++i) { String argValue = arguments.getArgument(i).getValue(); arguments.getArgument(i).setValue(ScriptEngineUtils.buildFunctionCallString(argValue)); } 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 649d5d755d..4e0d0ad78c 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 @@ -56,6 +56,7 @@ import java.net.URLEncoder; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; @Data @@ -233,7 +234,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { setHeader(httpSamplerTree, httpConfig.getHeaders()); } } - + // 场景头 + if (config != null && CollectionUtils.isNotEmpty(config.getHeaders())) { + setHeader(httpSamplerTree, config.getHeaders()); + } // 环境通用请求头 Arguments arguments = getConfigArguments(config); if (arguments != null) { @@ -254,10 +258,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (CollectionUtils.isNotEmpty(hashTree)) { for (MsTestElement el : hashTree) { - if(el.getEnvironmentId() == null){ - if(this.getEnvironmentId() == null){ + if (el.getEnvironmentId() == null) { + if (this.getEnvironmentId() == null) { el.setEnvironmentId(useEnvironment); - }else{ + } else { el.setEnvironmentId(this.getEnvironmentId()); } } @@ -604,16 +608,25 @@ public class MsHTTPSamplerProxy extends MsTestElement { } public void setHeader(HashTree tree, List headers) { + // 合并header HeaderManager headerManager = new HeaderManager(); headerManager.setEnabled(true); headerManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "HeaderManager" : "HeaderManager"); headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName()); headerManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HeaderPanel")); + boolean isAdd = true; + for (Object key : tree.keySet()) { + if (key instanceof HeaderManager) { + headerManager = (HeaderManager) key; + isAdd = false; + } + } // header 也支持 mock 参数 - headers.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> - headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()))) - ); - if (headerManager.getHeaders().size() > 0) { + List keyValues = headers.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).collect(Collectors.toList()); + for (KeyValue keyValue : keyValues) { + headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()))); + } + if (headerManager.getHeaders().size() > 0 && isAdd) { tree.add(headerManager); } }