From 782f32ce8e0f8f1722b2c9acbc8a7f358b892f23 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 13 Oct 2020 17:44:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=B8=80=E9=94=AE=E6=89=A7=E8=A1=8C=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=8E=A5=E5=8F=A3=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/APITestController.java | 5 +++ .../api/service/APITestService.java | 18 +++++++- .../components/api/test/OneClickOperation.vue | 44 ++++--------------- 3 files changed, 29 insertions(+), 38 deletions(-) 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 3eb82dddba..8df2169b88 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -73,6 +73,11 @@ public class APITestController { apiTestService.create(request, file, bodyFiles); } + @PostMapping(value = "/create/merge", consumes = {"multipart/form-data"}) + public void mergeCreate(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "selectIds") List selectIds) { + apiTestService.mergeCreate(request, file, selectIds); + } + @PostMapping(value = "/update", consumes = {"multipart/form-data"}) public void update(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List bodyFiles) { checkownerService.checkApiTestOwner(request.getId()); 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 44dd35677b..cc9238c205 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -83,15 +83,19 @@ public class APITestService { } public void create(SaveAPITestRequest request, MultipartFile file, List bodyFiles) { + List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); + ApiTest test = createTest(request, file); + createBodyFiles(test, bodyUploadIds, bodyFiles); + } + private ApiTest createTest(SaveAPITestRequest request, MultipartFile file) { if (file == null) { throw new IllegalArgumentException(Translator.get("file_cannot_be_null")); } checkQuota(); - List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); request.setBodyUploadIds(null); ApiTest test = createTest(request); - createBodyFiles(test, bodyUploadIds, bodyFiles); saveFile(test.getId(), file); + return test; } public void update(SaveAPITestRequest request, MultipartFile file, List bodyFiles) { @@ -108,6 +112,9 @@ public class APITestService { } private void createBodyFiles(ApiTest test, List bodyUploadIds, List bodyFiles) { + if (bodyUploadIds.size() <= 0) { + return; + } String dir = BODY_FILE_DIR + "/" + test.getId(); File testDir = new File(dir); if (!testDir.exists()) { @@ -436,4 +443,11 @@ public class APITestService { quotaService.checkAPITestQuota(); } } + + public void mergeCreate(SaveAPITestRequest request, MultipartFile file, List selectIds) { + ApiTest test = createTest(request, file); + selectIds.forEach(sourceId -> { + copyBodyFiles(test.getId(), sourceId); + }); + } } diff --git a/frontend/src/business/components/api/test/OneClickOperation.vue b/frontend/src/business/components/api/test/OneClickOperation.vue index 579dea0d8c..d2f5ecf3f8 100644 --- a/frontend/src/business/components/api/test/OneClickOperation.vue +++ b/frontend/src/business/components/api/test/OneClickOperation.vue @@ -156,37 +156,11 @@ }, save(callback) { this.change = false; - let url = "/api/create"; - let bodyFiles = this.getBodyUploadFiles(); - this.result = this.$request(this.getOptions(url, bodyFiles), () => { + let url = "/api/create/merge"; + this.result = this.$request(this.getOptions(url, this.selectIds), () => { if (callback) callback(); }); }, - getBodyUploadFiles() { - let bodyUploadFiles = []; - this.test.bodyUploadIds = []; - this.test.scenarioDefinition.forEach(scenario => { - scenario.requests.forEach(request => { - if (request.body) { - request.body.kvs.forEach(param => { - if (param.files) { - param.files.forEach(item => { - if (item.file) { - let fileId = getUUID().substring(0, 8); - item.name = item.file.name; - item.id = fileId; - this.test.bodyUploadIds.push(fileId); - bodyUploadFiles.push(item.file); - // item.file = undefined; - } - }); - } - }); - } - }); - }); - return bodyUploadFiles; - }, runTest() { this.result = this.$post("/api/run", {id: this.test.id, triggerMode: 'MANUAL'}, (response) => { this.$success(this.$t('api_test.running')); @@ -196,16 +170,14 @@ this.test = "" }); }, - getOptions(url, bodyFiles) { + getOptions(url, selectIds) { let formData = new FormData(); - if (bodyFiles) { - bodyFiles.forEach(f => { - formData.append("files", f); - }) - } - let requestJson = JSON.stringify(this.test); - formData.append('request', new Blob([requestJson], { + formData.append('request', new Blob([JSON.stringify(this.test)], { + type: "application/json" + })); + + formData.append('selectIds', new Blob([JSON.stringify(Array.from(selectIds))], { type: "application/json" }));