Spaces:
Runtime error
Runtime error
File size: 1,441 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
'use strict'
const { test } = require('node:test')
const Fastify = require('..')
test('Buffer test', async t => {
const fastify = Fastify()
fastify.addContentTypeParser('application/json', { parseAs: 'buffer' }, fastify.getDefaultJsonParser('error', 'ignore'))
fastify.delete('/', async (request) => {
return request.body
})
await test('should return 200 if the body is not empty', async t => {
t.plan(3)
const response = await fastify.inject({
method: 'DELETE',
url: '/',
payload: Buffer.from('{"hello":"world"}'),
headers: {
'content-type': 'application/json'
}
})
t.assert.ifError(response.error)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')
})
await test('should return 400 if the body is empty', async t => {
t.plan(3)
const response = await fastify.inject({
method: 'DELETE',
url: '/',
payload: Buffer.alloc(0),
headers: {
'content-type': 'application/json'
}
})
t.assert.ifError(response.error)
t.assert.strictEqual(response.statusCode, 400)
t.assert.deepStrictEqual(JSON.parse(response.payload.toString()), {
error: 'Bad Request',
code: 'FST_ERR_CTP_EMPTY_JSON_BODY',
message: 'Body cannot be empty when content-type is set to \'application/json\'',
statusCode: 400
})
})
})
|