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)
private MsAuthManager authManager;
@JSONField(ordinal = 37)
private Boolean isRefEnvironment;
private void setRefElement() {
try {
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
@ -185,26 +188,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
config.setConfig(getEnvironmentConfig(useEnvironment));
}
// 数据兼容处理
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());
}
}
}
compatible(config);
try {
if (config.isEffective(this.getProjectId())) {
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();
// 补充如果是完整URL 则用自身URL
boolean isUrl;
if (isUrl = (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl()))) {
if (StringUtils.isNotEmpty(this.getUrl()) && isURL(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("${")) {
url.replaceAll(this.getPort(), "10990");
}
URL urlObject = new URL(url);
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8"));
try {
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("${")) {
sampler.setProperty("HTTPSampler.port", this.getPort());
} else {
sampler.setPort(urlObject.getPort());
if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
sampler.setProperty("HTTPSampler.port", this.getPort());
} else {
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 {
//1.9 增加对Mock环境的判断
if (this.isMockEnvironment()) {
@ -331,7 +325,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
// 通用请求Headers
if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null
&& 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) {
Pattern p = Pattern.compile("(\\$\\{)([\\w]+)(\\})");
Matcher m = p.matcher(path);
@ -507,7 +550,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
// HTTP 环境中请求头
if (httpConfig != null && CollectionUtils.isNotEmpty(httpConfig.getHeaders())) {
setHeader(tree, httpConfig.getHeaders());
if (!this.isCustomizeReq() || this.isRefEnvironment) {
// 如果不是自定义请求,或者引用环境则添加环境请求头
setHeader(tree, httpConfig.getHeaders());
}
}
return httpConfig;
}

View File

@ -57,7 +57,14 @@
Object.assign(this.request, row.request);
this.request.name = name;
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.resourceId = getUUID();

View File

@ -1,9 +1,9 @@
<template>
<div>
<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">
<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-select>
</el-input>
@ -13,6 +13,7 @@
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select>
</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 v-if="request.protocol === 'TCP' && isCustomizeReq">
@ -46,6 +47,26 @@ export default {
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: {
pathChange() {
if (!this.request.path || this.request.path.indexOf('?') === -1) return;
@ -55,6 +76,9 @@ export default {
}
},
urlChange() {
if (this.isCustomizeReq) {
this.request.path = this.request.url;
}
if (!this.request.url || this.request.url.indexOf('?') === -1) return;
let url = this.getURL(this.addProtocol(this.request.url));
if (url) {
@ -90,4 +114,8 @@ export default {
.server-input {
width: 50%;
}
.is-ref-environment {
margin-left: 15px;
}
</style>

View File

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