refactor(接口测试): 优化自定义请求路径带变量时的处理逻辑

--bug=1027539 --user=王孝刚 [接口测试]
github#25333URL引用的自定义常量,实际请求会被[]包裹,常量格式${ip}:${port}
https://www.tapd.cn/55049933/s/1389897
This commit is contained in:
wxg0103 2023-07-05 17:20:26 +08:00 committed by 刘瑞斌
parent 6fa237bd2f
commit fef406925c
4 changed files with 22 additions and 51 deletions

View File

@ -427,16 +427,16 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (!isCustomizeReqCompleteUrl(this.path) || isRefEnvironment) {
try {
URL urlObject = new URL(url);
envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getFile();
if (StringUtils.isNotBlank(this.getPath())) {
envPath += this.getPath();
}
if (httpConfig.getDomain().startsWith("${")) {
if (httpConfig.getSocket().contains("${")) {
envPath = StringUtils.isNotBlank(this.path) ? StringUtils.join(url, this.path) : url;
} else if (StringUtils.isNotEmpty(httpConfig.getDomain())) {
sampler.setDomain(URLDecoder.decode(httpConfig.getDomain(), StandardCharsets.UTF_8.name()));
} else if (StringUtils.isNotEmpty(urlObject.getHost())) {
envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getFile();
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name()));
sampler.setProtocol(httpConfig.getProtocol());
sampler.setPort(httpConfig.getPort());
sampler.setPort(urlObject.getPort());
} else {
sampler.setDomain("");
sampler.setProtocol("");
@ -451,9 +451,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name()));
sampler.setProtocol(urlObject.getProtocol());
}
if (StringUtils.isNotEmpty(envPath) && !envPath.startsWith("/")) {
/*if (StringUtils.isNotEmpty(envPath) && !envPath.startsWith("/")) {
envPath = "/" + envPath;
}
}*/
sampler.setProperty("HTTPSampler.path", URLDecoder.decode(URLEncoder.encode(envPath, StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name()));
}
}
@ -468,14 +468,21 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
try {
URL urlObject = new URL(url);
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name()));
if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
sampler.setProperty("HTTPSampler.port", this.getPort());
} else if (urlObject.getPort() != -1) {
sampler.setPort(urlObject.getPort());
String envPath;
if (url.contains("${")){
envPath = url;
} else {
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name()));
if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
sampler.setProperty("HTTPSampler.port", this.getPort());
} else if (urlObject.getPort() != -1) {
sampler.setPort(urlObject.getPort());
}
envPath = urlObject.getPath();
}
sampler.setProtocol(urlObject.getProtocol());
sampler.setProperty("HTTPSampler.path", URLDecoder.decode(urlObject.getPath(), StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name());
sampler.setProperty("HTTPSampler.path", URLDecoder.decode(envPath, StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name());
} catch (Exception e) {
sampler.setProperty("HTTPSampler.path", url);
LogUtil.error(e.getMessage(), e);

View File

@ -20,7 +20,6 @@ public class HttpConfig {
private String socket;
private String domain;
private String protocol = "https";
private int port;
private boolean isMock;
private List<HttpConfigCondition> conditions;
private List<KeyValue> headers;

View File

@ -173,7 +173,6 @@ export default {
protocol: "http",
socket: "",
domain: "",
port: 0,
headers: [new KeyValue()],
headlessEnabled: true,
browser: 'CHROME'
@ -237,7 +236,6 @@ export default {
clearHisData() {
this.httpConfig.socket = undefined;
this.httpConfig.protocol = undefined;
this.httpConfig.port = undefined;
this.httpConfig.domain = undefined;
},
getDetails(row) {
@ -265,7 +263,6 @@ export default {
protocol: "http",
socket: "",
domain: "",
port: 0,
headers: [new KeyValue()]
};
if (row) {
@ -340,7 +337,6 @@ export default {
socket: this.condition.socket,
headers: this.condition.headers,
protocol: this.condition.protocol,
port: this.condition.port,
time: this.condition.time
};
if (obj.type === "PATH") {
@ -403,7 +399,6 @@ export default {
protocol: this.condition.protocol,
headers: this.condition.headers,
domain: this.condition.domain,
port: this.condition.port,
time: new Date().getTime(),
description: this.condition.description
};
@ -444,6 +439,7 @@ export default {
},
validateSocket(socket) {
if (!socket) return true;
this.condition.socket = socket;
let urlStr = this.condition.protocol + "://" + socket;
let url = {};
try {
@ -457,13 +453,6 @@ export default {
this.condition.domain = split[0];
return true;
}
this.condition.port = url.port;
let path = url.pathname === "/" ? "" : url.pathname;
if (url.port) {
this.condition.socket = this.condition.domain + ":" + url.port + path;
} else {
this.condition.socket = this.condition.domain + path;
}
return true;
},
validate() {

View File

@ -177,8 +177,6 @@ export default {
details: [new KeyValue({name: "", value: "contains"})],
protocol: "http",
socket: "",
domain: "",
port: 0,
headers: [new KeyValue()],
headlessEnabled: true,
browser: 'CHROME'
@ -206,8 +204,6 @@ export default {
this.condition.type = "NONE";
this.condition.socket = this.httpConfig.socket;
this.condition.protocol = this.httpConfig.protocol;
this.condition.port = this.httpConfig.port;
this.condition.domain = this.httpConfig.domain;
this.condition.time = new Date().getTime();
this.condition.headers = this.httpConfig.headers;
this.condition.description = this.httpConfig.description;
@ -219,8 +215,6 @@ export default {
details: [new KeyValue({name: "", value: "contains"})],
protocol: "http",
socket: "",
domain: "",
port: 0,
headers: [new KeyValue()]
};
},
@ -242,8 +236,6 @@ export default {
clearHisData() {
this.httpConfig.socket = undefined;
this.httpConfig.protocol = undefined;
this.httpConfig.port = undefined;
this.httpConfig.domain = undefined;
},
getDetails(row) {
if (row && row.type === "MODULE") {
@ -269,8 +261,6 @@ export default {
details: [new KeyValue({name: "", value: "contains"})],
protocol: "http",
socket: "",
domain: "",
port: 0,
headers: [new KeyValue()]
};
if (row) {
@ -345,7 +335,6 @@ export default {
socket: this.condition.socket,
headers: this.condition.headers,
protocol: this.condition.protocol,
port: this.condition.port,
time: this.condition.time
};
if (obj.type === "PATH") {
@ -360,7 +349,6 @@ export default {
details: [new KeyValue({name: "", value: "contains"})],
protocol: "http",
socket: "",
domain: "",
headers: [new KeyValue()]
};
this.reload();
@ -373,7 +361,6 @@ export default {
details: [new KeyValue({name: "", value: "contains"})],
protocol: "http",
socket: "",
domain: "",
headers: [new KeyValue()]
};
this.$refs.envTable.setCurrentRow(0);
@ -407,8 +394,6 @@ export default {
socket: this.condition.socket,
protocol: this.condition.protocol,
headers: this.condition.headers,
domain: this.condition.domain,
port: this.condition.port,
time: new Date().getTime(),
description: this.condition.description
};
@ -438,7 +423,6 @@ export default {
details: row.details,
protocol: row.protocol,
headers: JSON.parse(JSON.stringify(row.headers)),
domain: row.domain,
time: new Date().getTime()
};
if (index != -1) {
@ -448,7 +432,7 @@ export default {
}
},
validateSocket(socket) {
if (!socket) return true;
this.condition.socket = socket;
let urlStr = this.condition.protocol + "://" + socket;
let url = {};
try {
@ -462,14 +446,6 @@ export default {
this.condition.domain = split[0];
return true;
}
this.condition.port = url.port;
let path = url.pathname === "/" ? "" : url.pathname;
if (url.port) {
this.condition.socket = this.condition.domain + ":" + url.port + path;
} else {
this.condition.socket = this.condition.domain + path;
}
return true;
},
validate() {
let isValidate = false;