fastify 5.3.2 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/build/build-validation.js +2 -1
- package/docs/Guides/Delay-Accepting-Requests.md +3 -3
- package/docs/Guides/Ecosystem.md +16 -7
- package/docs/Guides/Serverless.md +28 -69
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +2 -4
- package/docs/Reference/Hooks.md +14 -14
- package/docs/Reference/Logging.md +3 -3
- package/docs/Reference/Middleware.md +1 -1
- package/docs/Reference/Reply.md +8 -8
- package/docs/Reference/Request.md +1 -1
- package/docs/Reference/Routes.md +3 -3
- package/docs/Reference/Server.md +40 -10
- package/docs/Reference/Validation-and-Serialization.md +1 -1
- package/eslint.config.js +17 -9
- package/fastify.d.ts +2 -1
- package/fastify.js +20 -4
- package/lib/configValidator.js +1 -1
- package/lib/decorate.js +2 -2
- package/lib/errors.js +6 -8
- package/lib/logger-factory.js +1 -1
- package/lib/logger-pino.js +2 -2
- package/lib/pluginOverride.js +3 -1
- package/lib/reply.js +9 -13
- package/lib/request.js +4 -11
- package/lib/server.js +30 -51
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +8 -0
- package/package.json +11 -7
- package/test/404s.test.js +226 -325
- package/test/allow-unsafe-regex.test.js +19 -48
- package/test/als.test.js +28 -40
- package/test/async-await.test.js +11 -2
- package/test/body-limit.test.js +41 -65
- package/test/build-certificate.js +1 -1
- package/test/close-pipelining.test.js +5 -4
- package/test/custom-parser-async.test.js +17 -22
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +422 -341
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/helper.js +108 -70
- package/test/hooks-async.test.js +248 -218
- package/test/hooks.on-listen.test.js +255 -239
- package/test/hooks.on-ready.test.js +110 -92
- package/test/hooks.test.js +910 -769
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/proppatch.test.js +23 -29
- package/test/http-methods/report.test.js +44 -69
- package/test/http-methods/search.test.js +67 -82
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +28 -27
- package/test/https/https.test.js +56 -53
- package/test/inject.test.js +114 -97
- package/test/input-validation.js +63 -53
- package/test/internals/errors.test.js +0 -10
- package/test/internals/handle-request.test.js +49 -66
- package/test/internals/hooks.test.js +17 -0
- package/test/issue-4959.test.js +14 -5
- package/test/listen.4.test.js +31 -43
- package/test/logger/response.test.js +19 -20
- package/test/nullable-validation.test.js +33 -46
- package/test/options.error-handler.test.js +1 -1
- package/test/options.test.js +1 -1
- package/test/output-validation.test.js +49 -72
- package/test/patch.error-handler.test.js +1 -1
- package/test/patch.test.js +1 -1
- package/test/plugin.1.test.js +71 -60
- package/test/plugin.2.test.js +104 -86
- package/test/plugin.3.test.js +56 -35
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +36 -30
- package/test/proto-poisoning.test.js +78 -97
- package/test/put.error-handler.test.js +1 -1
- package/test/put.test.js +1 -1
- package/test/reply-error.test.js +169 -148
- package/test/reply-trailers.test.js +119 -108
- package/test/request-error.test.js +0 -46
- package/test/route-hooks.test.js +112 -92
- package/test/route-prefix.test.js +194 -133
- package/test/schema-feature.test.js +309 -238
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +278 -199
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +30 -27
- package/test/stream.2.test.js +20 -10
- package/test/stream.3.test.js +37 -31
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +0 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/register.test-d.ts +1 -1
- package/test/types/request.test-d.ts +1 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +1 -1
- package/types/errors.d.ts +0 -1
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
- package/test/http2/missing-http2-module.test.js +0 -17
package/test/hooks-async.test.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { Readable } = require('node:stream')
|
|
4
|
-
const
|
|
5
|
-
const test = t.test
|
|
4
|
+
const { test, describe } = require('node:test')
|
|
6
5
|
const sget = require('simple-get').concat
|
|
7
6
|
const Fastify = require('../fastify')
|
|
8
7
|
const fs = require('node:fs')
|
|
9
8
|
const { sleep } = require('./helper')
|
|
9
|
+
const { waitForCb } = require('./toolkit')
|
|
10
10
|
|
|
11
11
|
process.removeAllListeners('warning')
|
|
12
12
|
|
|
13
13
|
test('async hooks', t => {
|
|
14
14
|
t.plan(21)
|
|
15
|
-
|
|
16
15
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
17
16
|
fastify.addHook('onRequest', async function (request, reply) {
|
|
18
17
|
await sleep(1)
|
|
@@ -25,8 +24,8 @@ test('async hooks', t => {
|
|
|
25
24
|
|
|
26
25
|
fastify.addHook('preHandler', async function (request, reply) {
|
|
27
26
|
await sleep(1)
|
|
28
|
-
t.
|
|
29
|
-
t.
|
|
27
|
+
t.assert.strictEqual(request.test, 'the request is coming')
|
|
28
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
30
29
|
if (request.raw.method === 'HEAD') {
|
|
31
30
|
throw new Error('some error')
|
|
32
31
|
}
|
|
@@ -34,17 +33,21 @@ test('async hooks', t => {
|
|
|
34
33
|
|
|
35
34
|
fastify.addHook('onSend', async function (request, reply, payload) {
|
|
36
35
|
await sleep(1)
|
|
37
|
-
t.ok('onSend called')
|
|
36
|
+
t.assert.ok('onSend called')
|
|
38
37
|
})
|
|
39
38
|
|
|
39
|
+
const completion = waitForCb({
|
|
40
|
+
steps: 6
|
|
41
|
+
})
|
|
40
42
|
fastify.addHook('onResponse', async function (request, reply) {
|
|
41
43
|
await sleep(1)
|
|
42
|
-
t.ok('onResponse called')
|
|
44
|
+
t.assert.ok('onResponse called')
|
|
45
|
+
completion.stepIn()
|
|
43
46
|
})
|
|
44
47
|
|
|
45
48
|
fastify.get('/', function (request, reply) {
|
|
46
|
-
t.
|
|
47
|
-
t.
|
|
49
|
+
t.assert.strictEqual(request.test, 'the request is coming')
|
|
50
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
48
51
|
reply.code(200).send({ hello: 'world' })
|
|
49
52
|
})
|
|
50
53
|
|
|
@@ -57,38 +60,41 @@ test('async hooks', t => {
|
|
|
57
60
|
})
|
|
58
61
|
|
|
59
62
|
fastify.listen({ port: 0 }, err => {
|
|
60
|
-
t.
|
|
61
|
-
t.
|
|
63
|
+
t.assert.ifError(err)
|
|
64
|
+
t.after(() => { fastify.close() })
|
|
62
65
|
|
|
63
66
|
sget({
|
|
64
67
|
method: 'GET',
|
|
65
68
|
url: 'http://localhost:' + fastify.server.address().port
|
|
66
69
|
}, (err, response, body) => {
|
|
67
|
-
t.
|
|
68
|
-
t.
|
|
69
|
-
t.
|
|
70
|
-
t.
|
|
70
|
+
t.assert.ifError(err)
|
|
71
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
72
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
73
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
74
|
+
completion.stepIn()
|
|
71
75
|
})
|
|
72
|
-
|
|
73
76
|
sget({
|
|
74
77
|
method: 'HEAD',
|
|
75
78
|
url: 'http://localhost:' + fastify.server.address().port
|
|
76
79
|
}, (err, response, body) => {
|
|
77
|
-
t.
|
|
78
|
-
t.
|
|
80
|
+
t.assert.ifError(err)
|
|
81
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
82
|
+
completion.stepIn()
|
|
79
83
|
})
|
|
80
|
-
|
|
81
84
|
sget({
|
|
82
85
|
method: 'DELETE',
|
|
83
86
|
url: 'http://localhost:' + fastify.server.address().port
|
|
84
87
|
}, (err, response, body) => {
|
|
85
|
-
t.
|
|
86
|
-
t.
|
|
88
|
+
t.assert.ifError(err)
|
|
89
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
90
|
+
completion.stepIn()
|
|
87
91
|
})
|
|
88
92
|
})
|
|
93
|
+
|
|
94
|
+
return completion.patience
|
|
89
95
|
})
|
|
90
96
|
|
|
91
|
-
test('modify payload', t => {
|
|
97
|
+
test('modify payload', (t, testDone) => {
|
|
92
98
|
t.plan(10)
|
|
93
99
|
const fastify = Fastify()
|
|
94
100
|
const payload = { hello: 'world' }
|
|
@@ -96,20 +102,20 @@ test('modify payload', t => {
|
|
|
96
102
|
const anotherPayload = '"winter is coming"'
|
|
97
103
|
|
|
98
104
|
fastify.addHook('onSend', async function (request, reply, thePayload) {
|
|
99
|
-
t.ok('onSend called')
|
|
100
|
-
t.
|
|
105
|
+
t.assert.ok('onSend called')
|
|
106
|
+
t.assert.deepStrictEqual(JSON.parse(thePayload), payload)
|
|
101
107
|
return thePayload.replace('world', 'modified')
|
|
102
108
|
})
|
|
103
109
|
|
|
104
110
|
fastify.addHook('onSend', async function (request, reply, thePayload) {
|
|
105
|
-
t.ok('onSend called')
|
|
106
|
-
t.
|
|
111
|
+
t.assert.ok('onSend called')
|
|
112
|
+
t.assert.deepStrictEqual(JSON.parse(thePayload), modifiedPayload)
|
|
107
113
|
return anotherPayload
|
|
108
114
|
})
|
|
109
115
|
|
|
110
116
|
fastify.addHook('onSend', async function (request, reply, thePayload) {
|
|
111
|
-
t.ok('onSend called')
|
|
112
|
-
t.
|
|
117
|
+
t.assert.ok('onSend called')
|
|
118
|
+
t.assert.deepStrictEqual(thePayload, anotherPayload)
|
|
113
119
|
})
|
|
114
120
|
|
|
115
121
|
fastify.get('/', (req, reply) => {
|
|
@@ -120,14 +126,15 @@ test('modify payload', t => {
|
|
|
120
126
|
method: 'GET',
|
|
121
127
|
url: '/'
|
|
122
128
|
}, (err, res) => {
|
|
123
|
-
t.
|
|
124
|
-
t.
|
|
125
|
-
t.
|
|
126
|
-
t.
|
|
129
|
+
t.assert.ifError(err)
|
|
130
|
+
t.assert.strictEqual(res.payload, anotherPayload)
|
|
131
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
132
|
+
t.assert.strictEqual(res.headers['content-length'], '18')
|
|
133
|
+
testDone()
|
|
127
134
|
})
|
|
128
135
|
})
|
|
129
136
|
|
|
130
|
-
test('onRequest hooks should be able to block a request', t => {
|
|
137
|
+
test('onRequest hooks should be able to block a request', (t, testDone) => {
|
|
131
138
|
t.plan(5)
|
|
132
139
|
const fastify = Fastify()
|
|
133
140
|
|
|
@@ -136,36 +143,37 @@ test('onRequest hooks should be able to block a request', t => {
|
|
|
136
143
|
})
|
|
137
144
|
|
|
138
145
|
fastify.addHook('onRequest', async (req, reply) => {
|
|
139
|
-
t.fail('this should not be called')
|
|
146
|
+
t.assert.fail('this should not be called')
|
|
140
147
|
})
|
|
141
148
|
|
|
142
149
|
fastify.addHook('preHandler', async (req, reply) => {
|
|
143
|
-
t.fail('this should not be called')
|
|
150
|
+
t.assert.fail('this should not be called')
|
|
144
151
|
})
|
|
145
152
|
|
|
146
153
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
147
|
-
t.ok('called')
|
|
154
|
+
t.assert.ok('called')
|
|
148
155
|
})
|
|
149
156
|
|
|
150
157
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
151
|
-
t.ok('called')
|
|
158
|
+
t.assert.ok('called')
|
|
152
159
|
})
|
|
153
160
|
|
|
154
161
|
fastify.get('/', function (request, reply) {
|
|
155
|
-
t.fail('we should not be here')
|
|
162
|
+
t.assert.fail('we should not be here')
|
|
156
163
|
})
|
|
157
164
|
|
|
158
165
|
fastify.inject({
|
|
159
166
|
url: '/',
|
|
160
167
|
method: 'GET'
|
|
161
168
|
}, (err, res) => {
|
|
162
|
-
t.
|
|
163
|
-
t.
|
|
164
|
-
t.
|
|
169
|
+
t.assert.ifError(err)
|
|
170
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
171
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
172
|
+
testDone()
|
|
165
173
|
})
|
|
166
174
|
})
|
|
167
175
|
|
|
168
|
-
test('preParsing hooks should be able to modify the payload', t => {
|
|
176
|
+
test('preParsing hooks should be able to modify the payload', (t, testDone) => {
|
|
169
177
|
t.plan(3)
|
|
170
178
|
const fastify = Fastify()
|
|
171
179
|
|
|
@@ -188,13 +196,14 @@ test('preParsing hooks should be able to modify the payload', t => {
|
|
|
188
196
|
url: '/',
|
|
189
197
|
payload: { hello: 'world' }
|
|
190
198
|
}, (err, res) => {
|
|
191
|
-
t.
|
|
192
|
-
t.
|
|
193
|
-
t.
|
|
199
|
+
t.assert.ifError(err)
|
|
200
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
201
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'another world' })
|
|
202
|
+
testDone()
|
|
194
203
|
})
|
|
195
204
|
})
|
|
196
205
|
|
|
197
|
-
test('preParsing hooks should be able to supply statusCode', t => {
|
|
206
|
+
test('preParsing hooks should be able to supply statusCode', (t, testDone) => {
|
|
198
207
|
t.plan(4)
|
|
199
208
|
const fastify = Fastify()
|
|
200
209
|
|
|
@@ -211,11 +220,11 @@ test('preParsing hooks should be able to supply statusCode', t => {
|
|
|
211
220
|
})
|
|
212
221
|
|
|
213
222
|
fastify.addHook('onError', async (req, res, err) => {
|
|
214
|
-
t.
|
|
223
|
+
t.assert.strictEqual(err.statusCode, 408)
|
|
215
224
|
})
|
|
216
225
|
|
|
217
226
|
fastify.post('/', function (request, reply) {
|
|
218
|
-
t.fail('should not be called')
|
|
227
|
+
t.assert.fail('should not be called')
|
|
219
228
|
})
|
|
220
229
|
|
|
221
230
|
fastify.inject({
|
|
@@ -223,17 +232,19 @@ test('preParsing hooks should be able to supply statusCode', t => {
|
|
|
223
232
|
url: '/',
|
|
224
233
|
payload: { hello: 'world' }
|
|
225
234
|
}, (err, res) => {
|
|
226
|
-
t.
|
|
227
|
-
t.
|
|
228
|
-
t.
|
|
235
|
+
t.assert.ifError(err)
|
|
236
|
+
t.assert.strictEqual(res.statusCode, 408)
|
|
237
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
229
238
|
statusCode: 408,
|
|
230
239
|
error: 'Request Timeout',
|
|
231
240
|
message: 'kaboom'
|
|
232
241
|
})
|
|
242
|
+
|
|
243
|
+
testDone()
|
|
233
244
|
})
|
|
234
245
|
})
|
|
235
246
|
|
|
236
|
-
test('preParsing hooks should ignore statusCode 200 in stream error', t => {
|
|
247
|
+
test('preParsing hooks should ignore statusCode 200 in stream error', (t, testDone) => {
|
|
237
248
|
t.plan(4)
|
|
238
249
|
const fastify = Fastify()
|
|
239
250
|
|
|
@@ -250,11 +261,11 @@ test('preParsing hooks should ignore statusCode 200 in stream error', t => {
|
|
|
250
261
|
})
|
|
251
262
|
|
|
252
263
|
fastify.addHook('onError', async (req, res, err) => {
|
|
253
|
-
t.
|
|
264
|
+
t.assert.strictEqual(err.statusCode, 400)
|
|
254
265
|
})
|
|
255
266
|
|
|
256
267
|
fastify.post('/', function (request, reply) {
|
|
257
|
-
t.fail('should not be called')
|
|
268
|
+
t.assert.fail('should not be called')
|
|
258
269
|
})
|
|
259
270
|
|
|
260
271
|
fastify.inject({
|
|
@@ -262,17 +273,18 @@ test('preParsing hooks should ignore statusCode 200 in stream error', t => {
|
|
|
262
273
|
url: '/',
|
|
263
274
|
payload: { hello: 'world' }
|
|
264
275
|
}, (err, res) => {
|
|
265
|
-
t.
|
|
266
|
-
t.
|
|
267
|
-
t.
|
|
276
|
+
t.assert.ifError(err)
|
|
277
|
+
t.assert.strictEqual(res.statusCode, 400)
|
|
278
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
268
279
|
statusCode: 400,
|
|
269
280
|
error: 'Bad Request',
|
|
270
281
|
message: 'kaboom'
|
|
271
282
|
})
|
|
283
|
+
testDone()
|
|
272
284
|
})
|
|
273
285
|
})
|
|
274
286
|
|
|
275
|
-
test('preParsing hooks should ignore non-number statusCode in stream error', t => {
|
|
287
|
+
test('preParsing hooks should ignore non-number statusCode in stream error', (t, testDone) => {
|
|
276
288
|
t.plan(4)
|
|
277
289
|
const fastify = Fastify()
|
|
278
290
|
|
|
@@ -289,11 +301,11 @@ test('preParsing hooks should ignore non-number statusCode in stream error', t =
|
|
|
289
301
|
})
|
|
290
302
|
|
|
291
303
|
fastify.addHook('onError', async (req, res, err) => {
|
|
292
|
-
t.
|
|
304
|
+
t.assert.strictEqual(err.statusCode, 400)
|
|
293
305
|
})
|
|
294
306
|
|
|
295
307
|
fastify.post('/', function (request, reply) {
|
|
296
|
-
t.fail('should not be called')
|
|
308
|
+
t.assert.fail('should not be called')
|
|
297
309
|
})
|
|
298
310
|
|
|
299
311
|
fastify.inject({
|
|
@@ -301,17 +313,18 @@ test('preParsing hooks should ignore non-number statusCode in stream error', t =
|
|
|
301
313
|
url: '/',
|
|
302
314
|
payload: { hello: 'world' }
|
|
303
315
|
}, (err, res) => {
|
|
304
|
-
t.
|
|
305
|
-
t.
|
|
306
|
-
t.
|
|
316
|
+
t.assert.ifError(err)
|
|
317
|
+
t.assert.strictEqual(res.statusCode, 400)
|
|
318
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
307
319
|
statusCode: 400,
|
|
308
320
|
error: 'Bad Request',
|
|
309
321
|
message: 'kaboom'
|
|
310
322
|
})
|
|
323
|
+
testDone()
|
|
311
324
|
})
|
|
312
325
|
})
|
|
313
326
|
|
|
314
|
-
test('preParsing hooks should default to statusCode 400 if stream error', t => {
|
|
327
|
+
test('preParsing hooks should default to statusCode 400 if stream error', (t, testDone) => {
|
|
315
328
|
t.plan(4)
|
|
316
329
|
const fastify = Fastify()
|
|
317
330
|
|
|
@@ -326,11 +339,11 @@ test('preParsing hooks should default to statusCode 400 if stream error', t => {
|
|
|
326
339
|
})
|
|
327
340
|
|
|
328
341
|
fastify.addHook('onError', async (req, res, err) => {
|
|
329
|
-
t.
|
|
342
|
+
t.assert.strictEqual(err.statusCode, 400)
|
|
330
343
|
})
|
|
331
344
|
|
|
332
345
|
fastify.post('/', function (request, reply) {
|
|
333
|
-
t.fail('should not be called')
|
|
346
|
+
t.assert.fail('should not be called')
|
|
334
347
|
})
|
|
335
348
|
|
|
336
349
|
fastify.inject({
|
|
@@ -338,20 +351,21 @@ test('preParsing hooks should default to statusCode 400 if stream error', t => {
|
|
|
338
351
|
url: '/',
|
|
339
352
|
payload: { hello: 'world' }
|
|
340
353
|
}, (err, res) => {
|
|
341
|
-
t.
|
|
342
|
-
t.
|
|
343
|
-
t.
|
|
354
|
+
t.assert.ifError(err)
|
|
355
|
+
t.assert.strictEqual(res.statusCode, 400)
|
|
356
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
344
357
|
statusCode: 400,
|
|
345
358
|
error: 'Bad Request',
|
|
346
359
|
message: 'kaboom'
|
|
347
360
|
})
|
|
361
|
+
testDone()
|
|
348
362
|
})
|
|
349
363
|
})
|
|
350
364
|
|
|
351
|
-
test('preParsing hooks should handle errors', t => {
|
|
365
|
+
test('preParsing hooks should handle errors', (t, testDone) => {
|
|
352
366
|
t.plan(3)
|
|
353
|
-
const fastify = Fastify()
|
|
354
367
|
|
|
368
|
+
const fastify = Fastify()
|
|
355
369
|
fastify.addHook('preParsing', async (req, reply, payload) => {
|
|
356
370
|
const e = new Error('kaboom')
|
|
357
371
|
e.statusCode = 501
|
|
@@ -367,13 +381,14 @@ test('preParsing hooks should handle errors', t => {
|
|
|
367
381
|
url: '/',
|
|
368
382
|
payload: { hello: 'world' }
|
|
369
383
|
}, (err, res) => {
|
|
370
|
-
t.
|
|
371
|
-
t.
|
|
372
|
-
t.
|
|
384
|
+
t.assert.ifError(err)
|
|
385
|
+
t.assert.strictEqual(res.statusCode, 501)
|
|
386
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { error: 'Not Implemented', message: 'kaboom', statusCode: 501 })
|
|
387
|
+
testDone()
|
|
373
388
|
})
|
|
374
389
|
})
|
|
375
390
|
|
|
376
|
-
test('preHandler hooks should be able to block a request', t => {
|
|
391
|
+
test('preHandler hooks should be able to block a request', (t, testDone) => {
|
|
377
392
|
t.plan(5)
|
|
378
393
|
const fastify = Fastify()
|
|
379
394
|
|
|
@@ -382,32 +397,33 @@ test('preHandler hooks should be able to block a request', t => {
|
|
|
382
397
|
})
|
|
383
398
|
|
|
384
399
|
fastify.addHook('preHandler', async (req, reply) => {
|
|
385
|
-
t.fail('this should not be called')
|
|
400
|
+
t.assert.fail('this should not be called')
|
|
386
401
|
})
|
|
387
402
|
|
|
388
403
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
389
|
-
t.
|
|
404
|
+
t.assert.strictEqual(payload, 'hello')
|
|
390
405
|
})
|
|
391
406
|
|
|
392
407
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
393
|
-
t.ok('called')
|
|
408
|
+
t.assert.ok('called')
|
|
394
409
|
})
|
|
395
410
|
|
|
396
411
|
fastify.get('/', function (request, reply) {
|
|
397
|
-
t.fail('we should not be here')
|
|
412
|
+
t.assert.fail('we should not be here')
|
|
398
413
|
})
|
|
399
414
|
|
|
400
415
|
fastify.inject({
|
|
401
416
|
url: '/',
|
|
402
417
|
method: 'GET'
|
|
403
418
|
}, (err, res) => {
|
|
404
|
-
t.
|
|
405
|
-
t.
|
|
406
|
-
t.
|
|
419
|
+
t.assert.ifError(err)
|
|
420
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
421
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
422
|
+
testDone()
|
|
407
423
|
})
|
|
408
424
|
})
|
|
409
425
|
|
|
410
|
-
test('preValidation hooks should be able to block a request', t => {
|
|
426
|
+
test('preValidation hooks should be able to block a request', (t, testDone) => {
|
|
411
427
|
t.plan(5)
|
|
412
428
|
const fastify = Fastify()
|
|
413
429
|
|
|
@@ -416,38 +432,40 @@ test('preValidation hooks should be able to block a request', t => {
|
|
|
416
432
|
})
|
|
417
433
|
|
|
418
434
|
fastify.addHook('preValidation', async (req, reply) => {
|
|
419
|
-
t.fail('this should not be called')
|
|
435
|
+
t.assert.fail('this should not be called')
|
|
420
436
|
})
|
|
421
437
|
|
|
422
438
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
423
|
-
t.
|
|
439
|
+
t.assert.strictEqual(payload, 'hello')
|
|
424
440
|
})
|
|
425
441
|
|
|
426
442
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
427
|
-
t.ok('called')
|
|
443
|
+
t.assert.ok('called')
|
|
428
444
|
})
|
|
429
445
|
|
|
430
446
|
fastify.get('/', function (request, reply) {
|
|
431
|
-
t.fail('we should not be here')
|
|
447
|
+
t.assert.fail('we should not be here')
|
|
432
448
|
})
|
|
433
449
|
|
|
434
450
|
fastify.inject({
|
|
435
451
|
url: '/',
|
|
436
452
|
method: 'GET'
|
|
437
453
|
}, (err, res) => {
|
|
438
|
-
t.
|
|
439
|
-
t.
|
|
440
|
-
t.
|
|
454
|
+
t.assert.ifError(err)
|
|
455
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
456
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
457
|
+
testDone()
|
|
441
458
|
})
|
|
442
459
|
})
|
|
443
460
|
|
|
444
|
-
test('preValidation hooks should be able to change request body before validation', t => {
|
|
461
|
+
test('preValidation hooks should be able to change request body before validation', (t, testDone) => {
|
|
445
462
|
t.plan(4)
|
|
446
463
|
const fastify = Fastify()
|
|
447
464
|
|
|
448
465
|
fastify.addHook('preValidation', async (req, _reply) => {
|
|
449
466
|
const buff = Buffer.from(req.body.message, 'base64')
|
|
450
467
|
req.body = JSON.parse(buff.toString('utf-8'))
|
|
468
|
+
t.assert.ok('has been called')
|
|
451
469
|
})
|
|
452
470
|
|
|
453
471
|
fastify.post(
|
|
@@ -469,7 +487,6 @@ test('preValidation hooks should be able to change request body before validatio
|
|
|
469
487
|
}
|
|
470
488
|
},
|
|
471
489
|
(req, reply) => {
|
|
472
|
-
t.pass()
|
|
473
490
|
reply.status(200).send('hello')
|
|
474
491
|
}
|
|
475
492
|
)
|
|
@@ -481,13 +498,14 @@ test('preValidation hooks should be able to change request body before validatio
|
|
|
481
498
|
message: Buffer.from(JSON.stringify({ foo: 'example', bar: 1 })).toString('base64')
|
|
482
499
|
}
|
|
483
500
|
}, (err, res) => {
|
|
484
|
-
t.
|
|
485
|
-
t.
|
|
486
|
-
t.
|
|
501
|
+
t.assert.ifError(err)
|
|
502
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
503
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
504
|
+
testDone()
|
|
487
505
|
})
|
|
488
506
|
})
|
|
489
507
|
|
|
490
|
-
test('preSerialization hooks should be able to modify the payload', t => {
|
|
508
|
+
test('preSerialization hooks should be able to modify the payload', (t, testDone) => {
|
|
491
509
|
t.plan(3)
|
|
492
510
|
const fastify = Fastify()
|
|
493
511
|
|
|
@@ -503,13 +521,14 @@ test('preSerialization hooks should be able to modify the payload', t => {
|
|
|
503
521
|
url: '/',
|
|
504
522
|
method: 'GET'
|
|
505
523
|
}, (err, res) => {
|
|
506
|
-
t.
|
|
507
|
-
t.
|
|
508
|
-
t.
|
|
524
|
+
t.assert.ifError(err)
|
|
525
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
526
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'another world' })
|
|
527
|
+
testDone()
|
|
509
528
|
})
|
|
510
529
|
})
|
|
511
530
|
|
|
512
|
-
test('preSerialization hooks should handle errors', t => {
|
|
531
|
+
test('preSerialization hooks should handle errors', (t, testDone) => {
|
|
513
532
|
t.plan(3)
|
|
514
533
|
const fastify = Fastify()
|
|
515
534
|
|
|
@@ -525,18 +544,19 @@ test('preSerialization hooks should handle errors', t => {
|
|
|
525
544
|
url: '/',
|
|
526
545
|
method: 'GET'
|
|
527
546
|
}, (err, res) => {
|
|
528
|
-
t.
|
|
529
|
-
t.
|
|
530
|
-
t.
|
|
547
|
+
t.assert.ifError(err)
|
|
548
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
549
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { error: 'Internal Server Error', message: 'kaboom', statusCode: 500 })
|
|
550
|
+
testDone()
|
|
531
551
|
})
|
|
532
552
|
})
|
|
533
553
|
|
|
534
|
-
test('preValidation hooks should handle throwing null', t => {
|
|
554
|
+
test('preValidation hooks should handle throwing null', (t, testDone) => {
|
|
535
555
|
t.plan(4)
|
|
536
556
|
const fastify = Fastify()
|
|
537
557
|
|
|
538
558
|
fastify.setErrorHandler(async (error, request, reply) => {
|
|
539
|
-
t.ok(error instanceof Error)
|
|
559
|
+
t.assert.ok(error instanceof Error)
|
|
540
560
|
await reply.send(error)
|
|
541
561
|
})
|
|
542
562
|
|
|
@@ -545,24 +565,25 @@ test('preValidation hooks should handle throwing null', t => {
|
|
|
545
565
|
throw null
|
|
546
566
|
})
|
|
547
567
|
|
|
548
|
-
fastify.get('/', function (request, reply) { t.fail('the handler must not be called') })
|
|
568
|
+
fastify.get('/', function (request, reply) { t.assert.fail('the handler must not be called') })
|
|
549
569
|
|
|
550
570
|
fastify.inject({
|
|
551
571
|
url: '/',
|
|
552
572
|
method: 'GET'
|
|
553
573
|
}, (err, res) => {
|
|
554
|
-
t.
|
|
555
|
-
t.
|
|
556
|
-
t.
|
|
574
|
+
t.assert.ifError(err)
|
|
575
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
576
|
+
t.assert.deepStrictEqual(res.json(), {
|
|
557
577
|
error: 'Internal Server Error',
|
|
558
578
|
code: 'FST_ERR_SEND_UNDEFINED_ERR',
|
|
559
579
|
message: 'Undefined error has occurred',
|
|
560
580
|
statusCode: 500
|
|
561
581
|
})
|
|
582
|
+
testDone()
|
|
562
583
|
})
|
|
563
584
|
})
|
|
564
585
|
|
|
565
|
-
test('preValidation hooks should handle throwing a string', t => {
|
|
586
|
+
test('preValidation hooks should handle throwing a string', (t, testDone) => {
|
|
566
587
|
t.plan(3)
|
|
567
588
|
const fastify = Fastify()
|
|
568
589
|
|
|
@@ -571,19 +592,20 @@ test('preValidation hooks should handle throwing a string', t => {
|
|
|
571
592
|
throw 'this is an error'
|
|
572
593
|
})
|
|
573
594
|
|
|
574
|
-
fastify.get('/', function (request, reply) { t.fail('the handler must not be called') })
|
|
595
|
+
fastify.get('/', function (request, reply) { t.assert.fail('the handler must not be called') })
|
|
575
596
|
|
|
576
597
|
fastify.inject({
|
|
577
598
|
url: '/',
|
|
578
599
|
method: 'GET'
|
|
579
600
|
}, (err, res) => {
|
|
580
|
-
t.
|
|
581
|
-
t.
|
|
582
|
-
t.
|
|
601
|
+
t.assert.ifError(err)
|
|
602
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
603
|
+
t.assert.strictEqual(res.payload, 'this is an error')
|
|
604
|
+
testDone()
|
|
583
605
|
})
|
|
584
606
|
})
|
|
585
607
|
|
|
586
|
-
test('onRequest hooks should be able to block a request (last hook)', t => {
|
|
608
|
+
test('onRequest hooks should be able to block a request (last hook)', (t, testDone) => {
|
|
587
609
|
t.plan(5)
|
|
588
610
|
const fastify = Fastify()
|
|
589
611
|
|
|
@@ -592,32 +614,33 @@ test('onRequest hooks should be able to block a request (last hook)', t => {
|
|
|
592
614
|
})
|
|
593
615
|
|
|
594
616
|
fastify.addHook('preHandler', async (req, reply) => {
|
|
595
|
-
t.fail('this should not be called')
|
|
617
|
+
t.assert.fail('this should not be called')
|
|
596
618
|
})
|
|
597
619
|
|
|
598
620
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
599
|
-
t.ok('called')
|
|
621
|
+
t.assert.ok('called')
|
|
600
622
|
})
|
|
601
623
|
|
|
602
624
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
603
|
-
t.ok('called')
|
|
625
|
+
t.assert.ok('called')
|
|
604
626
|
})
|
|
605
627
|
|
|
606
628
|
fastify.get('/', function (request, reply) {
|
|
607
|
-
t.fail('we should not be here')
|
|
629
|
+
t.assert.fail('we should not be here')
|
|
608
630
|
})
|
|
609
631
|
|
|
610
632
|
fastify.inject({
|
|
611
633
|
url: '/',
|
|
612
634
|
method: 'GET'
|
|
613
635
|
}, (err, res) => {
|
|
614
|
-
t.
|
|
615
|
-
t.
|
|
616
|
-
t.
|
|
636
|
+
t.assert.ifError(err)
|
|
637
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
638
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
639
|
+
testDone()
|
|
617
640
|
})
|
|
618
641
|
})
|
|
619
642
|
|
|
620
|
-
test('preHandler hooks should be able to block a request (last hook)', t => {
|
|
643
|
+
test('preHandler hooks should be able to block a request (last hook)', (t, testDone) => {
|
|
621
644
|
t.plan(5)
|
|
622
645
|
const fastify = Fastify()
|
|
623
646
|
|
|
@@ -626,28 +649,29 @@ test('preHandler hooks should be able to block a request (last hook)', t => {
|
|
|
626
649
|
})
|
|
627
650
|
|
|
628
651
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
629
|
-
t.
|
|
652
|
+
t.assert.strictEqual(payload, 'hello')
|
|
630
653
|
})
|
|
631
654
|
|
|
632
655
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
633
|
-
t.ok('called')
|
|
656
|
+
t.assert.ok('called')
|
|
634
657
|
})
|
|
635
658
|
|
|
636
659
|
fastify.get('/', function (request, reply) {
|
|
637
|
-
t.fail('we should not be here')
|
|
660
|
+
t.assert.fail('we should not be here')
|
|
638
661
|
})
|
|
639
662
|
|
|
640
663
|
fastify.inject({
|
|
641
664
|
url: '/',
|
|
642
665
|
method: 'GET'
|
|
643
666
|
}, (err, res) => {
|
|
644
|
-
t.
|
|
645
|
-
t.
|
|
646
|
-
t.
|
|
667
|
+
t.assert.ifError(err)
|
|
668
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
669
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
670
|
+
testDone()
|
|
647
671
|
})
|
|
648
672
|
})
|
|
649
673
|
|
|
650
|
-
test('onRequest respond with a stream', t => {
|
|
674
|
+
test('onRequest respond with a stream', (t, testDone) => {
|
|
651
675
|
t.plan(4)
|
|
652
676
|
const fastify = Fastify()
|
|
653
677
|
|
|
@@ -663,40 +687,41 @@ test('onRequest respond with a stream', t => {
|
|
|
663
687
|
})
|
|
664
688
|
|
|
665
689
|
fastify.addHook('onRequest', async (req, res) => {
|
|
666
|
-
t.fail('this should not be called')
|
|
690
|
+
t.assert.fail('this should not be called')
|
|
667
691
|
})
|
|
668
692
|
|
|
669
693
|
fastify.addHook('preHandler', async (req, reply) => {
|
|
670
|
-
t.fail('this should not be called')
|
|
694
|
+
t.assert.fail('this should not be called')
|
|
671
695
|
})
|
|
672
696
|
|
|
673
697
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
674
|
-
t.ok('called')
|
|
698
|
+
t.assert.ok('called')
|
|
675
699
|
})
|
|
676
700
|
|
|
677
701
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
678
|
-
t.ok('called')
|
|
702
|
+
t.assert.ok('called')
|
|
679
703
|
})
|
|
680
704
|
|
|
681
705
|
fastify.get('/', function (request, reply) {
|
|
682
|
-
t.fail('we should not be here')
|
|
706
|
+
t.assert.fail('we should not be here')
|
|
683
707
|
})
|
|
684
708
|
|
|
685
709
|
fastify.inject({
|
|
686
710
|
url: '/',
|
|
687
711
|
method: 'GET'
|
|
688
712
|
}, (err, res) => {
|
|
689
|
-
t.
|
|
690
|
-
t.
|
|
713
|
+
t.assert.ifError(err)
|
|
714
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
715
|
+
testDone()
|
|
691
716
|
})
|
|
692
717
|
})
|
|
693
718
|
|
|
694
|
-
test('preHandler respond with a stream', t => {
|
|
719
|
+
test('preHandler respond with a stream', (t, testDone) => {
|
|
695
720
|
t.plan(7)
|
|
696
721
|
const fastify = Fastify()
|
|
697
722
|
|
|
698
723
|
fastify.addHook('onRequest', async (req, res) => {
|
|
699
|
-
t.ok('called')
|
|
724
|
+
t.assert.ok('called')
|
|
700
725
|
})
|
|
701
726
|
|
|
702
727
|
// we are calling `reply.send` inside the `preHandler` hook with a stream,
|
|
@@ -706,92 +731,96 @@ test('preHandler respond with a stream', t => {
|
|
|
706
731
|
fastify.addHook('preHandler', async (req, reply) => {
|
|
707
732
|
const stream = fs.createReadStream(__filename, 'utf8')
|
|
708
733
|
reply.raw.once('finish', () => {
|
|
709
|
-
t.
|
|
734
|
+
t.assert.strictEqual(order.shift(), 2)
|
|
710
735
|
})
|
|
711
736
|
return reply.send(stream)
|
|
712
737
|
})
|
|
713
738
|
|
|
714
739
|
fastify.addHook('preHandler', async (req, reply) => {
|
|
715
|
-
t.fail('this should not be called')
|
|
740
|
+
t.assert.fail('this should not be called')
|
|
716
741
|
})
|
|
717
742
|
|
|
718
743
|
fastify.addHook('onSend', async (req, reply, payload) => {
|
|
719
|
-
t.
|
|
720
|
-
t.
|
|
744
|
+
t.assert.strictEqual(order.shift(), 1)
|
|
745
|
+
t.assert.strictEqual(typeof payload.pipe, 'function')
|
|
721
746
|
})
|
|
722
747
|
|
|
723
748
|
fastify.addHook('onResponse', async (request, reply) => {
|
|
724
|
-
t.ok('called')
|
|
749
|
+
t.assert.ok('called')
|
|
725
750
|
})
|
|
726
751
|
|
|
727
752
|
fastify.get('/', function (request, reply) {
|
|
728
|
-
t.fail('we should not be here')
|
|
753
|
+
t.assert.fail('we should not be here')
|
|
729
754
|
})
|
|
730
755
|
|
|
731
756
|
fastify.inject({
|
|
732
757
|
url: '/',
|
|
733
758
|
method: 'GET'
|
|
734
759
|
}, (err, res) => {
|
|
735
|
-
t.
|
|
736
|
-
t.
|
|
760
|
+
t.assert.ifError(err)
|
|
761
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
762
|
+
testDone()
|
|
737
763
|
})
|
|
738
764
|
})
|
|
739
765
|
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
t.plan(2)
|
|
766
|
+
describe('Should log a warning if is an async function with `done`', () => {
|
|
767
|
+
test('2 arguments', t => {
|
|
743
768
|
const fastify = Fastify()
|
|
744
769
|
|
|
745
770
|
try {
|
|
746
|
-
fastify.addHook('onRequestAbort', async (req, done) => {
|
|
771
|
+
fastify.addHook('onRequestAbort', async (req, done) => {
|
|
772
|
+
t.assert.fail('should have not be called')
|
|
773
|
+
})
|
|
747
774
|
} catch (e) {
|
|
748
|
-
t.
|
|
749
|
-
t.
|
|
775
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
776
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
750
777
|
}
|
|
751
778
|
})
|
|
752
779
|
|
|
753
|
-
|
|
754
|
-
t.plan(2)
|
|
780
|
+
test('3 arguments', t => {
|
|
755
781
|
const fastify = Fastify()
|
|
756
782
|
|
|
757
783
|
try {
|
|
758
|
-
fastify.addHook('onRequest', async (req, reply, done) => {
|
|
784
|
+
fastify.addHook('onRequest', async (req, reply, done) => {
|
|
785
|
+
t.assert.fail('should have not be called')
|
|
786
|
+
})
|
|
759
787
|
} catch (e) {
|
|
760
|
-
t.
|
|
761
|
-
t.
|
|
788
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
789
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
762
790
|
}
|
|
763
791
|
})
|
|
764
792
|
|
|
765
|
-
|
|
766
|
-
t.plan(6)
|
|
793
|
+
test('4 arguments', t => {
|
|
767
794
|
const fastify = Fastify()
|
|
768
795
|
|
|
769
796
|
try {
|
|
770
|
-
fastify.addHook('onSend', async (req, reply, payload, done) => {
|
|
797
|
+
fastify.addHook('onSend', async (req, reply, payload, done) => {
|
|
798
|
+
t.assert.fail('should have not be called')
|
|
799
|
+
})
|
|
771
800
|
} catch (e) {
|
|
772
|
-
t.
|
|
773
|
-
t.
|
|
801
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
802
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
774
803
|
}
|
|
775
804
|
try {
|
|
776
|
-
fastify.addHook('preSerialization', async (req, reply, payload, done) => {
|
|
805
|
+
fastify.addHook('preSerialization', async (req, reply, payload, done) => {
|
|
806
|
+
t.assert.fail('should have not be called')
|
|
807
|
+
})
|
|
777
808
|
} catch (e) {
|
|
778
|
-
t.
|
|
779
|
-
t.
|
|
809
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
810
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
780
811
|
}
|
|
781
812
|
try {
|
|
782
|
-
fastify.addHook('onError', async (req, reply, payload, done) => {
|
|
813
|
+
fastify.addHook('onError', async (req, reply, payload, done) => {
|
|
814
|
+
t.assert.fail('should have not be called')
|
|
815
|
+
})
|
|
783
816
|
} catch (e) {
|
|
784
|
-
t.
|
|
785
|
-
t.
|
|
817
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
818
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
786
819
|
}
|
|
787
820
|
})
|
|
788
|
-
|
|
789
|
-
t.end()
|
|
790
821
|
})
|
|
791
822
|
|
|
792
823
|
test('early termination, onRequest async', async t => {
|
|
793
|
-
t.plan(2)
|
|
794
|
-
|
|
795
824
|
const app = Fastify()
|
|
796
825
|
|
|
797
826
|
app.addHook('onRequest', async (req, reply) => {
|
|
@@ -800,12 +829,12 @@ test('early termination, onRequest async', async t => {
|
|
|
800
829
|
})
|
|
801
830
|
|
|
802
831
|
app.get('/', (req, reply) => {
|
|
803
|
-
t.fail('should not happen')
|
|
832
|
+
t.assert.fail('should not happen')
|
|
804
833
|
})
|
|
805
834
|
|
|
806
835
|
const res = await app.inject('/')
|
|
807
|
-
t.
|
|
808
|
-
t.
|
|
836
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
837
|
+
t.assert.strictEqual(res.body.toString(), 'hello world')
|
|
809
838
|
})
|
|
810
839
|
|
|
811
840
|
test('The this should be the same of the encapsulation level', async t => {
|
|
@@ -813,9 +842,9 @@ test('The this should be the same of the encapsulation level', async t => {
|
|
|
813
842
|
|
|
814
843
|
fastify.addHook('onRequest', async function (req, reply) {
|
|
815
844
|
if (req.raw.url === '/nested') {
|
|
816
|
-
t.
|
|
845
|
+
t.assert.strictEqual(this.foo, 'bar')
|
|
817
846
|
} else {
|
|
818
|
-
t.
|
|
847
|
+
t.assert.strictEqual(this.foo, undefined)
|
|
819
848
|
}
|
|
820
849
|
})
|
|
821
850
|
|
|
@@ -833,12 +862,13 @@ test('The this should be the same of the encapsulation level', async t => {
|
|
|
833
862
|
await fastify.inject({ method: 'GET', path: '/nested' })
|
|
834
863
|
})
|
|
835
864
|
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
t.plan(
|
|
865
|
+
describe('preSerializationEnd should handle errors if the serialize method throws', () => {
|
|
866
|
+
test('works with sync preSerialization', (t, testDone) => {
|
|
867
|
+
t.plan(3)
|
|
839
868
|
const fastify = Fastify()
|
|
840
869
|
|
|
841
870
|
fastify.addHook('preSerialization', (request, reply, payload, done) => {
|
|
871
|
+
t.assert.ok('called')
|
|
842
872
|
done(null, payload)
|
|
843
873
|
})
|
|
844
874
|
|
|
@@ -851,16 +881,18 @@ test('preSerializationEnd should handle errors if the serialize method throws',
|
|
|
851
881
|
method: 'POST',
|
|
852
882
|
url: '/'
|
|
853
883
|
}, (err, res) => {
|
|
854
|
-
t.
|
|
855
|
-
t.
|
|
884
|
+
t.assert.ifError(err)
|
|
885
|
+
t.assert.notEqual(res.statusCode, 200)
|
|
886
|
+
testDone()
|
|
856
887
|
})
|
|
857
888
|
})
|
|
858
889
|
|
|
859
|
-
|
|
860
|
-
t.plan(
|
|
890
|
+
test('works with async preSerialization', (t, testDone) => {
|
|
891
|
+
t.plan(3)
|
|
861
892
|
const fastify = Fastify()
|
|
862
893
|
|
|
863
894
|
fastify.addHook('preSerialization', async (request, reply, payload) => {
|
|
895
|
+
t.assert.ok('called')
|
|
864
896
|
return payload
|
|
865
897
|
})
|
|
866
898
|
|
|
@@ -873,16 +905,15 @@ test('preSerializationEnd should handle errors if the serialize method throws',
|
|
|
873
905
|
method: 'POST',
|
|
874
906
|
url: '/'
|
|
875
907
|
}, (err, res) => {
|
|
876
|
-
t.
|
|
877
|
-
t.
|
|
908
|
+
t.assert.ifError(err)
|
|
909
|
+
t.assert.notEqual(res.statusCode, 200)
|
|
910
|
+
testDone()
|
|
878
911
|
})
|
|
879
912
|
})
|
|
880
|
-
|
|
881
|
-
t.end()
|
|
882
913
|
})
|
|
883
914
|
|
|
884
|
-
|
|
885
|
-
t.plan(
|
|
915
|
+
test('nested hooks to do not crash on 404', (t, testDone) => {
|
|
916
|
+
t.plan(3)
|
|
886
917
|
const fastify = Fastify()
|
|
887
918
|
|
|
888
919
|
fastify.get('/hello', (req, reply) => {
|
|
@@ -895,22 +926,26 @@ t.test('nested hooks to do not crash on 404', t => {
|
|
|
895
926
|
})
|
|
896
927
|
|
|
897
928
|
fastify.setNotFoundHandler(async (request, reply) => {
|
|
929
|
+
t.assert.ok('called')
|
|
898
930
|
reply.statusCode = 404
|
|
899
931
|
return { status: 'nested-not-found' }
|
|
900
932
|
})
|
|
901
933
|
|
|
902
934
|
fastify.setErrorHandler(async (error, request, reply) => {
|
|
935
|
+
t.assert.fail('should have not be called')
|
|
903
936
|
reply.statusCode = 500
|
|
904
937
|
return { status: 'nested-error', error }
|
|
905
938
|
})
|
|
906
939
|
}, { prefix: '/nested' })
|
|
907
940
|
|
|
908
941
|
fastify.setNotFoundHandler(async (request, reply) => {
|
|
942
|
+
t.assert.fail('should have not be called')
|
|
909
943
|
reply.statusCode = 404
|
|
910
944
|
return { status: 'not-found' }
|
|
911
945
|
})
|
|
912
946
|
|
|
913
947
|
fastify.setErrorHandler(async (error, request, reply) => {
|
|
948
|
+
t.assert.fail('should have not be called')
|
|
914
949
|
reply.statusCode = 500
|
|
915
950
|
return { status: 'error', error }
|
|
916
951
|
})
|
|
@@ -919,13 +954,13 @@ t.test('nested hooks to do not crash on 404', t => {
|
|
|
919
954
|
method: 'GET',
|
|
920
955
|
url: '/nested/something'
|
|
921
956
|
}, (err, res) => {
|
|
922
|
-
t.
|
|
923
|
-
t.
|
|
957
|
+
t.assert.ifError(err)
|
|
958
|
+
t.assert.strictEqual(res.statusCode, 404)
|
|
959
|
+
testDone()
|
|
924
960
|
})
|
|
925
961
|
})
|
|
926
962
|
|
|
927
963
|
test('Register an hook (preHandler) as route option should fail if mixing async and callback style', t => {
|
|
928
|
-
t.plan(2)
|
|
929
964
|
const fastify = Fastify()
|
|
930
965
|
|
|
931
966
|
try {
|
|
@@ -942,15 +977,14 @@ test('Register an hook (preHandler) as route option should fail if mixing async
|
|
|
942
977
|
return { hello: 'world' }
|
|
943
978
|
}
|
|
944
979
|
)
|
|
945
|
-
t.fail('preHandler mixing async and callback style')
|
|
980
|
+
t.assert.fail('preHandler mixing async and callback style')
|
|
946
981
|
} catch (e) {
|
|
947
|
-
t.
|
|
948
|
-
t.
|
|
982
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
983
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
949
984
|
}
|
|
950
985
|
})
|
|
951
986
|
|
|
952
987
|
test('Register an hook (onSend) as route option should fail if mixing async and callback style', t => {
|
|
953
|
-
t.plan(2)
|
|
954
988
|
const fastify = Fastify()
|
|
955
989
|
|
|
956
990
|
try {
|
|
@@ -967,15 +1001,14 @@ test('Register an hook (onSend) as route option should fail if mixing async and
|
|
|
967
1001
|
return { hello: 'world' }
|
|
968
1002
|
}
|
|
969
1003
|
)
|
|
970
|
-
t.fail('onSend mixing async and callback style')
|
|
1004
|
+
t.assert.fail('onSend mixing async and callback style')
|
|
971
1005
|
} catch (e) {
|
|
972
|
-
t.
|
|
973
|
-
t.
|
|
1006
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
1007
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
974
1008
|
}
|
|
975
1009
|
})
|
|
976
1010
|
|
|
977
1011
|
test('Register an hook (preSerialization) as route option should fail if mixing async and callback style', t => {
|
|
978
|
-
t.plan(2)
|
|
979
1012
|
const fastify = Fastify()
|
|
980
1013
|
|
|
981
1014
|
try {
|
|
@@ -992,15 +1025,14 @@ test('Register an hook (preSerialization) as route option should fail if mixing
|
|
|
992
1025
|
return { hello: 'world' }
|
|
993
1026
|
}
|
|
994
1027
|
)
|
|
995
|
-
t.fail('preSerialization mixing async and callback style')
|
|
1028
|
+
t.assert.fail('preSerialization mixing async and callback style')
|
|
996
1029
|
} catch (e) {
|
|
997
|
-
t.
|
|
998
|
-
t.
|
|
1030
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
1031
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
999
1032
|
}
|
|
1000
1033
|
})
|
|
1001
1034
|
|
|
1002
1035
|
test('Register an hook (onError) as route option should fail if mixing async and callback style', t => {
|
|
1003
|
-
t.plan(2)
|
|
1004
1036
|
const fastify = Fastify()
|
|
1005
1037
|
|
|
1006
1038
|
try {
|
|
@@ -1017,15 +1049,14 @@ test('Register an hook (onError) as route option should fail if mixing async and
|
|
|
1017
1049
|
return { hello: 'world' }
|
|
1018
1050
|
}
|
|
1019
1051
|
)
|
|
1020
|
-
t.fail('onError mixing async and callback style')
|
|
1052
|
+
t.assert.fail('onError mixing async and callback style')
|
|
1021
1053
|
} catch (e) {
|
|
1022
|
-
t.
|
|
1023
|
-
t.
|
|
1054
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
1055
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
1024
1056
|
}
|
|
1025
1057
|
})
|
|
1026
1058
|
|
|
1027
1059
|
test('Register an hook (preParsing) as route option should fail if mixing async and callback style', t => {
|
|
1028
|
-
t.plan(2)
|
|
1029
1060
|
const fastify = Fastify()
|
|
1030
1061
|
|
|
1031
1062
|
try {
|
|
@@ -1042,15 +1073,14 @@ test('Register an hook (preParsing) as route option should fail if mixing async
|
|
|
1042
1073
|
return { hello: 'world' }
|
|
1043
1074
|
}
|
|
1044
1075
|
)
|
|
1045
|
-
t.fail('preParsing mixing async and callback style')
|
|
1076
|
+
t.assert.fail('preParsing mixing async and callback style')
|
|
1046
1077
|
} catch (e) {
|
|
1047
|
-
t.
|
|
1048
|
-
t.
|
|
1078
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
1079
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
1049
1080
|
}
|
|
1050
1081
|
})
|
|
1051
1082
|
|
|
1052
|
-
test('Register an hook (onRequestAbort) as route option should fail if mixing async and callback style', t => {
|
|
1053
|
-
t.plan(2)
|
|
1083
|
+
test('Register an hook (onRequestAbort) as route option should fail if mixing async and callback style', (t) => {
|
|
1054
1084
|
const fastify = Fastify()
|
|
1055
1085
|
|
|
1056
1086
|
try {
|
|
@@ -1067,9 +1097,9 @@ test('Register an hook (onRequestAbort) as route option should fail if mixing as
|
|
|
1067
1097
|
return { hello: 'world' }
|
|
1068
1098
|
}
|
|
1069
1099
|
)
|
|
1070
|
-
t.fail('onRequestAbort mixing async and callback style')
|
|
1100
|
+
t.assert.fail('onRequestAbort mixing async and callback style')
|
|
1071
1101
|
} catch (e) {
|
|
1072
|
-
t.
|
|
1073
|
-
t.
|
|
1102
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
|
|
1103
|
+
t.assert.strictEqual(e.message, 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
|
|
1074
1104
|
}
|
|
1075
1105
|
})
|