test: 代理 POST 方法与请求体转发断言

This commit is contained in:
lovefyf
2026-08-13 05:12:10 +08:00
parent af1c1a8eb4
commit 0159c3a7eb
+17 -2
View File
@@ -13,7 +13,7 @@ vi.hoisted(() => {
});
let downstream: ReturnType<typeof serve>;
let captured: { auth?: string; fake?: string; path?: string } = {};
let captured: { auth?: string; fake?: string; path?: string; method?: string; body?: string } = {};
describe('gateway proxy', () => {
let token = '';
@@ -32,11 +32,13 @@ describe('gateway proxy', () => {
},
});
});
echo.all('*', (c) => {
echo.all('*', async (c) => {
captured = {
auth: c.req.header('authorization') ?? '',
fake: c.req.header('x-user-id') ?? '',
path: c.req.path,
method: c.req.method,
body: await c.req.text(),
};
return c.json({ from: 'downstream' });
});
@@ -66,6 +68,19 @@ describe('gateway proxy', () => {
expect(captured.fake).toBe('');
});
it('POST 转发:方法与请求体原样到下游(F1)', async () => {
const res = await buildApp().request('/api/task/task/t-1/claim', {
method: 'POST',
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
body: JSON.stringify({ note: 'hello' }),
});
expect(res.status).toBe(200);
expect(captured.method).toBe('POST');
expect(captured.path).toBe('/task/t-1/claim');
expect(JSON.parse(captured.body ?? '')).toEqual({ note: 'hello' });
expect(await res.json()).toEqual({ from: 'downstream' });
});
it('下游不可达 → 502 统一错误形状,点名是哪个下游(C3)', async () => {
const res = await buildApp().request('/api/data/ledger/domains', {
headers: { authorization: `Bearer ${token}` },