fix(接口测试): 修复合并分支问题

This commit is contained in:
q4speed 2020-11-13 11:07:15 +08:00
parent 1a7d70a142
commit 0218d3411e
1 changed files with 70 additions and 9 deletions

View File

@ -1,5 +1,6 @@
import {
Arguments,
ConstantTimer as JMXConstantTimer,
CookieManager,
DNSCacheManager,
DubboSample,
@ -10,22 +11,24 @@ import {
HTTPSamplerArguments,
HTTPsamplerFiles,
HTTPSamplerProxy,
IfController as JMXIfController,
JDBCDataSource,
JDBCSampler,
JSONPathAssertion,
JSONPostProcessor,
JSR223Assertion,
JSR223PostProcessor,
JSR223PreProcessor,
RegexExtractor,
ResponseCodeAssertion,
ResponseDataAssertion,
ResponseHeadersAssertion,
TCPSampler,
TestElement,
TestPlan,
ThreadGroup,
XPath2Assertion,
XPath2Extractor,
IfController as JMXIfController,
ConstantTimer as JMXConstantTimer, TCPSampler, JSR223Assertion,
} from "./JMX";
import Mock from "mockjs";
import {funcFilters} from "@/common/js/func-filter";
@ -96,6 +99,7 @@ export const ASSERTION_TYPE = {
JSON_PATH: "JSON",
DURATION: "Duration",
JSR223: "JSR223",
XPATH2: "XPath2",
}
export const ASSERTION_REGEX_SUBJECT = {
@ -225,6 +229,7 @@ export class Scenario extends BaseConfig {
this.enable = true;
this.databaseConfigs = [];
this.tcpConfig = undefined;
this.assertions = undefined;
this.set(options);
this.sets({
@ -241,6 +246,7 @@ export class Scenario extends BaseConfig {
options.databaseConfigs = options.databaseConfigs || [];
options.dubboConfig = new DubboConfig(options.dubboConfig);
options.tcpConfig = new TCPConfig(options.tcpConfig);
options.assertions = new Assertions(options.assertions);
return options;
}
@ -741,10 +747,11 @@ export class Assertions extends BaseConfig {
this.regex = [];
this.jsonPath = [];
this.jsr223 = [];
this.xpath2 = [];
this.duration = undefined;
this.set(options);
this.sets({text: Text, regex: Regex, jsonPath: JSONPath, jsr223: AssertionJSR223}, options);
this.sets({text: Text, regex: Regex, jsonPath: JSONPath, jsr223: AssertionJSR223, xpath2: XPath2}, options);
}
initOptions(options) {
@ -826,6 +833,23 @@ export class JSONPath extends AssertionType {
}
}
export class XPath2 extends AssertionType {
constructor(options) {
super(ASSERTION_TYPE.XPATH2);
this.expression = undefined;
this.description = undefined;
this.set(options);
}
// setJSONPathDescription() {
// this.description = this.expression + " expect: " + (this.expect ? this.expect : '');
// }
isValid() {
return !!this.expression;
}
}
export class Duration extends AssertionType {
constructor(options) {
super(ASSERTION_TYPE.DURATION);
@ -1132,6 +1156,9 @@ class JMXGenerator {
this.addScenarioCookieManager(threadGroup, scenario);
this.addJDBCDataSources(threadGroup, scenario);
this.addAssertion(threadGroup, scenario);
scenario.requests.forEach(request => {
if (request.enable) {
if (!request.isValid()) return;
@ -1156,7 +1183,7 @@ class JMXGenerator {
this.addRequestExtractor(sampler, request);
this.addRequestAssertion(sampler, request);
this.addAssertion(sampler, request);
this.addJSR223PreProcessor(sampler, request);
@ -1179,16 +1206,25 @@ class JMXGenerator {
}
addEnvironments(environments, target) {
let keys = new Set();
let targetMap = new Map();
target.forEach(item => {
keys.add(item.name);
if (item.name) {
targetMap.set(item.name, item.enable);
}
});
let envArray = environments;
if (!(envArray instanceof Array)) {
envArray = JSON.parse(environments);
}
envArray.forEach(item => {
if (item.enable !== false && item.name && !keys.has(item.name)) {
let targetItem = targetMap.get(item.name);
let hasItem;
if (targetItem) {
hasItem = (targetItem.enable !== false);
} else {
hasItem = false;
}
if (item.enable !== false && item.name && !hasItem) {
target.push(new KeyValue({name: item.name, value: item.value}));
}
})
@ -1292,7 +1328,10 @@ class JMXGenerator {
if (!(scenario.environment.config instanceof Object)) {
config = JSON.parse(scenario.environment.config);
}
this.addEnvironments(config.httpConfig.headers, request.headers)
this.addEnvironments(config.httpConfig.headers, request.headers);
if (request.doMultipartPost) {
this.removeContentType(request);
}
}
let name = request.name + " Headers";
this.addBodyFormat(request);
@ -1384,6 +1423,17 @@ class JMXGenerator {
}
}
removeContentType(request) {
for (let index in request.headers) {
if (request.headers.hasOwnProperty(index)) {
if (request.headers[index].name === 'Content-Type' && request.headers[index].enable !== false) {
request.headers.splice(index, 1);
break;
}
}
}
}
addRequestArguments(httpSamplerProxy, request) {
let args = this.filterKV(request.parameters);
if (args.length > 0) {
@ -1425,7 +1475,7 @@ class JMXGenerator {
httpSamplerProxy.add(new HTTPsamplerFiles(files));
}
addRequestAssertion(httpSamplerProxy, request) {
addAssertion(httpSamplerProxy, request) {
let assertions = request.assertions;
if (assertions.regex.length > 0) {
assertions.regex.filter(this.filter).forEach(regex => {
@ -1439,6 +1489,12 @@ class JMXGenerator {
})
}
if (assertions.xpath2.length > 0) {
assertions.xpath2.filter(this.filter).forEach(item => {
httpSamplerProxy.put(this.getXpathAssertion(item));
})
}
if (assertions.jsr223.length > 0) {
assertions.jsr223.filter(this.filter).forEach(item => {
httpSamplerProxy.put(this.getJSR223Assertion(item));
@ -1461,6 +1517,11 @@ class JMXGenerator {
return new JSR223Assertion(name, item);
}
getXpathAssertion(item) {
let name = item.expression;
return new XPath2Assertion(name, item);
}
getResponseAssertion(regex) {
let name = regex.description;
let type = JMX_ASSERTION_CONDITION.CONTAINS;