This commit is contained in:
2026-10-27 23:26:21 +08:00
parent 0699995c16
commit feaf90bb78
10 changed files with 476 additions and 2 deletions
+2 -1
View File
@@ -27,6 +27,7 @@ export function mockPlugin(globalMock = false): Plugin {
const requestUrl = new URL(req.url, 'http://localhost')
void (async () => {
const raw = req.method === 'GET' ? '' : await readBody(req)
const isJsonBody = (req.headers['content-type'] ?? '').includes('application/json')
const rule = mockRules.find(
(item) =>
item.method === (req.method ?? 'GET').toUpperCase() &&
@@ -41,7 +42,7 @@ export function mockPlugin(globalMock = false): Plugin {
url: requestUrl.pathname,
params: matched ? matched.slice(1) : [],
query: Object.fromEntries(requestUrl.searchParams.entries()),
body: raw ? JSON.parse(raw) : {},
body: raw && isJsonBody ? JSON.parse(raw) : {},
}
res.setHeader('Content-Type', 'application/json;charset=utf-8')