refactor: postman导入变量格式转换

This commit is contained in:
chenjianxing 2021-07-21 15:44:47 +08:00 committed by jianxing
parent 3176446947
commit cb61770480
2 changed files with 35 additions and 7 deletions

View File

@ -131,7 +131,8 @@ public abstract class ApiImportAbstractParser<T> implements ApiImportParser<T> {
private String formatPath(String url) {
try {
URL urlObject = new URL(url);
StringBuffer pathBuffer = new StringBuffer(urlObject.getPath());
String path = StringUtils.isBlank(urlObject.getPath()) ? "/" : urlObject.getPath();
StringBuffer pathBuffer = new StringBuffer(path);
if (StringUtils.isNotEmpty(urlObject.getQuery())) {
pathBuffer.append("?").append(urlObject.getQuery());
}

View File

@ -10,12 +10,15 @@ import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.MsRequestBodyType;
import io.metersphere.commons.constants.PostmanRequestBodyMode;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractParser<T> {
@ -30,8 +33,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url.getRaw(), requestDesc.getMethod());
if (StringUtils.isNotBlank(request.getPath())) {
String path = request.getPath().split("\\?")[0];
path = path.replace("{{", "${");
path = path.replace("}}", "}");
path = parseVariable(path);
request.setPath(path);
} else {
request.setPath("/");
@ -45,6 +47,26 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
}
/**
* 将postman的变量转换成ms变量
* {{xxx}} -> ${xxx}
* @param value
* @return
*/
public String parseVariable(String value) {
try {
Pattern pattern = Pattern.compile("(\\{\\{(.*?)\\}\\})");
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
value = matcher.replaceFirst("\\$\\{" + matcher.group(2) + "\\}");
matcher = pattern.matcher(value);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
return value;
}
private void addPreScript(MsHTTPSamplerProxy request, List<PostmanEvent> event) {
if (request != null && CollectionUtils.isNotEmpty(event)) {
StringBuilder scriptStr = new StringBuilder();
@ -68,7 +90,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
MsJSR223PreProcessor jsr223PreProcessor = new MsJSR223PreProcessor();
jsr223PreProcessor.setName("JSR223PreProcessor");
jsr223PreProcessor.setScriptLanguage("nashornScript");
jsr223PreProcessor.setScript(scriptStr.toString());
jsr223PreProcessor.setScript(parseVariable(scriptStr.toString()));
LinkedList<MsTestElement> hashTree = new LinkedList<>();
hashTree.add(jsr223PreProcessor);
request.setHashTree(hashTree);
@ -81,7 +103,12 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
return null;
}
List<KeyValue> keyValues = new ArrayList<>();
postmanKeyValues.forEach(item -> keyValues.add(new KeyValue(item.getKey(), item.getValue(), item.getDescription(), item.getContentType())));
postmanKeyValues.forEach(item -> {
String k = parseVariable(item.getKey());
String v = parseVariable(item.getValue());
String desc = parseVariable(item.getDescription());
keyValues.add(new KeyValue(k, v, desc, item.getContentType()));
});
return keyValues;
}
@ -97,7 +124,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
parseRawBody(body, postmanBody, bodyMode);
} else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) {
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(parseVariable(postmanBody.getString(bodyMode)), PostmanKeyValue.class);
body.setKvs(parseKeyValue(postmanKeyValues));
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) {
body.setType(Body.FORM_DATA);
@ -111,7 +138,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
}
private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) {
body.setRaw(postmanBody.getString(bodyMode));
body.setRaw(parseVariable(postmanBody.getString(bodyMode)));
body.setType(MsRequestBodyType.RAW.value());
JSONObject options = postmanBody.getJSONObject("options");
if (options != null) {