UNPKG

3.71 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const Fastify = require('..')
6const symbols = require('../lib/symbols.js')
7
8test('default 500', t => {
9 t.plan(4)
10
11 const fastify = Fastify()
12
13 fastify.get('/', function (req, reply) {
14 reply.send(new Error('kaboom'))
15 })
16
17 fastify.inject({
18 method: 'GET',
19 url: '/'
20 }, (err, res) => {
21 t.error(err)
22 t.strictEqual(res.statusCode, 500)
23 t.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
24 t.deepEqual(JSON.parse(res.payload), {
25 error: 'Internal Server Error',
26 message: 'kaboom',
27 statusCode: 500
28 })
29 })
30})
31
32test('custom 500', t => {
33 t.plan(6)
34
35 const fastify = Fastify()
36
37 fastify.get('/', function (req, reply) {
38 reply.send(new Error('kaboom'))
39 })
40
41 fastify.setErrorHandler(function (err, request, reply) {
42 t.type(request, 'object')
43 t.type(request, fastify[symbols.kRequest])
44 reply
45 .code(500)
46 .type('text/plain')
47 .send('an error happened: ' + err.message)
48 })
49
50 fastify.inject({
51 method: 'GET',
52 url: '/'
53 }, (err, res) => {
54 t.error(err)
55 t.strictEqual(res.statusCode, 500)
56 t.strictEqual(res.headers['content-type'], 'text/plain')
57 t.deepEqual(res.payload.toString(), 'an error happened: kaboom')
58 })
59})
60
61test('encapsulated 500', t => {
62 t.plan(10)
63
64 const fastify = Fastify()
65
66 fastify.get('/', function (req, reply) {
67 reply.send(new Error('kaboom'))
68 })
69
70 fastify.register(function (f, opts, next) {
71 f.get('/', function (req, reply) {
72 reply.send(new Error('kaboom'))
73 })
74
75 f.setErrorHandler(function (err, request, reply) {
76 t.type(request, 'object')
77 t.type(request, f[symbols.kRequest])
78 reply
79 .code(500)
80 .type('text/plain')
81 .send('an error happened: ' + err.message)
82 })
83
84 next()
85 }, { prefix: 'test' })
86
87 fastify.inject({
88 method: 'GET',
89 url: '/test'
90 }, (err, res) => {
91 t.error(err)
92 t.strictEqual(res.statusCode, 500)
93 t.strictEqual(res.headers['content-type'], 'text/plain')
94 t.deepEqual(res.payload.toString(), 'an error happened: kaboom')
95 })
96
97 fastify.inject({
98 method: 'GET',
99 url: '/'
100 }, (err, res) => {
101 t.error(err)
102 t.strictEqual(res.statusCode, 500)
103 t.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
104 t.deepEqual(JSON.parse(res.payload), {
105 error: 'Internal Server Error',
106 message: 'kaboom',
107 statusCode: 500
108 })
109 })
110})
111
112test('custom 500 with hooks', t => {
113 t.plan(7)
114
115 const fastify = Fastify()
116
117 fastify.get('/', function (req, reply) {
118 reply.send(new Error('kaboom'))
119 })
120
121 fastify.setErrorHandler(function (err, request, reply) {
122 reply
123 .code(500)
124 .type('text/plain')
125 .send('an error happened: ' + err.message)
126 })
127
128 fastify.addHook('onSend', (req, res, payload, next) => {
129 t.ok('called', 'onSend')
130 next()
131 })
132 fastify.addHook('onRequest', (req, res, next) => {
133 t.ok('called', 'onRequest')
134 next()
135 })
136 fastify.addHook('onResponse', (request, reply, next) => {
137 t.ok('called', 'onResponse')
138 next()
139 })
140
141 fastify.inject({
142 method: 'GET',
143 url: '/'
144 }, (err, res) => {
145 t.error(err)
146 t.strictEqual(res.statusCode, 500)
147 t.strictEqual(res.headers['content-type'], 'text/plain')
148 t.deepEqual(res.payload.toString(), 'an error happened: kaboom')
149 })
150})
151
152test('cannot set errorHandler after binding', t => {
153 t.plan(2)
154
155 const fastify = Fastify()
156 t.tearDown(fastify.close.bind(fastify))
157
158 fastify.listen(0, err => {
159 t.error(err)
160
161 try {
162 fastify.setErrorHandler(() => { })
163 t.fail()
164 } catch (e) {
165 t.pass()
166 }
167 })
168})