fix(接口自动化): 外部导入和执行重构去除域名部分

This commit is contained in:
fit2-zhao 2020-12-23 13:11:56 +08:00
parent abda6aa758
commit 4e01bf5af3
7 changed files with 56 additions and 27 deletions

View File

@ -119,9 +119,15 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setPort(config.getConfig().getHttpConfig().getPort()); sampler.setPort(config.getConfig().getHttpConfig().getPort());
sampler.setProtocol(config.getConfig().getHttpConfig().getProtocol()); sampler.setProtocol(config.getConfig().getHttpConfig().getProtocol());
url = config.getConfig().getHttpConfig().getProtocol() + "://" + config.getConfig().getHttpConfig().getSocket(); url = config.getConfig().getHttpConfig().getProtocol() + "://" + config.getConfig().getHttpConfig().getSocket();
// 补充如果是完整URL 则用自身URL
boolean isUrl = false;
if (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl())) {
url = this.getUrl();
isUrl = true;
}
URL urlObject = new URL(url); URL urlObject = new URL(url);
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath(); String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
if (StringUtils.isNotBlank(this.getPath())) { if (StringUtils.isNotBlank(this.getPath()) && !isUrl) {
envPath += this.getPath(); envPath += this.getPath();
} }
if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) { if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) {
@ -243,6 +249,15 @@ public class MsHTTPSamplerProxy extends MsTestElement {
tree.add(headerManager); tree.add(headerManager);
} }
public boolean isURL(String str) {
//转换为小写
try {
new URL(str);
return true;
} catch (Exception e) {
return false;
}
}
private boolean isRest() { private boolean isRest() {
return this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).toArray().length > 0; return this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).toArray().length > 0;

View File

@ -98,11 +98,14 @@ public class Body {
return StringUtils.equals(type, XML); return StringUtils.equals(type, XML);
} }
public boolean isWwwFROM() { public void initKvs() {
return StringUtils.equals(type, WWW_FROM); this.kvs = new ArrayList<>();
this.kvs.add(new KeyValue());
} }
public boolean isFromData() { public void initBinary() {
return StringUtils.equals(type, FORM_DATA); this.binary = new ArrayList<>();
this.binary.add(new KeyValue());
} }
} }

View File

