fix(接口测试): 一键执行文件上传接口报错
This commit is contained in:
parent
4b43a2cd8b
commit
782f32ce8e
|
@ -73,6 +73,11 @@ public class APITestController {
|
||||||
apiTestService.create(request, file, bodyFiles);
|
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<String> selectIds) {
|
||||||
|
apiTestService.mergeCreate(request, file, selectIds);
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/update", consumes = {"multipart/form-data"})
|
@PostMapping(value = "/update", consumes = {"multipart/form-data"})
|
||||||
public void update(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
public void update(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||||
checkownerService.checkApiTestOwner(request.getId());
|
checkownerService.checkApiTestOwner(request.getId());
|
||||||
|
|
|
@ -83,15 +83,19 @@ public class APITestService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create(SaveAPITestRequest request, MultipartFile file, List<MultipartFile> bodyFiles) {
|
public void create(SaveAPITestRequest request, MultipartFile file, List<MultipartFile> bodyFiles) {
|
||||||
|
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
|
||||||
|
ApiTest test = createTest(request, file);
|
||||||
|
createBodyFiles(test, bodyUploadIds, bodyFiles);
|
||||||
|
}
|
||||||
|
private ApiTest createTest(SaveAPITestRequest request, MultipartFile file) {
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
throw new IllegalArgumentException(Translator.get("file_cannot_be_null"));
|
throw new IllegalArgumentException(Translator.get("file_cannot_be_null"));
|
||||||
}
|
}
|
||||||
checkQuota();
|
checkQuota();
|
||||||
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
|
|
||||||
request.setBodyUploadIds(null);
|
request.setBodyUploadIds(null);
|
||||||
ApiTest test = createTest(request);
|
ApiTest test = createTest(request);
|
||||||
createBodyFiles(test, bodyUploadIds, bodyFiles);
|
|
||||||
saveFile(test.getId(), file);
|
saveFile(test.getId(), file);
|
||||||
|
return test;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(SaveAPITestRequest request, MultipartFile file, List<MultipartFile> bodyFiles) {
|
public void update(SaveAPITestRequest request, MultipartFile file, List<MultipartFile> bodyFiles) {
|
||||||
|
@ -108,6 +112,9 @@ public class APITestService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createBodyFiles(ApiTest test, List<String> bodyUploadIds, List<MultipartFile> bodyFiles) {
|
private void createBodyFiles(ApiTest test, List<String> bodyUploadIds, List<MultipartFile> bodyFiles) {
|
||||||
|
if (bodyUploadIds.size() <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String dir = BODY_FILE_DIR + "/" + test.getId();
|
String dir = BODY_FILE_DIR + "/" + test.getId();
|
||||||
File testDir = new File(dir);
|
File testDir = new File(dir);
|
||||||
if (!testDir.exists()) {
|
if (!testDir.exists()) {
|
||||||
|
@ -436,4 +443,11 @@ public class APITestService {
|
||||||
quotaService.checkAPITestQuota();
|
quotaService.checkAPITestQuota();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void mergeCreate(SaveAPITestRequest request, MultipartFile file, List<String> selectIds) {
|
||||||
|
ApiTest test = createTest(request, file);
|
||||||
|
selectIds.forEach(sourceId -> {
|
||||||
|
copyBodyFiles(test.getId(), sourceId);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,37 +156,11 @@
|
||||||
},
|
},
|
||||||
save(callback) {
|
save(callback) {
|
||||||
this.change = false;
|
this.change = false;
|
||||||
let url = "/api/create";
|
let url = "/api/create/merge";
|
||||||
let bodyFiles = this.getBodyUploadFiles();
|
this.result = this.$request(this.getOptions(url, this.selectIds), () => {
|
||||||
this.result = this.$request(this.getOptions(url, bodyFiles), () => {
|
|
||||||
if (callback) callback();
|
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() {
|
runTest() {
|
||||||
this.result = this.$post("/api/run", {id: this.test.id, triggerMode: 'MANUAL'}, (response) => {
|
this.result = this.$post("/api/run", {id: this.test.id, triggerMode: 'MANUAL'}, (response) => {
|
||||||
this.$success(this.$t('api_test.running'));
|
this.$success(this.$t('api_test.running'));
|
||||||
|
@ -196,16 +170,14 @@
|
||||||
this.test = ""
|
this.test = ""
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getOptions(url, bodyFiles) {
|
getOptions(url, selectIds) {
|
||||||
|
|
||||||
let formData = new FormData();
|
let formData = new FormData();
|
||||||
if (bodyFiles) {
|
formData.append('request', new Blob([JSON.stringify(this.test)], {
|
||||||
bodyFiles.forEach(f => {
|
type: "application/json"
|
||||||
formData.append("files", f);
|
}));
|
||||||
})
|
|
||||||
}
|
formData.append('selectIds', new Blob([JSON.stringify(Array.from(selectIds))], {
|
||||||
let requestJson = JSON.stringify(this.test);
|
|
||||||
formData.append('request', new Blob([requestJson], {
|
|
||||||
type: "application/json"
|
type: "application/json"
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue