refactor: 自定义请求添加是否引用环境

This commit is contained in:
chenjianxing 2021-05-10 22:34:45 +08:00 committed by jianxing
parent d04f74d787
commit cc6ac8f768
4 changed files with 122 additions and 39 deletions

View File

@ -107,6 +107,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
@JSONField(ordinal = 36) @JSONField(ordinal = 36)
private MsAuthManager authManager; private MsAuthManager authManager;
@JSONField(ordinal = 37)
private Boolean isRefEnvironment;
private void setRefElement() { private void setRefElement() {
try { try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
@ -185,26 +188,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
config.setConfig(getEnvironmentConfig(useEnvironment)); config.setConfig(getEnvironmentConfig(useEnvironment));
} }
// 数据兼容处理 compatible(config);
if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
// 1.8 前后 混合数据
this.setProjectId(getParentProjectId());
} else if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
// 1.8 之后 当前正常数据
} else {
// 1.8 之前 数据
if (config.getConfig() != null) {
if (!config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
// 测试计划执行
Iterator<String> it = config.getConfig().keySet().iterator();
if (it.hasNext()) {
this.setProjectId(it.next());
}
} else {
this.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
}
}
}
try { try {
if (config.isEffective(this.getProjectId())) { if (config.isEffective(this.getProjectId())) {
HttpConfig httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig(), tree); HttpConfig httpConfig = getHttpConfig(config.getConfig().get(this.getProjectId()).getHttpConfig(), tree);
@ -213,24 +198,33 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
String url = httpConfig.getProtocol() + "://" + httpConfig.getSocket(); String url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
// 补充如果是完整URL 则用自身URL // 补充如果是完整URL 则用自身URL
boolean isUrl;
if (isUrl = (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl()))) { if (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl())) {
url = this.getUrl(); url = this.getUrl();
} }
if (isUrl) {
if (isUrl()) {
if (this.isCustomizeReq()) {
url = this.getUrl();
sampler.setPath(url);
}
if (StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { if (StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
url.replaceAll(this.getPort(), "10990"); url.replaceAll(this.getPort(), "10990");
} }
URL urlObject = new URL(url); try {
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); URL urlObject = new URL(url);
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8"));
if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
sampler.setProperty("HTTPSampler.port", this.getPort()); sampler.setProperty("HTTPSampler.port", this.getPort());
} else { } else {
sampler.setPort(urlObject.getPort()); sampler.setPort(urlObject.getPort());
}
sampler.setProtocol(urlObject.getProtocol());
sampler.setPath(urlObject.getPath());
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
} }
sampler.setProtocol(urlObject.getProtocol());
sampler.setPath(urlObject.getPath());
} else { } else {
//1.9 增加对Mock环境的判断 //1.9 增加对Mock环境的判断
if (this.isMockEnvironment()) { if (this.isMockEnvironment()) {
@ -331,7 +325,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
// 通用请求Headers // 通用请求Headers
if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) { && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) {
setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders()); if (!this.isCustomizeReq() || this.isRefEnvironment) {
// 如果不是自定义请求,或者引用环境则添加环境请求头
setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders());
}
} }
// 环境通用请求头 // 环境通用请求头
@ -355,6 +352,52 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
} }
// 兼容旧数据
private void compatible(ParameterConfig config) {
if (this.isCustomizeReq() && this.isRefEnvironment == null) {
if (StringUtils.isNotBlank(this.url)) {
this.isRefEnvironment = false;
} else {
this.isRefEnvironment = true;
}
}
// 数据兼容处理
if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
// 1.8 前后 混合数据
this.setProjectId(getParentProjectId());
} else if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
// 1.8 之后 当前正常数据
} else {
// 1.8 之前 数据
if (config.getConfig() != null) {
if (!config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
// 测试计划执行
Iterator<String> it = config.getConfig().keySet().iterator();
if (it.hasNext()) {
this.setProjectId(it.next());
}
} else {
this.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
}
}
}
}
private boolean isUrl() {
// 自定义字段没有引用环境则非url
if (this.isCustomizeReq()) {
if (this.isRefEnvironment) {
return false;
}
return true;
}
if (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl())) {
return true;
}
return false;
}
private boolean isVariable(String path, String value) { private boolean isVariable(String path, String value) {
Pattern p = Pattern.compile("(\\$\\{)([\\w]+)(\\})"); Pattern p = Pattern.compile("(\\$\\{)([\\w]+)(\\})");
Matcher m = p.matcher(path); Matcher m = p.matcher(path);
@ -507,7 +550,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
// HTTP 环境中请求头 // HTTP 环境中请求头
if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) { if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) {
setHeader(tree, httpConfig.getHeaders()); if (!this.isCustomizeReq() || this.isRefEnvironment) {
// 如果不是自定义请求,或者引用环境则添加环境请求头
setHeader(tree, httpConfig.getHeaders());
}
} }
return httpConfig; return httpConfig;
} }

