fastify
Version:
Fast and low overhead web framework, for Node.js
43 lines (38 loc) • 1.34 kB
JavaScript
const { test } = require('node:test')
const Fastify = require('..')
test('should remove content-type for setErrorHandler', async t => {
t.plan(8)
let count = 0
const fastify = Fastify()
fastify.setErrorHandler(function (error, request, reply) {
t.assert.strictEqual(error.message, 'kaboom')
t.assert.strictEqual(reply.hasHeader('content-type'), false)
reply.code(400).send({ foo: 'bar' })
})
fastify.addHook('onSend', async function (request, reply, payload) {
count++
t.assert.strictEqual(typeof payload, 'string')
switch (count) {
case 1: {
// should guess the correct content-type based on payload
t.assert.strictEqual(reply.getHeader('content-type'), 'text/plain; charset=utf-8')
throw Error('kaboom')
}
case 2: {
// should guess the correct content-type based on payload
t.assert.strictEqual(reply.getHeader('content-type'), 'application/json; charset=utf-8')
return payload
}
default: {
t.fail('should not reach')
}
}
})
fastify.get('/', function (request, reply) {
reply.send('plain-text')
})
const { statusCode, body } = await fastify.inject({ method: 'GET', path: '/' })
t.assert.strictEqual(statusCode, 400)
t.assert.strictEqual(body, JSON.stringify({ foo: 'bar' }))
})