fix(接口测试): 导入jmx接口文件请求头导入为空

--bug=1010912 --user=李玉号 【接口测试】-导入jmx接口文件-请求头导入为空
https://www.tapd.cn/55049933/s/1115190
This commit is contained in:
shiziyuan9527 2022-03-08 16:45:59 +08:00 committed by shiziyuan9527
parent f032a7d5e3
commit 62ee9e513c
1 changed files with 43 additions and 0 deletions

View File

@ -58,6 +58,7 @@ import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan; import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.timers.ConstantTimer; import org.apache.jmeter.timers.ConstantTimer;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
@ -75,6 +76,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
private ApiModule apiModule; private ApiModule apiModule;
private String selectModulePath; private String selectModulePath;
private String planName = "default"; private String planName = "default";
private static final Integer GROUP_GLOBAL = 1;
@Override @Override
public ApiDefinitionImport parse(InputStream inputSource, ApiTestImportRequest request) { public ApiDefinitionImport parse(InputStream inputSource, ApiTestImportRequest request) {
@ -167,6 +169,30 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
} }
dataPools.getDataSources().put(dataSourceElement.getPropertyAsString("dataSource"), newConfig); dataPools.getDataSources().put(dataSourceElement.getPropertyAsString("dataSource"), newConfig);
} }
} else if (key instanceof ThreadGroup) {
HashTree group = tree.get(key);
if (group != null) {
List<Object> nk = new ArrayList<>();
for (Object groupNode : group.keySet()) {
if (groupNode instanceof HeaderManager) {
nk.add(groupNode);
}
}
if (CollectionUtils.isNotEmpty(nk)) {
for (Object gn : group.keySet()) {
if (gn instanceof HTTPSamplerProxy) {
if (headerMap.containsKey(gn.hashCode() + GROUP_GLOBAL)) {
headerMap.get(gn.hashCode()).addAll(nk);
} else {
List<Object> objects = new LinkedList<Object>() {{
this.addAll(nk);
}};
headerMap.put(gn.hashCode() + GROUP_GLOBAL, objects);
}
}
}
}
}
} else if (key instanceof HTTPSamplerProxy) { } else if (key instanceof HTTPSamplerProxy) {
// 把HTTP 请求下的HeaderManager 取出来 // 把HTTP 请求下的HeaderManager 取出来
HashTree node = tree.get(key); HashTree node = tree.get(key);
@ -637,6 +663,23 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
}); });
samplerProxy.setHeaders(keyValues); samplerProxy.setHeaders(keyValues);
} }
// 处理ThreadGroup组内请求头
if (headerMap.containsKey(key.hashCode() + GROUP_GLOBAL)) {
List<KeyValue> keyValues = new LinkedList<>();
headerMap.get(key.hashCode() + GROUP_GLOBAL).forEach(item -> {
HeaderManager headerManager = (HeaderManager) item;
if (headerManager.getHeaders() != null) {
for (int i = 0; i < headerManager.getHeaders().size(); i++) {
keyValues.add(new KeyValue(headerManager.getHeader(i).getName(), headerManager.getHeader(i).getValue()));
}
}
});
// 合并Header
if (CollectionUtils.isNotEmpty(samplerProxy.getHeaders())) {
keyValues.addAll(samplerProxy.getHeaders());
}
samplerProxy.setHeaders(keyValues);
}
// 初始化body // 初始化body
Body body = new Body(); Body body = new Body();
body.init(); body.init();