From 5318537e12c69dde90c92f40e29165401a573ce7 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Wed, 8 Jul 2020 17:56:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20postman=20=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/APITestController.java | 6 +- .../dto/parse/postman/PostmanCollection.java | 13 + .../parse/postman/PostmanCollectionInfo.java | 13 + .../api/dto/parse/postman/PostmanItem.java | 13 + .../dto/parse/postman/PostmanKeyValue.java | 18 ++ .../api/dto/parse/postman/PostmanRequest.java | 18 ++ .../api/dto/parse/postman/PostmanUrl.java | 14 + .../api/dto/scenario/KeyValue.java | 8 + .../api/parse/ApiImportAbstractParser.java | 37 +++ .../api/parse/ApiImportParserFactory.java | 2 + .../io/metersphere/api/parse/MsParser.java | 24 +- .../metersphere/api/parse/PostmanParser.java | 262 ++++++++++++++++++ .../api/service/APITestService.java | 24 +- .../commons/constants/MsRequestBodyType.java | 17 ++ .../constants/PostmanRequestBodyMode.java | 16 ++ .../components/api/test/ApiTestConfig.vue | 5 +- .../api/test/components/import/ApiImport.vue | 32 ++- frontend/src/i18n/en-US.js | 13 + frontend/src/i18n/zh-CN.js | 13 + frontend/src/i18n/zh-TW.js | 13 + 20 files changed, 509 insertions(+), 52 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollection.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollectionInfo.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanItem.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanKeyValue.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanRequest.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanUrl.java create mode 100644 backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java create mode 100644 backend/src/main/java/io/metersphere/api/parse/PostmanParser.java create mode 100644 backend/src/main/java/io/metersphere/commons/constants/MsRequestBodyType.java create mode 100644 backend/src/main/java/io/metersphere/commons/constants/PostmanRequestBodyMode.java diff --git a/backend/src/main/java/io/metersphere/api/controller/APITestController.java b/backend/src/main/java/io/metersphere/api/controller/APITestController.java index ced4f7f2df..035d922ec5 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -96,10 +96,10 @@ public class APITestController { return apiTestService.run(request); } - @PostMapping("/import/{platform}/{projectId}") + @PostMapping("/import/{platform}") @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) - public ApiTest testCaseImport(MultipartFile file, @PathVariable String platform, @PathVariable String projectId) { - return apiTestService.apiTestImport(file, platform, projectId); + public ApiTest testCaseImport(MultipartFile file, @PathVariable String platform) { + return apiTestService.apiTestImport(file, platform); } } diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollection.java b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollection.java new file mode 100644 index 0000000000..636feab232 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollection.java @@ -0,0 +1,13 @@ +package io.metersphere.api.dto.parse.postman; + +import lombok.Data; + +import java.util.List; + +@Data +public class PostmanCollection { + + private PostmanCollectionInfo info; + + private List item; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollectionInfo.java b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollectionInfo.java new file mode 100644 index 0000000000..d5307a7cda --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanCollectionInfo.java @@ -0,0 +1,13 @@ +package io.metersphere.api.dto.parse.postman; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +@Data +public class PostmanCollectionInfo { + + @JSONField(name = "_postman_id") + private String postmanId; + private String name; + private String schema; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanItem.java b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanItem.java new file mode 100644 index 0000000000..1d780e7d23 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanItem.java @@ -0,0 +1,13 @@ +package io.metersphere.api.dto.parse.postman; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +import java.util.List; + +@Data +public class PostmanItem { + + private String name; + private PostmanRequest request; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanKeyValue.java b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanKeyValue.java new file mode 100644 index 0000000000..c68e4fc114 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanKeyValue.java @@ -0,0 +1,18 @@ +package io.metersphere.api.dto.parse.postman; + +import lombok.Data; + +@Data +public class PostmanKeyValue { + private String key; + private String value; + private String type; + + public PostmanKeyValue() { + } + + public PostmanKeyValue(String key, String value) { + this.key = key; + this.value = value; + } +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanRequest.java b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanRequest.java new file mode 100644 index 0000000000..59c0e97fa7 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanRequest.java @@ -0,0 +1,18 @@ +package io.metersphere.api.dto.parse.postman; + +import com.alibaba.fastjson.JSONObject; +import lombok.Data; + +import java.util.List; + +@Data +public class PostmanRequest { + + private String method; + private String schema; + private List header; + private JSONObject body; + private JSONObject auth; + private PostmanUrl url; + private String description; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanUrl.java b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanUrl.java new file mode 100644 index 0000000000..55481b4b12 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/parse/postman/PostmanUrl.java @@ -0,0 +1,14 @@ +package io.metersphere.api.dto.parse.postman; + +import lombok.Data; + +import java.util.List; + +@Data +public class PostmanUrl { + + private String raw; + private String protocol; + private String port; + private List query; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java b/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java index 26d1450608..67c7311b5d 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/KeyValue.java @@ -6,4 +6,12 @@ import lombok.Data; public class KeyValue { private String name; private String value; + + public KeyValue() { + } + + public KeyValue(String name, String value) { + this.name = name; + this.value = value; + } } diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java new file mode 100644 index 0000000000..0e3ef0e4c1 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java @@ -0,0 +1,37 @@ +package io.metersphere.api.parse; + +import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.LogUtil; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public abstract class ApiImportAbstractParser implements ApiImportParser { + + protected String getApiTestStr(InputStream source) { + BufferedReader bufferedReader = null; + StringBuilder testStr = null; + try { + bufferedReader = new BufferedReader(new InputStreamReader(source, "UTF-8")); + testStr = new StringBuilder(); + String inputStr = null; + while ((inputStr = bufferedReader.readLine()) != null) { + testStr.append(inputStr); + } + } catch (Exception e) { + MSException.throwException(e.getMessage()); + LogUtil.error(e.getMessage(), e); + } finally { + try { + source.close(); + } catch (IOException e) { + MSException.throwException(e.getMessage()); + LogUtil.error(e.getMessage(), e); + } + } + return testStr.toString(); + } + +} diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportParserFactory.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportParserFactory.java index a6b425df5f..d2a25ba819 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportParserFactory.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportParserFactory.java @@ -10,6 +10,8 @@ public class ApiImportParserFactory { public static ApiImportParser getApiImportParser(String platform) { if (StringUtils.equals(ApiImportPlatform.Metersphere.name(), platform)) { return new MsParser(); + } else if (StringUtils.equals(ApiImportPlatform.Postman.name(), platform)) { + return new PostmanParser(); } return null; } diff --git a/backend/src/main/java/io/metersphere/api/parse/MsParser.java b/backend/src/main/java/io/metersphere/api/parse/MsParser.java index 8eec7b8367..26af2afc63 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -7,29 +7,11 @@ import io.metersphere.commons.utils.LogUtil; import java.io.*; -public class MsParser implements ApiImportParser { +public class MsParser extends ApiImportAbstractParser { @Override - public ApiImport parse(InputStream source) {BufferedReader bufferedReader = null; - StringBuilder testStr = null; - try { - bufferedReader = new BufferedReader(new InputStreamReader(source, "UTF-8")); - testStr = new StringBuilder(); - String inputStr = null; - while ((inputStr = bufferedReader.readLine()) != null) { - testStr.append(inputStr); - } - } catch (Exception e) { - MSException.throwException(e.getMessage()); - LogUtil.error(e.getMessage(), e); - } finally { - try { - source.close(); - } catch (IOException e) { - MSException.throwException(e.getMessage()); - LogUtil.error(e.getMessage(), e); - } - } + public ApiImport parse(InputStream source) { + String testStr = getApiTestStr(source); return JSON.parseObject(testStr.toString(), ApiImport.class); } diff --git a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java new file mode 100644 index 0000000000..8b676fa1c6 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java @@ -0,0 +1,262 @@ +package io.metersphere.api.parse; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import io.metersphere.api.dto.parse.ApiImport; +import io.metersphere.api.dto.parse.postman.*; +import io.metersphere.api.dto.scenario.Body; +import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.api.dto.scenario.Request; +import io.metersphere.api.dto.scenario.Scenario; +import io.metersphere.commons.constants.MsRequestBodyType; +import io.metersphere.commons.constants.PostmanRequestBodyMode; +import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.LogUtil; +import org.apache.commons.lang3.StringUtils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +public class PostmanParser extends ApiImportAbstractParser { + + @Override + public ApiImport parse(InputStream source) { + String testStr = getApiTestStr(source); + +// String testStr = "{\n" + +// "\t\"info\": {\n" + +// "\t\t\"_postman_id\": \"9721cd51-8626-4f61-9ac1-e77b8399cca8\",\n" + +// "\t\t\"name\": \"test\",\n" + +// "\t\t\"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n" + +// "\t},\n" + +// "\t\"item\": [\n" + +// "\t\t{\n" + +// "\t\t\t\"name\": \"test\",\n" + +// "\t\t\t\"request\": {\n" + +// "\t\t\t\t\"auth\": {\n" + +// "\t\t\t\t\t\"type\": \"basic\",\n" + +// "\t\t\t\t\t\"basic\": [\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"password\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + +// "\t\t\t\t\t\t},\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"username\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t]\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"method\": \"POST\",\n" + +// "\t\t\t\t\"header\": [\n" + +// "\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\"key\": \"aaa\",\n" + +// "\t\t\t\t\t\t\"value\": \"testH\",\n" + +// "\t\t\t\t\t\t\"type\": \"text\"\n" + +// "\t\t\t\t\t}\n" + +// "\t\t\t\t],\n" + +// "\t\t\t\t\"body\": {\n" + +// "\t\t\t\t\t\"mode\": \"raw\",\n" + +// "\t\t\t\t\t\"raw\": \"\\n\\n\",\n" + +// "\t\t\t\t\t\"options\": {\n" + +// "\t\t\t\t\t\t\"raw\": {\n" + +// "\t\t\t\t\t\t\t\"language\": \"html\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t}\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"url\": {\n" + +// "\t\t\t\t\t\"raw\": \"https://localhost:8080?tset=test\",\n" + +// "\t\t\t\t\t\"protocol\": \"https\",\n" + +// "\t\t\t\t\t\"host\": [\n" + +// "\t\t\t\t\t\t\"localhost\"\n" + +// "\t\t\t\t\t],\n" + +// "\t\t\t\t\t\"port\": \"8080\",\n" + +// "\t\t\t\t\t\"query\": [\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"tset\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"description\": \"test\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t]\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"description\": \"dd\"\n" + +// "\t\t\t},\n" + +// "\t\t\t\"response\": []\n" + +// "\t\t},\n" + +// "\t\t{\n" + +// "\t\t\t\"name\": \"test Copy\",\n" + +// "\t\t\t\"request\": {\n" + +// "\t\t\t\t\"auth\": {\n" + +// "\t\t\t\t\t\"type\": \"basic\",\n" + +// "\t\t\t\t\t\"basic\": [\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"password\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + +// "\t\t\t\t\t\t},\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"username\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t]\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"method\": \"POST\",\n" + +// "\t\t\t\t\"header\": [\n" + +// "\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\"key\": \"testH\",\n" + +// "\t\t\t\t\t\t\"value\": \"testH\",\n" + +// "\t\t\t\t\t\t\"type\": \"text\"\n" + +// "\t\t\t\t\t}\n" + +// "\t\t\t\t],\n" + +// "\t\t\t\t\"body\": {\n" + +// "\t\t\t\t\t\"mode\": \"raw\",\n" + +// "\t\t\t\t\t\"raw\": \"{\\\"name\\\":\\\"test\\\"}\\n\",\n" + +// "\t\t\t\t\t\"options\": {\n" + +// "\t\t\t\t\t\t\"raw\": {\n" + +// "\t\t\t\t\t\t\t\"language\": \"json\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t}\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"url\": {\n" + +// "\t\t\t\t\t\"raw\": \"http://localhost:8081\",\n" + +// "\t\t\t\t\t\"protocol\": \"http\",\n" + +// "\t\t\t\t\t\"host\": [\n" + +// "\t\t\t\t\t\t\"localhost\"\n" + +// "\t\t\t\t\t],\n" + +// "\t\t\t\t\t\"port\": \"8081\"\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"description\": \"dd\"\n" + +// "\t\t\t},\n" + +// "\t\t\t\"response\": []\n" + +// "\t\t},\n" + +// "\t\t{\n" + +// "\t\t\t\"name\": \"test Copy Copy\",\n" + +// "\t\t\t\"request\": {\n" + +// "\t\t\t\t\"auth\": {\n" + +// "\t\t\t\t\t\"type\": \"basic\",\n" + +// "\t\t\t\t\t\"basic\": [\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"password\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + +// "\t\t\t\t\t\t},\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"username\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"string\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t]\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"method\": \"POST\",\n" + +// "\t\t\t\t\"header\": [\n" + +// "\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\"key\": \"testH\",\n" + +// "\t\t\t\t\t\t\"value\": \"testH\",\n" + +// "\t\t\t\t\t\t\"type\": \"text\"\n" + +// "\t\t\t\t\t}\n" + +// "\t\t\t\t],\n" + +// "\t\t\t\t\"body\": {\n" + +// "\t\t\t\t\t\"mode\": \"urlencoded\",\n" + +// "\t\t\t\t\t\"urlencoded\": [\n" + +// "\t\t\t\t\t\t{\n" + +// "\t\t\t\t\t\t\t\"key\": \"test\",\n" + +// "\t\t\t\t\t\t\t\"value\": \"tset\",\n" + +// "\t\t\t\t\t\t\t\"description\": \"dd\",\n" + +// "\t\t\t\t\t\t\t\"type\": \"text\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t],\n" + +// "\t\t\t\t\t\"options\": {\n" + +// "\t\t\t\t\t\t\"raw\": {\n" + +// "\t\t\t\t\t\t\t\"language\": \"json\"\n" + +// "\t\t\t\t\t\t}\n" + +// "\t\t\t\t\t}\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"url\": {\n" + +// "\t\t\t\t\t\"raw\": \"http://localhost:8081\",\n" + +// "\t\t\t\t\t\"protocol\": \"http\",\n" + +// "\t\t\t\t\t\"host\": [\n" + +// "\t\t\t\t\t\t\"localhost\"\n" + +// "\t\t\t\t\t],\n" + +// "\t\t\t\t\t\"port\": \"8081\"\n" + +// "\t\t\t\t},\n" + +// "\t\t\t\t\"description\": \"dd\"\n" + +// "\t\t\t},\n" + +// "\t\t\t\"response\": []\n" + +// "\t\t}\n" + +// "\t],\n" + +// "\t\"protocolProfileBehavior\": {}\n" + +// "}"; + + PostmanCollection postmanCollection = JSON.parseObject(testStr.toString(), PostmanCollection.class); + PostmanCollectionInfo info = postmanCollection.getInfo(); + List requests = parseRequests(postmanCollection); + ApiImport apiImport = new ApiImport(); + List scenarios = new ArrayList<>(); + Scenario scenario = new Scenario(); + scenario.setRequests(requests); + scenario.setName(info.getName()); + scenarios.add(scenario); + apiImport.setScenarios(scenarios); + return apiImport; + } + + private List parseKeyValue(List postmanKeyValues) { + if (postmanKeyValues == null) { + return null; + } + List keyValues = new ArrayList<>(); + postmanKeyValues.forEach(item -> { + keyValues.add(new KeyValue(item.getKey(), item.getValue())); + }); + return keyValues; + } + + private List parseRequests(PostmanCollection postmanCollection) { + List item = postmanCollection.getItem(); + List requests = new ArrayList<>(); + for (PostmanItem requestItem : item) { + Request request = new Request(); + PostmanRequest requestDesc = requestItem.getRequest(); + PostmanUrl url = requestDesc.getUrl(); + request.setName(requestItem.getName()); + request.setUrl(url.getRaw()); + request.setUseEnvironment(false); + request.setMethod(requestDesc.getMethod()); + request.setHeaders(parseKeyValue(requestDesc.getHeader())); + request.setParameters(parseKeyValue(url.getQuery())); + Body body = new Body(); + JSONObject postmanBody = requestDesc.getBody(); + String bodyMode = postmanBody.getString("mode"); + if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) { + body.setRaw(postmanBody.getString(bodyMode)); + body.setType(MsRequestBodyType.RAW.value()); + String contentType = postmanBody.getJSONObject("options").getJSONObject("raw").getString("language"); + List headers = request.getHeaders(); + boolean hasContentType = false; + for (KeyValue header : headers) { + if (StringUtils.equalsIgnoreCase(header.getName(), "Content-Type")) { + hasContentType = true; + } + } + if (!hasContentType) { + headers.add(new KeyValue("Content-Type", contentType)); + } + } else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) { + List postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class); + body.setType(MsRequestBodyType.KV.value()); + body.setKvs(parseKeyValue(postmanKeyValues)); + } + request.setBody(body); + requests.add(request); + } + return requests; + } + +} diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index 52cea01489..8739dd253d 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -19,6 +19,7 @@ import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.constants.ScheduleType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; @@ -260,24 +261,23 @@ public class APITestService { scheduleService.addOrUpdateCronJob(request, ApiTestJob.getJobKey(request.getResourceId()), ApiTestJob.getTriggerKey(request.getResourceId()), ApiTestJob.class); } - public ApiTest apiTestImport(MultipartFile file, String platform, String projectId) { + public ApiTest apiTestImport(MultipartFile file, String platform) { + ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(platform); + ApiImport apiImport = null; try { - - ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(platform); - ApiImport apiImport = apiImportParser.parse(file.getInputStream()); - SaveAPITestRequest request = getImportApiTest(file, projectId, apiImport); - ApiTest test = createTest(request); - return test; - } catch (IOException e) { - e.printStackTrace(); + apiImport = apiImportParser.parse(file.getInputStream()); + } catch (Exception e) { + LogUtil.error(e.getMessage(), e); + MSException.throwException(Translator.get("parse_data_error")); } - return null; + SaveAPITestRequest request = getImportApiTest(file, apiImport); + return createTest(request); } - private SaveAPITestRequest getImportApiTest(MultipartFile file, String projectId, ApiImport apiImport) { + private SaveAPITestRequest getImportApiTest(MultipartFile file, ApiImport apiImport) { SaveAPITestRequest request = new SaveAPITestRequest(); request.setName(file.getOriginalFilename()); - request.setProjectId(projectId); + request.setProjectId(""); request.setScenarioDefinition(apiImport.getScenarios()); request.setUserId(SessionUtils.getUser().getId()); request.setId(UUID.randomUUID().toString()); diff --git a/backend/src/main/java/io/metersphere/commons/constants/MsRequestBodyType.java b/backend/src/main/java/io/metersphere/commons/constants/MsRequestBodyType.java new file mode 100644 index 0000000000..85a08b821d --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/MsRequestBodyType.java @@ -0,0 +1,17 @@ +package io.metersphere.commons.constants; + +public enum MsRequestBodyType { + + + KV("KeyValue"), FORM_DATA("Form Data"), RAW("Raw"); + + private String value; + + MsRequestBodyType(String value) { + this.value = value; + } + + public String value() { + return this.value; + } +} diff --git a/backend/src/main/java/io/metersphere/commons/constants/PostmanRequestBodyMode.java b/backend/src/main/java/io/metersphere/commons/constants/PostmanRequestBodyMode.java new file mode 100644 index 0000000000..b0ce2714b1 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/PostmanRequestBodyMode.java @@ -0,0 +1,16 @@ +package io.metersphere.commons.constants; + +public enum PostmanRequestBodyMode { + + RAW("raw"), FORM_DATA("formdata"), URLENCODED("urlencoded"), FILE("file"); + + private String value; + + PostmanRequestBodyMode(String value) { + this.value = value; + } + + public String value() { + return this.value; + } +} diff --git a/frontend/src/business/components/api/test/ApiTestConfig.vue b/frontend/src/business/components/api/test/ApiTestConfig.vue index 1013f64f18..9b9e34d004 100644 --- a/frontend/src/business/components/api/test/ApiTestConfig.vue +++ b/frontend/src/business/components/api/test/ApiTestConfig.vue @@ -43,13 +43,12 @@ {{$t('api_test.export_config')}} - 导入 - + {{$t('api_test.api_import.label')}} - + diff --git a/frontend/src/business/components/api/test/components/import/ApiImport.vue b/frontend/src/business/components/api/test/components/import/ApiImport.vue index ab74065566..5aad534353 100644 --- a/frontend/src/business/components/api/test/components/import/ApiImport.vue +++ b/frontend/src/business/components/api/test/components/import/ApiImport.vue @@ -1,8 +1,8 @@