fix(接口测试): 导入jmx文件query参数处理

--bug=1008540 --user=赵勇 【github#8283】jmeter脚本上传到ms之后url拼接丢失 https://www.tapd.cn/55049933/s/1077896
This commit is contained in:
fit2-zhao 2021-12-06 11:40:31 +08:00 committed by fit2-zhao
parent 7f18cf17cd
commit cb6f8f716c
1 changed files with 44 additions and 17 deletions

View File

@ -175,7 +175,6 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
if (!path.startsWith("http://") && !path.startsWith("https://")) { if (!path.startsWith("http://") && !path.startsWith("https://")) {
String domain = source.getDomain(); String domain = source.getDomain();
String protocol = source.getProtocol(); String protocol = source.getProtocol();
String method = source.getMethod();
StringBuilder pathAndQuery = new StringBuilder(100); StringBuilder pathAndQuery = new StringBuilder(100);
if ("file".equalsIgnoreCase(protocol)) { if ("file".equalsIgnoreCase(protocol)) {
domain = null; domain = null;
@ -184,18 +183,6 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
} }
pathAndQuery.append(path); 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"); String portAsString = source.getPropertyAsString("HTTPSampler.port");
return this.isProtocolDefaultPort(source) ? new URL(protocol, domain, pathAndQuery.toString()).toExternalForm() : this.url(protocol, domain, portAsString, pathAndQuery.toString()); return this.isProtocolDefaultPort(source) ? new URL(protocol, domain, pathAndQuery.toString()).toExternalForm() : this.url(protocol, domain, portAsString, pathAndQuery.toString());
} else { } else {
@ -213,6 +200,44 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
return null; return null;
} }
private static String truncateUrlPage(String strURL) {
String strAllParam = null;
if (StringUtils.isNotEmpty(strURL)) {
String[] arrSplit = strURL.split("[?]");
if (strURL.length() > 1) {
if (arrSplit.length > 1) {
if (arrSplit[1] != null) {
strAllParam = arrSplit[1];
}
}
}
}
return strAllParam;
}
public static Map<String, String> parseURLParam(String URL) {
Map<String, String> mapRequest = new LinkedHashMap<>();
String strUrlParam = truncateUrlPage(URL);
if (StringUtils.isEmpty(strUrlParam)) {
return mapRequest;
}
//每个键值为一组
String[] arrSplit = strUrlParam.split("[&]");
for (String strSplit : arrSplit) {
String[] arrSplitEqual = strSplit.split("[=]");
//解析出键值
if (arrSplitEqual.length > 1) {
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
} else {
if (StringUtils.isNotEmpty(arrSplitEqual[0])) {
//只有参数没有值不加入
mapRequest.put(arrSplitEqual[0], "");
}
}
}
return mapRequest;
}
private void convertHttpSampler(MsHTTPSamplerProxy samplerProxy, Object key) { private void convertHttpSampler(MsHTTPSamplerProxy samplerProxy, Object key) {
try { try {
HTTPSamplerProxy source = (HTTPSamplerProxy) key; HTTPSamplerProxy source = (HTTPSamplerProxy) key;
@ -303,9 +328,9 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
} }
samplerProxy.getBody().initBinary(); samplerProxy.getBody().initBinary();
} }
// samplerProxy.setPath(source.getPath());
samplerProxy.setMethod(source.getMethod()); samplerProxy.setMethod(source.getMethod());
if (this.getUrl(source) != null) { URL url = source.getUrl();
if (url != null) {
samplerProxy.setUrl(this.getUrl(source)); samplerProxy.setUrl(this.getUrl(source));
samplerProxy.setPath(null); samplerProxy.setPath(null);
} }
@ -530,9 +555,11 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
RegexExtractor regexExtractor = (RegexExtractor) key; RegexExtractor regexExtractor = (RegexExtractor) key;
if (regexExtractor.useRequestHeaders()) { if (regexExtractor.useRequestHeaders()) {
regex.setUseHeaders("request_headers"); regex.setUseHeaders("request_headers");
}if (regexExtractor.useHeaders()) { }
if (regexExtractor.useHeaders()) {
regex.setUseHeaders("true"); regex.setUseHeaders("true");
} if (regexExtractor.useBody()) { }
if (regexExtractor.useBody()) {
regex.setUseHeaders("false"); regex.setUseHeaders("false");
} else if (regexExtractor.useUnescapedBody()) { } else if (regexExtractor.useUnescapedBody()) {
regex.setUseHeaders("unescaped"); regex.setUseHeaders("unescaped");