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