@ -15,7 +15,7 @@ public class KeyValue {
private List<BodyFile> files; private List<BodyFile> files;
private String description; private String description;
private String contentType; private String contentType;
private boolean enable; private boolean enable = true;
private boolean encode = true; private boolean encode = true;
private boolean required; private boolean required;

View File

@ -20,6 +20,7 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -72,7 +73,7 @@ public abstract class ApiImportAbstractParser implements ApiImportParser {
protected ApiDefinitionResult buildApiDefinition(String id, String name, String path, String method) { protected ApiDefinitionResult buildApiDefinition(String id, String name, String path, String method) {
ApiDefinitionResult apiDefinition = new ApiDefinitionResult(); ApiDefinitionResult apiDefinition = new ApiDefinitionResult();
apiDefinition.setName(name); apiDefinition.setName(name);
apiDefinition.setPath(path); apiDefinition.setPath(formatPath(path));
apiDefinition.setProtocol(RequestType.HTTP); apiDefinition.setProtocol(RequestType.HTTP);
apiDefinition.setMethod(method); apiDefinition.setMethod(method);
apiDefinition.setId(id); apiDefinition.setId(id);
@ -81,17 +82,34 @@ public abstract class ApiImportAbstractParser implements ApiImportParser {
return apiDefinition; return apiDefinition;
} }
private String formatPath(String url) {
try {
URL urlObject = new URL(url);
StringBuffer pathBuffer = new StringBuffer(urlObject.getPath());
if (StringUtils.isNotEmpty(urlObject.getQuery())) {
pathBuffer.append("?").append(urlObject.getQuery());
}
return pathBuffer.toString();
} catch (Exception ex) {
return url;
}
}
protected MsHTTPSamplerProxy buildRequest(String name, String path, String method) { protected MsHTTPSamplerProxy buildRequest(String name, String path, String method) {
MsHTTPSamplerProxy request = new MsHTTPSamplerProxy(); MsHTTPSamplerProxy request = new MsHTTPSamplerProxy();
request.setName(name); request.setName(name);
request.setPath(path); // 路径去掉域名/IP 地址保留方法名称及参数
request.setPath(formatPath(path));
request.setMethod(method); request.setMethod(method);
request.setProtocol(RequestType.HTTP); request.setProtocol(RequestType.HTTP);
request.setId(UUID.randomUUID().toString()); request.setId(UUID.randomUUID().toString());
request.setHeaders(new ArrayList<>()); request.setHeaders(new ArrayList<>());
request.setArguments(new ArrayList<>()); request.setArguments(new ArrayList<>());
request.setRest(new ArrayList<>()); request.setRest(new ArrayList<>());
request.setBody(new Body()); Body body = new Body();
body.initKvs();
body.initBinary();
request.setBody(body);
return request; return request;
} }

View File

@ -37,7 +37,12 @@
<el-collapse-transition> <el-collapse-transition>
<div v-if="request.active"> <div v-if="request.active">
<div v-if="request.protocol === 'HTTP'"> <div v-if="request.protocol === 'HTTP'">
<el-input :placeholder="$t('api_test.definition.request.path_all_info')" v-model="request.url" style="width: 85%;margin-top: 10px" size="small"> <el-input :placeholder="$t('api_test.definition.request.path_all_info')" v-if="request.url" v-model="request.url" style="width: 85%;margin-top: 10px" size="small">
<el-select v-model="request.method" slot="prepend" style="width: 100px" size="small">
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select>
</el-input>
<el-input :placeholder="$t('api_test.definition.request.path_all_info')" v-else v-model="request.path" style="width: 85%;margin-top: 10px" size="small">
<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">
<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>
@ -96,20 +101,10 @@
try { try {
let urlObject = new URL(this.request.url); let urlObject = new URL(this.request.url);
let url = urlObject.protocol + "//" + urlObject.host + "/"; let url = urlObject.protocol + "//" + urlObject.host + "/";
if (url) {
let path = this.request.url.substr(url.length);
if (!path.startsWith('/')) {
path = "/" + path;
}
this.request.path = path;
} else {
this.request.url = this.request.path;
}
} catch (e) { } catch (e) {
if (this.request.url) { if (this.request.url) {
this.request.path = this.request.url; this.request.path = this.request.url;
} else { this.request.url = undefined;
this.request.url = this.request.path;
} }
} }
} }

View File

@ -945,8 +945,4 @@
font-size: 13px; font-size: 13px;
} }
/deep/ .el-form-item__content {
line-height: 100%;
}
</style> </style>

View File

@ -16,7 +16,7 @@
<el-form-item> <el-form-item>
<el-dropdown split-button type="primary" class="ms-api-buttion" @click="handleCommand" <el-dropdown split-button type="primary" class="ms-api-buttion" @click="handleCommand"
@command="handleCommand" size="small" v-if="testCase===undefined"> @command="handleCommand" size="small" v-if="testCase===undefined && !scenario">
{{$t('commons.test')}} {{$t('commons.test')}}
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item command="save_as">{{$t('api_test.definition.request.save_as')}}</el-dropdown-item> <el-dropdown-item command="save_as">{{$t('api_test.definition.request.save_as')}}</el-dropdown-item>
@ -76,7 +76,9 @@
method: [{required: true, message: this.$t('test_track.case.input_maintainer'), trigger: 'change'}], method: [{required: true, message: this.$t('test_track.case.input_maintainer'), trigger: 'change'}],
url: [ url: [
{max: 500, required: true, message: this.$t('commons.input_limit', [1, 500]), trigger: 'blur'}, {max: 500, required: true, message: this.$t('commons.input_limit', [1, 500]), trigger: 'blur'},
{validator: validateURL, trigger: 'blur'} /*
{validator: validateURL, trigger: 'blur'}
*/
], ],
}, },
debugForm: {method: REQ_METHOD[0].id, environmentId: ""}, debugForm: {method: REQ_METHOD[0].id, environmentId: ""},