feat(接口定义): 基础参数重构

This commit is contained in:
fit2-zhao 2020-11-23 19:00:21 +08:00
parent 637253f538
commit 0f0b11fd85
18 changed files with 62 additions and 225 deletions

View File

@ -3,7 +3,6 @@ package io.metersphere.api.dto.definition.request.processors.post;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.prop.StringProp;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -21,10 +20,10 @@ public class MsJSR223PostProcessor extends MsTestElement {
private String type = "JSR223PostProcessor";
@JSONField(ordinal = 10)
private StringProp script;
private String script;
@JSONField(ordinal = 11)
private StringProp scriptLanguage;
private String scriptLanguage;
public void toHashTree(HashTree tree, List<MsTestElement> hashTree) {
@ -34,8 +33,8 @@ public class MsJSR223PostProcessor extends MsTestElement {
processor.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
processor.setProperty("cacheKey", "true");
processor.setProperty("scriptLanguage", this.getScriptLanguage().getValue());
processor.setProperty("script", this.getScript().getValue());
processor.setProperty("scriptLanguage", this.getScriptLanguage());
processor.setProperty("script", this.getScript());
final HashTree jsr223PostTree = tree.add(processor);
if (CollectionUtils.isNotEmpty(hashTree)) {

View File

@ -3,7 +3,6 @@ package io.metersphere.api.dto.definition.request.processors.pre;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.prop.StringProp;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -21,10 +20,10 @@ public class MsJSR223PreProcessor extends MsTestElement {
private String type = "JSR223PreProcessor";
@JSONField(ordinal = 10)
private StringProp script;
private String script;
@JSONField(ordinal = 11)
private StringProp scriptLanguage;
private String scriptLanguage;
public void toHashTree(HashTree tree, List<MsTestElement> hashTree) {
JSR223PreProcessor processor = new JSR223PreProcessor();
@ -33,8 +32,8 @@ public class MsJSR223PreProcessor extends MsTestElement {
processor.setProperty(TestElement.TEST_CLASS, JSR223PreProcessor.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
processor.setProperty("cacheKey", "true");
processor.setProperty("scriptLanguage", this.getScriptLanguage().getValue());
processor.setProperty("script", this.getScript().getValue());
processor.setProperty("scriptLanguage", this.getScriptLanguage());
processor.setProperty("script", this.getScript());
final HashTree jsr223PreTree = tree.add(processor);
if (CollectionUtils.isNotEmpty(hashTree)) {

View File

@ -1,12 +0,0 @@
package io.metersphere.api.dto.definition.request.prop;
import lombok.Data;
@Data
public class BoolProp {
private String id;
private String key;
private String name;
private String type;
private boolean value;
}

View File

@ -1,12 +0,0 @@
package io.metersphere.api.dto.definition.request.prop;
import lombok.Data;
@Data
public class StringProp {
private String id;
private String key;
private String name;
private String type;
private String value;
}

View File

@ -6,8 +6,6 @@ import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager;
import io.metersphere.api.dto.definition.request.prop.BoolProp;
import io.metersphere.api.dto.definition.request.prop.StringProp;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
@ -41,25 +39,25 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private String type = "HTTPSamplerProxy";
@JSONField(ordinal = 10)
private StringProp protocol;
private String protocol;
@JSONField(ordinal = 11)
private StringProp domain;
private String domain;
@JSONField(ordinal = 12)
private StringProp port;
private String port;
@JSONField(ordinal = 13)
private StringProp method;
private String method;
@JSONField(ordinal = 14)
private StringProp path;
private String path;
@JSONField(ordinal = 15)
private StringProp connectTimeout;
private String connectTimeout;
@JSONField(ordinal = 16)
private StringProp responseTimeout;
private String responseTimeout;
@JSONField(ordinal = 17)
private List<KeyValue> arguments;
@ -74,10 +72,10 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private String url;
@JSONField(ordinal = 21)
private BoolProp followRedirects;
private boolean followRedirects;
@JSONField(ordinal = 22)
private BoolProp doMultipartPost;
private boolean doMultipartPost;
@JSONField(ordinal = 23)
private String useEnvironment;
@ -89,13 +87,13 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setName(this.getName());
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui"));
sampler.setMethod(this.getMethod().getValue());
sampler.setMethod(this.getMethod());
sampler.setContentEncoding("UTF-8");
sampler.setConnectTimeout(this.getConnectTimeout().getValue() == null ? "6000" : this.getConnectTimeout().getValue());
sampler.setResponseTimeout(this.getResponseTimeout().getValue() == null ? "6000" : this.getResponseTimeout().getValue());
sampler.setFollowRedirects(this.getFollowRedirects() != null ? this.getFollowRedirects().isValue() : true);
sampler.setConnectTimeout(this.getConnectTimeout() == null ? "6000" : this.getConnectTimeout());
sampler.setResponseTimeout(this.getResponseTimeout() == null ? "6000" : this.getResponseTimeout());
sampler.setFollowRedirects(this.isFollowRedirects());
sampler.setUseKeepAlive(true);
sampler.setDoMultipart(this.getDoMultipartPost() != null ? this.getDoMultipartPost().isValue() : true);
sampler.setDoMultipart(this.isDoMultipartPost());
EnvironmentConfig config = null;
if (useEnvironment != null) {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
@ -111,8 +109,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
url = config.getHttpConfig().getProtocol() + "://" + config.getHttpConfig().getSocket();
URL urlObject = new URL(url);
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
if (StringUtils.isNotBlank(this.getPath().getValue())) {
envPath += this.getPath().getValue();
if (StringUtils.isNotBlank(this.getPath())) {
envPath += this.getPath();
}
if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) {
sampler.setPath(getRestParameters(URLDecoder.decode(envPath, "UTF-8")));
@ -141,7 +139,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setArguments(httpArguments(this.getArguments()));
}
// 请求体
if (!StringUtils.equals(this.getMethod().getValue(), "GET")) {
if (!StringUtils.equals(this.getMethod(), "GET")) {
List<KeyValue> body = new ArrayList<>();
if (this.getBody().isKV() || this.getBody().isBinary()) {
body = this.getBody().getKvs().stream().filter(KeyValue::isValid).collect(Collectors.toList());

View File

@ -56,7 +56,7 @@
}
if (this.currentApi != null && this.currentApi.id != null) {
this.reqUrl = "/api/definition/update";
this.currentApi.url = this.request.path.value;
this.currentApi.url = this.request.path;
} else {
this.reqUrl = "/api/definition/create";
this.currentApi.id = getUUID().substring(0, 8);

View File

@ -246,7 +246,7 @@
handleTestCase(testCase) {
this.selectApi = testCase;
let request = JSON.parse(testCase.request);
this.selectApi.url = request.path.value;
this.selectApi.url = request.path;
this.isHide = false;
},
handleDelete(testCase) {

View File

@ -156,6 +156,7 @@
getApiGroupData() {
this.getProjects();
this.getApiModuleTree();
this.changeProtocol();
},
findTreeByNodeId(rootNode, nodeId) {
if (rootNode.id == nodeId) {

View File

@ -100,7 +100,7 @@
this.httpForm.protocol = this.currentProtocol;
let header = createComponent("HeaderManager");
let request = createComponent("HTTPSamplerProxy");
request.path.value = this.httpForm.url;
request.path = this.httpForm.url;
request.hashTree = [header];
this.httpForm.request = request;
if (this.currentModule != null) {

View File

@ -24,7 +24,7 @@
<el-form-item :label="$t('api_report.request')" prop="url">
<el-input :placeholder="$t('api_test.definition.request.path_info')" v-model="httpForm.url"
class="ms-http-input" size="small" style="margin-top: 5px">
<el-select v-model="request.method.value" 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-select>
</el-input>
@ -127,7 +127,7 @@
},
setParameter() {
this.httpForm.modulePath = this.getPath(this.httpForm.moduleId);
this.request.path.value = this.httpForm.url;
this.request.path = this.httpForm.url;
this.httpForm.request.useEnvironment = undefined;
},
saveApi() {

View File

@ -111,7 +111,7 @@
if (valid) {
this.loading = true;
this.request.url = this.debugForm.url;
this.request.method.value = this.debugForm.method;
this.request.method = this.debugForm.method;
this.request.hashTree[0].headers = this.headers;
this.request.name = getUUID().substring(0, 8);
this.runData = [];

View File

@ -1,4 +1,3 @@
import {elementProp, stringProp} from "../../../props";
import Configuration from "../configuration";
const DEFAULT_OPTIONS = {
@ -17,36 +16,6 @@ export default class Arguments extends Configuration {
super(options);
this.arguments = [];
let collectionProp = this.initCollectionProp('Arguments.arguments');
collectionProp.forEach(elementProp => {
let name = elementProp.initStringProp('Argument.name').value;
let value = elementProp.initStringProp('Argument.value').value;
let desc = elementProp.initStringProp('Argument.desc').value;
let metadata = elementProp.initStringProp('Argument.metadata', "=").value;
let argument = {name: name, value: value, desc: desc, metadata: metadata, enable: true};
this.arguments.push(argument);
})
}
updateProps() {
let collectionProp = this.props['Arguments.arguments'];
collectionProp.clear();
this.arguments.forEach(arg => {
if (arg.enable !== false) {
let ep = elementProp(arg.name, "Argument");
ep.add(stringProp("Argument.name", arg.name));
ep.add(stringProp("Argument.value", arg.value));
ep.add(stringProp("Argument.desc", arg.desc));
ep.add(stringProp("Argument.metadata", arg.metadata || "="));
collectionProp.add(ep)
}
})
}
toJson() {
this.updateProps();
return super.toJson();
}
}

View File

@ -1,4 +1,3 @@
import {elementProp, stringProp} from "../../../props";
import Configuration from "../configuration";
const DEFAULT_OPTIONS = {
@ -17,32 +16,6 @@ export default class HeaderManager extends Configuration {
super(options);
this.type = "HeaderManager";
this.headers = [];
let collectionProp = this.initCollectionProp('HeaderManager.headers');
collectionProp.forEach(elementProp => {
let name = elementProp.initStringProp('Header.name').value;
let value = elementProp.initStringProp('Header.value').value;
let header = {name: name, value: value, enable: true};
this.headers.push(header);
})
}
updateProps() {
let collectionProp = this.props['HeaderManager.headers'];
collectionProp.clear();
this.headers.forEach(header => {
if (header.enable !== false) {
let ep = elementProp("", "Header");
ep.add(stringProp("Header.name", header.name));
ep.add(stringProp("Header.value", header.value));
collectionProp.add(ep)
}
})
}
toJson() {
this.updateProps();
return super.toJson();
}
}

View File

@ -15,11 +15,11 @@ export default class JSR223PostProcessor extends PostProcessor {
constructor(options = DEFAULT_OPTIONS) {
super(options);
this.type = "JSR223PostProcessor";
this.scriptLanguage = this.initStringProp("scriptLanguage", "java")
this.parameters = this.initStringProp("parameters")
this.filename = this.initStringProp("filename")
this.cacheKey = this.initStringProp("cacheKey", true)
this.script = this.initStringProp("script")
this.scriptLanguage = "java";
this.parameters = [];
this.filename = undefined;
this.cacheKey = true;
this.script = undefined;
}
}

View File

@ -15,11 +15,11 @@ export default class JSR223PreProcessor extends PostProcessor {
constructor(options = DEFAULT_OPTIONS) {
super(options);
this.type = "JSR223PreProcessor";
this.scriptLanguage = this.initStringProp("scriptLanguage", "java")
this.parameters = this.initStringProp("parameters")
this.filename = this.initStringProp("filename")
this.cacheKey = this.initStringProp("cacheKey", true)
this.script = this.initStringProp("script")
this.scriptLanguage = "java";
this.parameters = [];
this.filename = undefined;
this.cacheKey = undefined;
this.script = undefined;
}
}

View File

@ -1,6 +1,6 @@
import {boolProp, elementProp, stringProp} from "../../../props";
import Sampler from "../sampler";
import {Body} from "../../../../../model/ApiTestModel";
const DEFAULT_OPTIONS = {
options: {
attributes: {
@ -14,106 +14,28 @@ const DEFAULT_OPTIONS = {
export default class HTTPSamplerProxy extends Sampler {
constructor(options = DEFAULT_OPTIONS) {
super(options);
this.protocol = this.initStringProp('HTTPSampler.protocol', "https");
this.domain = this.initStringProp('HTTPSampler.domain');
this.port = this.initStringProp('HTTPSampler.port');
this.type = "HTTPSamplerProxy";
this.method = this.initStringProp('HTTPSampler.method', "GET");
this.path = this.initStringProp('HTTPSampler.path');
this.contentEncoding = this.initStringProp('HTTPSampler.contentEncoding', "UTF-8");
this.protocol = "HTTP";
this.domain = undefined;
this.port = undefined;
this.method = undefined;
this.path = undefined;
this.contentEncoding = undefined;
this.autoRedirects = this.initBoolProp('HTTPSampler.auto_redirects');
this.followRedirects = this.initBoolProp('HTTPSampler.follow_redirects', true);
this.useKeepalive = this.initBoolProp('HTTPSampler.use_keepalive', true);
this.postBodyRaw = this.initBoolProp('HTTPSampler.postBodyRaw');
this.doMultipartPost = this.initBoolProp('HTTPSampler.DO_MULTIPART_POST', false);
this.browserCompatibleMultipart = this.initBoolProp('HTTPSampler.BROWSER_COMPATIBLE_MULTIPART');
this.embeddedUrlRe = this.initStringProp('HTTPSampler.embedded_url_re');
this.connectTimeout = this.initStringProp('HTTPSampler.connect_timeout');
this.responseTimeout = this.initStringProp('HTTPSampler.response_timeout');
this.autoRedirects = false;
this.followRedirects = true;
this.useKeepalive = true;
this.postBodyRaw = undefined;
this.doMultipartPost = false;
this.browserCompatibleMultipart = undefined;
this.embeddedUrlRe = undefined;
this.connectTimeout = 6000;
this.responseTimeout = 6000;
// 初始化主体对象
this.body = new Body();
this.arguments = [];
this.rest = [];
let elementProp = this.initElementProp('HTTPsampler.Arguments', 'Arguments');
let collectionProp = elementProp.initCollectionProp('Arguments.arguments');
collectionProp.forEach(elementProp => {
let name = elementProp.initStringProp('Argument.name').value;
let value = elementProp.initStringProp('Argument.value').value;
let alwaysEncode = elementProp.initBoolProp('HTTPArgument.always_encode').value;
let useEquals = elementProp.initBoolProp('HTTPArgument.use_equals', true).value;
let contentType = elementProp.initStringProp('HTTPArgument.content_type', "text/plain").value;
let arg = {
name: name,
value: value,
alwaysEncode: alwaysEncode,
useEquals: useEquals,
contentType: contentType,
enable: true
};
this.arguments.push(arg);
})
if (this.arguments.length > 0 && this.arguments[0].name === "") {
this.body = this.arguments[0].value;
}
this.files = [];
let filesProp = this.initElementProp("HTTPsampler.Files", "HTTPFileArgs");
let filesCollectionProp = filesProp.initCollectionProp('HTTPFileArgs.files');
filesCollectionProp.forEach(elementProp => {
let path = elementProp.initStringProp('File.path').value;
let name = elementProp.initStringProp('File.paramname').value;
let type = elementProp.initBoolProp('File.mimetype').value;
let file = {path: path, name: name, type: type, enable: true};
this.files.push(file);
});
}
updateProps() {
if (this.body && this.postBodyRaw.value) {
this.arguments = [{name: "", value: this.body, alwaysEncode: false}];
}
let collectionProp = this.props['HTTPsampler.Arguments'].elements['Arguments.arguments'];
collectionProp.clear();
this.arguments.forEach(variable => {
if (variable.enable !== false) {
let ep = elementProp(variable.name, "HTTPArgument");
ep.add(stringProp("Argument.name", variable.name));
ep.add(stringProp("Argument.value", variable.value));
ep.add(stringProp("Argument.metadata", "="));
ep.add(boolProp("HTTPArgument.always_encode", variable.alwaysEncode));
ep.add(boolProp("HTTPArgument.use_equals", variable.useEquals));
if (variable.contentType && variable.contentType !== "text/plain") {
ep.add(stringProp("HTTPArgument.content_type", variable.contentType));
}
collectionProp.add(ep)
}
})
let filesProp = this.props["HTTPsampler.Files"].elements["HTTPFileArgs.files"];
filesProp.clear();
this.files.forEach(file => {
if (file.enable !== false) {
let ep = elementProp(file.path, "HTTPFileArg");
ep.add(stringProp("File.path", file.path));
ep.add(stringProp("File.paramname", file.name));
ep.add(stringProp("File.mimetype", file.type));
filesProp.add(ep);
}
})
}
toJson() {
this.updateProps();
return super.toJson();
}
}

View File

@ -22,9 +22,9 @@
</el-row>
<el-row>
<el-col :span="20" class="script-content">
<ms-code-edit v-if="isCodeEditAlive" :mode="jsr223ProcessorData.scriptLanguage.value"
<ms-code-edit v-if="isCodeEditAlive" :mode="jsr223ProcessorData.scriptLanguage"
:read-only="isReadOnly"
:data.sync="jsr223ProcessorData.script.value" theme="eclipse" :modes="['java','python']"
:data.sync="jsr223ProcessorData.script" theme="eclipse" :modes="['java','python']"
ref="codeEdit"/>
</el-col>
<el-col :span="4" class="script-index">

View File

@ -8,7 +8,7 @@
<p class="tip">{{$t('test_track.plan_view.base_info')}} </p>
<!-- 请求方法 -->
<el-form-item :label="$t('api_report.request')" prop="method">
<el-select v-model="api.request.method.value" style="width: 100px" size="small">
<el-select v-model="api.request.method" style="width: 100px" size="small">
<el-option v-for="item in reqOptions" :key="item.id" :label="item.label" :value="item.id"/>
</el-select>
</el-form-item>
@ -36,7 +36,7 @@
<!-- 请求地址 -->
<el-form-item prop="url">
<el-input :placeholder="$t('api_test.definition.request.path_info')" v-model="api.request.path.value" class="ms-htt-width"
<el-input :placeholder="$t('api_test.definition.request.path_info')" v-model="api.request.path" class="ms-htt-width"
size="small" :disabled="false"/>
</el-form-item>