fix: 自定义请求完整url带IP拼接了环境

This commit is contained in:
chenjianxing 2021-05-31 10:39:50 +08:00 committed by jianxing
parent 5f3cde6725
commit 16b3e64702
4 changed files with 121 additions and 78 deletions

View File

@ -14,6 +14,8 @@ import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition; import io.metersphere.api.dto.scenario.HttpConfigCondition;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.ssl.KeyStoreConfig;
import io.metersphere.api.dto.ssl.KeyStoreFile; import io.metersphere.api.dto.ssl.KeyStoreFile;
import io.metersphere.api.dto.ssl.MsKeyStore; import io.metersphere.api.dto.ssl.MsKeyStore;
import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiDefinitionService;
@ -198,10 +200,78 @@ public class MsHTTPSamplerProxy extends MsTestElement {
compatible(config); compatible(config);
HttpConfig httpConfig = null; HttpConfig httpConfig = getHttpConfig(config);
setSamplerPath(config, httpConfig, sampler);
// 请求体
if (!StringUtils.equals(this.getMethod(), "GET")) {
if (this.body != null) {
List<KeyValue> bodyParams = this.body.getBodyParams(sampler, this.getId());
if (StringUtils.isNotEmpty(this.body.getType()) && "Form Data".equals(this.body.getType())) {
sampler.setDoMultipart(true);
}
if (CollectionUtils.isNotEmpty(bodyParams)) {
sampler.setArguments(httpArguments(bodyParams));
}
}
}
final HashTree httpSamplerTree = tree.add(sampler);
// 注意顺序放在config前面会优先于环境的请求头生效
if (CollectionUtils.isNotEmpty(this.headers)) {
setHeader(httpSamplerTree, this.headers);
}
// 新版本符合条件 HTTP 请求头
if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) {
if (!this.isCustomizeReq() || this.isRefEnvironment) {
// 如果不是自定义请求,或者引用环境则添加环境请求头
setHeader(httpSamplerTree, httpConfig.getHeaders());
}
}
// 环境通用请求头
Arguments arguments = getConfigArguments(config);
if (arguments != null) {
httpSamplerTree.add(arguments);
}
//判断是否要开启DNS
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()), httpConfig);
}
if (this.authManager != null) {
this.authManager.setAuth(tree, this.authManager, sampler);
}
addCertificate(config, httpSamplerTree);
if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {
el.setUseEnviroment(useEnvironment);
el.toHashTree(httpSamplerTree, el.getHashTree(), config);
}
}
}
private EnvironmentConfig getEnvironmentConfig(ParameterConfig config) {
return config.getConfig().get(this.getProjectId());
}
private HttpConfig getHttpConfig(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) {
return getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig());
}
return null;
}
private void setSamplerPath(ParameterConfig config, HttpConfig httpConfig, HTTPSamplerProxy sampler) {
try { try {
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig());
if (httpConfig == null && !isURL(this.getUrl())) { if (httpConfig == null && !isURL(this.getUrl())) {
MSException.throwException("未匹配到环境,请检查环境配置"); MSException.throwException("未匹配到环境,请检查环境配置");
} }
@ -242,22 +312,32 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
} else { } else {
if (!isCustomizeReq() || isRefEnvironment) { if (!isCustomizeReq() || isRefEnvironment) {
if (isNeedAddMockUrl(url)) { if (isCustomizeReqCompleteUrl(this.path)) {
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket(); url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
} }
URL urlObject = new URL(url);
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath(); String envPath = "";
if (StringUtils.isNotBlank(this.getPath())) {
envPath += this.getPath(); if (!isCustomizeReqCompleteUrl(this.path)) {
} URL urlObject = new URL(url);
if (StringUtils.isNotEmpty(httpConfig.getDomain())) { envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
sampler.setDomain(URLDecoder.decode(httpConfig.getDomain(), "UTF-8")); if (StringUtils.isNotBlank(this.getPath())) {
sampler.setProtocol(httpConfig.getProtocol()); envPath += this.getPath();
}
if (StringUtils.isNotEmpty(httpConfig.getDomain())) {
sampler.setDomain(URLDecoder.decode(httpConfig.getDomain(), "UTF-8"));
sampler.setProtocol(httpConfig.getProtocol());
} else {
sampler.setDomain("");
sampler.setProtocol("");
}
sampler.setPort(httpConfig.getPort());
} else { } else {
sampler.setDomain(""); URL urlObject = new URL(this.path);
sampler.setProtocol(""); envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8"));
sampler.setProtocol(urlObject.getProtocol());
} }
sampler.setPort(httpConfig.getPort());
sampler.setPath(URLDecoder.decode(envPath, "UTF-8")); sampler.setPath(URLDecoder.decode(envPath, "UTF-8"));
} }
} }
@ -315,70 +395,37 @@ public class MsHTTPSamplerProxy extends MsTestElement {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
MSException.throwException(e.getMessage()); MSException.throwException(e.getMessage());
} }
// 请求体 }
if (!StringUtils.equals(this.getMethod(), "GET")) {
if (this.body != null) {
List<KeyValue> bodyParams = this.body.getBodyParams(sampler, this.getId());
if (StringUtils.isNotEmpty(this.body.getType()) && "Form Data".equals(this.body.getType())) {
sampler.setDoMultipart(true);
}
if (CollectionUtils.isNotEmpty(bodyParams)) {
sampler.setArguments(httpArguments(bodyParams));
}
}
}
final HashTree httpSamplerTree = tree.add(sampler); /**
* 加载SSL认证
// 注意顺序放在config前面会优先于环境的请求头生效 * @param config
if (CollectionUtils.isNotEmpty(this.headers)) { * @param httpSamplerTree
setHeader(httpSamplerTree, this.headers); * @return
} */
// 新版本符合条件 HTTP 请求头 private void addCertificate(ParameterConfig config, HashTree httpSamplerTree) {
if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) { if (config != null && config.isEffective(this.getProjectId()) && getEnvironmentConfig(config).getSslConfig() != null) {
if (!this.isCustomizeReq() || this.isRefEnvironment) { KeyStoreConfig sslConfig = getEnvironmentConfig(config).getSslConfig();
// 如果不是自定义请求,或者引用环境则添加环境请求头 List<KeyStoreFile> files = sslConfig.getFiles();
setHeader(httpSamplerTree, httpConfig.getHeaders()); if (CollectionUtils.isNotEmpty(files)) {
}
}
// 环境通用请求头
Arguments arguments = getConfigArguments(config);
if (arguments != null) {
httpSamplerTree.add(arguments);
}
//判断是否要开启DNS
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()), httpConfig);
}
if (this.authManager != null) {
this.authManager.setAuth(tree, this.authManager, sampler);
}
// 加载SSL认证
if (config != null && config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getSslConfig() != null) {
if (CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getSslConfig().getFiles())) {
MsKeyStore msKeyStore = config.getKeyStoreMap().get(this.getProjectId()); MsKeyStore msKeyStore = config.getKeyStoreMap().get(this.getProjectId());
CommandService commandService = CommonBeanFactory.getBean(CommandService.class); CommandService commandService = CommonBeanFactory.getBean(CommandService.class);
if (msKeyStore == null) { if (msKeyStore == null) {
msKeyStore = new MsKeyStore(); msKeyStore = new MsKeyStore();
if (config.getConfig().get(this.getProjectId()).getSslConfig().getFiles().size() == 1) { if (files.size() == 1) {
// 加载认证文件 // 加载认证文件
KeyStoreFile file = config.getConfig().get(this.getProjectId()).getSslConfig().getFiles().get(0); KeyStoreFile file = files.get(0);
msKeyStore.setPath(FileUtils.BODY_FILE_DIR + "/ssl/" + file.getId() + "_" + file.getName()); msKeyStore.setPath(FileUtils.BODY_FILE_DIR + "/ssl/" + file.getId() + "_" + file.getName());
msKeyStore.setPassword(file.getPassword()); msKeyStore.setPassword(file.getPassword());
} else { } else {
// 合并多个认证文件 // 合并多个认证文件
msKeyStore.setPath(FileUtils.BODY_FILE_DIR + "/ssl/tmp." + this.getId() + ".jks"); msKeyStore.setPath(FileUtils.BODY_FILE_DIR + "/ssl/tmp." + this.getId() + ".jks");
msKeyStore.setPassword("ms123..."); msKeyStore.setPassword("ms123...");
commandService.mergeKeyStore(msKeyStore.getPath(), config.getConfig().get(this.getProjectId()).getSslConfig()); commandService.mergeKeyStore(msKeyStore.getPath(), sslConfig);
} }
} }
if (StringUtils.isEmpty(this.alias)) { if (StringUtils.isEmpty(this.alias)) {
this.alias = config.getConfig().get(this.getProjectId()).getSslConfig().getDefaultAlias(); this.alias = sslConfig.getDefaultAlias();
} }
if (StringUtils.isNotEmpty(this.alias)) { if (StringUtils.isNotEmpty(this.alias)) {
@ -404,13 +451,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
} }
} }
if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {
el.setUseEnviroment(useEnvironment);
el.toHashTree(httpSamplerTree, el.getHashTree(), config);
}
}
} }
/** /**
@ -418,11 +458,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
* @param url * @param url
* @return * @return
*/ */
private boolean isNeedAddMockUrl(String url) { private boolean isCustomizeReqCompleteUrl(String url) {
if (isCustomizeReq() && (url.startsWith("http://") || url.startsWith("https://"))) { if (isCustomizeReq() && (url.startsWith("http://") || url.startsWith("https://"))) {
return false; return true;
} }
return true; return false;
} }
// 兼容旧数据 // 兼容旧数据

