fix (接口自动化): 修复请求头变量找不到值问题 (#5474)

Co-authored-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
metersphere-bot 2021-08-18 14:11:26 +08:00 committed by GitHub
parent 23c56b7c2e
commit 75866a7054
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 10 deletions

View File

@ -165,7 +165,8 @@ public class MsScenario extends MsTestElement {
this.addCounter(tree, variables); this.addCounter(tree, variables);
this.addRandom(tree, variables); this.addRandom(tree, variables);
if (CollectionUtils.isNotEmpty(this.headers)) { if (CollectionUtils.isNotEmpty(this.headers)) {
setHeader(tree, this.headers); //setHeader(tree, this.headers);
config.setHeaders(this.headers);
} }
if (CollectionUtils.isNotEmpty(hashTree)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.definition.request; package io.metersphere.api.dto.definition.request;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; 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.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.ssl.MsKeyStore; import io.metersphere.api.dto.ssl.MsKeyStore;
import io.metersphere.jmeter.utils.ScriptEngineUtils; import io.metersphere.jmeter.utils.ScriptEngineUtils;
@ -26,6 +27,12 @@ public class ParameterConfig {
* 公共场景参数 * 公共场景参数
*/ */
private List<ScenarioVariable> variables; private List<ScenarioVariable> variables;
/**
* 公共场景参数
*/
private List<KeyValue> headers;
/** /**
* 公共Cookie * 公共Cookie
*/ */
@ -55,7 +62,7 @@ public class ParameterConfig {
} }
static public Arguments valueSupposeMock(Arguments arguments) { 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(); String argValue = arguments.getArgument(i).getValue();
arguments.getArgument(i).setValue(ScriptEngineUtils.buildFunctionCallString(argValue)); arguments.getArgument(i).setValue(ScriptEngineUtils.buildFunctionCallString(argValue));
} }

View File

@ -56,6 +56,7 @@ import java.net.URLEncoder;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Data @Data
@ -233,7 +234,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
setHeader(httpSamplerTree, httpConfig.getHeaders()); setHeader(httpSamplerTree, httpConfig.getHeaders());
} }
} }
// 场景头
if (config != null && CollectionUtils.isNotEmpty(config.getHeaders())) {
setHeader(httpSamplerTree, config.getHeaders());
}
// 环境通用请求头 // 环境通用请求头
Arguments arguments = getConfigArguments(config); Arguments arguments = getConfigArguments(config);
if (arguments != null) { if (arguments != null) {
@ -254,10 +258,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (CollectionUtils.isNotEmpty(hashTree)) { if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) { for (MsTestElement el : hashTree) {
if(el.getEnvironmentId() == null){ if (el.getEnvironmentId() == null) {
if(this.getEnvironmentId() == null){ if (this.getEnvironmentId() == null) {
el.setEnvironmentId(useEnvironment); el.setEnvironmentId(useEnvironment);
}else{ } else {
el.setEnvironmentId(this.getEnvironmentId()); el.setEnvironmentId(this.getEnvironmentId());
} }
} }
@ -604,16 +608,25 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
public void setHeader(HashTree tree, List<KeyValue> headers) { public void setHeader(HashTree tree, List<KeyValue> headers) {
// 合并header
HeaderManager headerManager = new HeaderManager(); HeaderManager headerManager = new HeaderManager();
headerManager.setEnabled(true); headerManager.setEnabled(true);
headerManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "HeaderManager" : "HeaderManager"); headerManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "HeaderManager" : "HeaderManager");
headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName()); headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
headerManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HeaderPanel")); 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 参数 // header 也支持 mock 参数
headers.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> List<KeyValue> keyValues = headers.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).collect(Collectors.toList());
headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()))) for (KeyValue keyValue : keyValues) {
); headerManager.add(new Header(keyValue.getName(), ScriptEngineUtils.buildFunctionCallString(keyValue.getValue())));
if (headerManager.getHeaders().size() > 0) { }
if (headerManager.getHeaders().size() > 0 && isAdd) {
tree.add(headerManager); tree.add(headerManager);
} }
} }