feat(接口测试): 导入 curl 支持请求体

This commit is contained in:
baiqi 2024-09-04 15:22:17 +08:00 committed by Craftsman
parent 2586b73b9a
commit d74d6bf190
4 changed files with 56 additions and 5 deletions

View File

@ -468,6 +468,7 @@ export interface CurlParseResult {
method: RequestMethods | string; method: RequestMethods | string;
url: string; url: string;
headers: Record<string, any>; headers: Record<string, any>;
body: string; body: Record<string, any>;
bodyType: RequestBodyFormat;
queryParams: Record<string, any>; queryParams: Record<string, any>;
} }

View File

@ -7,9 +7,10 @@ import {
type ResponseDefinition, type ResponseDefinition,
} from '@/models/apiTest/common'; } from '@/models/apiTest/common';
import type { MockBody } from '@/models/apiTest/mock'; import type { MockBody } from '@/models/apiTest/mock';
import { RequestConditionProcessor, RequestParamsType } from '@/enums/apiEnum'; import { RequestBodyFormat, RequestConditionProcessor, RequestParamsType } from '@/enums/apiEnum';
import { import {
defaultBodyParams,
defaultBodyParamsItem, defaultBodyParamsItem,
defaultHeaderParamsItem, defaultHeaderParamsItem,
defaultKeyValueParamItem, defaultKeyValueParamItem,
@ -273,3 +274,47 @@ export function filterAssertions(assertionConfig: ExecuteAssertionConfig, isExec
}; };
}); });
} }
/**
* curl body
* @param bodyType body
* @param body body
*/
export function parseCurlBody(bodyType: RequestBodyFormat, body: Record<string, any>) {
const requestBody = cloneDeep(defaultBodyParams);
switch (bodyType) {
case RequestBodyFormat.JSON:
requestBody.bodyType = bodyType;
requestBody.jsonBody = {
...cloneDeep(defaultBodyParams.jsonBody),
enableJsonSchema: false,
jsonValue: JSON.stringify(body),
};
break;
case RequestBodyFormat.FORM_DATA:
requestBody.bodyType = bodyType;
requestBody.formDataBody = {
...cloneDeep(defaultBodyParams.formDataBody),
formValues: Object.keys(body).map((e) => ({
...defaultBodyParamsItem,
key: e,
value: body[e],
})),
};
break;
case RequestBodyFormat.WWW_FORM:
requestBody.bodyType = bodyType;
requestBody.wwwFormBody = {
...cloneDeep(defaultBodyParams.wwwFormBody),
formValues: Object.keys(body).map((e) => ({
...defaultBodyParamsItem,
key: e,
value: body[e],
})),
};
break;
default:
break;
}
return requestBody;
}

View File

@ -110,7 +110,7 @@
} from '@/enums/apiEnum'; } from '@/enums/apiEnum';
import { defaultBodyParams, defaultResponse } from '../components/config'; import { defaultBodyParams, defaultResponse } from '../components/config';
import { parseRequestBodyFiles } from '../components/utils'; import { parseCurlBody, parseRequestBodyFiles } from '../components/utils';
const route = useRoute(); const route = useRoute();
const { t } = useI18n(); const { t } = useI18n();
@ -250,7 +250,8 @@
} }
function handleImportCurlDone(res: CurlParseResult) { function handleImportCurlDone(res: CurlParseResult) {
const { url, method, headers, queryParams } = res; const { url, method, headers, queryParams, bodyType, body } = res;
const requestBody = parseCurlBody(bodyType, body);
addDebugTab({ addDebugTab({
url, url,
method: method?.toUpperCase() || RequestMethods.GET, method: method?.toUpperCase() || RequestMethods.GET,
@ -274,6 +275,7 @@
key: e, key: e,
value: queryParams[e], value: queryParams[e],
})) || [], })) || [],
body: requestBody,
}); });
importDialogVisible.value = false; importDialogVisible.value = false;
nextTick(() => { nextTick(() => {

View File

@ -418,6 +418,7 @@
filterAssertions, filterAssertions,
filterConditionsSqlValidParams, filterConditionsSqlValidParams,
filterKeyValParams, filterKeyValParams,
parseCurlBody,
parseRequestBodyFiles, parseRequestBodyFiles,
} from '@/views/api-test/components/utils'; } from '@/views/api-test/components/utils';
import type { Api } from '@form-create/arco-design'; import type { Api } from '@form-create/arco-design';
@ -1282,7 +1283,8 @@
const importDialogVisible = ref(false); const importDialogVisible = ref(false);
function handleImportCurlDone(res: CurlParseResult) { function handleImportCurlDone(res: CurlParseResult) {
const { url, method, headers, queryParams } = res; const { url, method, headers, queryParams, bodyType, body } = res;
const requestBody = parseCurlBody(bodyType, body);
requestVModel.value.url = url; requestVModel.value.url = url;
requestVModel.value.method = method; requestVModel.value.method = method;
requestVModel.value.headers = Object.keys(headers).map((e) => ({ requestVModel.value.headers = Object.keys(headers).map((e) => ({
@ -1295,6 +1297,7 @@
key: e, key: e,
value: queryParams[e], value: queryParams[e],
})); }));
requestVModel.value.body = requestBody;
importDialogVisible.value = false; importDialogVisible.value = false;
nextTick(() => { nextTick(() => {
handleActiveDebugChange(); handleActiveDebugChange();