37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import Fastify from 'fastify';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { registerJsonBodyParser } from '../../utils/json-parser.js';
|
|
|
|
describe('registerJsonBodyParser', () => {
|
|
it('treats an empty application/json body as an empty object', async () => {
|
|
const app = Fastify();
|
|
registerJsonBodyParser(app);
|
|
app.post('/empty-json', async (request) => ({ body: request.body }));
|
|
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/empty-json',
|
|
headers: { 'content-type': 'application/json' },
|
|
payload: '',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.json()).toEqual({ body: {} });
|
|
});
|
|
|
|
it('still rejects malformed JSON', async () => {
|
|
const app = Fastify();
|
|
registerJsonBodyParser(app);
|
|
app.post('/bad-json', async () => ({ ok: true }));
|
|
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/bad-json',
|
|
headers: { 'content-type': 'application/json' },
|
|
payload: '{',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
});
|
|
});
|