fastify
Version:
Fast and low overhead web framework, for Node.js
96 lines (77 loc) • 2.1 kB
JavaScript
const t = require('tap')
const test = t.test
const Fastify = require('..')
test('default 500', t => {
t.plan(3)
const fastify = Fastify()
fastify.get('/', function (req, reply) {
reply.send(new Error('kaboom'))
})
fastify.inject({
method: 'GET',
url: '/'
}, (res) => {
t.strictEqual(res.statusCode, 500)
t.strictEqual(res.headers['content-type'], 'application/json')
t.deepEqual(JSON.parse(res.payload), {
error: 'Internal Server Error',
message: 'kaboom',
statusCode: 500
})
})
})
test('custom 500', t => {
t.plan(3)
const fastify = Fastify()
fastify.get('/', function (req, reply) {
reply.send(new Error('kaboom'))
})
fastify.setErrorHandler(function (err, reply) {
reply.type('text/plain').send('an error happened: ' + err.message)
})
fastify.inject({
method: 'GET',
url: '/'
}, (res) => {
t.strictEqual(res.statusCode, 500)
t.strictEqual(res.headers['content-type'], 'text/plain')
t.deepEqual(res.payload.toString(), 'an error happened: kaboom')
})
})
test('encapsulated 500', t => {
t.plan(6)
const fastify = Fastify()
fastify.get('/', function (req, reply) {
reply.send(new Error('kaboom'))
})
fastify.register(function (f, opts, next) {
f.get('/', function (req, reply) {
reply.send(new Error('kaboom'))
})
f.setErrorHandler(function (err, reply) {
reply.type('text/plain').send('an error happened: ' + err.message)
})
next()
}, { prefix: 'test' })
fastify.inject({
method: 'GET',
url: '/test'
}, (res) => {
t.strictEqual(res.statusCode, 500)
t.strictEqual(res.headers['content-type'], 'text/plain')
t.deepEqual(res.payload.toString(), 'an error happened: kaboom')
})
fastify.inject({
method: 'GET',
url: '/'
}, (res) => {
t.strictEqual(res.statusCode, 500)
t.strictEqual(res.headers['content-type'], 'application/json')
t.deepEqual(JSON.parse(res.payload), {
error: 'Internal Server Error',
message: 'kaboom',
statusCode: 500
})
})
})