fix(接口自动化): 修复导入场景步骤请求 ip 和端口 是变量的问题
This commit is contained in:
parent
e903441002
commit
dda1217c1a
|
@ -75,6 +75,8 @@ import org.apache.jorphan.collections.HashTree;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
|
public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
|
||||||
|
@ -132,12 +134,66 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
|
||||||
return (HashTree) field.get(scriptWrapper);
|
return (HashTree) field.get(scriptWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isProtocolDefaultPort(HTTPSamplerProxy source) {
|
||||||
|
String portAsString = source.getPropertyAsString("HTTPSampler.port");
|
||||||
|
if (portAsString != null && !portAsString.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String url(String protocol, String host, String port, String file) {
|
||||||
|
protocol = protocol.toLowerCase();
|
||||||
|
if (StringUtils.isNotEmpty(file) && !file.startsWith("/")) {
|
||||||
|
file += "/";
|
||||||
|
}
|
||||||
|
return protocol + "://" + host + ":" + port + file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl(HTTPSamplerProxy source) throws MalformedURLException {
|
||||||
|
String path = source.getPath();
|
||||||
|
if (!path.startsWith("http://") && !path.startsWith("https://")) {
|
||||||
|
String domain = source.getDomain();
|
||||||
|
String protocol = source.getProtocol();
|
||||||
|
String method = source.getMethod();
|
||||||
|
StringBuilder pathAndQuery = new StringBuilder(100);
|
||||||
|
if ("file".equalsIgnoreCase(protocol)) {
|
||||||
|
domain = null;
|
||||||
|
} else if (!path.startsWith("/")) {
|
||||||
|
pathAndQuery.append('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
pathAndQuery.append(path);
|
||||||
|
if ("GET".equals(method) || "DELETE".equals(method) || "OPTIONS".equals(method)) {
|
||||||
|
String queryString = source.getQueryString(source.getContentEncoding());
|
||||||
|
if (queryString.length() > 0) {
|
||||||
|
if (path.contains("?")) {
|
||||||
|
pathAndQuery.append("&");
|
||||||
|
} else {
|
||||||
|
pathAndQuery.append("?");
|
||||||
|
}
|
||||||
|
|
||||||
|
pathAndQuery.append(queryString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String portAsString = source.getPropertyAsString("HTTPSampler.port");
|
||||||
|
return this.isProtocolDefaultPort(source) ? new URL(protocol, domain, pathAndQuery.toString()).toExternalForm() : this.url(protocol, domain, portAsString, pathAndQuery.toString());
|
||||||
|
} else {
|
||||||
|
return new URL(path).toExternalForm();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void convertHttpSampler(MsHTTPSamplerProxy samplerProxy, Object key) {
|
private void convertHttpSampler(MsHTTPSamplerProxy samplerProxy, Object key) {
|
||||||
try {
|
try {
|
||||||
HTTPSamplerProxy source = (HTTPSamplerProxy) key;
|
HTTPSamplerProxy source = (HTTPSamplerProxy) key;
|
||||||
BeanUtils.copyBean(samplerProxy, source);
|
BeanUtils.copyBean(samplerProxy, source);
|
||||||
samplerProxy.setRest(new ArrayList<KeyValue>(){{this.add(new KeyValue());}});
|
samplerProxy.setRest(new ArrayList<KeyValue>() {{
|
||||||
samplerProxy.setArguments(new ArrayList<KeyValue>(){{this.add(new KeyValue());}});
|
this.add(new KeyValue());
|
||||||
|
}});
|
||||||
|
samplerProxy.setArguments(new ArrayList<KeyValue>() {{
|
||||||
|
this.add(new KeyValue());
|
||||||
|
}});
|
||||||
if (source != null && source.getHTTPFiles().length > 0) {
|
if (source != null && source.getHTTPFiles().length > 0) {
|
||||||
samplerProxy.getBody().initBinary();
|
samplerProxy.getBody().initBinary();
|
||||||
samplerProxy.getBody().setType(Body.FORM_DATA);
|
samplerProxy.getBody().setType(Body.FORM_DATA);
|
||||||
|
@ -158,7 +214,8 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
|
||||||
samplerProxy.getBody().setKvs(keyValues);
|
samplerProxy.getBody().setKvs(keyValues);
|
||||||
}
|
}
|
||||||
samplerProxy.setProtocol(RequestType.HTTP);
|
samplerProxy.setProtocol(RequestType.HTTP);
|
||||||
samplerProxy.setPort(source.getPort() + "");
|
samplerProxy.setPort(source.getPropertyAsString("HTTPSampler.port"));
|
||||||
|
samplerProxy.setDomain(source.getDomain());
|
||||||
if (source.getArguments() != null) {
|
if (source.getArguments() != null) {
|
||||||
if (source.getPostBodyRaw()) {
|
if (source.getPostBodyRaw()) {
|
||||||
samplerProxy.getBody().setType(Body.RAW);
|
samplerProxy.getBody().setType(Body.RAW);
|
||||||
|
@ -178,10 +235,10 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
|
||||||
}
|
}
|
||||||
samplerProxy.getBody().initBinary();
|
samplerProxy.getBody().initBinary();
|
||||||
}
|
}
|
||||||
samplerProxy.setPath("");
|
// samplerProxy.setPath(source.getPath());
|
||||||
samplerProxy.setMethod(source.getMethod());
|
samplerProxy.setMethod(source.getMethod());
|
||||||
if (source.getUrl() != null) {
|
if (this.getUrl(source) != null) {
|
||||||
samplerProxy.setUrl(source.getUrl().toString());
|
samplerProxy.setUrl(this.getUrl(source));
|
||||||
}
|
}
|
||||||
samplerProxy.setId(UUID.randomUUID().toString());
|
samplerProxy.setId(UUID.randomUUID().toString());
|
||||||
samplerProxy.setType("HTTPSamplerProxy");
|
samplerProxy.setType("HTTPSamplerProxy");
|
||||||
|
|
|
@ -91,6 +91,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
||||||
@JSONField(ordinal = 36)
|
@JSONField(ordinal = 36)
|
||||||
private MsAuthManager authManager;
|
private MsAuthManager authManager;
|
||||||
|
|
||||||
|
@JSONField(ordinal = 37)
|
||||||
|
private boolean urlOrPath;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
|
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
|
||||||
// 非导出操作,且不是启用状态则跳过执行
|
// 非导出操作,且不是启用状态则跳过执行
|
||||||
|
@ -140,23 +143,26 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
||||||
url = this.getUrl();
|
url = this.getUrl();
|
||||||
isUrl = true;
|
isUrl = true;
|
||||||
}
|
}
|
||||||
URL urlObject = new URL(url);
|
|
||||||
if (isUrl) {
|
if (isUrl) {
|
||||||
|
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"));
|
sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8"));
|
||||||
if (urlObject.getPort() > 0) {
|
if (urlObject.getPort() > 0 && urlObject.getPort() != 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
|
||||||
sampler.setPort(urlObject.getPort());
|
sampler.setPort(urlObject.getPort());
|
||||||
|
} else {
|
||||||
|
sampler.setProperty("HTTPSampler.port", this.getPort());
|
||||||
}
|
}
|
||||||
sampler.setProtocol(urlObject.getProtocol());
|
sampler.setProtocol(urlObject.getProtocol());
|
||||||
|
sampler.setPath(urlObject.getPath());
|
||||||
} else {
|
} else {
|
||||||
sampler.setDomain(config.getConfig().get(this.getProjectId()).getHttpConfig().getDomain());
|
sampler.setDomain(config.getConfig().get(this.getProjectId()).getHttpConfig().getDomain());
|
||||||
sampler.setPort(config.getConfig().get(this.getProjectId()).getHttpConfig().getPort());
|
sampler.setPort(config.getConfig().get(this.getProjectId()).getHttpConfig().getPort());
|
||||||
sampler.setProtocol(config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol());
|
sampler.setProtocol(config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol());
|
||||||
|
sampler.setPath(this.getPath());
|
||||||
}
|
}
|
||||||
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
|
String envPath = sampler.getPath();
|
||||||
if (StringUtils.isNotBlank(this.getPath()) && !isUrl) {
|
|
||||||
envPath += this.getPath();
|
|
||||||
sampler.setPath(envPath);
|
|
||||||
}
|
|
||||||
if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) {
|
if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) {
|
||||||
envPath = getRestParameters(URLDecoder.decode(envPath, "UTF-8"));
|
envPath = getRestParameters(URLDecoder.decode(envPath, "UTF-8"));
|
||||||
sampler.setPath(envPath);
|
sampler.setPath(envPath);
|
||||||
|
@ -327,10 +333,16 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isURL(String str) {
|
public boolean isURL(String str) {
|
||||||
//转换为小写
|
|
||||||
try {
|
try {
|
||||||
new URL(str);
|
String regex = "^((https|http|ftp|rtsp|mms)?://)"
|
||||||
return true;
|
+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"
|
||||||
|
+ "(([0-9]{1,3}\\.){3}[0-9]{1,3}" + "|" + "([0-9a-z_!~*'()-]+\\.)*"
|
||||||
|
+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\."
|
||||||
|
+ "[a-z]{2,6})"
|
||||||
|
+ "(:[0-9]{1,5})?"
|
||||||
|
+ "((/?)|"
|
||||||
|
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
|
||||||
|
return str.matches(regex) || (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$"));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -339,5 +351,5 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -926,7 +926,7 @@
|
||||||
return bodyUploadFiles;
|
return bodyUploadFiles;
|
||||||
},
|
},
|
||||||
editScenario() {
|
editScenario() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
document.getElementById("inputDelay").focus(); // 保存前在input框自动失焦,以免保存失败
|
document.getElementById("inputDelay").focus(); // 保存前在input框自动失焦,以免保存失败
|
||||||
this.$refs['currentScenario'].validate((valid) => {
|
this.$refs['currentScenario'].validate((valid) => {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
|
|
Loading…
Reference in New Issue