refactor(接口测试): 接口测试转性能测试时不再删除环境设置中的dnsCacheManager

--bug=1014056 --user=宋天阳
【性能测试】github#14499接口测试转性能测试的时候,自动把域名替换成了ip,由于domain不一致,导致cookie没法保存
https://www.tapd.cn/55049933/s/1182189
This commit is contained in:
song-tianyang 2022-06-15 11:08:37 +08:00 committed by f2c-ci-robot[bot]
parent fccd0424ec
commit a78bde82a6
2 changed files with 21 additions and 62 deletions

View File

@ -19,9 +19,7 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@EqualsAndHashCode(callSuper = true)
@ -67,24 +65,6 @@ public class MsDNSCacheManager extends MsTestElement {
}
}
public static Map<String, String> getEnvironmentDns(EnvironmentConfig config, HttpConfig httpConfig) {
Map<String, String> dnsMap = new HashMap<>();
if (config.getCommonConfig().isEnableHost() && CollectionUtils.isNotEmpty(config.getCommonConfig().getHosts()) && httpConfig != null && httpConfig.getDomain() != null) {
String domain = httpConfig.getDomain().trim();
config.getCommonConfig().getHosts().forEach(host -> {
if (StringUtils.isNotBlank(host.getDomain())) {
String hostDomain = host.getDomain().trim().replace("http://", "").replace("https://", "");
if (StringUtils.equals(hostDomain, domain)) {
dnsMap.put(hostDomain, host.getIp());
} else if (StringUtils.startsWith(hostDomain, domain + ":")) {
dnsMap.put(domain, StringUtils.replace(hostDomain, domain, host.getIp()));
}
}
});
}
return dnsMap;
}
private static Arguments arguments(String name, List<KeyValue> variables) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);

View File

@ -210,27 +210,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
}
if (config.isOperating() && config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) {
//导出的需要将DNSCache去掉并把域名进行ip替换
Map<String, String> dnsMap = MsDNSCacheManager.getEnvironmentDns(config.getConfig().get(this.getProjectId()), httpConfig);
String domain = sampler.getDomain();
if (dnsMap.containsKey(domain)) {
String address = dnsMap.get(domain);
if (address.contains(":")) {
String[] addressArr = StringUtils.split(address, ":");
if (addressArr.length == 2) {
try {
sampler.setDomain(addressArr[0]);
sampler.setPort(Integer.parseInt(addressArr[1]));
} catch (Exception ignored) {
}
}
} else {
sampler.setDomain(dnsMap.get(domain));
}
}
}
final HashTree httpSamplerTree = tree.add(sampler);
// 注意顺序放在config前面会优先于环境的请求头生效
@ -687,28 +666,28 @@ public class MsHTTPSamplerProxy extends MsTestElement {
list.stream().
filter(KeyValue::isValid).
filter(KeyValue::isEnable).forEach(keyValue -> {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
if (StringUtils.equalsIgnoreCase(this.method, "get")) {
if (StringUtils.isNotEmpty(httpArgument.getValue())) {
arguments.addArgument(httpArgument);
}
} else {
arguments.addArgument(httpArgument);
}
} catch (Exception e) {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
if (StringUtils.equalsIgnoreCase(this.method, "get")) {
if (StringUtils.isNotEmpty(httpArgument.getValue())) {
arguments.addArgument(httpArgument);
}
} else {
arguments.addArgument(httpArgument);
}
} catch (Exception e) {
}
}
);
}
}
);
return arguments;
}