fix(接口测试): 一键执行文件上传接口报错
This commit is contained in:
parent
4b43a2cd8b
commit
782f32ce8e
|
@ -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<String> 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<MultipartFile> bodyFiles) {
|
||||
checkownerService.checkApiTestOwner(request.getId());
|
||||
|
|
|
@ -83,15 +83,19 @@ public class APITestService {
|
|||
}
|
||||
|
||||
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) {
|
||||
throw new IllegalArgumentException(Translator.get("file_cannot_be_null"));
|
||||
}
|
||||
checkQuota();
|
||||
List<String> 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<MultipartFile> bodyFiles) {
|
||||
|
@ -108,6 +112,9 @@ public class APITestService {
|
|||
}
|
||||
|
||||
private void createBodyFiles(ApiTest test, List<String> bodyUploadIds, List<MultipartFile> 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<String> selectIds) {
|
||||
ApiTest test = createTest(request, file);
|
||||
selectIds.forEach(sourceId -> {
|
||||
copyBodyFiles(test.getId(), sourceId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}));
|
||||
|
||||
|
|
Loading…
Reference in New Issue