View File

@ -57,7 +57,14 @@
Object.assign(this.request, row.request); Object.assign(this.request, row.request);
this.request.name = name; this.request.name = name;
if (this.request.protocol === 'HTTP') { if (this.request.protocol === 'HTTP') {
this.request.url = row.url; if (row.url) {
//
this.request.url = row.url;
this.request.path = row.url;
} else {
this.request.url = row.path;
this.request.path = row.path;
}
this.request.method = row.method; this.request.method = row.method;
} }
this.request.resourceId = getUUID(); this.request.resourceId = getUUID();

View File

@ -1,9 +1,9 @@
<template> <template>
<div> <div>
<div v-if="request.protocol === 'HTTP'"> <div v-if="request.protocol === 'HTTP'">
<el-input :placeholder="$t('api_test.definition.request.path_all_info')" v-if="request.url" v-model="request.url" <el-input :placeholder="$t('api_test.definition.request.path_all_info')" v-if="request.url || isCustomizeReq" v-model="request.url"
style="width: 85%;margin-top: 10px" size="small" @blur="urlChange"> style="width: 85%;margin-top: 10px" size="small" @blur="urlChange">
<el-select v-model="request.method" slot="prepend" style="width: 100px" size="small"> <el-select v-model="request.method" slot="prepend" style="width: 100px" size="small">ssss
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/> <el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select> </el-select>
</el-input> </el-input>
@ -13,6 +13,7 @@
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/> <el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select> </el-select>
</el-input> </el-input>
<el-checkbox v-if="isCustomizeReq" class="is-ref-environment" v-model="request.isRefEnvironment">{{$t('api_test.request.refer_to_environment')}}</el-checkbox>
</div> </div>
<div v-if="request.protocol === 'TCP' && isCustomizeReq"> <div v-if="request.protocol === 'TCP' && isCustomizeReq">
@ -46,6 +47,26 @@ export default {
reqOptions: REQ_METHOD, reqOptions: REQ_METHOD,
} }
}, },
mounted() {
if (this.isCustomizeReq) {
if (this.request.isRefEnvironment === undefined || this.request.isRefEnvironment === null) {
//
if (this.request.url) {
this.$set(this.request, 'isRefEnvironment', false);
} else {
this.$set(this.request, 'isRefEnvironment', true);
}
}
// urlpath
if (this.request.url) {
this.$set(this.request, 'path', this.request.url);
} else {
this.$set(this.request, 'url', this.request.path);
}
}
},
methods: { methods: {
pathChange() { pathChange() {
if (!this.request.path || this.request.path.indexOf('?') === -1) return; if (!this.request.path || this.request.path.indexOf('?') === -1) return;
@ -55,6 +76,9 @@ export default {
} }
}, },
urlChange() { urlChange() {
if (this.isCustomizeReq) {
this.request.path = this.request.url;
}
if (!this.request.url || this.request.url.indexOf('?') === -1) return; if (!this.request.url || this.request.url.indexOf('?') === -1) return;
let url = this.getURL(this.addProtocol(this.request.url)); let url = this.getURL(this.addProtocol(this.request.url));
if (url) { if (url) {
@ -90,4 +114,8 @@ export default {
.server-input { .server-input {
width: 50%; width: 50%;
} }
.is-ref-environment {
margin-left: 15px;
}
</style> </style>

View File

@ -245,8 +245,10 @@
this.request.url = url; this.request.url = url;
} catch (e) { } catch (e) {
if (url && (!url.startsWith("http://") || !url.startsWith("https://"))) { if (url && (!url.startsWith("http://") || !url.startsWith("https://"))) {
this.request.path = url; if (!this.isCustomizeReq) {
this.request.url = undefined; this.request.path = url;
this.request.url = undefined;
}
} }
} }
}, },
@ -307,7 +309,7 @@
this.reload(); this.reload();
}, },
run() { run() {
if (this.isApiImport) { if (this.isApiImport || this.request.isRefEnvironment) {
if (this.request.type && (this.request.type === "HTTPSamplerProxy" || this.request.type === "JDBCSampler" || this.request.type === "TCPSampler")) { if (this.request.type && (this.request.type === "HTTPSamplerProxy" || this.request.type === "JDBCSampler" || this.request.type === "TCPSampler")) {
if (!this.envMap || this.envMap.size === 0) { if (!this.envMap || this.envMap.size === 0) {
this.$warning("请在环境配置中为该步骤所属项目选择运行环境!"); this.$warning("请在环境配置中为该步骤所属项目选择运行环境!");