diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java index 9f1ee10b21..f2b3fa6ce5 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java @@ -131,7 +131,8 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { 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()); } diff --git a/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java b/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java index 0b94178d65..b5ace2564d 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java @@ -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 extends ApiImportAbstractParser { @@ -30,8 +33,7 @@ public abstract class PostmanAbstractParserParser 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 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 event) { if (request != null && CollectionUtils.isNotEmpty(event)) { StringBuilder scriptStr = new StringBuilder(); @@ -68,7 +90,7 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa MsJSR223PreProcessor jsr223PreProcessor = new MsJSR223PreProcessor(); jsr223PreProcessor.setName("JSR223PreProcessor"); jsr223PreProcessor.setScriptLanguage("nashornScript"); - jsr223PreProcessor.setScript(scriptStr.toString()); + jsr223PreProcessor.setScript(parseVariable(scriptStr.toString())); LinkedList hashTree = new LinkedList<>(); hashTree.add(jsr223PreProcessor); request.setHashTree(hashTree); @@ -81,7 +103,12 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa return null; } List 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 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 postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class); + List 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 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) {