View File

@ -2,7 +2,7 @@
<span></span> <span></span>
</template> </template>
<script> <script>
import {getUUID, getBodyUploadFiles, getCurrentProjectID, strMapToObj} from "@/common/js/utils"; import {getBodyUploadFiles, strMapToObj} from "@/common/js/utils";
import ThreadGroup from "./jmeter/components/thread-group"; import ThreadGroup from "./jmeter/components/thread-group";
import TestPlan from "./jmeter/components/test-plan"; import TestPlan from "./jmeter/components/test-plan";

View File

@ -44,12 +44,16 @@ export default {
tableData: [] tableData: []
}; };
}, },
computed: {
projectId() {
return this.$store.state.projectId
},
},
methods: { methods: {
search() { search() {
let condition = { let condition = {
workspaceId: getCurrentWorkspaceId(), workspaceId: getCurrentWorkspaceId(),
projectId: getCurrentProjectID(), projectId: this.projectId,
}; };
this.result = this.$post("/performance/report/recent/5", condition, response => { this.result = this.$post("/performance/report/recent/5", condition, response => {
this.tableData = response.data; this.tableData = response.data;

View File

@ -197,8 +197,7 @@ export function getCurrentUserId() {
} }
export function getCurrentProjectID() { export function getCurrentProjectID() {
let user = getCurrentUser(); return localStorage.getItem(PROJECT_ID);
return user.lastProjectId;
} }
export function enableModules(...modules) { export function enableModules(...modules) {