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.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const sget = require('simple-get').concat
|
|
6
5
|
const stream = require('node:stream')
|
|
7
6
|
const Fastify = require('..')
|
|
@@ -13,88 +12,89 @@ const payload = { hello: 'world' }
|
|
|
13
12
|
const proxyquire = require('proxyquire')
|
|
14
13
|
const { connect } = require('node:net')
|
|
15
14
|
const { sleep, getServerUrl } = require('./helper')
|
|
15
|
+
const { waitForCb } = require('./toolkit.js')
|
|
16
16
|
|
|
17
17
|
process.removeAllListeners('warning')
|
|
18
18
|
|
|
19
|
-
test('hooks', t => {
|
|
19
|
+
test('hooks', (t, testDone) => {
|
|
20
20
|
t.plan(49)
|
|
21
21
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
22
22
|
|
|
23
23
|
try {
|
|
24
24
|
fastify.addHook('preHandler', function (request, reply, done) {
|
|
25
|
-
t.
|
|
26
|
-
t.
|
|
25
|
+
t.assert.strictEqual(request.test, 'the request is coming')
|
|
26
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
27
27
|
if (request.raw.method === 'HEAD') {
|
|
28
28
|
done(new Error('some error'))
|
|
29
29
|
} else {
|
|
30
30
|
done()
|
|
31
31
|
}
|
|
32
32
|
})
|
|
33
|
-
t.pass
|
|
33
|
+
t.assert.ok('should pass')
|
|
34
34
|
} catch (e) {
|
|
35
|
-
t.fail()
|
|
35
|
+
t.assert.fail()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
try {
|
|
39
39
|
fastify.addHook('preHandler', null)
|
|
40
40
|
} catch (e) {
|
|
41
|
-
t.
|
|
42
|
-
t.
|
|
43
|
-
t.pass
|
|
41
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_HANDLER')
|
|
42
|
+
t.assert.strictEqual(e.message, 'preHandler hook should be a function, instead got null')
|
|
43
|
+
t.assert.ok('should pass')
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
try {
|
|
47
47
|
fastify.addHook('preParsing')
|
|
48
48
|
} catch (e) {
|
|
49
|
-
t.
|
|
50
|
-
t.
|
|
51
|
-
t.pass
|
|
49
|
+
t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_HANDLER')
|
|
50
|
+
t.assert.strictEqual(e.message, 'preParsing hook should be a function, instead got undefined')
|
|
51
|
+
t.assert.ok('should pass')
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
try {
|
|
55
55
|
fastify.addHook('preParsing', function (request, reply, payload, done) {
|
|
56
56
|
request.preParsing = true
|
|
57
|
-
t.
|
|
58
|
-
t.
|
|
57
|
+
t.assert.strictEqual(request.test, 'the request is coming')
|
|
58
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
59
59
|
done()
|
|
60
60
|
})
|
|
61
|
-
t.pass
|
|
61
|
+
t.assert.ok('should pass')
|
|
62
62
|
} catch (e) {
|
|
63
|
-
t.fail()
|
|
63
|
+
t.assert.fail()
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
try {
|
|
67
67
|
fastify.addHook('preParsing', function (request, reply, payload, done) {
|
|
68
68
|
request.preParsing = true
|
|
69
|
-
t.
|
|
70
|
-
t.
|
|
69
|
+
t.assert.strictEqual(request.test, 'the request is coming')
|
|
70
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
71
71
|
done()
|
|
72
72
|
})
|
|
73
|
-
t.pass
|
|
73
|
+
t.assert.ok('should pass')
|
|
74
74
|
} catch (e) {
|
|
75
|
-
t.fail()
|
|
75
|
+
t.assert.fail()
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
try {
|
|
79
79
|
fastify.addHook('preValidation', function (request, reply, done) {
|
|
80
|
-
t.
|
|
81
|
-
t.
|
|
82
|
-
t.
|
|
80
|
+
t.assert.strictEqual(request.preParsing, true)
|
|
81
|
+
t.assert.strictEqual(request.test, 'the request is coming')
|
|
82
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
83
83
|
done()
|
|
84
84
|
})
|
|
85
|
-
t.pass
|
|
85
|
+
t.assert.ok('should pass')
|
|
86
86
|
} catch (e) {
|
|
87
|
-
t.fail()
|
|
87
|
+
t.assert.fail()
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
try {
|
|
91
91
|
fastify.addHook('preSerialization', function (request, reply, payload, done) {
|
|
92
|
-
t.ok('preSerialization called')
|
|
92
|
+
t.assert.ok('preSerialization called')
|
|
93
93
|
done()
|
|
94
94
|
})
|
|
95
|
-
t.pass
|
|
95
|
+
t.assert.ok('should pass')
|
|
96
96
|
} catch (e) {
|
|
97
|
-
t.fail()
|
|
97
|
+
t.assert.fail()
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
try {
|
|
@@ -107,18 +107,18 @@ test('hooks', t => {
|
|
|
107
107
|
done()
|
|
108
108
|
}
|
|
109
109
|
})
|
|
110
|
-
t.pass
|
|
110
|
+
t.assert.ok('should pass')
|
|
111
111
|
} catch (e) {
|
|
112
|
-
t.fail()
|
|
112
|
+
t.assert.fail()
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
fastify.addHook('onResponse', function (request, reply, done) {
|
|
116
|
-
t.ok('onResponse called')
|
|
116
|
+
t.assert.ok('onResponse called')
|
|
117
117
|
done()
|
|
118
118
|
})
|
|
119
119
|
|
|
120
120
|
fastify.addHook('onSend', function (req, reply, thePayload, done) {
|
|
121
|
-
t.ok('onSend called')
|
|
121
|
+
t.assert.ok('onSend called')
|
|
122
122
|
done()
|
|
123
123
|
})
|
|
124
124
|
|
|
@@ -126,12 +126,12 @@ test('hooks', t => {
|
|
|
126
126
|
method: 'GET',
|
|
127
127
|
url: '/',
|
|
128
128
|
handler: function (req, reply) {
|
|
129
|
-
t.
|
|
130
|
-
t.
|
|
129
|
+
t.assert.strictEqual(req.test, 'the request is coming')
|
|
130
|
+
t.assert.strictEqual(reply.test, 'the reply has come')
|
|
131
131
|
reply.code(200).send(payload)
|
|
132
132
|
},
|
|
133
133
|
onResponse: function (req, reply, done) {
|
|
134
|
-
t.ok('onResponse inside hook')
|
|
134
|
+
t.assert.ok('onResponse inside hook')
|
|
135
135
|
},
|
|
136
136
|
response: {
|
|
137
137
|
200: {
|
|
@@ -149,44 +149,47 @@ test('hooks', t => {
|
|
|
149
149
|
})
|
|
150
150
|
|
|
151
151
|
fastify.listen({ port: 0 }, err => {
|
|
152
|
-
t.
|
|
153
|
-
t.
|
|
152
|
+
t.assert.ifError(err)
|
|
153
|
+
t.after(() => { fastify.close() })
|
|
154
154
|
|
|
155
|
+
const completion = waitForCb({ steps: 3 })
|
|
155
156
|
sget({
|
|
156
157
|
method: 'GET',
|
|
157
158
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
158
159
|
}, (err, response, body) => {
|
|
159
|
-
t.
|
|
160
|
-
t.
|
|
161
|
-
t.
|
|
162
|
-
t.
|
|
160
|
+
t.assert.ifError(err)
|
|
161
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
162
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
163
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
164
|
+
completion.stepIn()
|
|
163
165
|
})
|
|
164
|
-
|
|
165
166
|
sget({
|
|
166
167
|
method: 'HEAD',
|
|
167
168
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
168
169
|
}, (err, response, body) => {
|
|
169
|
-
t.
|
|
170
|
-
t.
|
|
170
|
+
t.assert.ifError(err)
|
|
171
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
172
|
+
completion.stepIn()
|
|
171
173
|
})
|
|
172
|
-
|
|
173
174
|
sget({
|
|
174
175
|
method: 'DELETE',
|
|
175
176
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
176
177
|
}, (err, response, body) => {
|
|
177
|
-
t.
|
|
178
|
-
t.
|
|
178
|
+
t.assert.ifError(err)
|
|
179
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
180
|
+
completion.stepIn()
|
|
179
181
|
})
|
|
182
|
+
completion.patience.then(testDone)
|
|
180
183
|
})
|
|
181
184
|
})
|
|
182
185
|
|
|
183
|
-
test('onRequest hook should support encapsulation / 1', t => {
|
|
186
|
+
test('onRequest hook should support encapsulation / 1', (t, testDone) => {
|
|
184
187
|
t.plan(5)
|
|
185
188
|
const fastify = Fastify()
|
|
186
189
|
|
|
187
190
|
fastify.register((instance, opts, done) => {
|
|
188
191
|
instance.addHook('onRequest', (req, reply, done) => {
|
|
189
|
-
t.
|
|
192
|
+
t.assert.strictEqual(req.raw.url, '/plugin')
|
|
190
193
|
done()
|
|
191
194
|
})
|
|
192
195
|
|
|
@@ -202,17 +205,18 @@ test('onRequest hook should support encapsulation / 1', t => {
|
|
|
202
205
|
})
|
|
203
206
|
|
|
204
207
|
fastify.inject('/root', (err, res) => {
|
|
205
|
-
t.
|
|
206
|
-
t.
|
|
207
|
-
})
|
|
208
|
+
t.assert.ifError(err)
|
|
209
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
208
210
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
211
|
+
fastify.inject('/plugin', (err, res) => {
|
|
212
|
+
t.assert.ifError(err)
|
|
213
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
214
|
+
testDone()
|
|
215
|
+
})
|
|
212
216
|
})
|
|
213
217
|
})
|
|
214
218
|
|
|
215
|
-
test('onRequest hook should support encapsulation / 2', (t) => {
|
|
219
|
+
test('onRequest hook should support encapsulation / 2', (t, testDone) => {
|
|
216
220
|
t.plan(3)
|
|
217
221
|
const fastify = Fastify()
|
|
218
222
|
let pluginInstance
|
|
@@ -226,20 +230,21 @@ test('onRequest hook should support encapsulation / 2', (t) => {
|
|
|
226
230
|
})
|
|
227
231
|
|
|
228
232
|
fastify.ready(err => {
|
|
229
|
-
t.
|
|
230
|
-
t.
|
|
231
|
-
t.
|
|
233
|
+
t.assert.ifError(err)
|
|
234
|
+
t.assert.strictEqual(fastify[symbols.kHooks].onRequest.length, 1)
|
|
235
|
+
t.assert.strictEqual(pluginInstance[symbols.kHooks].onRequest.length, 2)
|
|
236
|
+
testDone()
|
|
232
237
|
})
|
|
233
238
|
})
|
|
234
239
|
|
|
235
|
-
test('onRequest hook should support encapsulation / 3', t => {
|
|
240
|
+
test('onRequest hook should support encapsulation / 3', (t, testDone) => {
|
|
236
241
|
t.plan(20)
|
|
237
242
|
const fastify = Fastify()
|
|
238
243
|
fastify.decorate('hello', 'world')
|
|
239
244
|
|
|
240
245
|
fastify.addHook('onRequest', function (req, reply, done) {
|
|
241
|
-
t.ok(this.hello)
|
|
242
|
-
t.ok(this.hello2)
|
|
246
|
+
t.assert.ok(this.hello)
|
|
247
|
+
t.assert.ok(this.hello2)
|
|
243
248
|
req.first = true
|
|
244
249
|
done()
|
|
245
250
|
})
|
|
@@ -247,24 +252,24 @@ test('onRequest hook should support encapsulation / 3', t => {
|
|
|
247
252
|
fastify.decorate('hello2', 'world')
|
|
248
253
|
|
|
249
254
|
fastify.get('/first', (req, reply) => {
|
|
250
|
-
t.ok(req.first)
|
|
251
|
-
t.
|
|
255
|
+
t.assert.ok(req.first)
|
|
256
|
+
t.assert.ok(!req.second)
|
|
252
257
|
reply.send({ hello: 'world' })
|
|
253
258
|
})
|
|
254
259
|
|
|
255
260
|
fastify.register((instance, opts, done) => {
|
|
256
261
|
instance.decorate('hello3', 'world')
|
|
257
262
|
instance.addHook('onRequest', function (req, reply, done) {
|
|
258
|
-
t.ok(this.hello)
|
|
259
|
-
t.ok(this.hello2)
|
|
260
|
-
t.ok(this.hello3)
|
|
263
|
+
t.assert.ok(this.hello)
|
|
264
|
+
t.assert.ok(this.hello2)
|
|
265
|
+
t.assert.ok(this.hello3)
|
|
261
266
|
req.second = true
|
|
262
267
|
done()
|
|
263
268
|
})
|
|
264
269
|
|
|
265
270
|
instance.get('/second', (req, reply) => {
|
|
266
|
-
t.ok(req.first)
|
|
267
|
-
t.ok(req.second)
|
|
271
|
+
t.assert.ok(req.first)
|
|
272
|
+
t.assert.ok(req.second)
|
|
268
273
|
reply.send({ hello: 'world' })
|
|
269
274
|
})
|
|
270
275
|
|
|
@@ -272,61 +277,64 @@ test('onRequest hook should support encapsulation / 3', t => {
|
|
|
272
277
|
})
|
|
273
278
|
|
|
274
279
|
fastify.listen({ port: 0 }, err => {
|
|
275
|
-
t.
|
|
276
|
-
t.
|
|
280
|
+
t.assert.ifError(err)
|
|
281
|
+
t.after(() => { fastify.close() })
|
|
277
282
|
|
|
283
|
+
const completion = waitForCb({ steps: 2 })
|
|
278
284
|
sget({
|
|
279
285
|
method: 'GET',
|
|
280
286
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
281
287
|
}, (err, response, body) => {
|
|
282
|
-
t.
|
|
283
|
-
t.
|
|
284
|
-
t.
|
|
285
|
-
t.
|
|
288
|
+
t.assert.ifError(err)
|
|
289
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
290
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
291
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
292
|
+
completion.stepIn()
|
|
286
293
|
})
|
|
287
|
-
|
|
288
294
|
sget({
|
|
289
295
|
method: 'GET',
|
|
290
296
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
291
297
|
}, (err, response, body) => {
|
|
292
|
-
t.
|
|
293
|
-
t.
|
|
294
|
-
t.
|
|
295
|
-
t.
|
|
298
|
+
t.assert.ifError(err)
|
|
299
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
300
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
301
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
302
|
+
completion.stepIn()
|
|
296
303
|
})
|
|
304
|
+
completion.patience.then(testDone)
|
|
297
305
|
})
|
|
298
306
|
})
|
|
299
307
|
|
|
300
|
-
test('preHandler hook should support encapsulation / 5', t => {
|
|
308
|
+
test('preHandler hook should support encapsulation / 5', (t, testDone) => {
|
|
301
309
|
t.plan(17)
|
|
302
310
|
const fastify = Fastify()
|
|
303
|
-
t.
|
|
311
|
+
t.after(() => { fastify.close() })
|
|
304
312
|
fastify.decorate('hello', 'world')
|
|
305
313
|
|
|
306
314
|
fastify.addHook('preHandler', function (req, res, done) {
|
|
307
|
-
t.ok(this.hello)
|
|
315
|
+
t.assert.ok(this.hello)
|
|
308
316
|
req.first = true
|
|
309
317
|
done()
|
|
310
318
|
})
|
|
311
319
|
|
|
312
320
|
fastify.get('/first', (req, reply) => {
|
|
313
|
-
t.ok(req.first)
|
|
314
|
-
t.
|
|
321
|
+
t.assert.ok(req.first)
|
|
322
|
+
t.assert.ok(!req.second)
|
|
315
323
|
reply.send({ hello: 'world' })
|
|
316
324
|
})
|
|
317
325
|
|
|
318
326
|
fastify.register((instance, opts, done) => {
|
|
319
327
|
instance.decorate('hello2', 'world')
|
|
320
328
|
instance.addHook('preHandler', function (req, res, done) {
|
|
321
|
-
t.ok(this.hello)
|
|
322
|
-
t.ok(this.hello2)
|
|
329
|
+
t.assert.ok(this.hello)
|
|
330
|
+
t.assert.ok(this.hello2)
|
|
323
331
|
req.second = true
|
|
324
332
|
done()
|
|
325
333
|
})
|
|
326
334
|
|
|
327
335
|
instance.get('/second', (req, reply) => {
|
|
328
|
-
t.ok(req.first)
|
|
329
|
-
t.ok(req.second)
|
|
336
|
+
t.assert.ok(req.first)
|
|
337
|
+
t.assert.ok(req.second)
|
|
330
338
|
reply.send({ hello: 'world' })
|
|
331
339
|
})
|
|
332
340
|
|
|
@@ -334,37 +342,40 @@ test('preHandler hook should support encapsulation / 5', t => {
|
|
|
334
342
|
})
|
|
335
343
|
|
|
336
344
|
fastify.listen({ port: 0 }, err => {
|
|
337
|
-
t.
|
|
345
|
+
t.assert.ifError(err)
|
|
338
346
|
|
|
347
|
+
const completion = waitForCb({ steps: 2 })
|
|
339
348
|
sget({
|
|
340
349
|
method: 'GET',
|
|
341
350
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
342
351
|
}, (err, response, body) => {
|
|
343
|
-
t.
|
|
344
|
-
t.
|
|
345
|
-
t.
|
|
346
|
-
t.
|
|
352
|
+
t.assert.ifError(err)
|
|
353
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
354
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
355
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
356
|
+
completion.stepIn()
|
|
347
357
|
})
|
|
348
|
-
|
|
349
358
|
sget({
|
|
350
359
|
method: 'GET',
|
|
351
360
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
352
361
|
}, (err, response, body) => {
|
|
353
|
-
t.
|
|
354
|
-
t.
|
|
355
|
-
t.
|
|
356
|
-
t.
|
|
362
|
+
t.assert.ifError(err)
|
|
363
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
364
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
365
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
366
|
+
completion.stepIn()
|
|
357
367
|
})
|
|
368
|
+
completion.patience.then(testDone)
|
|
358
369
|
})
|
|
359
370
|
})
|
|
360
371
|
|
|
361
|
-
test('onRoute hook should be called / 1', t => {
|
|
372
|
+
test('onRoute hook should be called / 1', (t, testDone) => {
|
|
362
373
|
t.plan(2)
|
|
363
374
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
364
375
|
|
|
365
376
|
fastify.register((instance, opts, done) => {
|
|
366
377
|
instance.addHook('onRoute', () => {
|
|
367
|
-
t.pass
|
|
378
|
+
t.assert.ok('should pass')
|
|
368
379
|
})
|
|
369
380
|
instance.get('/', opts, function (req, reply) {
|
|
370
381
|
reply.send()
|
|
@@ -373,23 +384,24 @@ test('onRoute hook should be called / 1', t => {
|
|
|
373
384
|
})
|
|
374
385
|
|
|
375
386
|
fastify.ready(err => {
|
|
376
|
-
t.
|
|
387
|
+
t.assert.ifError(err)
|
|
388
|
+
testDone()
|
|
377
389
|
})
|
|
378
390
|
})
|
|
379
391
|
|
|
380
|
-
test('onRoute hook should be called / 2', t => {
|
|
392
|
+
test('onRoute hook should be called / 2', (t, testDone) => {
|
|
381
393
|
t.plan(5)
|
|
382
394
|
let firstHandler = 0
|
|
383
395
|
let secondHandler = 0
|
|
384
396
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
385
397
|
fastify.addHook('onRoute', (route) => {
|
|
386
|
-
t.pass
|
|
398
|
+
t.assert.ok('should pass')
|
|
387
399
|
firstHandler++
|
|
388
400
|
})
|
|
389
401
|
|
|
390
402
|
fastify.register((instance, opts, done) => {
|
|
391
403
|
instance.addHook('onRoute', (route) => {
|
|
392
|
-
t.pass
|
|
404
|
+
t.assert.ok('should pass')
|
|
393
405
|
secondHandler++
|
|
394
406
|
})
|
|
395
407
|
instance.get('/', opts, function (req, reply) {
|
|
@@ -398,16 +410,17 @@ test('onRoute hook should be called / 2', t => {
|
|
|
398
410
|
done()
|
|
399
411
|
})
|
|
400
412
|
.after(() => {
|
|
401
|
-
t.
|
|
402
|
-
t.
|
|
413
|
+
t.assert.strictEqual(firstHandler, 1)
|
|
414
|
+
t.assert.strictEqual(secondHandler, 1)
|
|
403
415
|
})
|
|
404
416
|
|
|
405
417
|
fastify.ready(err => {
|
|
406
|
-
t.
|
|
418
|
+
t.assert.ifError(err)
|
|
419
|
+
testDone()
|
|
407
420
|
})
|
|
408
421
|
})
|
|
409
422
|
|
|
410
|
-
test('onRoute hook should be called / 3', t => {
|
|
423
|
+
test('onRoute hook should be called / 3', (t, testDone) => {
|
|
411
424
|
t.plan(5)
|
|
412
425
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
413
426
|
|
|
@@ -416,18 +429,18 @@ test('onRoute hook should be called / 3', t => {
|
|
|
416
429
|
}
|
|
417
430
|
|
|
418
431
|
fastify.addHook('onRoute', (route) => {
|
|
419
|
-
t.pass
|
|
432
|
+
t.assert.ok('should pass')
|
|
420
433
|
})
|
|
421
434
|
|
|
422
435
|
fastify.register((instance, opts, done) => {
|
|
423
436
|
instance.addHook('onRoute', (route) => {
|
|
424
|
-
t.pass
|
|
437
|
+
t.assert.ok('should pass')
|
|
425
438
|
})
|
|
426
439
|
instance.get('/a', handler)
|
|
427
440
|
done()
|
|
428
441
|
})
|
|
429
442
|
.after((err, done) => {
|
|
430
|
-
t.
|
|
443
|
+
t.assert.ifError(err)
|
|
431
444
|
setTimeout(() => {
|
|
432
445
|
fastify.get('/b', handler)
|
|
433
446
|
done()
|
|
@@ -435,21 +448,22 @@ test('onRoute hook should be called / 3', t => {
|
|
|
435
448
|
})
|
|
436
449
|
|
|
437
450
|
fastify.ready(err => {
|
|
438
|
-
t.
|
|
451
|
+
t.assert.ifError(err)
|
|
452
|
+
testDone()
|
|
439
453
|
})
|
|
440
454
|
})
|
|
441
455
|
|
|
442
|
-
test('onRoute hook should be called (encapsulation support) / 4', t => {
|
|
456
|
+
test('onRoute hook should be called (encapsulation support) / 4', (t, testDone) => {
|
|
443
457
|
t.plan(4)
|
|
444
458
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
445
459
|
|
|
446
460
|
fastify.addHook('onRoute', () => {
|
|
447
|
-
t.pass
|
|
461
|
+
t.assert.ok('should pass')
|
|
448
462
|
})
|
|
449
463
|
|
|
450
464
|
fastify.register((instance, opts, done) => {
|
|
451
465
|
instance.addHook('onRoute', () => {
|
|
452
|
-
t.pass
|
|
466
|
+
t.assert.ok('should pass')
|
|
453
467
|
})
|
|
454
468
|
instance.get('/nested', opts, function (req, reply) {
|
|
455
469
|
reply.send()
|
|
@@ -462,11 +476,12 @@ test('onRoute hook should be called (encapsulation support) / 4', t => {
|
|
|
462
476
|
})
|
|
463
477
|
|
|
464
478
|
fastify.ready(err => {
|
|
465
|
-
t.
|
|
479
|
+
t.assert.ifError(err)
|
|
480
|
+
testDone()
|
|
466
481
|
})
|
|
467
482
|
})
|
|
468
483
|
|
|
469
|
-
test('onRoute hook should be called (encapsulation support) / 5', t => {
|
|
484
|
+
test('onRoute hook should be called (encapsulation support) / 5', (t, testDone) => {
|
|
470
485
|
t.plan(2)
|
|
471
486
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
472
487
|
|
|
@@ -476,7 +491,7 @@ test('onRoute hook should be called (encapsulation support) / 5', t => {
|
|
|
476
491
|
|
|
477
492
|
fastify.register((instance, opts, done) => {
|
|
478
493
|
instance.addHook('onRoute', () => {
|
|
479
|
-
t.pass
|
|
494
|
+
t.assert.ok('should pass')
|
|
480
495
|
})
|
|
481
496
|
instance.get('/nested', opts, function (req, reply) {
|
|
482
497
|
reply.send()
|
|
@@ -489,11 +504,12 @@ test('onRoute hook should be called (encapsulation support) / 5', t => {
|
|
|
489
504
|
})
|
|
490
505
|
|
|
491
506
|
fastify.ready(err => {
|
|
492
|
-
t.
|
|
507
|
+
t.assert.ifError(err)
|
|
508
|
+
testDone()
|
|
493
509
|
})
|
|
494
510
|
})
|
|
495
511
|
|
|
496
|
-
test('onRoute hook should be called (encapsulation support) / 6', t => {
|
|
512
|
+
test('onRoute hook should be called (encapsulation support) / 6', (t, testDone) => {
|
|
497
513
|
t.plan(1)
|
|
498
514
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
499
515
|
|
|
@@ -502,25 +518,26 @@ test('onRoute hook should be called (encapsulation support) / 6', t => {
|
|
|
502
518
|
})
|
|
503
519
|
|
|
504
520
|
fastify.addHook('onRoute', () => {
|
|
505
|
-
t.fail('This should not be called')
|
|
521
|
+
t.assert.fail('This should not be called')
|
|
506
522
|
})
|
|
507
523
|
|
|
508
524
|
fastify.ready(err => {
|
|
509
|
-
t.
|
|
525
|
+
t.assert.ifError(err)
|
|
526
|
+
testDone()
|
|
510
527
|
})
|
|
511
528
|
})
|
|
512
529
|
|
|
513
|
-
test('onRoute should keep the context', t => {
|
|
530
|
+
test('onRoute should keep the context', (t, testDone) => {
|
|
514
531
|
t.plan(4)
|
|
515
532
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
516
533
|
fastify.register((instance, opts, done) => {
|
|
517
534
|
instance.decorate('test', true)
|
|
518
535
|
instance.addHook('onRoute', onRoute)
|
|
519
|
-
t.ok(instance.prototype === fastify.prototype)
|
|
536
|
+
t.assert.ok(instance.prototype === fastify.prototype)
|
|
520
537
|
|
|
521
538
|
function onRoute (route) {
|
|
522
|
-
t.ok(this.test)
|
|
523
|
-
t.
|
|
539
|
+
t.assert.ok(this.test)
|
|
540
|
+
t.assert.strictEqual(this, instance)
|
|
524
541
|
}
|
|
525
542
|
|
|
526
543
|
instance.get('/', opts, function (req, reply) {
|
|
@@ -531,26 +548,27 @@ test('onRoute should keep the context', t => {
|
|
|
531
548
|
})
|
|
532
549
|
|
|
533
550
|
fastify.close((err) => {
|
|
534
|
-
t.
|
|
551
|
+
t.assert.ifError(err)
|
|
552
|
+
testDone()
|
|
535
553
|
})
|
|
536
554
|
})
|
|
537
555
|
|
|
538
|
-
test('onRoute hook should pass correct route', t => {
|
|
556
|
+
test('onRoute hook should pass correct route', (t, testDone) => {
|
|
539
557
|
t.plan(9)
|
|
540
558
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
541
559
|
fastify.addHook('onRoute', (route) => {
|
|
542
|
-
t.
|
|
543
|
-
t.
|
|
544
|
-
t.
|
|
545
|
-
t.
|
|
560
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
561
|
+
t.assert.strictEqual(route.url, '/')
|
|
562
|
+
t.assert.strictEqual(route.path, '/')
|
|
563
|
+
t.assert.strictEqual(route.routePath, '/')
|
|
546
564
|
})
|
|
547
565
|
|
|
548
566
|
fastify.register((instance, opts, done) => {
|
|
549
567
|
instance.addHook('onRoute', (route) => {
|
|
550
|
-
t.
|
|
551
|
-
t.
|
|
552
|
-
t.
|
|
553
|
-
t.
|
|
568
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
569
|
+
t.assert.strictEqual(route.url, '/')
|
|
570
|
+
t.assert.strictEqual(route.path, '/')
|
|
571
|
+
t.assert.strictEqual(route.routePath, '/')
|
|
554
572
|
})
|
|
555
573
|
instance.get('/', opts, function (req, reply) {
|
|
556
574
|
reply.send()
|
|
@@ -559,28 +577,29 @@ test('onRoute hook should pass correct route', t => {
|
|
|
559
577
|
})
|
|
560
578
|
|
|
561
579
|
fastify.ready(err => {
|
|
562
|
-
t.
|
|
580
|
+
t.assert.ifError(err)
|
|
581
|
+
testDone()
|
|
563
582
|
})
|
|
564
583
|
})
|
|
565
584
|
|
|
566
|
-
test('onRoute hook should pass correct route with custom prefix', t => {
|
|
585
|
+
test('onRoute hook should pass correct route with custom prefix', (t, testDone) => {
|
|
567
586
|
t.plan(11)
|
|
568
587
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
569
588
|
fastify.addHook('onRoute', function (route) {
|
|
570
|
-
t.
|
|
571
|
-
t.
|
|
572
|
-
t.
|
|
573
|
-
t.
|
|
574
|
-
t.
|
|
589
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
590
|
+
t.assert.strictEqual(route.url, '/v1/foo')
|
|
591
|
+
t.assert.strictEqual(route.path, '/v1/foo')
|
|
592
|
+
t.assert.strictEqual(route.routePath, '/foo')
|
|
593
|
+
t.assert.strictEqual(route.prefix, '/v1')
|
|
575
594
|
})
|
|
576
595
|
|
|
577
596
|
fastify.register((instance, opts, done) => {
|
|
578
597
|
instance.addHook('onRoute', function (route) {
|
|
579
|
-
t.
|
|
580
|
-
t.
|
|
581
|
-
t.
|
|
582
|
-
t.
|
|
583
|
-
t.
|
|
598
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
599
|
+
t.assert.strictEqual(route.url, '/v1/foo')
|
|
600
|
+
t.assert.strictEqual(route.path, '/v1/foo')
|
|
601
|
+
t.assert.strictEqual(route.routePath, '/foo')
|
|
602
|
+
t.assert.strictEqual(route.prefix, '/v1')
|
|
584
603
|
})
|
|
585
604
|
instance.get('/foo', opts, function (req, reply) {
|
|
586
605
|
reply.send()
|
|
@@ -589,20 +608,21 @@ test('onRoute hook should pass correct route with custom prefix', t => {
|
|
|
589
608
|
}, { prefix: '/v1' })
|
|
590
609
|
|
|
591
610
|
fastify.ready(err => {
|
|
592
|
-
t.
|
|
611
|
+
t.assert.ifError(err)
|
|
612
|
+
testDone()
|
|
593
613
|
})
|
|
594
614
|
})
|
|
595
615
|
|
|
596
|
-
test('onRoute hook should pass correct route with custom options', t => {
|
|
616
|
+
test('onRoute hook should pass correct route with custom options', (t, testDone) => {
|
|
597
617
|
t.plan(6)
|
|
598
618
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
599
619
|
fastify.register((instance, opts, done) => {
|
|
600
620
|
instance.addHook('onRoute', function (route) {
|
|
601
|
-
t.
|
|
602
|
-
t.
|
|
603
|
-
t.
|
|
604
|
-
t.
|
|
605
|
-
t.
|
|
621
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
622
|
+
t.assert.strictEqual(route.url, '/foo')
|
|
623
|
+
t.assert.strictEqual(route.logLevel, 'info')
|
|
624
|
+
t.assert.strictEqual(route.bodyLimit, 100)
|
|
625
|
+
t.assert.ok(typeof route.logSerializers.test === 'function')
|
|
606
626
|
})
|
|
607
627
|
instance.get('/foo', {
|
|
608
628
|
logLevel: 'info',
|
|
@@ -617,19 +637,20 @@ test('onRoute hook should pass correct route with custom options', t => {
|
|
|
617
637
|
})
|
|
618
638
|
|
|
619
639
|
fastify.ready(err => {
|
|
620
|
-
t.
|
|
640
|
+
t.assert.ifError(err)
|
|
641
|
+
testDone()
|
|
621
642
|
})
|
|
622
643
|
})
|
|
623
644
|
|
|
624
|
-
test('onRoute hook should receive any route option', t => {
|
|
645
|
+
test('onRoute hook should receive any route option', (t, testDone) => {
|
|
625
646
|
t.plan(5)
|
|
626
647
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
627
648
|
fastify.register((instance, opts, done) => {
|
|
628
649
|
instance.addHook('onRoute', function (route) {
|
|
629
|
-
t.
|
|
630
|
-
t.
|
|
631
|
-
t.
|
|
632
|
-
t.
|
|
650
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
651
|
+
t.assert.strictEqual(route.url, '/foo')
|
|
652
|
+
t.assert.strictEqual(route.routePath, '/foo')
|
|
653
|
+
t.assert.strictEqual(route.auth, 'basic')
|
|
633
654
|
})
|
|
634
655
|
instance.get('/foo', { auth: 'basic' }, function (req, reply) {
|
|
635
656
|
reply.send()
|
|
@@ -638,19 +659,20 @@ test('onRoute hook should receive any route option', t => {
|
|
|
638
659
|
})
|
|
639
660
|
|
|
640
661
|
fastify.ready(err => {
|
|
641
|
-
t.
|
|
662
|
+
t.assert.ifError(err)
|
|
663
|
+
testDone()
|
|
642
664
|
})
|
|
643
665
|
})
|
|
644
666
|
|
|
645
|
-
test('onRoute hook should preserve system route configuration', t => {
|
|
667
|
+
test('onRoute hook should preserve system route configuration', (t, testDone) => {
|
|
646
668
|
t.plan(5)
|
|
647
669
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
648
670
|
fastify.register((instance, opts, done) => {
|
|
649
671
|
instance.addHook('onRoute', function (route) {
|
|
650
|
-
t.
|
|
651
|
-
t.
|
|
652
|
-
t.
|
|
653
|
-
t.
|
|
672
|
+
t.assert.strictEqual(route.method, 'GET')
|
|
673
|
+
t.assert.strictEqual(route.url, '/foo')
|
|
674
|
+
t.assert.strictEqual(route.routePath, '/foo')
|
|
675
|
+
t.assert.strictEqual(route.handler.length, 2)
|
|
654
676
|
})
|
|
655
677
|
instance.get('/foo', { url: '/bar', method: 'POST' }, function (req, reply) {
|
|
656
678
|
reply.send()
|
|
@@ -659,11 +681,12 @@ test('onRoute hook should preserve system route configuration', t => {
|
|
|
659
681
|
})
|
|
660
682
|
|
|
661
683
|
fastify.ready(err => {
|
|
662
|
-
t.
|
|
684
|
+
t.assert.ifError(err)
|
|
685
|
+
testDone()
|
|
663
686
|
})
|
|
664
687
|
})
|
|
665
688
|
|
|
666
|
-
test('onRoute hook should preserve handler function in options of shorthand route system configuration', t => {
|
|
689
|
+
test('onRoute hook should preserve handler function in options of shorthand route system configuration', (t, testDone) => {
|
|
667
690
|
t.plan(2)
|
|
668
691
|
|
|
669
692
|
const handler = (req, reply) => {}
|
|
@@ -671,19 +694,20 @@ test('onRoute hook should preserve handler function in options of shorthand rout
|
|
|
671
694
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
672
695
|
fastify.register((instance, opts, done) => {
|
|
673
696
|
instance.addHook('onRoute', function (route) {
|
|
674
|
-
t.
|
|
697
|
+
t.assert.strictEqual(route.handler, handler)
|
|
675
698
|
})
|
|
676
699
|
instance.get('/foo', { handler })
|
|
677
700
|
done()
|
|
678
701
|
})
|
|
679
702
|
|
|
680
703
|
fastify.ready(err => {
|
|
681
|
-
t.
|
|
704
|
+
t.assert.ifError(err)
|
|
705
|
+
testDone()
|
|
682
706
|
})
|
|
683
707
|
})
|
|
684
708
|
|
|
685
709
|
// issue ref https://github.com/fastify/fastify-compress/issues/140
|
|
686
|
-
test('onRoute hook should be called once when prefixTrailingSlash', t => {
|
|
710
|
+
test('onRoute hook should be called once when prefixTrailingSlash', (t, testDone) => {
|
|
687
711
|
t.plan(3)
|
|
688
712
|
|
|
689
713
|
let onRouteCalled = 0
|
|
@@ -719,21 +743,22 @@ test('onRoute hook should be called once when prefixTrailingSlash', t => {
|
|
|
719
743
|
}, { prefix: '/prefix' })
|
|
720
744
|
|
|
721
745
|
fastify.ready(err => {
|
|
722
|
-
t.
|
|
723
|
-
t.
|
|
724
|
-
t.
|
|
746
|
+
t.assert.ifError(err)
|
|
747
|
+
t.assert.strictEqual(onRouteCalled, 1) // onRoute hook was called once
|
|
748
|
+
t.assert.strictEqual(routePatched, 1) // and plugin acted once and avoided redundant route patching
|
|
749
|
+
testDone()
|
|
725
750
|
})
|
|
726
751
|
})
|
|
727
752
|
|
|
728
|
-
test('onRoute hook should able to change the route url', t => {
|
|
753
|
+
test('onRoute hook should able to change the route url', (t, testDone) => {
|
|
729
754
|
t.plan(5)
|
|
730
755
|
|
|
731
756
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
732
|
-
t.
|
|
757
|
+
t.after(() => { fastify.close() })
|
|
733
758
|
|
|
734
759
|
fastify.register((instance, opts, done) => {
|
|
735
760
|
instance.addHook('onRoute', (route) => {
|
|
736
|
-
t.
|
|
761
|
+
t.assert.strictEqual(route.url, '/foo')
|
|
737
762
|
route.url = encodeURI(route.url)
|
|
738
763
|
})
|
|
739
764
|
|
|
@@ -745,20 +770,21 @@ test('onRoute hook should able to change the route url', t => {
|
|
|
745
770
|
})
|
|
746
771
|
|
|
747
772
|
fastify.listen({ port: 0 }, err => {
|
|
748
|
-
t.
|
|
773
|
+
t.assert.ifError(err)
|
|
749
774
|
|
|
750
775
|
sget({
|
|
751
776
|
method: 'GET',
|
|
752
777
|
url: getServerUrl(fastify) + encodeURI('/foo')
|
|
753
778
|
}, (err, response, body) => {
|
|
754
|
-
t.
|
|
755
|
-
t.
|
|
756
|
-
t.
|
|
779
|
+
t.assert.ifError(err)
|
|
780
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
781
|
+
t.assert.strictEqual(body.toString(), 'here /foo')
|
|
782
|
+
testDone()
|
|
757
783
|
})
|
|
758
784
|
})
|
|
759
785
|
})
|
|
760
786
|
|
|
761
|
-
test('onRoute hook that throws should be caught', t => {
|
|
787
|
+
test('onRoute hook that throws should be caught', (t, testDone) => {
|
|
762
788
|
t.plan(1)
|
|
763
789
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
764
790
|
|
|
@@ -772,30 +798,30 @@ test('onRoute hook that throws should be caught', t => {
|
|
|
772
798
|
reply.send()
|
|
773
799
|
})
|
|
774
800
|
|
|
775
|
-
t.fail('onRoute should throw sync if error')
|
|
801
|
+
t.assert.fail('onRoute should throw sync if error')
|
|
776
802
|
} catch (error) {
|
|
777
|
-
t.ok(error)
|
|
803
|
+
t.assert.ok(error)
|
|
778
804
|
}
|
|
779
805
|
|
|
780
806
|
done()
|
|
781
807
|
})
|
|
782
808
|
|
|
783
|
-
fastify.ready()
|
|
809
|
+
fastify.ready(testDone)
|
|
784
810
|
})
|
|
785
811
|
|
|
786
|
-
test('onRoute hook with many prefix', t => {
|
|
812
|
+
test('onRoute hook with many prefix', (t, testDone) => {
|
|
787
813
|
t.plan(3)
|
|
788
814
|
const fastify = Fastify({ exposeHeadRoutes: false })
|
|
789
815
|
const handler = (req, reply) => { reply.send({}) }
|
|
790
816
|
|
|
791
817
|
const onRouteChecks = [
|
|
792
|
-
{ routePath: '/anotherPath', prefix: '/two', url: '/one/two/anotherPath' },
|
|
818
|
+
{ routePath: '/anotherPath', prefix: '/one/two', url: '/one/two/anotherPath' },
|
|
793
819
|
{ routePath: '/aPath', prefix: '/one', url: '/one/aPath' }
|
|
794
820
|
]
|
|
795
821
|
|
|
796
822
|
fastify.register((instance, opts, done) => {
|
|
797
|
-
instance.addHook('onRoute', (
|
|
798
|
-
t.
|
|
823
|
+
instance.addHook('onRoute', ({ routePath, prefix, url }) => {
|
|
824
|
+
t.assert.deepStrictEqual({ routePath, prefix, url }, onRouteChecks.pop())
|
|
799
825
|
})
|
|
800
826
|
instance.route({ method: 'GET', url: '/aPath', handler })
|
|
801
827
|
|
|
@@ -806,15 +832,18 @@ test('onRoute hook with many prefix', t => {
|
|
|
806
832
|
done()
|
|
807
833
|
}, { prefix: '/one' })
|
|
808
834
|
|
|
809
|
-
fastify.ready(err => {
|
|
835
|
+
fastify.ready(err => {
|
|
836
|
+
t.assert.ifError(err)
|
|
837
|
+
testDone()
|
|
838
|
+
})
|
|
810
839
|
})
|
|
811
840
|
|
|
812
|
-
test('onRoute hook should not be called when it registered after route', t => {
|
|
841
|
+
test('onRoute hook should not be called when it registered after route', (t, testDone) => {
|
|
813
842
|
t.plan(3)
|
|
814
843
|
const fastify = Fastify()
|
|
815
844
|
|
|
816
845
|
fastify.addHook('onRoute', () => {
|
|
817
|
-
t.pass
|
|
846
|
+
t.assert.ok('should pass')
|
|
818
847
|
})
|
|
819
848
|
|
|
820
849
|
fastify.get('/', function (req, reply) {
|
|
@@ -822,15 +851,16 @@ test('onRoute hook should not be called when it registered after route', t => {
|
|
|
822
851
|
})
|
|
823
852
|
|
|
824
853
|
fastify.addHook('onRoute', () => {
|
|
825
|
-
t.fail('should not be called')
|
|
854
|
+
t.assert.fail('should not be called')
|
|
826
855
|
})
|
|
827
856
|
|
|
828
857
|
fastify.ready(err => {
|
|
829
|
-
t.
|
|
858
|
+
t.assert.ifError(err)
|
|
859
|
+
testDone()
|
|
830
860
|
})
|
|
831
861
|
})
|
|
832
862
|
|
|
833
|
-
test('onResponse hook should log request error', t => {
|
|
863
|
+
test('onResponse hook should log request error', (t, testDone) => {
|
|
834
864
|
t.plan(4)
|
|
835
865
|
|
|
836
866
|
let fastify = null
|
|
@@ -843,12 +873,12 @@ test('onResponse hook should log request error', t => {
|
|
|
843
873
|
}
|
|
844
874
|
})
|
|
845
875
|
} catch (e) {
|
|
846
|
-
t.fail()
|
|
876
|
+
t.assert.fail()
|
|
847
877
|
}
|
|
848
878
|
|
|
849
879
|
logStream.once('data', line => {
|
|
850
|
-
t.
|
|
851
|
-
t.
|
|
880
|
+
t.assert.strictEqual(line.msg, 'request errored')
|
|
881
|
+
t.assert.strictEqual(line.level, 50)
|
|
852
882
|
})
|
|
853
883
|
|
|
854
884
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
@@ -860,18 +890,19 @@ test('onResponse hook should log request error', t => {
|
|
|
860
890
|
})
|
|
861
891
|
|
|
862
892
|
fastify.inject('/root', (err, res) => {
|
|
863
|
-
t.
|
|
864
|
-
t.
|
|
893
|
+
t.assert.ifError(err)
|
|
894
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
895
|
+
testDone()
|
|
865
896
|
})
|
|
866
897
|
})
|
|
867
898
|
|
|
868
|
-
test('onResponse hook should support encapsulation / 1', t => {
|
|
899
|
+
test('onResponse hook should support encapsulation / 1', (t, testDone) => {
|
|
869
900
|
t.plan(5)
|
|
870
901
|
const fastify = Fastify()
|
|
871
902
|
|
|
872
903
|
fastify.register((instance, opts, done) => {
|
|
873
904
|
instance.addHook('onResponse', (request, reply, done) => {
|
|
874
|
-
t.
|
|
905
|
+
t.assert.strictEqual(reply.plugin, true)
|
|
875
906
|
done()
|
|
876
907
|
})
|
|
877
908
|
|
|
@@ -888,17 +919,18 @@ test('onResponse hook should support encapsulation / 1', t => {
|
|
|
888
919
|
})
|
|
889
920
|
|
|
890
921
|
fastify.inject('/root', (err, res) => {
|
|
891
|
-
t.
|
|
892
|
-
t.
|
|
922
|
+
t.assert.ifError(err)
|
|
923
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
893
924
|
})
|
|
894
925
|
|
|
895
926
|
fastify.inject('/plugin', (err, res) => {
|
|
896
|
-
t.
|
|
897
|
-
t.
|
|
927
|
+
t.assert.ifError(err)
|
|
928
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
929
|
+
testDone()
|
|
898
930
|
})
|
|
899
931
|
})
|
|
900
932
|
|
|
901
|
-
test('onResponse hook should support encapsulation / 2', t => {
|
|
933
|
+
test('onResponse hook should support encapsulation / 2', (t, testDone) => {
|
|
902
934
|
t.plan(3)
|
|
903
935
|
const fastify = Fastify()
|
|
904
936
|
let pluginInstance
|
|
@@ -912,21 +944,22 @@ test('onResponse hook should support encapsulation / 2', t => {
|
|
|
912
944
|
})
|
|
913
945
|
|
|
914
946
|
fastify.ready(err => {
|
|
915
|
-
t.
|
|
916
|
-
t.
|
|
917
|
-
t.
|
|
947
|
+
t.assert.ifError(err)
|
|
948
|
+
t.assert.strictEqual(fastify[symbols.kHooks].onResponse.length, 1)
|
|
949
|
+
t.assert.strictEqual(pluginInstance[symbols.kHooks].onResponse.length, 2)
|
|
950
|
+
testDone()
|
|
918
951
|
})
|
|
919
952
|
})
|
|
920
953
|
|
|
921
|
-
test('onResponse hook should support encapsulation / 3', t => {
|
|
954
|
+
test('onResponse hook should support encapsulation / 3', (t, testDone) => {
|
|
922
955
|
t.plan(16)
|
|
923
956
|
const fastify = Fastify()
|
|
924
|
-
t.
|
|
957
|
+
t.after(() => { fastify.close() })
|
|
925
958
|
fastify.decorate('hello', 'world')
|
|
926
959
|
|
|
927
960
|
fastify.addHook('onResponse', function (request, reply, done) {
|
|
928
|
-
t.ok(this.hello)
|
|
929
|
-
t.ok('onResponse called')
|
|
961
|
+
t.assert.ok(this.hello)
|
|
962
|
+
t.assert.ok('onResponse called')
|
|
930
963
|
done()
|
|
931
964
|
})
|
|
932
965
|
|
|
@@ -937,9 +970,9 @@ test('onResponse hook should support encapsulation / 3', t => {
|
|
|
937
970
|
fastify.register((instance, opts, done) => {
|
|
938
971
|
instance.decorate('hello2', 'world')
|
|
939
972
|
instance.addHook('onResponse', function (request, reply, done) {
|
|
940
|
-
t.ok(this.hello)
|
|
941
|
-
t.ok(this.hello2)
|
|
942
|
-
t.ok('onResponse called')
|
|
973
|
+
t.assert.ok(this.hello)
|
|
974
|
+
t.assert.ok(this.hello2)
|
|
975
|
+
t.assert.ok('onResponse called')
|
|
943
976
|
done()
|
|
944
977
|
})
|
|
945
978
|
|
|
@@ -951,31 +984,34 @@ test('onResponse hook should support encapsulation / 3', t => {
|
|
|
951
984
|
})
|
|
952
985
|
|
|
953
986
|
fastify.listen({ port: 0 }, err => {
|
|
954
|
-
t.
|
|
987
|
+
t.assert.ifError(err)
|
|
955
988
|
|
|
989
|
+
const completion = waitForCb({ steps: 2 })
|
|
956
990
|
sget({
|
|
957
991
|
method: 'GET',
|
|
958
992
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
959
993
|
}, (err, response, body) => {
|
|
960
|
-
t.
|
|
961
|
-
t.
|
|
962
|
-
t.
|
|
963
|
-
t.
|
|
994
|
+
t.assert.ifError(err)
|
|
995
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
996
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
997
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
998
|
+
completion.stepIn()
|
|
964
999
|
})
|
|
965
|
-
|
|
966
1000
|
sget({
|
|
967
1001
|
method: 'GET',
|
|
968
1002
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
969
1003
|
}, (err, response, body) => {
|
|
970
|
-
t.
|
|
971
|
-
t.
|
|
972
|
-
t.
|
|
973
|
-
t.
|
|
1004
|
+
t.assert.ifError(err)
|
|
1005
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1006
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1007
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1008
|
+
completion.stepIn()
|
|
974
1009
|
})
|
|
1010
|
+
completion.patience.then(testDone)
|
|
975
1011
|
})
|
|
976
1012
|
})
|
|
977
1013
|
|
|
978
|
-
test('onSend hook should support encapsulation / 1', t => {
|
|
1014
|
+
test('onSend hook should support encapsulation / 1', (t, testDone) => {
|
|
979
1015
|
t.plan(3)
|
|
980
1016
|
const fastify = Fastify()
|
|
981
1017
|
let pluginInstance
|
|
@@ -989,21 +1025,22 @@ test('onSend hook should support encapsulation / 1', t => {
|
|
|
989
1025
|
})
|
|
990
1026
|
|
|
991
1027
|
fastify.ready(err => {
|
|
992
|
-
t.
|
|
993
|
-
t.
|
|
994
|
-
t.
|
|
1028
|
+
t.assert.ifError(err)
|
|
1029
|
+
t.assert.strictEqual(fastify[symbols.kHooks].onSend.length, 1)
|
|
1030
|
+
t.assert.strictEqual(pluginInstance[symbols.kHooks].onSend.length, 2)
|
|
1031
|
+
testDone()
|
|
995
1032
|
})
|
|
996
1033
|
})
|
|
997
1034
|
|
|
998
|
-
test('onSend hook should support encapsulation / 2', t => {
|
|
1035
|
+
test('onSend hook should support encapsulation / 2', (t, testDone) => {
|
|
999
1036
|
t.plan(16)
|
|
1000
1037
|
const fastify = Fastify()
|
|
1001
|
-
t.
|
|
1038
|
+
t.after(() => { fastify.close() })
|
|
1002
1039
|
fastify.decorate('hello', 'world')
|
|
1003
1040
|
|
|
1004
1041
|
fastify.addHook('onSend', function (request, reply, thePayload, done) {
|
|
1005
|
-
t.ok(this.hello)
|
|
1006
|
-
t.ok('onSend called')
|
|
1042
|
+
t.assert.ok(this.hello)
|
|
1043
|
+
t.assert.ok('onSend called')
|
|
1007
1044
|
done()
|
|
1008
1045
|
})
|
|
1009
1046
|
|
|
@@ -1014,9 +1051,9 @@ test('onSend hook should support encapsulation / 2', t => {
|
|
|
1014
1051
|
fastify.register((instance, opts, done) => {
|
|
1015
1052
|
instance.decorate('hello2', 'world')
|
|
1016
1053
|
instance.addHook('onSend', function (request, reply, thePayload, done) {
|
|
1017
|
-
t.ok(this.hello)
|
|
1018
|
-
t.ok(this.hello2)
|
|
1019
|
-
t.ok('onSend called')
|
|
1054
|
+
t.assert.ok(this.hello)
|
|
1055
|
+
t.assert.ok(this.hello2)
|
|
1056
|
+
t.assert.ok('onSend called')
|
|
1020
1057
|
done()
|
|
1021
1058
|
})
|
|
1022
1059
|
|
|
@@ -1028,31 +1065,35 @@ test('onSend hook should support encapsulation / 2', t => {
|
|
|
1028
1065
|
})
|
|
1029
1066
|
|
|
1030
1067
|
fastify.listen({ port: 0 }, err => {
|
|
1031
|
-
t.
|
|
1068
|
+
t.assert.ifError(err)
|
|
1032
1069
|
|
|
1070
|
+
const completion = waitForCb({ steps: 2 })
|
|
1033
1071
|
sget({
|
|
1034
1072
|
method: 'GET',
|
|
1035
1073
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
1036
1074
|
}, (err, response, body) => {
|
|
1037
|
-
t.
|
|
1038
|
-
t.
|
|
1039
|
-
t.
|
|
1040
|
-
t.
|
|
1075
|
+
t.assert.ifError(err)
|
|
1076
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1077
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1078
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1079
|
+
completion.stepIn()
|
|
1041
1080
|
})
|
|
1042
1081
|
|
|
1043
1082
|
sget({
|
|
1044
1083
|
method: 'GET',
|
|
1045
1084
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
1046
1085
|
}, (err, response, body) => {
|
|
1047
|
-
t.
|
|
1048
|
-
t.
|
|
1049
|
-
t.
|
|
1050
|
-
t.
|
|
1086
|
+
t.assert.ifError(err)
|
|
1087
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1088
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1089
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1090
|
+
completion.stepIn()
|
|
1051
1091
|
})
|
|
1092
|
+
completion.patience.then(testDone)
|
|
1052
1093
|
})
|
|
1053
1094
|
})
|
|
1054
1095
|
|
|
1055
|
-
test('onSend hook is called after payload is serialized and headers are set', t => {
|
|
1096
|
+
test('onSend hook is called after payload is serialized and headers are set', (t, testDone) => {
|
|
1056
1097
|
t.plan(30)
|
|
1057
1098
|
const fastify = Fastify()
|
|
1058
1099
|
|
|
@@ -1060,8 +1101,8 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1060
1101
|
const thePayload = { hello: 'world' }
|
|
1061
1102
|
|
|
1062
1103
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
1063
|
-
t.
|
|
1064
|
-
t.
|
|
1104
|
+
t.assert.deepStrictEqual(JSON.parse(payload), thePayload)
|
|
1105
|
+
t.assert.strictEqual(reply[symbols.kReplyHeaders]['content-type'], 'application/json; charset=utf-8')
|
|
1065
1106
|
done()
|
|
1066
1107
|
})
|
|
1067
1108
|
|
|
@@ -1074,8 +1115,8 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1074
1115
|
|
|
1075
1116
|
fastify.register((instance, opts, done) => {
|
|
1076
1117
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
1077
|
-
t.
|
|
1078
|
-
t.
|
|
1118
|
+
t.assert.strictEqual(payload, 'some text')
|
|
1119
|
+
t.assert.strictEqual(reply[symbols.kReplyHeaders]['content-type'], 'text/plain; charset=utf-8')
|
|
1079
1120
|
done()
|
|
1080
1121
|
})
|
|
1081
1122
|
|
|
@@ -1090,8 +1131,8 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1090
1131
|
const thePayload = Buffer.from('buffer payload')
|
|
1091
1132
|
|
|
1092
1133
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
1093
|
-
t.
|
|
1094
|
-
t.
|
|
1134
|
+
t.assert.strictEqual(payload, thePayload)
|
|
1135
|
+
t.assert.strictEqual(reply[symbols.kReplyHeaders]['content-type'], 'application/octet-stream')
|
|
1095
1136
|
done()
|
|
1096
1137
|
})
|
|
1097
1138
|
|
|
@@ -1112,8 +1153,8 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1112
1153
|
})
|
|
1113
1154
|
|
|
1114
1155
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
1115
|
-
t.
|
|
1116
|
-
t.
|
|
1156
|
+
t.assert.strictEqual(payload, thePayload)
|
|
1157
|
+
t.assert.strictEqual(reply[symbols.kReplyHeaders]['content-type'], 'application/octet-stream')
|
|
1117
1158
|
done()
|
|
1118
1159
|
})
|
|
1119
1160
|
|
|
@@ -1129,8 +1170,8 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1129
1170
|
const serializedPayload = 'serialized'
|
|
1130
1171
|
|
|
1131
1172
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
1132
|
-
t.
|
|
1133
|
-
t.
|
|
1173
|
+
t.assert.strictEqual(payload, serializedPayload)
|
|
1174
|
+
t.assert.strictEqual(reply[symbols.kReplyHeaders]['content-type'], 'text/custom')
|
|
1134
1175
|
done()
|
|
1135
1176
|
})
|
|
1136
1177
|
|
|
@@ -1144,58 +1185,65 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1144
1185
|
done()
|
|
1145
1186
|
})
|
|
1146
1187
|
|
|
1188
|
+
const completion = waitForCb({ steps: 5 })
|
|
1147
1189
|
fastify.inject({
|
|
1148
1190
|
method: 'GET',
|
|
1149
1191
|
url: '/json'
|
|
1150
1192
|
}, (err, res) => {
|
|
1151
|
-
t.
|
|
1152
|
-
t.
|
|
1153
|
-
t.
|
|
1154
|
-
t.
|
|
1193
|
+
t.assert.ifError(err)
|
|
1194
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1195
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
1196
|
+
t.assert.strictEqual(res.headers['content-length'], '17')
|
|
1197
|
+
completion.stepIn()
|
|
1155
1198
|
})
|
|
1156
1199
|
|
|
1157
1200
|
fastify.inject({
|
|
1158
1201
|
method: 'GET',
|
|
1159
1202
|
url: '/text'
|
|
1160
1203
|
}, (err, res) => {
|
|
1161
|
-
t.
|
|
1162
|
-
t.
|
|
1163
|
-
t.
|
|
1164
|
-
t.
|
|
1204
|
+
t.assert.ifError(err)
|
|
1205
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1206
|
+
t.assert.deepStrictEqual(res.payload, 'some text')
|
|
1207
|
+
t.assert.strictEqual(res.headers['content-length'], '9')
|
|
1208
|
+
completion.stepIn()
|
|
1165
1209
|
})
|
|
1166
1210
|
|
|
1167
1211
|
fastify.inject({
|
|
1168
1212
|
method: 'GET',
|
|
1169
1213
|
url: '/buffer'
|
|
1170
1214
|
}, (err, res) => {
|
|
1171
|
-
t.
|
|
1172
|
-
t.
|
|
1173
|
-
t.
|
|
1174
|
-
t.
|
|
1215
|
+
t.assert.ifError(err)
|
|
1216
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1217
|
+
t.assert.deepStrictEqual(res.payload, 'buffer payload')
|
|
1218
|
+
t.assert.strictEqual(res.headers['content-length'], '14')
|
|
1219
|
+
completion.stepIn()
|
|
1175
1220
|
})
|
|
1176
1221
|
|
|
1177
1222
|
fastify.inject({
|
|
1178
1223
|
method: 'GET',
|
|
1179
1224
|
url: '/stream'
|
|
1180
1225
|
}, (err, res) => {
|
|
1181
|
-
t.
|
|
1182
|
-
t.
|
|
1183
|
-
t.
|
|
1184
|
-
t.
|
|
1226
|
+
t.assert.ifError(err)
|
|
1227
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1228
|
+
t.assert.deepStrictEqual(res.payload, 'stream payload')
|
|
1229
|
+
t.assert.strictEqual(res.headers['transfer-encoding'], 'chunked')
|
|
1230
|
+
completion.stepIn()
|
|
1185
1231
|
})
|
|
1186
1232
|
|
|
1187
1233
|
fastify.inject({
|
|
1188
1234
|
method: 'GET',
|
|
1189
1235
|
url: '/custom-serializer'
|
|
1190
1236
|
}, (err, res) => {
|
|
1191
|
-
t.
|
|
1192
|
-
t.
|
|
1193
|
-
t.
|
|
1194
|
-
t.
|
|
1237
|
+
t.assert.ifError(err)
|
|
1238
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1239
|
+
t.assert.deepStrictEqual(res.payload, 'serialized')
|
|
1240
|
+
t.assert.strictEqual(res.headers['content-type'], 'text/custom')
|
|
1241
|
+
completion.stepIn()
|
|
1195
1242
|
})
|
|
1243
|
+
completion.patience.then(testDone)
|
|
1196
1244
|
})
|
|
1197
1245
|
|
|
1198
|
-
test('modify payload', t => {
|
|
1246
|
+
test('modify payload', (t, testDone) => {
|
|
1199
1247
|
t.plan(10)
|
|
1200
1248
|
const fastify = Fastify()
|
|
1201
1249
|
const payload = { hello: 'world' }
|
|
@@ -1203,21 +1251,21 @@ test('modify payload', t => {
|
|
|
1203
1251
|
const anotherPayload = '"winter is coming"'
|
|
1204
1252
|
|
|
1205
1253
|
fastify.addHook('onSend', function (request, reply, thePayload, done) {
|
|
1206
|
-
t.ok('onSend called')
|
|
1207
|
-
t.
|
|
1254
|
+
t.assert.ok('onSend called')
|
|
1255
|
+
t.assert.deepStrictEqual(JSON.parse(thePayload), payload)
|
|
1208
1256
|
thePayload = thePayload.replace('world', 'modified')
|
|
1209
1257
|
done(null, thePayload)
|
|
1210
1258
|
})
|
|
1211
1259
|
|
|
1212
1260
|
fastify.addHook('onSend', function (request, reply, thePayload, done) {
|
|
1213
|
-
t.ok('onSend called')
|
|
1214
|
-
t.
|
|
1261
|
+
t.assert.ok('onSend called')
|
|
1262
|
+
t.assert.deepStrictEqual(JSON.parse(thePayload), modifiedPayload)
|
|
1215
1263
|
done(null, anotherPayload)
|
|
1216
1264
|
})
|
|
1217
1265
|
|
|
1218
1266
|
fastify.addHook('onSend', function (request, reply, thePayload, done) {
|
|
1219
|
-
t.ok('onSend called')
|
|
1220
|
-
t.
|
|
1267
|
+
t.assert.ok('onSend called')
|
|
1268
|
+
t.assert.strictEqual(thePayload, anotherPayload)
|
|
1221
1269
|
done()
|
|
1222
1270
|
})
|
|
1223
1271
|
|
|
@@ -1229,19 +1277,20 @@ test('modify payload', t => {
|
|
|
1229
1277
|
method: 'GET',
|
|
1230
1278
|
url: '/'
|
|
1231
1279
|
}, (err, res) => {
|
|
1232
|
-
t.
|
|
1233
|
-
t.
|
|
1234
|
-
t.
|
|
1235
|
-
t.
|
|
1280
|
+
t.assert.ifError(err)
|
|
1281
|
+
t.assert.strictEqual(res.payload, anotherPayload)
|
|
1282
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1283
|
+
t.assert.strictEqual(res.headers['content-length'], '18')
|
|
1284
|
+
testDone()
|
|
1236
1285
|
})
|
|
1237
1286
|
})
|
|
1238
1287
|
|
|
1239
|
-
test('clear payload', t => {
|
|
1288
|
+
test('clear payload', (t, testDone) => {
|
|
1240
1289
|
t.plan(6)
|
|
1241
1290
|
const fastify = Fastify()
|
|
1242
1291
|
|
|
1243
1292
|
fastify.addHook('onSend', function (request, reply, payload, done) {
|
|
1244
|
-
t.ok('onSend called')
|
|
1293
|
+
t.assert.ok('onSend called')
|
|
1245
1294
|
reply.code(304)
|
|
1246
1295
|
done(null, null)
|
|
1247
1296
|
})
|
|
@@ -1254,25 +1303,26 @@ test('clear payload', t => {
|
|
|
1254
1303
|
method: 'GET',
|
|
1255
1304
|
url: '/'
|
|
1256
1305
|
}, (err, res) => {
|
|
1257
|
-
t.
|
|
1258
|
-
t.
|
|
1259
|
-
t.
|
|
1260
|
-
t.
|
|
1261
|
-
t.
|
|
1306
|
+
t.assert.ifError(err)
|
|
1307
|
+
t.assert.strictEqual(res.statusCode, 304)
|
|
1308
|
+
t.assert.strictEqual(res.payload, '')
|
|
1309
|
+
t.assert.strictEqual(res.headers['content-length'], undefined)
|
|
1310
|
+
t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
|
|
1311
|
+
testDone()
|
|
1262
1312
|
})
|
|
1263
1313
|
})
|
|
1264
1314
|
|
|
1265
|
-
test('onSend hook throws', t => {
|
|
1315
|
+
test('onSend hook throws', (t, testDone) => {
|
|
1266
1316
|
t.plan(11)
|
|
1267
1317
|
const Fastify = proxyquire('..', {
|
|
1268
1318
|
'./lib/schemas.js': {
|
|
1269
1319
|
getSchemaSerializer: (param1, param2, param3) => {
|
|
1270
|
-
t.
|
|
1320
|
+
t.assert.strictEqual(param3, 'application/json; charset=utf-8', 'param3 should be "application/json; charset=utf-8"')
|
|
1271
1321
|
}
|
|
1272
1322
|
}
|
|
1273
1323
|
})
|
|
1274
1324
|
const fastify = Fastify()
|
|
1275
|
-
t.
|
|
1325
|
+
t.after(() => { fastify.close() })
|
|
1276
1326
|
fastify.addHook('onSend', function (request, reply, payload, done) {
|
|
1277
1327
|
if (request.raw.method === 'DELETE') {
|
|
1278
1328
|
done(new Error('some error'))
|
|
@@ -1323,42 +1373,48 @@ test('onSend hook throws', t => {
|
|
|
1323
1373
|
})
|
|
1324
1374
|
|
|
1325
1375
|
fastify.listen({ port: 0 }, err => {
|
|
1326
|
-
t.
|
|
1376
|
+
t.assert.ifError(err)
|
|
1327
1377
|
|
|
1378
|
+
const completion = waitForCb({ steps: 4 })
|
|
1328
1379
|
sget({
|
|
1329
1380
|
method: 'GET',
|
|
1330
1381
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
1331
1382
|
}, (err, response, body) => {
|
|
1332
|
-
t.
|
|
1333
|
-
t.
|
|
1334
|
-
t.
|
|
1335
|
-
t.
|
|
1383
|
+
t.assert.ifError(err)
|
|
1384
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
1385
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
1386
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
1387
|
+
completion.stepIn()
|
|
1336
1388
|
})
|
|
1337
1389
|
sget({
|
|
1338
1390
|
method: 'POST',
|
|
1339
1391
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
1340
1392
|
}, (err, response, body) => {
|
|
1341
|
-
t.
|
|
1342
|
-
t.
|
|
1393
|
+
t.assert.ifError(err)
|
|
1394
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
1395
|
+
completion.stepIn()
|
|
1343
1396
|
})
|
|
1344
1397
|
sget({
|
|
1345
1398
|
method: 'DELETE',
|
|
1346
1399
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
1347
1400
|
}, (err, response, body) => {
|
|
1348
|
-
t.
|
|
1349
|
-
t.
|
|
1401
|
+
t.assert.ifError(err)
|
|
1402
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
1403
|
+
completion.stepIn()
|
|
1350
1404
|
})
|
|
1351
1405
|
sget({
|
|
1352
1406
|
method: 'PUT',
|
|
1353
1407
|
url: 'http://127.0.0.1:' + fastify.server.address().port
|
|
1354
1408
|
}, (err, response, body) => {
|
|
1355
|
-
t.
|
|
1356
|
-
t.
|
|
1409
|
+
t.assert.ifError(err)
|
|
1410
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
1411
|
+
completion.stepIn()
|
|
1357
1412
|
})
|
|
1413
|
+
completion.patience.then(testDone)
|
|
1358
1414
|
})
|
|
1359
1415
|
})
|
|
1360
1416
|
|
|
1361
|
-
test('onSend hook should receive valid request and reply objects if onRequest hook fails', t => {
|
|
1417
|
+
test('onSend hook should receive valid request and reply objects if onRequest hook fails', (t, testDone) => {
|
|
1362
1418
|
t.plan(4)
|
|
1363
1419
|
const fastify = Fastify()
|
|
1364
1420
|
|
|
@@ -1370,8 +1426,8 @@ test('onSend hook should receive valid request and reply objects if onRequest ho
|
|
|
1370
1426
|
})
|
|
1371
1427
|
|
|
1372
1428
|
fastify.addHook('onSend', function (request, reply, payload, done) {
|
|
1373
|
-
t.
|
|
1374
|
-
t.
|
|
1429
|
+
t.assert.strictEqual(request.testDecorator, 'testDecoratorVal')
|
|
1430
|
+
t.assert.strictEqual(reply.testDecorator, 'testDecoratorVal')
|
|
1375
1431
|
done()
|
|
1376
1432
|
})
|
|
1377
1433
|
|
|
@@ -1383,12 +1439,13 @@ test('onSend hook should receive valid request and reply objects if onRequest ho
|
|
|
1383
1439
|
method: 'GET',
|
|
1384
1440
|
url: '/'
|
|
1385
1441
|
}, (err, res) => {
|
|
1386
|
-
t.
|
|
1387
|
-
t.
|
|
1442
|
+
t.assert.ifError(err)
|
|
1443
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
1444
|
+
testDone()
|
|
1388
1445
|
})
|
|
1389
1446
|
})
|
|
1390
1447
|
|
|
1391
|
-
test('onSend hook should receive valid request and reply objects if a custom content type parser fails', t => {
|
|
1448
|
+
test('onSend hook should receive valid request and reply objects if a custom content type parser fails', (t, testDone) => {
|
|
1392
1449
|
t.plan(4)
|
|
1393
1450
|
const fastify = Fastify()
|
|
1394
1451
|
|
|
@@ -1400,8 +1457,8 @@ test('onSend hook should receive valid request and reply objects if a custom con
|
|
|
1400
1457
|
})
|
|
1401
1458
|
|
|
1402
1459
|
fastify.addHook('onSend', function (request, reply, payload, done) {
|
|
1403
|
-
t.
|
|
1404
|
-
t.
|
|
1460
|
+
t.assert.strictEqual(request.testDecorator, 'testDecoratorVal')
|
|
1461
|
+
t.assert.strictEqual(reply.testDecorator, 'testDecoratorVal')
|
|
1405
1462
|
done()
|
|
1406
1463
|
})
|
|
1407
1464
|
|
|
@@ -1414,12 +1471,13 @@ test('onSend hook should receive valid request and reply objects if a custom con
|
|
|
1414
1471
|
url: '/',
|
|
1415
1472
|
payload: 'body'
|
|
1416
1473
|
}, (err, res) => {
|
|
1417
|
-
t.
|
|
1418
|
-
t.
|
|
1474
|
+
t.assert.ifError(err)
|
|
1475
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
1476
|
+
testDone()
|
|
1419
1477
|
})
|
|
1420
1478
|
})
|
|
1421
1479
|
|
|
1422
|
-
test('Content-Length header should be updated if onSend hook modifies the payload', t => {
|
|
1480
|
+
test('Content-Length header should be updated if onSend hook modifies the payload', (t, testDone) => {
|
|
1423
1481
|
t.plan(2)
|
|
1424
1482
|
|
|
1425
1483
|
const instance = Fastify()
|
|
@@ -1435,36 +1493,37 @@ test('Content-Length header should be updated if onSend hook modifies the payloa
|
|
|
1435
1493
|
method: 'GET',
|
|
1436
1494
|
url: '/'
|
|
1437
1495
|
}, (err, res) => {
|
|
1438
|
-
t.
|
|
1496
|
+
t.assert.ifError(err)
|
|
1439
1497
|
const payloadLength = Buffer.byteLength(res.body)
|
|
1440
1498
|
const contentLength = Number(res.headers['content-length'])
|
|
1441
1499
|
|
|
1442
|
-
t.
|
|
1500
|
+
t.assert.strictEqual(payloadLength, contentLength)
|
|
1501
|
+
testDone()
|
|
1443
1502
|
})
|
|
1444
1503
|
})
|
|
1445
1504
|
|
|
1446
|
-
test('cannot add hook after binding', t => {
|
|
1447
|
-
t.plan(
|
|
1505
|
+
test('cannot add hook after binding', (t, testDone) => {
|
|
1506
|
+
t.plan(1)
|
|
1448
1507
|
const instance = Fastify()
|
|
1449
|
-
t.
|
|
1508
|
+
t.after(() => instance.close())
|
|
1450
1509
|
|
|
1451
1510
|
instance.get('/', function (request, reply) {
|
|
1452
1511
|
reply.send({ hello: 'world' })
|
|
1453
1512
|
})
|
|
1454
1513
|
|
|
1455
1514
|
instance.listen({ port: 0 }, err => {
|
|
1456
|
-
t.
|
|
1515
|
+
t.assert.ifError(err)
|
|
1457
1516
|
|
|
1458
1517
|
try {
|
|
1459
1518
|
instance.addHook('onRequest', () => {})
|
|
1460
|
-
t.fail()
|
|
1519
|
+
t.assert.fail()
|
|
1461
1520
|
} catch (e) {
|
|
1462
|
-
|
|
1521
|
+
testDone()
|
|
1463
1522
|
}
|
|
1464
1523
|
})
|
|
1465
1524
|
})
|
|
1466
1525
|
|
|
1467
|
-
test('onRequest hooks should be able to block a request', t => {
|
|
1526
|
+
test('onRequest hooks should be able to block a request', (t, testDone) => {
|
|
1468
1527
|
t.plan(5)
|
|
1469
1528
|
const fastify = Fastify()
|
|
1470
1529
|
|
|
@@ -1474,38 +1533,39 @@ test('onRequest hooks should be able to block a request', t => {
|
|
|
1474
1533
|
})
|
|
1475
1534
|
|
|
1476
1535
|
fastify.addHook('onRequest', (req, reply, done) => {
|
|
1477
|
-
t.fail('this should not be called')
|
|
1536
|
+
t.assert.fail('this should not be called')
|
|
1478
1537
|
})
|
|
1479
1538
|
|
|
1480
1539
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1481
|
-
t.fail('this should not be called')
|
|
1540
|
+
t.assert.fail('this should not be called')
|
|
1482
1541
|
})
|
|
1483
1542
|
|
|
1484
1543
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1485
|
-
t.ok('called')
|
|
1544
|
+
t.assert.ok('called')
|
|
1486
1545
|
done()
|
|
1487
1546
|
})
|
|
1488
1547
|
|
|
1489
1548
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1490
|
-
t.ok('called')
|
|
1549
|
+
t.assert.ok('called')
|
|
1491
1550
|
done()
|
|
1492
1551
|
})
|
|
1493
1552
|
|
|
1494
1553
|
fastify.get('/', function (request, reply) {
|
|
1495
|
-
t.fail('we should not be here')
|
|
1554
|
+
t.assert.fail('we should not be here')
|
|
1496
1555
|
})
|
|
1497
1556
|
|
|
1498
1557
|
fastify.inject({
|
|
1499
1558
|
url: '/',
|
|
1500
1559
|
method: 'GET'
|
|
1501
1560
|
}, (err, res) => {
|
|
1502
|
-
t.
|
|
1503
|
-
t.
|
|
1504
|
-
t.
|
|
1561
|
+
t.assert.ifError(err)
|
|
1562
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1563
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1564
|
+
testDone()
|
|
1505
1565
|
})
|
|
1506
1566
|
})
|
|
1507
1567
|
|
|
1508
|
-
test('preValidation hooks should be able to block a request', t => {
|
|
1568
|
+
test('preValidation hooks should be able to block a request', (t, testDone) => {
|
|
1509
1569
|
t.plan(5)
|
|
1510
1570
|
const fastify = Fastify()
|
|
1511
1571
|
|
|
@@ -1515,38 +1575,39 @@ test('preValidation hooks should be able to block a request', t => {
|
|
|
1515
1575
|
})
|
|
1516
1576
|
|
|
1517
1577
|
fastify.addHook('preValidation', (req, reply, done) => {
|
|
1518
|
-
t.fail('this should not be called')
|
|
1578
|
+
t.assert.fail('this should not be called')
|
|
1519
1579
|
})
|
|
1520
1580
|
|
|
1521
1581
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1522
|
-
t.fail('this should not be called')
|
|
1582
|
+
t.assert.fail('this should not be called')
|
|
1523
1583
|
})
|
|
1524
1584
|
|
|
1525
1585
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1526
|
-
t.ok('called')
|
|
1586
|
+
t.assert.ok('called')
|
|
1527
1587
|
done()
|
|
1528
1588
|
})
|
|
1529
1589
|
|
|
1530
1590
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1531
|
-
t.ok('called')
|
|
1591
|
+
t.assert.ok('called')
|
|
1532
1592
|
done()
|
|
1533
1593
|
})
|
|
1534
1594
|
|
|
1535
1595
|
fastify.get('/', function (request, reply) {
|
|
1536
|
-
t.fail('we should not be here')
|
|
1596
|
+
t.assert.fail('we should not be here')
|
|
1537
1597
|
})
|
|
1538
1598
|
|
|
1539
1599
|
fastify.inject({
|
|
1540
1600
|
url: '/',
|
|
1541
1601
|
method: 'GET'
|
|
1542
1602
|
}, (err, res) => {
|
|
1543
|
-
t.
|
|
1544
|
-
t.
|
|
1545
|
-
t.
|
|
1603
|
+
t.assert.ifError(err)
|
|
1604
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1605
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1606
|
+
testDone()
|
|
1546
1607
|
})
|
|
1547
1608
|
})
|
|
1548
1609
|
|
|
1549
|
-
test('preValidation hooks should be able to change request body before validation', t => {
|
|
1610
|
+
test('preValidation hooks should be able to change request body before validation', (t, testDone) => {
|
|
1550
1611
|
t.plan(4)
|
|
1551
1612
|
const fastify = Fastify()
|
|
1552
1613
|
|
|
@@ -1575,7 +1636,7 @@ test('preValidation hooks should be able to change request body before validatio
|
|
|
1575
1636
|
}
|
|
1576
1637
|
},
|
|
1577
1638
|
(req, reply) => {
|
|
1578
|
-
t.pass
|
|
1639
|
+
t.assert.ok('should pass')
|
|
1579
1640
|
reply.status(200).send('hello')
|
|
1580
1641
|
}
|
|
1581
1642
|
)
|
|
@@ -1587,13 +1648,14 @@ test('preValidation hooks should be able to change request body before validatio
|
|
|
1587
1648
|
message: Buffer.from(JSON.stringify({ foo: 'example', bar: 1 })).toString('base64')
|
|
1588
1649
|
}
|
|
1589
1650
|
}, (err, res) => {
|
|
1590
|
-
t.
|
|
1591
|
-
t.
|
|
1592
|
-
t.
|
|
1651
|
+
t.assert.ifError(err)
|
|
1652
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1653
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1654
|
+
testDone()
|
|
1593
1655
|
})
|
|
1594
1656
|
})
|
|
1595
1657
|
|
|
1596
|
-
test('preParsing hooks should be able to block a request', t => {
|
|
1658
|
+
test('preParsing hooks should be able to block a request', (t, testDone) => {
|
|
1597
1659
|
t.plan(5)
|
|
1598
1660
|
const fastify = Fastify()
|
|
1599
1661
|
|
|
@@ -1603,38 +1665,39 @@ test('preParsing hooks should be able to block a request', t => {
|
|
|
1603
1665
|
})
|
|
1604
1666
|
|
|
1605
1667
|
fastify.addHook('preParsing', (req, reply, payload, done) => {
|
|
1606
|
-
t.fail('this should not be called')
|
|
1668
|
+
t.assert.fail('this should not be called')
|
|
1607
1669
|
})
|
|
1608
1670
|
|
|
1609
1671
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1610
|
-
t.fail('this should not be called')
|
|
1672
|
+
t.assert.fail('this should not be called')
|
|
1611
1673
|
})
|
|
1612
1674
|
|
|
1613
1675
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1614
|
-
t.ok('called')
|
|
1676
|
+
t.assert.ok('called')
|
|
1615
1677
|
done()
|
|
1616
1678
|
})
|
|
1617
1679
|
|
|
1618
1680
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1619
|
-
t.ok('called')
|
|
1681
|
+
t.assert.ok('called')
|
|
1620
1682
|
done()
|
|
1621
1683
|
})
|
|
1622
1684
|
|
|
1623
1685
|
fastify.get('/', function (request, reply) {
|
|
1624
|
-
t.fail('we should not be here')
|
|
1686
|
+
t.assert.fail('we should not be here')
|
|
1625
1687
|
})
|
|
1626
1688
|
|
|
1627
1689
|
fastify.inject({
|
|
1628
1690
|
url: '/',
|
|
1629
1691
|
method: 'GET'
|
|
1630
1692
|
}, (err, res) => {
|
|
1631
|
-
t.
|
|
1632
|
-
t.
|
|
1633
|
-
t.
|
|
1693
|
+
t.assert.ifError(err)
|
|
1694
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1695
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1696
|
+
testDone()
|
|
1634
1697
|
})
|
|
1635
1698
|
})
|
|
1636
1699
|
|
|
1637
|
-
test('preHandler hooks should be able to block a request', t => {
|
|
1700
|
+
test('preHandler hooks should be able to block a request', (t, testDone) => {
|
|
1638
1701
|
t.plan(5)
|
|
1639
1702
|
const fastify = Fastify()
|
|
1640
1703
|
|
|
@@ -1644,34 +1707,35 @@ test('preHandler hooks should be able to block a request', t => {
|
|
|
1644
1707
|
})
|
|
1645
1708
|
|
|
1646
1709
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1647
|
-
t.fail('this should not be called')
|
|
1710
|
+
t.assert.fail('this should not be called')
|
|
1648
1711
|
})
|
|
1649
1712
|
|
|
1650
1713
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1651
|
-
t.
|
|
1714
|
+
t.assert.strictEqual(payload, 'hello')
|
|
1652
1715
|
done()
|
|
1653
1716
|
})
|
|
1654
1717
|
|
|
1655
1718
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1656
|
-
t.ok('called')
|
|
1719
|
+
t.assert.ok('called')
|
|
1657
1720
|
done()
|
|
1658
1721
|
})
|
|
1659
1722
|
|
|
1660
1723
|
fastify.get('/', function (request, reply) {
|
|
1661
|
-
t.fail('we should not be here')
|
|
1724
|
+
t.assert.fail('we should not be here')
|
|
1662
1725
|
})
|
|
1663
1726
|
|
|
1664
1727
|
fastify.inject({
|
|
1665
1728
|
url: '/',
|
|
1666
1729
|
method: 'GET'
|
|
1667
1730
|
}, (err, res) => {
|
|
1668
|
-
t.
|
|
1669
|
-
t.
|
|
1670
|
-
t.
|
|
1731
|
+
t.assert.ifError(err)
|
|
1732
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1733
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1734
|
+
testDone()
|
|
1671
1735
|
})
|
|
1672
1736
|
})
|
|
1673
1737
|
|
|
1674
|
-
test('onRequest hooks should be able to block a request (last hook)', t => {
|
|
1738
|
+
test('onRequest hooks should be able to block a request (last hook)', (t, testDone) => {
|
|
1675
1739
|
t.plan(5)
|
|
1676
1740
|
const fastify = Fastify()
|
|
1677
1741
|
|
|
@@ -1681,34 +1745,35 @@ test('onRequest hooks should be able to block a request (last hook)', t => {
|
|
|
1681
1745
|
})
|
|
1682
1746
|
|
|
1683
1747
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1684
|
-
t.fail('this should not be called')
|
|
1748
|
+
t.assert.fail('this should not be called')
|
|
1685
1749
|
})
|
|
1686
1750
|
|
|
1687
1751
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1688
|
-
t.ok('called')
|
|
1752
|
+
t.assert.ok('called')
|
|
1689
1753
|
done()
|
|
1690
1754
|
})
|
|
1691
1755
|
|
|
1692
1756
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1693
|
-
t.ok('called')
|
|
1757
|
+
t.assert.ok('called')
|
|
1694
1758
|
done()
|
|
1695
1759
|
})
|
|
1696
1760
|
|
|
1697
1761
|
fastify.get('/', function (request, reply) {
|
|
1698
|
-
t.fail('we should not be here')
|
|
1762
|
+
t.assert.fail('we should not be here')
|
|
1699
1763
|
})
|
|
1700
1764
|
|
|
1701
1765
|
fastify.inject({
|
|
1702
1766
|
url: '/',
|
|
1703
1767
|
method: 'GET'
|
|
1704
1768
|
}, (err, res) => {
|
|
1705
|
-
t.
|
|
1706
|
-
t.
|
|
1707
|
-
t.
|
|
1769
|
+
t.assert.ifError(err)
|
|
1770
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1771
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1772
|
+
testDone()
|
|
1708
1773
|
})
|
|
1709
1774
|
})
|
|
1710
1775
|
|
|
1711
|
-
test('preHandler hooks should be able to block a request (last hook)', t => {
|
|
1776
|
+
test('preHandler hooks should be able to block a request (last hook)', (t, testDone) => {
|
|
1712
1777
|
t.plan(5)
|
|
1713
1778
|
const fastify = Fastify()
|
|
1714
1779
|
|
|
@@ -1718,30 +1783,31 @@ test('preHandler hooks should be able to block a request (last hook)', t => {
|
|
|
1718
1783
|
})
|
|
1719
1784
|
|
|
1720
1785
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1721
|
-
t.
|
|
1786
|
+
t.assert.strictEqual(payload, 'hello')
|
|
1722
1787
|
done()
|
|
1723
1788
|
})
|
|
1724
1789
|
|
|
1725
1790
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1726
|
-
t.ok('called')
|
|
1791
|
+
t.assert.ok('called')
|
|
1727
1792
|
done()
|
|
1728
1793
|
})
|
|
1729
1794
|
|
|
1730
1795
|
fastify.get('/', function (request, reply) {
|
|
1731
|
-
t.fail('we should not be here')
|
|
1796
|
+
t.assert.fail('we should not be here')
|
|
1732
1797
|
})
|
|
1733
1798
|
|
|
1734
1799
|
fastify.inject({
|
|
1735
1800
|
url: '/',
|
|
1736
1801
|
method: 'GET'
|
|
1737
1802
|
}, (err, res) => {
|
|
1738
|
-
t.
|
|
1739
|
-
t.
|
|
1740
|
-
t.
|
|
1803
|
+
t.assert.ifError(err)
|
|
1804
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1805
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
1806
|
+
testDone()
|
|
1741
1807
|
})
|
|
1742
1808
|
})
|
|
1743
1809
|
|
|
1744
|
-
test('preParsing hooks should handle errors', t => {
|
|
1810
|
+
test('preParsing hooks should handle errors', (t, testDone) => {
|
|
1745
1811
|
t.plan(3)
|
|
1746
1812
|
const fastify = Fastify()
|
|
1747
1813
|
|
|
@@ -1760,13 +1826,14 @@ test('preParsing hooks should handle errors', t => {
|
|
|
1760
1826
|
url: '/',
|
|
1761
1827
|
payload: { hello: 'world' }
|
|
1762
1828
|
}, (err, res) => {
|
|
1763
|
-
t.
|
|
1764
|
-
t.
|
|
1765
|
-
t.
|
|
1829
|
+
t.assert.ifError(err)
|
|
1830
|
+
t.assert.strictEqual(res.statusCode, 501)
|
|
1831
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { error: 'Not Implemented', message: 'kaboom', statusCode: 501 })
|
|
1832
|
+
testDone()
|
|
1766
1833
|
})
|
|
1767
1834
|
})
|
|
1768
1835
|
|
|
1769
|
-
test('onRequest respond with a stream', t => {
|
|
1836
|
+
test('onRequest respond with a stream', (t, testDone) => {
|
|
1770
1837
|
t.plan(4)
|
|
1771
1838
|
const fastify = Fastify()
|
|
1772
1839
|
|
|
@@ -1778,42 +1845,43 @@ test('onRequest respond with a stream', t => {
|
|
|
1778
1845
|
})
|
|
1779
1846
|
|
|
1780
1847
|
fastify.addHook('onRequest', (req, res, done) => {
|
|
1781
|
-
t.fail('this should not be called')
|
|
1848
|
+
t.assert.fail('this should not be called')
|
|
1782
1849
|
})
|
|
1783
1850
|
|
|
1784
1851
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1785
|
-
t.fail('this should not be called')
|
|
1852
|
+
t.assert.fail('this should not be called')
|
|
1786
1853
|
})
|
|
1787
1854
|
|
|
1788
1855
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1789
|
-
t.ok('called')
|
|
1856
|
+
t.assert.ok('called')
|
|
1790
1857
|
done()
|
|
1791
1858
|
})
|
|
1792
1859
|
|
|
1793
1860
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1794
|
-
t.ok('called')
|
|
1861
|
+
t.assert.ok('called')
|
|
1795
1862
|
done()
|
|
1796
1863
|
})
|
|
1797
1864
|
|
|
1798
1865
|
fastify.get('/', function (request, reply) {
|
|
1799
|
-
t.fail('we should not be here')
|
|
1866
|
+
t.assert.fail('we should not be here')
|
|
1800
1867
|
})
|
|
1801
1868
|
|
|
1802
1869
|
fastify.inject({
|
|
1803
1870
|
url: '/',
|
|
1804
1871
|
method: 'GET'
|
|
1805
1872
|
}, (err, res) => {
|
|
1806
|
-
t.
|
|
1807
|
-
t.
|
|
1873
|
+
t.assert.ifError(err)
|
|
1874
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1875
|
+
testDone()
|
|
1808
1876
|
})
|
|
1809
1877
|
})
|
|
1810
1878
|
|
|
1811
|
-
test('preHandler respond with a stream', t => {
|
|
1879
|
+
test('preHandler respond with a stream', (t, testDone) => {
|
|
1812
1880
|
t.plan(7)
|
|
1813
1881
|
const fastify = Fastify()
|
|
1814
1882
|
|
|
1815
1883
|
fastify.addHook('onRequest', (req, reply, done) => {
|
|
1816
|
-
t.ok('called')
|
|
1884
|
+
t.assert.ok('called')
|
|
1817
1885
|
done()
|
|
1818
1886
|
})
|
|
1819
1887
|
|
|
@@ -1825,46 +1893,47 @@ test('preHandler respond with a stream', t => {
|
|
|
1825
1893
|
const stream = fs.createReadStream(__filename, 'utf8')
|
|
1826
1894
|
reply.send(stream)
|
|
1827
1895
|
reply.raw.once('finish', () => {
|
|
1828
|
-
t.
|
|
1896
|
+
t.assert.strictEqual(order.shift(), 2)
|
|
1829
1897
|
done()
|
|
1830
1898
|
})
|
|
1831
1899
|
})
|
|
1832
1900
|
|
|
1833
1901
|
fastify.addHook('preHandler', (req, reply, done) => {
|
|
1834
|
-
t.fail('this should not be called')
|
|
1902
|
+
t.assert.fail('this should not be called')
|
|
1835
1903
|
})
|
|
1836
1904
|
|
|
1837
1905
|
fastify.addHook('onSend', (req, reply, payload, done) => {
|
|
1838
|
-
t.
|
|
1839
|
-
t.
|
|
1906
|
+
t.assert.strictEqual(order.shift(), 1)
|
|
1907
|
+
t.assert.strictEqual(typeof payload.pipe, 'function')
|
|
1840
1908
|
done()
|
|
1841
1909
|
})
|
|
1842
1910
|
|
|
1843
1911
|
fastify.addHook('onResponse', (request, reply, done) => {
|
|
1844
|
-
t.ok('called')
|
|
1912
|
+
t.assert.ok('called')
|
|
1845
1913
|
done()
|
|
1846
1914
|
})
|
|
1847
1915
|
|
|
1848
1916
|
fastify.get('/', function (request, reply) {
|
|
1849
|
-
t.fail('we should not be here')
|
|
1917
|
+
t.assert.fail('we should not be here')
|
|
1850
1918
|
})
|
|
1851
1919
|
|
|
1852
1920
|
fastify.inject({
|
|
1853
1921
|
url: '/',
|
|
1854
1922
|
method: 'GET'
|
|
1855
1923
|
}, (err, res) => {
|
|
1856
|
-
t.
|
|
1857
|
-
t.
|
|
1924
|
+
t.assert.ifError(err)
|
|
1925
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1926
|
+
testDone()
|
|
1858
1927
|
})
|
|
1859
1928
|
})
|
|
1860
1929
|
|
|
1861
|
-
test('Register an hook after a plugin inside a plugin', t => {
|
|
1930
|
+
test('Register an hook after a plugin inside a plugin', (t, testDone) => {
|
|
1862
1931
|
t.plan(6)
|
|
1863
1932
|
const fastify = Fastify()
|
|
1864
1933
|
|
|
1865
1934
|
fastify.register(fp(function (instance, opts, done) {
|
|
1866
1935
|
instance.addHook('preHandler', function (req, reply, done) {
|
|
1867
|
-
t.ok('called')
|
|
1936
|
+
t.assert.ok('called')
|
|
1868
1937
|
done()
|
|
1869
1938
|
})
|
|
1870
1939
|
|
|
@@ -1877,12 +1946,12 @@ test('Register an hook after a plugin inside a plugin', t => {
|
|
|
1877
1946
|
|
|
1878
1947
|
fastify.register(fp(function (instance, opts, done) {
|
|
1879
1948
|
instance.addHook('preHandler', function (req, reply, done) {
|
|
1880
|
-
t.ok('called')
|
|
1949
|
+
t.assert.ok('called')
|
|
1881
1950
|
done()
|
|
1882
1951
|
})
|
|
1883
1952
|
|
|
1884
1953
|
instance.addHook('preHandler', function (req, reply, done) {
|
|
1885
|
-
t.ok('called')
|
|
1954
|
+
t.assert.ok('called')
|
|
1886
1955
|
done()
|
|
1887
1956
|
})
|
|
1888
1957
|
|
|
@@ -1893,25 +1962,26 @@ test('Register an hook after a plugin inside a plugin', t => {
|
|
|
1893
1962
|
url: '/',
|
|
1894
1963
|
method: 'GET'
|
|
1895
1964
|
}, (err, res) => {
|
|
1896
|
-
t.
|
|
1897
|
-
t.
|
|
1898
|
-
t.
|
|
1965
|
+
t.assert.ifError(err)
|
|
1966
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
1967
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
1968
|
+
testDone()
|
|
1899
1969
|
})
|
|
1900
1970
|
})
|
|
1901
1971
|
|
|
1902
|
-
test('Register an hook after a plugin inside a plugin (with preHandler option)', t => {
|
|
1972
|
+
test('Register an hook after a plugin inside a plugin (with preHandler option)', (t, testDone) => {
|
|
1903
1973
|
t.plan(7)
|
|
1904
1974
|
const fastify = Fastify()
|
|
1905
1975
|
|
|
1906
1976
|
fastify.register(fp(function (instance, opts, done) {
|
|
1907
1977
|
instance.addHook('preHandler', function (req, reply, done) {
|
|
1908
|
-
t.ok('called')
|
|
1978
|
+
t.assert.ok('called')
|
|
1909
1979
|
done()
|
|
1910
1980
|
})
|
|
1911
1981
|
|
|
1912
1982
|
instance.get('/', {
|
|
1913
1983
|
preHandler: (req, reply, done) => {
|
|
1914
|
-
t.ok('called')
|
|
1984
|
+
t.assert.ok('called')
|
|
1915
1985
|
done()
|
|
1916
1986
|
}
|
|
1917
1987
|
}, function (request, reply) {
|
|
@@ -1923,12 +1993,12 @@ test('Register an hook after a plugin inside a plugin (with preHandler option)',
|
|
|
1923
1993
|
|
|
1924
1994
|
fastify.register(fp(function (instance, opts, done) {
|
|
1925
1995
|
instance.addHook('preHandler', function (req, reply, done) {
|
|
1926
|
-
t.ok('called')
|
|
1996
|
+
t.assert.ok('called')
|
|
1927
1997
|
done()
|
|
1928
1998
|
})
|
|
1929
1999
|
|
|
1930
2000
|
instance.addHook('preHandler', function (req, reply, done) {
|
|
1931
|
-
t.ok('called')
|
|
2001
|
+
t.assert.ok('called')
|
|
1932
2002
|
done()
|
|
1933
2003
|
})
|
|
1934
2004
|
|
|
@@ -1939,13 +2009,14 @@ test('Register an hook after a plugin inside a plugin (with preHandler option)',
|
|
|
1939
2009
|
url: '/',
|
|
1940
2010
|
method: 'GET'
|
|
1941
2011
|
}, (err, res) => {
|
|
1942
|
-
t.
|
|
1943
|
-
t.
|
|
1944
|
-
t.
|
|
2012
|
+
t.assert.ifError(err)
|
|
2013
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2014
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
2015
|
+
testDone()
|
|
1945
2016
|
})
|
|
1946
2017
|
})
|
|
1947
2018
|
|
|
1948
|
-
test('Register hooks inside a plugin after an encapsulated plugin', t => {
|
|
2019
|
+
test('Register hooks inside a plugin after an encapsulated plugin', (t, testDone) => {
|
|
1949
2020
|
t.plan(7)
|
|
1950
2021
|
const fastify = Fastify()
|
|
1951
2022
|
|
|
@@ -1959,22 +2030,22 @@ test('Register hooks inside a plugin after an encapsulated plugin', t => {
|
|
|
1959
2030
|
|
|
1960
2031
|
fastify.register(fp(function (instance, opts, done) {
|
|
1961
2032
|
instance.addHook('onRequest', function (req, reply, done) {
|
|
1962
|
-
t.ok('called')
|
|
2033
|
+
t.assert.ok('called')
|
|
1963
2034
|
done()
|
|
1964
2035
|
})
|
|
1965
2036
|
|
|
1966
2037
|
instance.addHook('preHandler', function (request, reply, done) {
|
|
1967
|
-
t.ok('called')
|
|
2038
|
+
t.assert.ok('called')
|
|
1968
2039
|
done()
|
|
1969
2040
|
})
|
|
1970
2041
|
|
|
1971
2042
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
1972
|
-
t.ok('called')
|
|
2043
|
+
t.assert.ok('called')
|
|
1973
2044
|
done()
|
|
1974
2045
|
})
|
|
1975
2046
|
|
|
1976
2047
|
instance.addHook('onResponse', function (request, reply, done) {
|
|
1977
|
-
t.ok('called')
|
|
2048
|
+
t.assert.ok('called')
|
|
1978
2049
|
done()
|
|
1979
2050
|
})
|
|
1980
2051
|
|
|
@@ -1982,31 +2053,32 @@ test('Register hooks inside a plugin after an encapsulated plugin', t => {
|
|
|
1982
2053
|
}))
|
|
1983
2054
|
|
|
1984
2055
|
fastify.inject('/', (err, res) => {
|
|
1985
|
-
t.
|
|
1986
|
-
t.
|
|
1987
|
-
t.
|
|
2056
|
+
t.assert.ifError(err)
|
|
2057
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2058
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
2059
|
+
testDone()
|
|
1988
2060
|
})
|
|
1989
2061
|
})
|
|
1990
2062
|
|
|
1991
|
-
test('onRequest hooks should run in the order in which they are defined', t => {
|
|
2063
|
+
test('onRequest hooks should run in the order in which they are defined', (t, testDone) => {
|
|
1992
2064
|
t.plan(9)
|
|
1993
2065
|
const fastify = Fastify()
|
|
1994
2066
|
|
|
1995
2067
|
fastify.register(function (instance, opts, done) {
|
|
1996
2068
|
instance.addHook('onRequest', function (req, reply, done) {
|
|
1997
|
-
t.
|
|
2069
|
+
t.assert.strictEqual(req.previous, undefined)
|
|
1998
2070
|
req.previous = 1
|
|
1999
2071
|
done()
|
|
2000
2072
|
})
|
|
2001
2073
|
|
|
2002
2074
|
instance.get('/', function (request, reply) {
|
|
2003
|
-
t.
|
|
2075
|
+
t.assert.strictEqual(request.previous, 5)
|
|
2004
2076
|
reply.send({ hello: 'world' })
|
|
2005
2077
|
})
|
|
2006
2078
|
|
|
2007
2079
|
instance.register(fp(function (i, opts, done) {
|
|
2008
2080
|
i.addHook('onRequest', function (req, reply, done) {
|
|
2009
|
-
t.
|
|
2081
|
+
t.assert.strictEqual(req.previous, 1)
|
|
2010
2082
|
req.previous = 2
|
|
2011
2083
|
done()
|
|
2012
2084
|
})
|
|
@@ -2018,14 +2090,14 @@ test('onRequest hooks should run in the order in which they are defined', t => {
|
|
|
2018
2090
|
|
|
2019
2091
|
fastify.register(fp(function (instance, opts, done) {
|
|
2020
2092
|
instance.addHook('onRequest', function (req, reply, done) {
|
|
2021
|
-
t.
|
|
2093
|
+
t.assert.strictEqual(req.previous, 2)
|
|
2022
2094
|
req.previous = 3
|
|
2023
2095
|
done()
|
|
2024
2096
|
})
|
|
2025
2097
|
|
|
2026
2098
|
instance.register(fp(function (i, opts, done) {
|
|
2027
2099
|
i.addHook('onRequest', function (req, reply, done) {
|
|
2028
|
-
t.
|
|
2100
|
+
t.assert.strictEqual(req.previous, 3)
|
|
2029
2101
|
req.previous = 4
|
|
2030
2102
|
done()
|
|
2031
2103
|
})
|
|
@@ -2033,7 +2105,7 @@ test('onRequest hooks should run in the order in which they are defined', t => {
|
|
|
2033
2105
|
}))
|
|
2034
2106
|
|
|
2035
2107
|
instance.addHook('onRequest', function (req, reply, done) {
|
|
2036
|
-
t.
|
|
2108
|
+
t.assert.strictEqual(req.previous, 4)
|
|
2037
2109
|
req.previous = 5
|
|
2038
2110
|
done()
|
|
2039
2111
|
})
|
|
@@ -2042,31 +2114,32 @@ test('onRequest hooks should run in the order in which they are defined', t => {
|
|
|
2042
2114
|
}))
|
|
2043
2115
|
|
|
2044
2116
|
fastify.inject('/', (err, res) => {
|
|
2045
|
-
t.
|
|
2046
|
-
t.
|
|
2047
|
-
t.
|
|
2117
|
+
t.assert.ifError(err)
|
|
2118
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2119
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
2120
|
+
testDone()
|
|
2048
2121
|
})
|
|
2049
2122
|
})
|
|
2050
2123
|
|
|
2051
|
-
test('preHandler hooks should run in the order in which they are defined', t => {
|
|
2124
|
+
test('preHandler hooks should run in the order in which they are defined', (t, testDone) => {
|
|
2052
2125
|
t.plan(9)
|
|
2053
2126
|
const fastify = Fastify()
|
|
2054
2127
|
|
|
2055
2128
|
fastify.register(function (instance, opts, done) {
|
|
2056
2129
|
instance.addHook('preHandler', function (request, reply, done) {
|
|
2057
|
-
t.
|
|
2130
|
+
t.assert.strictEqual(request.previous, undefined)
|
|
2058
2131
|
request.previous = 1
|
|
2059
2132
|
done()
|
|
2060
2133
|
})
|
|
2061
2134
|
|
|
2062
2135
|
instance.get('/', function (request, reply) {
|
|
2063
|
-
t.
|
|
2136
|
+
t.assert.strictEqual(request.previous, 5)
|
|
2064
2137
|
reply.send({ hello: 'world' })
|
|
2065
2138
|
})
|
|
2066
2139
|
|
|
2067
2140
|
instance.register(fp(function (i, opts, done) {
|
|
2068
2141
|
i.addHook('preHandler', function (request, reply, done) {
|
|
2069
|
-
t.
|
|
2142
|
+
t.assert.strictEqual(request.previous, 1)
|
|
2070
2143
|
request.previous = 2
|
|
2071
2144
|
done()
|
|
2072
2145
|
})
|
|
@@ -2078,14 +2151,14 @@ test('preHandler hooks should run in the order in which they are defined', t =>
|
|
|
2078
2151
|
|
|
2079
2152
|
fastify.register(fp(function (instance, opts, done) {
|
|
2080
2153
|
instance.addHook('preHandler', function (request, reply, done) {
|
|
2081
|
-
t.
|
|
2154
|
+
t.assert.strictEqual(request.previous, 2)
|
|
2082
2155
|
request.previous = 3
|
|
2083
2156
|
done()
|
|
2084
2157
|
})
|
|
2085
2158
|
|
|
2086
2159
|
instance.register(fp(function (i, opts, done) {
|
|
2087
2160
|
i.addHook('preHandler', function (request, reply, done) {
|
|
2088
|
-
t.
|
|
2161
|
+
t.assert.strictEqual(request.previous, 3)
|
|
2089
2162
|
request.previous = 4
|
|
2090
2163
|
done()
|
|
2091
2164
|
})
|
|
@@ -2093,7 +2166,7 @@ test('preHandler hooks should run in the order in which they are defined', t =>
|
|
|
2093
2166
|
}))
|
|
2094
2167
|
|
|
2095
2168
|
instance.addHook('preHandler', function (request, reply, done) {
|
|
2096
|
-
t.
|
|
2169
|
+
t.assert.strictEqual(request.previous, 4)
|
|
2097
2170
|
request.previous = 5
|
|
2098
2171
|
done()
|
|
2099
2172
|
})
|
|
@@ -2102,19 +2175,20 @@ test('preHandler hooks should run in the order in which they are defined', t =>
|
|
|
2102
2175
|
}))
|
|
2103
2176
|
|
|
2104
2177
|
fastify.inject('/', (err, res) => {
|
|
2105
|
-
t.
|
|
2106
|
-
t.
|
|
2107
|
-
t.
|
|
2178
|
+
t.assert.ifError(err)
|
|
2179
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2180
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
2181
|
+
testDone()
|
|
2108
2182
|
})
|
|
2109
2183
|
})
|
|
2110
2184
|
|
|
2111
|
-
test('onSend hooks should run in the order in which they are defined', t => {
|
|
2185
|
+
test('onSend hooks should run in the order in which they are defined', (t, testDone) => {
|
|
2112
2186
|
t.plan(8)
|
|
2113
2187
|
const fastify = Fastify()
|
|
2114
2188
|
|
|
2115
2189
|
fastify.register(function (instance, opts, done) {
|
|
2116
2190
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
2117
|
-
t.
|
|
2191
|
+
t.assert.strictEqual(request.previous, undefined)
|
|
2118
2192
|
request.previous = 1
|
|
2119
2193
|
done()
|
|
2120
2194
|
})
|
|
@@ -2125,7 +2199,7 @@ test('onSend hooks should run in the order in which they are defined', t => {
|
|
|
2125
2199
|
|
|
2126
2200
|
instance.register(fp(function (i, opts, done) {
|
|
2127
2201
|
i.addHook('onSend', function (request, reply, payload, done) {
|
|
2128
|
-
t.
|
|
2202
|
+
t.assert.strictEqual(request.previous, 1)
|
|
2129
2203
|
request.previous = 2
|
|
2130
2204
|
done()
|
|
2131
2205
|
})
|
|
@@ -2137,14 +2211,14 @@ test('onSend hooks should run in the order in which they are defined', t => {
|
|
|
2137
2211
|
|
|
2138
2212
|
fastify.register(fp(function (instance, opts, done) {
|
|
2139
2213
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
2140
|
-
t.
|
|
2214
|
+
t.assert.strictEqual(request.previous, 2)
|
|
2141
2215
|
request.previous = 3
|
|
2142
2216
|
done()
|
|
2143
2217
|
})
|
|
2144
2218
|
|
|
2145
2219
|
instance.register(fp(function (i, opts, done) {
|
|
2146
2220
|
i.addHook('onSend', function (request, reply, payload, done) {
|
|
2147
|
-
t.
|
|
2221
|
+
t.assert.strictEqual(request.previous, 3)
|
|
2148
2222
|
request.previous = 4
|
|
2149
2223
|
done()
|
|
2150
2224
|
})
|
|
@@ -2152,7 +2226,7 @@ test('onSend hooks should run in the order in which they are defined', t => {
|
|
|
2152
2226
|
}))
|
|
2153
2227
|
|
|
2154
2228
|
instance.addHook('onSend', function (request, reply, payload, done) {
|
|
2155
|
-
t.
|
|
2229
|
+
t.assert.strictEqual(request.previous, 4)
|
|
2156
2230
|
done(null, '5')
|
|
2157
2231
|
})
|
|
2158
2232
|
|
|
@@ -2160,19 +2234,20 @@ test('onSend hooks should run in the order in which they are defined', t => {
|
|
|
2160
2234
|
}))
|
|
2161
2235
|
|
|
2162
2236
|
fastify.inject('/', (err, res) => {
|
|
2163
|
-
t.
|
|
2164
|
-
t.
|
|
2165
|
-
t.
|
|
2237
|
+
t.assert.ifError(err)
|
|
2238
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2239
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), 5)
|
|
2240
|
+
testDone()
|
|
2166
2241
|
})
|
|
2167
2242
|
})
|
|
2168
2243
|
|
|
2169
|
-
test('onResponse hooks should run in the order in which they are defined', t => {
|
|
2244
|
+
test('onResponse hooks should run in the order in which they are defined', (t, testDone) => {
|
|
2170
2245
|
t.plan(8)
|
|
2171
2246
|
const fastify = Fastify()
|
|
2172
2247
|
|
|
2173
2248
|
fastify.register(function (instance, opts, done) {
|
|
2174
2249
|
instance.addHook('onResponse', function (request, reply, done) {
|
|
2175
|
-
t.
|
|
2250
|
+
t.assert.strictEqual(reply.previous, undefined)
|
|
2176
2251
|
reply.previous = 1
|
|
2177
2252
|
done()
|
|
2178
2253
|
})
|
|
@@ -2183,7 +2258,7 @@ test('onResponse hooks should run in the order in which they are defined', t =>
|
|
|
2183
2258
|
|
|
2184
2259
|
instance.register(fp(function (i, opts, done) {
|
|
2185
2260
|
i.addHook('onResponse', function (request, reply, done) {
|
|
2186
|
-
t.
|
|
2261
|
+
t.assert.strictEqual(reply.previous, 1)
|
|
2187
2262
|
reply.previous = 2
|
|
2188
2263
|
done()
|
|
2189
2264
|
})
|
|
@@ -2195,14 +2270,14 @@ test('onResponse hooks should run in the order in which they are defined', t =>
|
|
|
2195
2270
|
|
|
2196
2271
|
fastify.register(fp(function (instance, opts, done) {
|
|
2197
2272
|
instance.addHook('onResponse', function (request, reply, done) {
|
|
2198
|
-
t.
|
|
2273
|
+
t.assert.strictEqual(reply.previous, 2)
|
|
2199
2274
|
reply.previous = 3
|
|
2200
2275
|
done()
|
|
2201
2276
|
})
|
|
2202
2277
|
|
|
2203
2278
|
instance.register(fp(function (i, opts, done) {
|
|
2204
2279
|
i.addHook('onResponse', function (request, reply, done) {
|
|
2205
|
-
t.
|
|
2280
|
+
t.assert.strictEqual(reply.previous, 3)
|
|
2206
2281
|
reply.previous = 4
|
|
2207
2282
|
done()
|
|
2208
2283
|
})
|
|
@@ -2210,7 +2285,7 @@ test('onResponse hooks should run in the order in which they are defined', t =>
|
|
|
2210
2285
|
}))
|
|
2211
2286
|
|
|
2212
2287
|
instance.addHook('onResponse', function (request, reply, done) {
|
|
2213
|
-
t.
|
|
2288
|
+
t.assert.strictEqual(reply.previous, 4)
|
|
2214
2289
|
done()
|
|
2215
2290
|
})
|
|
2216
2291
|
|
|
@@ -2218,13 +2293,14 @@ test('onResponse hooks should run in the order in which they are defined', t =>
|
|
|
2218
2293
|
}))
|
|
2219
2294
|
|
|
2220
2295
|
fastify.inject('/', (err, res) => {
|
|
2221
|
-
t.
|
|
2222
|
-
t.
|
|
2223
|
-
t.
|
|
2296
|
+
t.assert.ifError(err)
|
|
2297
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2298
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
|
|
2299
|
+
testDone()
|
|
2224
2300
|
})
|
|
2225
2301
|
})
|
|
2226
2302
|
|
|
2227
|
-
test('onRequest, preHandler, and onResponse hooks that resolve to a value do not cause an error', t => {
|
|
2303
|
+
test('onRequest, preHandler, and onResponse hooks that resolve to a value do not cause an error', (t, testDone) => {
|
|
2228
2304
|
t.plan(3)
|
|
2229
2305
|
const fastify = Fastify()
|
|
2230
2306
|
|
|
@@ -2243,13 +2319,14 @@ test('onRequest, preHandler, and onResponse hooks that resolve to a value do not
|
|
|
2243
2319
|
})
|
|
2244
2320
|
|
|
2245
2321
|
fastify.inject('/', (err, res) => {
|
|
2246
|
-
t.
|
|
2247
|
-
t.
|
|
2248
|
-
t.
|
|
2322
|
+
t.assert.ifError(err)
|
|
2323
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2324
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
2325
|
+
testDone()
|
|
2249
2326
|
})
|
|
2250
2327
|
})
|
|
2251
2328
|
|
|
2252
|
-
test('If a response header has been set inside an hook it should not be overwritten by the final response handler', t => {
|
|
2329
|
+
test('If a response header has been set inside an hook it should not be overwritten by the final response handler', (t, testDone) => {
|
|
2253
2330
|
t.plan(5)
|
|
2254
2331
|
const fastify = Fastify()
|
|
2255
2332
|
|
|
@@ -2263,15 +2340,16 @@ test('If a response header has been set inside an hook it should not be overwrit
|
|
|
2263
2340
|
})
|
|
2264
2341
|
|
|
2265
2342
|
fastify.inject('/', (err, res) => {
|
|
2266
|
-
t.
|
|
2267
|
-
t.
|
|
2268
|
-
t.
|
|
2269
|
-
t.
|
|
2270
|
-
t.
|
|
2343
|
+
t.assert.ifError(err)
|
|
2344
|
+
t.assert.strictEqual(res.headers['x-custom-header'], 'hello')
|
|
2345
|
+
t.assert.strictEqual(res.headers['content-type'], 'text/plain; charset=utf-8')
|
|
2346
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2347
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
2348
|
+
testDone()
|
|
2271
2349
|
})
|
|
2272
2350
|
})
|
|
2273
2351
|
|
|
2274
|
-
test('If the content type has been set inside an hook it should not be changed', t => {
|
|
2352
|
+
test('If the content type has been set inside an hook it should not be changed', (t, testDone) => {
|
|
2275
2353
|
t.plan(5)
|
|
2276
2354
|
const fastify = Fastify()
|
|
2277
2355
|
|
|
@@ -2281,27 +2359,28 @@ test('If the content type has been set inside an hook it should not be changed',
|
|
|
2281
2359
|
})
|
|
2282
2360
|
|
|
2283
2361
|
fastify.get('/', (request, reply) => {
|
|
2284
|
-
t.ok(reply[symbols.kReplyHeaders]['content-type'])
|
|
2362
|
+
t.assert.ok(reply[symbols.kReplyHeaders]['content-type'])
|
|
2285
2363
|
reply.send('hello')
|
|
2286
2364
|
})
|
|
2287
2365
|
|
|
2288
2366
|
fastify.inject('/', (err, res) => {
|
|
2289
|
-
t.
|
|
2290
|
-
t.
|
|
2291
|
-
t.
|
|
2292
|
-
t.
|
|
2367
|
+
t.assert.ifError(err)
|
|
2368
|
+
t.assert.strictEqual(res.headers['content-type'], 'text/html')
|
|
2369
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2370
|
+
t.assert.strictEqual(res.payload, 'hello')
|
|
2371
|
+
testDone()
|
|
2293
2372
|
})
|
|
2294
2373
|
})
|
|
2295
2374
|
|
|
2296
|
-
test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
2375
|
+
test('request in onRequest, preParsing, preValidation and onResponse', (t, testDone) => {
|
|
2297
2376
|
t.plan(18)
|
|
2298
2377
|
const fastify = Fastify()
|
|
2299
2378
|
|
|
2300
2379
|
fastify.addHook('onRequest', function (request, reply, done) {
|
|
2301
|
-
t.
|
|
2302
|
-
t.
|
|
2303
|
-
t.
|
|
2304
|
-
t.
|
|
2380
|
+
t.assert.deepStrictEqual(request.body, undefined)
|
|
2381
|
+
t.assert.deepStrictEqual(request.query.key, 'value')
|
|
2382
|
+
t.assert.deepStrictEqual(request.params.greeting, 'hello')
|
|
2383
|
+
t.assert.deepStrictEqual(request.headers, {
|
|
2305
2384
|
'content-length': '17',
|
|
2306
2385
|
'content-type': 'application/json',
|
|
2307
2386
|
host: 'localhost:80',
|
|
@@ -2312,10 +2391,10 @@ test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
|
2312
2391
|
})
|
|
2313
2392
|
|
|
2314
2393
|
fastify.addHook('preParsing', function (request, reply, payload, done) {
|
|
2315
|
-
t.
|
|
2316
|
-
t.
|
|
2317
|
-
t.
|
|
2318
|
-
t.
|
|
2394
|
+
t.assert.deepStrictEqual(request.body, undefined)
|
|
2395
|
+
t.assert.deepStrictEqual(request.query.key, 'value')
|
|
2396
|
+
t.assert.deepStrictEqual(request.params.greeting, 'hello')
|
|
2397
|
+
t.assert.deepStrictEqual(request.headers, {
|
|
2319
2398
|
'content-length': '17',
|
|
2320
2399
|
'content-type': 'application/json',
|
|
2321
2400
|
host: 'localhost:80',
|
|
@@ -2326,10 +2405,10 @@ test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
|
2326
2405
|
})
|
|
2327
2406
|
|
|
2328
2407
|
fastify.addHook('preValidation', function (request, reply, done) {
|
|
2329
|
-
t.
|
|
2330
|
-
t.
|
|
2331
|
-
t.
|
|
2332
|
-
t.
|
|
2408
|
+
t.assert.deepStrictEqual(request.body, { hello: 'world' })
|
|
2409
|
+
t.assert.deepStrictEqual(request.query.key, 'value')
|
|
2410
|
+
t.assert.deepStrictEqual(request.params.greeting, 'hello')
|
|
2411
|
+
t.assert.deepStrictEqual(request.headers, {
|
|
2333
2412
|
'content-length': '17',
|
|
2334
2413
|
'content-type': 'application/json',
|
|
2335
2414
|
host: 'localhost:80',
|
|
@@ -2340,10 +2419,10 @@ test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
|
2340
2419
|
})
|
|
2341
2420
|
|
|
2342
2421
|
fastify.addHook('onResponse', function (request, reply, done) {
|
|
2343
|
-
t.
|
|
2344
|
-
t.
|
|
2345
|
-
t.
|
|
2346
|
-
t.
|
|
2422
|
+
t.assert.deepStrictEqual(request.body, { hello: 'world' })
|
|
2423
|
+
t.assert.deepStrictEqual(request.query.key, 'value')
|
|
2424
|
+
t.assert.deepStrictEqual(request.params.greeting, 'hello')
|
|
2425
|
+
t.assert.deepStrictEqual(request.headers, {
|
|
2347
2426
|
'content-length': '17',
|
|
2348
2427
|
'content-type': 'application/json',
|
|
2349
2428
|
host: 'localhost:80',
|
|
@@ -2363,18 +2442,19 @@ test('request in onRequest, preParsing, preValidation and onResponse', t => {
|
|
|
2363
2442
|
headers: { 'x-custom': 'hello' },
|
|
2364
2443
|
payload: { hello: 'world' }
|
|
2365
2444
|
}, (err, res) => {
|
|
2366
|
-
t.
|
|
2367
|
-
t.
|
|
2445
|
+
t.assert.ifError(err)
|
|
2446
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2447
|
+
testDone()
|
|
2368
2448
|
})
|
|
2369
2449
|
})
|
|
2370
2450
|
|
|
2371
|
-
test('preValidation hook should support encapsulation / 1', t => {
|
|
2451
|
+
test('preValidation hook should support encapsulation / 1', (t, testDone) => {
|
|
2372
2452
|
t.plan(5)
|
|
2373
2453
|
const fastify = Fastify()
|
|
2374
2454
|
|
|
2375
2455
|
fastify.register((instance, opts, done) => {
|
|
2376
2456
|
instance.addHook('preValidation', (req, reply, done) => {
|
|
2377
|
-
t.
|
|
2457
|
+
t.assert.strictEqual(req.raw.url, '/plugin')
|
|
2378
2458
|
done()
|
|
2379
2459
|
})
|
|
2380
2460
|
|
|
@@ -2390,17 +2470,17 @@ test('preValidation hook should support encapsulation / 1', t => {
|
|
|
2390
2470
|
})
|
|
2391
2471
|
|
|
2392
2472
|
fastify.inject('/root', (err, res) => {
|
|
2393
|
-
t.
|
|
2394
|
-
t.
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2473
|
+
t.assert.ifError(err)
|
|
2474
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2475
|
+
fastify.inject('/plugin', (err, res) => {
|
|
2476
|
+
t.assert.ifError(err)
|
|
2477
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2478
|
+
testDone()
|
|
2479
|
+
})
|
|
2400
2480
|
})
|
|
2401
2481
|
})
|
|
2402
2482
|
|
|
2403
|
-
test('preValidation hook should support encapsulation / 2', t => {
|
|
2483
|
+
test('preValidation hook should support encapsulation / 2', (t, testDone) => {
|
|
2404
2484
|
t.plan(3)
|
|
2405
2485
|
const fastify = Fastify()
|
|
2406
2486
|
let pluginInstance
|
|
@@ -2414,21 +2494,22 @@ test('preValidation hook should support encapsulation / 2', t => {
|
|
|
2414
2494
|
})
|
|
2415
2495
|
|
|
2416
2496
|
fastify.ready(err => {
|
|
2417
|
-
t.
|
|
2418
|
-
t.
|
|
2419
|
-
t.
|
|
2497
|
+
t.assert.ifError(err)
|
|
2498
|
+
t.assert.strictEqual(fastify[symbols.kHooks].preValidation.length, 1)
|
|
2499
|
+
t.assert.strictEqual(pluginInstance[symbols.kHooks].preValidation.length, 2)
|
|
2500
|
+
testDone()
|
|
2420
2501
|
})
|
|
2421
2502
|
})
|
|
2422
2503
|
|
|
2423
|
-
test('preValidation hook should support encapsulation / 3', t => {
|
|
2504
|
+
test('preValidation hook should support encapsulation / 3', (t, testDone) => {
|
|
2424
2505
|
t.plan(20)
|
|
2425
2506
|
const fastify = Fastify()
|
|
2426
|
-
t.
|
|
2507
|
+
t.after(() => { fastify.close() })
|
|
2427
2508
|
fastify.decorate('hello', 'world')
|
|
2428
2509
|
|
|
2429
2510
|
fastify.addHook('preValidation', function (req, reply, done) {
|
|
2430
|
-
t.ok(this.hello)
|
|
2431
|
-
t.ok(this.hello2)
|
|
2511
|
+
t.assert.ok(this.hello)
|
|
2512
|
+
t.assert.ok(this.hello2)
|
|
2432
2513
|
req.first = true
|
|
2433
2514
|
done()
|
|
2434
2515
|
})
|
|
@@ -2436,24 +2517,24 @@ test('preValidation hook should support encapsulation / 3', t => {
|
|
|
2436
2517
|
fastify.decorate('hello2', 'world')
|
|
2437
2518
|
|
|
2438
2519
|
fastify.get('/first', (req, reply) => {
|
|
2439
|
-
t.ok(req.first)
|
|
2440
|
-
t.
|
|
2520
|
+
t.assert.ok(req.first)
|
|
2521
|
+
t.assert.ok(!req.second)
|
|
2441
2522
|
reply.send({ hello: 'world' })
|
|
2442
2523
|
})
|
|
2443
2524
|
|
|
2444
2525
|
fastify.register((instance, opts, done) => {
|
|
2445
2526
|
instance.decorate('hello3', 'world')
|
|
2446
2527
|
instance.addHook('preValidation', function (req, reply, done) {
|
|
2447
|
-
t.ok(this.hello)
|
|
2448
|
-
t.ok(this.hello2)
|
|
2449
|
-
t.ok(this.hello3)
|
|
2528
|
+
t.assert.ok(this.hello)
|
|
2529
|
+
t.assert.ok(this.hello2)
|
|
2530
|
+
t.assert.ok(this.hello3)
|
|
2450
2531
|
req.second = true
|
|
2451
2532
|
done()
|
|
2452
2533
|
})
|
|
2453
2534
|
|
|
2454
2535
|
instance.get('/second', (req, reply) => {
|
|
2455
|
-
t.ok(req.first)
|
|
2456
|
-
t.ok(req.second)
|
|
2536
|
+
t.assert.ok(req.first)
|
|
2537
|
+
t.assert.ok(req.second)
|
|
2457
2538
|
reply.send({ hello: 'world' })
|
|
2458
2539
|
})
|
|
2459
2540
|
|
|
@@ -2461,31 +2542,34 @@ test('preValidation hook should support encapsulation / 3', t => {
|
|
|
2461
2542
|
})
|
|
2462
2543
|
|
|
2463
2544
|
fastify.listen({ port: 0 }, err => {
|
|
2464
|
-
t.
|
|
2545
|
+
t.assert.ifError(err)
|
|
2465
2546
|
|
|
2547
|
+
const completion = waitForCb({ steps: 2 })
|
|
2466
2548
|
sget({
|
|
2467
2549
|
method: 'GET',
|
|
2468
2550
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
2469
2551
|
}, (err, response, body) => {
|
|
2470
|
-
t.
|
|
2471
|
-
t.
|
|
2472
|
-
t.
|
|
2473
|
-
t.
|
|
2552
|
+
t.assert.ifError(err)
|
|
2553
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2554
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
2555
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
2556
|
+
completion.stepIn()
|
|
2474
2557
|
})
|
|
2475
|
-
|
|
2476
2558
|
sget({
|
|
2477
2559
|
method: 'GET',
|
|
2478
2560
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
2479
2561
|
}, (err, response, body) => {
|
|
2480
|
-
t.
|
|
2481
|
-
t.
|
|
2482
|
-
t.
|
|
2483
|
-
t.
|
|
2562
|
+
t.assert.ifError(err)
|
|
2563
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2564
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
2565
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
2566
|
+
completion.stepIn()
|
|
2484
2567
|
})
|
|
2568
|
+
completion.patience.then(testDone)
|
|
2485
2569
|
})
|
|
2486
2570
|
})
|
|
2487
2571
|
|
|
2488
|
-
test('onError hook', t => {
|
|
2572
|
+
test('onError hook', (t, testDone) => {
|
|
2489
2573
|
t.plan(3)
|
|
2490
2574
|
|
|
2491
2575
|
const fastify = Fastify()
|
|
@@ -2493,7 +2577,7 @@ test('onError hook', t => {
|
|
|
2493
2577
|
const err = new Error('kaboom')
|
|
2494
2578
|
|
|
2495
2579
|
fastify.addHook('onError', (request, reply, error, done) => {
|
|
2496
|
-
t.
|
|
2580
|
+
t.assert.deepStrictEqual(error, err)
|
|
2497
2581
|
done()
|
|
2498
2582
|
})
|
|
2499
2583
|
|
|
@@ -2505,16 +2589,17 @@ test('onError hook', t => {
|
|
|
2505
2589
|
method: 'GET',
|
|
2506
2590
|
url: '/'
|
|
2507
2591
|
}, (err, res) => {
|
|
2508
|
-
t.
|
|
2509
|
-
t.
|
|
2592
|
+
t.assert.ifError(err)
|
|
2593
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
2510
2594
|
error: 'Internal Server Error',
|
|
2511
2595
|
message: 'kaboom',
|
|
2512
2596
|
statusCode: 500
|
|
2513
2597
|
})
|
|
2598
|
+
testDone()
|
|
2514
2599
|
})
|
|
2515
2600
|
})
|
|
2516
2601
|
|
|
2517
|
-
test('reply.send should throw if called inside the onError hook', t => {
|
|
2602
|
+
test('reply.send should throw if called inside the onError hook', (t, testDone) => {
|
|
2518
2603
|
t.plan(3)
|
|
2519
2604
|
|
|
2520
2605
|
const fastify = Fastify()
|
|
@@ -2524,9 +2609,9 @@ test('reply.send should throw if called inside the onError hook', t => {
|
|
|
2524
2609
|
fastify.addHook('onError', (request, reply, error, done) => {
|
|
2525
2610
|
try {
|
|
2526
2611
|
reply.send()
|
|
2527
|
-
t.fail('Should throw')
|
|
2612
|
+
t.assert.fail('Should throw')
|
|
2528
2613
|
} catch (err) {
|
|
2529
|
-
t.
|
|
2614
|
+
t.assert.strictEqual(err.code, 'FST_ERR_SEND_INSIDE_ONERR')
|
|
2530
2615
|
}
|
|
2531
2616
|
done()
|
|
2532
2617
|
})
|
|
@@ -2539,57 +2624,55 @@ test('reply.send should throw if called inside the onError hook', t => {
|
|
|
2539
2624
|
method: 'GET',
|
|
2540
2625
|
url: '/'
|
|
2541
2626
|
}, (err, res) => {
|
|
2542
|
-
t.
|
|
2543
|
-
t.
|
|
2627
|
+
t.assert.ifError(err)
|
|
2628
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
2544
2629
|
error: 'Internal Server Error',
|
|
2545
2630
|
message: 'kaboom',
|
|
2546
2631
|
statusCode: 500
|
|
2547
2632
|
})
|
|
2633
|
+
testDone()
|
|
2548
2634
|
})
|
|
2549
2635
|
})
|
|
2550
2636
|
|
|
2551
|
-
test('onError hook with setErrorHandler', t => {
|
|
2552
|
-
t.
|
|
2553
|
-
t.plan(3)
|
|
2637
|
+
test('onError hook with setErrorHandler', (t, testDone) => {
|
|
2638
|
+
t.plan(3)
|
|
2554
2639
|
|
|
2555
|
-
|
|
2640
|
+
const fastify = Fastify()
|
|
2556
2641
|
|
|
2557
|
-
|
|
2558
|
-
|
|
2642
|
+
const external = new Error('ouch')
|
|
2643
|
+
const internal = new Error('kaboom')
|
|
2559
2644
|
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2645
|
+
fastify.setErrorHandler((_, req, reply) => {
|
|
2646
|
+
reply.send(external)
|
|
2647
|
+
})
|
|
2563
2648
|
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2649
|
+
fastify.addHook('onError', (request, reply, error, done) => {
|
|
2650
|
+
t.assert.deepStrictEqual(error, internal)
|
|
2651
|
+
done()
|
|
2652
|
+
})
|
|
2568
2653
|
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2654
|
+
fastify.get('/', (req, reply) => {
|
|
2655
|
+
reply.send(internal)
|
|
2656
|
+
})
|
|
2572
2657
|
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
})
|
|
2658
|
+
fastify.inject({
|
|
2659
|
+
method: 'GET',
|
|
2660
|
+
url: '/'
|
|
2661
|
+
}, (err, res) => {
|
|
2662
|
+
t.assert.ifError(err)
|
|
2663
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
2664
|
+
error: 'Internal Server Error',
|
|
2665
|
+
message: 'ouch',
|
|
2666
|
+
statusCode: 500
|
|
2583
2667
|
})
|
|
2668
|
+
testDone()
|
|
2584
2669
|
})
|
|
2585
|
-
|
|
2586
|
-
t.end()
|
|
2587
2670
|
})
|
|
2588
2671
|
|
|
2589
|
-
test('preParsing hook should run before parsing and be able to modify the payload', t => {
|
|
2672
|
+
test('preParsing hook should run before parsing and be able to modify the payload', (t, testDone) => {
|
|
2590
2673
|
t.plan(5)
|
|
2591
2674
|
const fastify = Fastify()
|
|
2592
|
-
t.
|
|
2675
|
+
t.after(() => { fastify.close() })
|
|
2593
2676
|
|
|
2594
2677
|
fastify.addHook('preParsing', function (req, reply, payload, done) {
|
|
2595
2678
|
const modified = new stream.Readable()
|
|
@@ -2608,7 +2691,7 @@ test('preParsing hook should run before parsing and be able to modify the payloa
|
|
|
2608
2691
|
})
|
|
2609
2692
|
|
|
2610
2693
|
fastify.listen({ port: 0 }, err => {
|
|
2611
|
-
t.
|
|
2694
|
+
t.assert.ifError(err)
|
|
2612
2695
|
|
|
2613
2696
|
sget({
|
|
2614
2697
|
method: 'POST',
|
|
@@ -2616,18 +2699,19 @@ test('preParsing hook should run before parsing and be able to modify the payloa
|
|
|
2616
2699
|
body: { hello: 'world' },
|
|
2617
2700
|
json: true
|
|
2618
2701
|
}, (err, response, body) => {
|
|
2619
|
-
t.
|
|
2620
|
-
t.
|
|
2621
|
-
t.
|
|
2622
|
-
t.
|
|
2702
|
+
t.assert.ifError(err)
|
|
2703
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2704
|
+
t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
|
|
2705
|
+
t.assert.deepStrictEqual(body, { hello: 'another world' })
|
|
2706
|
+
testDone()
|
|
2623
2707
|
})
|
|
2624
2708
|
})
|
|
2625
2709
|
})
|
|
2626
2710
|
|
|
2627
|
-
test('preParsing hooks should run in the order in which they are defined', t => {
|
|
2711
|
+
test('preParsing hooks should run in the order in which they are defined', (t, testDone) => {
|
|
2628
2712
|
t.plan(5)
|
|
2629
2713
|
const fastify = Fastify()
|
|
2630
|
-
t.
|
|
2714
|
+
t.after(() => { fastify.close() })
|
|
2631
2715
|
|
|
2632
2716
|
fastify.addHook('preParsing', function (req, reply, payload, done) {
|
|
2633
2717
|
const modified = new stream.Readable()
|
|
@@ -2651,7 +2735,7 @@ test('preParsing hooks should run in the order in which they are defined', t =>
|
|
|
2651
2735
|
})
|
|
2652
2736
|
|
|
2653
2737
|
fastify.listen({ port: 0 }, err => {
|
|
2654
|
-
t.
|
|
2738
|
+
t.assert.ifError(err)
|
|
2655
2739
|
|
|
2656
2740
|
sget({
|
|
2657
2741
|
method: 'POST',
|
|
@@ -2659,18 +2743,19 @@ test('preParsing hooks should run in the order in which they are defined', t =>
|
|
|
2659
2743
|
body: { hello: 'world' },
|
|
2660
2744
|
json: true
|
|
2661
2745
|
}, (err, response, body) => {
|
|
2662
|
-
t.
|
|
2663
|
-
t.
|
|
2664
|
-
t.
|
|
2665
|
-
t.
|
|
2746
|
+
t.assert.ifError(err)
|
|
2747
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2748
|
+
t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
|
|
2749
|
+
t.assert.deepStrictEqual(body, { hello: 'another world' })
|
|
2750
|
+
testDone()
|
|
2666
2751
|
})
|
|
2667
2752
|
})
|
|
2668
2753
|
})
|
|
2669
2754
|
|
|
2670
|
-
test('preParsing hooks should support encapsulation', t => {
|
|
2755
|
+
test('preParsing hooks should support encapsulation', (t, testDone) => {
|
|
2671
2756
|
t.plan(9)
|
|
2672
2757
|
const fastify = Fastify()
|
|
2673
|
-
t.
|
|
2758
|
+
t.after(() => { fastify.close() })
|
|
2674
2759
|
|
|
2675
2760
|
fastify.addHook('preParsing', function (req, reply, payload, done) {
|
|
2676
2761
|
const modified = new stream.Readable()
|
|
@@ -2701,41 +2786,44 @@ test('preParsing hooks should support encapsulation', t => {
|
|
|
2701
2786
|
})
|
|
2702
2787
|
|
|
2703
2788
|
fastify.listen({ port: 0 }, err => {
|
|
2704
|
-
t.
|
|
2789
|
+
t.assert.ifError(err)
|
|
2705
2790
|
|
|
2791
|
+
const completion = waitForCb({ steps: 2 })
|
|
2706
2792
|
sget({
|
|
2707
2793
|
method: 'POST',
|
|
2708
2794
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first',
|
|
2709
2795
|
body: { hello: 'world' },
|
|
2710
2796
|
json: true
|
|
2711
2797
|
}, (err, response, body) => {
|
|
2712
|
-
t.
|
|
2713
|
-
t.
|
|
2714
|
-
t.
|
|
2715
|
-
t.
|
|
2798
|
+
t.assert.ifError(err)
|
|
2799
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2800
|
+
t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
|
|
2801
|
+
t.assert.deepStrictEqual(body, { hello: 'another world' })
|
|
2802
|
+
completion.stepIn()
|
|
2716
2803
|
})
|
|
2717
|
-
|
|
2718
2804
|
sget({
|
|
2719
2805
|
method: 'POST',
|
|
2720
2806
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second',
|
|
2721
2807
|
body: { hello: 'world' },
|
|
2722
2808
|
json: true
|
|
2723
2809
|
}, (err, response, body) => {
|
|
2724
|
-
t.
|
|
2725
|
-
t.
|
|
2726
|
-
t.
|
|
2727
|
-
t.
|
|
2810
|
+
t.assert.ifError(err)
|
|
2811
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2812
|
+
t.assert.strictEqual(response.headers['content-length'], '' + JSON.stringify(body).length)
|
|
2813
|
+
t.assert.deepStrictEqual(body, { hello: 'encapsulated world' })
|
|
2814
|
+
completion.stepIn()
|
|
2728
2815
|
})
|
|
2816
|
+
completion.patience.then(testDone)
|
|
2729
2817
|
})
|
|
2730
2818
|
})
|
|
2731
2819
|
|
|
2732
|
-
test('preParsing hook should support encapsulation / 1', t => {
|
|
2820
|
+
test('preParsing hook should support encapsulation / 1', (t, testDone) => {
|
|
2733
2821
|
t.plan(5)
|
|
2734
2822
|
const fastify = Fastify()
|
|
2735
2823
|
|
|
2736
2824
|
fastify.register((instance, opts, done) => {
|
|
2737
2825
|
instance.addHook('preParsing', (req, reply, payload, done) => {
|
|
2738
|
-
t.
|
|
2826
|
+
t.assert.strictEqual(req.raw.url, '/plugin')
|
|
2739
2827
|
done()
|
|
2740
2828
|
})
|
|
2741
2829
|
|
|
@@ -2751,17 +2839,17 @@ test('preParsing hook should support encapsulation / 1', t => {
|
|
|
2751
2839
|
})
|
|
2752
2840
|
|
|
2753
2841
|
fastify.inject('/root', (err, res) => {
|
|
2754
|
-
t.
|
|
2755
|
-
t.
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2842
|
+
t.assert.ifError(err)
|
|
2843
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2844
|
+
fastify.inject('/plugin', (err, res) => {
|
|
2845
|
+
t.assert.ifError(err)
|
|
2846
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
2847
|
+
testDone()
|
|
2848
|
+
})
|
|
2761
2849
|
})
|
|
2762
2850
|
})
|
|
2763
2851
|
|
|
2764
|
-
test('preParsing hook should support encapsulation / 2', t => {
|
|
2852
|
+
test('preParsing hook should support encapsulation / 2', (t, testDone) => {
|
|
2765
2853
|
t.plan(3)
|
|
2766
2854
|
const fastify = Fastify()
|
|
2767
2855
|
let pluginInstance
|
|
@@ -2775,21 +2863,22 @@ test('preParsing hook should support encapsulation / 2', t => {
|
|
|
2775
2863
|
})
|
|
2776
2864
|
|
|
2777
2865
|
fastify.ready(err => {
|
|
2778
|
-
t.
|
|
2779
|
-
t.
|
|
2780
|
-
t.
|
|
2866
|
+
t.assert.ifError(err)
|
|
2867
|
+
t.assert.strictEqual(fastify[symbols.kHooks].preParsing.length, 1)
|
|
2868
|
+
t.assert.strictEqual(pluginInstance[symbols.kHooks].preParsing.length, 2)
|
|
2869
|
+
testDone()
|
|
2781
2870
|
})
|
|
2782
2871
|
})
|
|
2783
2872
|
|
|
2784
|
-
test('preParsing hook should support encapsulation / 3', t => {
|
|
2873
|
+
test('preParsing hook should support encapsulation / 3', (t, testDone) => {
|
|
2785
2874
|
t.plan(20)
|
|
2786
2875
|
const fastify = Fastify()
|
|
2787
|
-
t.
|
|
2876
|
+
t.after(() => { fastify.close() })
|
|
2788
2877
|
fastify.decorate('hello', 'world')
|
|
2789
2878
|
|
|
2790
2879
|
fastify.addHook('preParsing', function (req, reply, payload, done) {
|
|
2791
|
-
t.ok(this.hello)
|
|
2792
|
-
t.ok(this.hello2)
|
|
2880
|
+
t.assert.ok(this.hello)
|
|
2881
|
+
t.assert.ok(this.hello2)
|
|
2793
2882
|
req.first = true
|
|
2794
2883
|
done()
|
|
2795
2884
|
})
|
|
@@ -2797,24 +2886,24 @@ test('preParsing hook should support encapsulation / 3', t => {
|
|
|
2797
2886
|
fastify.decorate('hello2', 'world')
|
|
2798
2887
|
|
|
2799
2888
|
fastify.get('/first', (req, reply) => {
|
|
2800
|
-
t.ok(req.first)
|
|
2801
|
-
t.
|
|
2889
|
+
t.assert.ok(req.first)
|
|
2890
|
+
t.assert.ok(!req.second)
|
|
2802
2891
|
reply.send({ hello: 'world' })
|
|
2803
2892
|
})
|
|
2804
2893
|
|
|
2805
2894
|
fastify.register((instance, opts, done) => {
|
|
2806
2895
|
instance.decorate('hello3', 'world')
|
|
2807
2896
|
instance.addHook('preParsing', function (req, reply, payload, done) {
|
|
2808
|
-
t.ok(this.hello)
|
|
2809
|
-
t.ok(this.hello2)
|
|
2810
|
-
t.ok(this.hello3)
|
|
2897
|
+
t.assert.ok(this.hello)
|
|
2898
|
+
t.assert.ok(this.hello2)
|
|
2899
|
+
t.assert.ok(this.hello3)
|
|
2811
2900
|
req.second = true
|
|
2812
2901
|
done()
|
|
2813
2902
|
})
|
|
2814
2903
|
|
|
2815
2904
|
instance.get('/second', (req, reply) => {
|
|
2816
|
-
t.ok(req.first)
|
|
2817
|
-
t.ok(req.second)
|
|
2905
|
+
t.assert.ok(req.first)
|
|
2906
|
+
t.assert.ok(req.second)
|
|
2818
2907
|
reply.send({ hello: 'world' })
|
|
2819
2908
|
})
|
|
2820
2909
|
|
|
@@ -2822,34 +2911,37 @@ test('preParsing hook should support encapsulation / 3', t => {
|
|
|
2822
2911
|
})
|
|
2823
2912
|
|
|
2824
2913
|
fastify.listen({ port: 0 }, err => {
|
|
2825
|
-
t.
|
|
2914
|
+
t.assert.ifError(err)
|
|
2826
2915
|
|
|
2916
|
+
const completion = waitForCb({ steps: 2 })
|
|
2827
2917
|
sget({
|
|
2828
2918
|
method: 'GET',
|
|
2829
2919
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
2830
2920
|
}, (err, response, body) => {
|
|
2831
|
-
t.
|
|
2832
|
-
t.
|
|
2833
|
-
t.
|
|
2834
|
-
t.
|
|
2921
|
+
t.assert.ifError(err)
|
|
2922
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2923
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
2924
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
2925
|
+
completion.stepIn()
|
|
2835
2926
|
})
|
|
2836
|
-
|
|
2837
2927
|
sget({
|
|
2838
2928
|
method: 'GET',
|
|
2839
2929
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
2840
2930
|
}, (err, response, body) => {
|
|
2841
|
-
t.
|
|
2842
|
-
t.
|
|
2843
|
-
t.
|
|
2844
|
-
t.
|
|
2931
|
+
t.assert.ifError(err)
|
|
2932
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2933
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
2934
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
2935
|
+
completion.stepIn()
|
|
2845
2936
|
})
|
|
2937
|
+
completion.patience.then(testDone)
|
|
2846
2938
|
})
|
|
2847
2939
|
})
|
|
2848
2940
|
|
|
2849
|
-
test('preSerialization hook should run before serialization and be able to modify the payload', t => {
|
|
2941
|
+
test('preSerialization hook should run before serialization and be able to modify the payload', (t, testDone) => {
|
|
2850
2942
|
t.plan(5)
|
|
2851
2943
|
const fastify = Fastify()
|
|
2852
|
-
t.
|
|
2944
|
+
t.after(() => { fastify.close() })
|
|
2853
2945
|
|
|
2854
2946
|
fastify.addHook('preSerialization', function (req, reply, payload, done) {
|
|
2855
2947
|
payload.hello += '1'
|
|
@@ -2884,30 +2976,31 @@ test('preSerialization hook should run before serialization and be able to modif
|
|
|
2884
2976
|
})
|
|
2885
2977
|
|
|
2886
2978
|
fastify.listen({ port: 0 }, err => {
|
|
2887
|
-
t.
|
|
2979
|
+
t.assert.ifError(err)
|
|
2888
2980
|
|
|
2889
2981
|
sget({
|
|
2890
2982
|
method: 'GET',
|
|
2891
2983
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
2892
2984
|
}, (err, response, body) => {
|
|
2893
|
-
t.
|
|
2894
|
-
t.
|
|
2895
|
-
t.
|
|
2896
|
-
t.
|
|
2985
|
+
t.assert.ifError(err)
|
|
2986
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
2987
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
2988
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world1', world: 'ok' })
|
|
2989
|
+
testDone()
|
|
2897
2990
|
})
|
|
2898
2991
|
})
|
|
2899
2992
|
})
|
|
2900
2993
|
|
|
2901
|
-
test('preSerialization hook should be able to throw errors which are validated against schema response', t => {
|
|
2994
|
+
test('preSerialization hook should be able to throw errors which are validated against schema response', (t, testDone) => {
|
|
2902
2995
|
const fastify = Fastify()
|
|
2903
|
-
t.
|
|
2996
|
+
t.after(() => { fastify.close() })
|
|
2904
2997
|
|
|
2905
2998
|
fastify.addHook('preSerialization', function (req, reply, payload, done) {
|
|
2906
2999
|
done(new Error('preSerialization aborted'))
|
|
2907
3000
|
})
|
|
2908
3001
|
|
|
2909
3002
|
fastify.setErrorHandler((err, request, reply) => {
|
|
2910
|
-
t.
|
|
3003
|
+
t.assert.strictEqual(err.message, 'preSerialization aborted')
|
|
2911
3004
|
err.world = 'error'
|
|
2912
3005
|
reply.send(err)
|
|
2913
3006
|
})
|
|
@@ -2935,32 +3028,32 @@ test('preSerialization hook should be able to throw errors which are validated a
|
|
|
2935
3028
|
})
|
|
2936
3029
|
|
|
2937
3030
|
fastify.listen({ port: 0 }, err => {
|
|
2938
|
-
t.
|
|
3031
|
+
t.assert.ifError(err)
|
|
2939
3032
|
|
|
2940
3033
|
sget({
|
|
2941
3034
|
method: 'GET',
|
|
2942
3035
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
2943
3036
|
}, (err, response, body) => {
|
|
2944
|
-
t.
|
|
2945
|
-
t.
|
|
2946
|
-
t.
|
|
2947
|
-
t.
|
|
2948
|
-
|
|
3037
|
+
t.assert.ifError(err)
|
|
3038
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
3039
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
3040
|
+
t.assert.deepStrictEqual(JSON.parse(body), { world: 'error' })
|
|
3041
|
+
testDone()
|
|
2949
3042
|
})
|
|
2950
3043
|
})
|
|
2951
3044
|
})
|
|
2952
3045
|
|
|
2953
|
-
test('preSerialization hook which returned error should still run onError hooks', t => {
|
|
3046
|
+
test('preSerialization hook which returned error should still run onError hooks', (t, testDone) => {
|
|
2954
3047
|
t.plan(4)
|
|
2955
3048
|
const fastify = Fastify()
|
|
2956
|
-
t.
|
|
3049
|
+
t.after(() => { fastify.close() })
|
|
2957
3050
|
|
|
2958
3051
|
fastify.addHook('preSerialization', function (req, reply, payload, done) {
|
|
2959
3052
|
done(new Error('preSerialization aborted'))
|
|
2960
3053
|
})
|
|
2961
3054
|
|
|
2962
3055
|
fastify.addHook('onError', function (req, reply, payload, done) {
|
|
2963
|
-
t.pass
|
|
3056
|
+
t.assert.ok('should pass')
|
|
2964
3057
|
done()
|
|
2965
3058
|
})
|
|
2966
3059
|
|
|
@@ -2969,22 +3062,23 @@ test('preSerialization hook which returned error should still run onError hooks'
|
|
|
2969
3062
|
})
|
|
2970
3063
|
|
|
2971
3064
|
fastify.listen({ port: 0 }, err => {
|
|
2972
|
-
t.
|
|
3065
|
+
t.assert.ifError(err)
|
|
2973
3066
|
|
|
2974
3067
|
sget({
|
|
2975
3068
|
method: 'GET',
|
|
2976
3069
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
2977
3070
|
}, (err, response, body) => {
|
|
2978
|
-
t.
|
|
2979
|
-
t.
|
|
3071
|
+
t.assert.ifError(err)
|
|
3072
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
3073
|
+
testDone()
|
|
2980
3074
|
})
|
|
2981
3075
|
})
|
|
2982
3076
|
})
|
|
2983
3077
|
|
|
2984
|
-
test('preSerialization hooks should run in the order in which they are defined', t => {
|
|
3078
|
+
test('preSerialization hooks should run in the order in which they are defined', (t, testDone) => {
|
|
2985
3079
|
t.plan(5)
|
|
2986
3080
|
const fastify = Fastify()
|
|
2987
|
-
t.
|
|
3081
|
+
t.after(() => { fastify.close() })
|
|
2988
3082
|
|
|
2989
3083
|
fastify.addHook('preSerialization', function (req, reply, payload, done) {
|
|
2990
3084
|
payload.hello += '2'
|
|
@@ -3003,24 +3097,25 @@ test('preSerialization hooks should run in the order in which they are defined',
|
|
|
3003
3097
|
})
|
|
3004
3098
|
|
|
3005
3099
|
fastify.listen({ port: 0 }, err => {
|
|
3006
|
-
t.
|
|
3100
|
+
t.assert.ifError(err)
|
|
3007
3101
|
|
|
3008
3102
|
sget({
|
|
3009
3103
|
method: 'GET',
|
|
3010
3104
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
3011
3105
|
}, (err, response, body) => {
|
|
3012
|
-
t.
|
|
3013
|
-
t.
|
|
3014
|
-
t.
|
|
3015
|
-
t.
|
|
3106
|
+
t.assert.ifError(err)
|
|
3107
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
3108
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
3109
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world21' })
|
|
3110
|
+
testDone()
|
|
3016
3111
|
})
|
|
3017
3112
|
})
|
|
3018
3113
|
})
|
|
3019
3114
|
|
|
3020
|
-
test('preSerialization hooks should support encapsulation', t => {
|
|
3115
|
+
test('preSerialization hooks should support encapsulation', (t, testDone) => {
|
|
3021
3116
|
t.plan(9)
|
|
3022
3117
|
const fastify = Fastify()
|
|
3023
|
-
t.
|
|
3118
|
+
t.after(() => { fastify.close() })
|
|
3024
3119
|
|
|
3025
3120
|
fastify.addHook('preSerialization', function (req, reply, payload, done) {
|
|
3026
3121
|
payload.hello += '1'
|
|
@@ -3047,39 +3142,42 @@ test('preSerialization hooks should support encapsulation', t => {
|
|
|
3047
3142
|
})
|
|
3048
3143
|
|
|
3049
3144
|
fastify.listen({ port: 0 }, err => {
|
|
3050
|
-
t.
|
|
3145
|
+
t.assert.ifError(err)
|
|
3051
3146
|
|
|
3147
|
+
const completion = waitForCb({ steps: 2 })
|
|
3052
3148
|
sget({
|
|
3053
3149
|
method: 'GET',
|
|
3054
3150
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/first'
|
|
3055
3151
|
}, (err, response, body) => {
|
|
3056
|
-
t.
|
|
3057
|
-
t.
|
|
3058
|
-
t.
|
|
3059
|
-
t.
|
|
3152
|
+
t.assert.ifError(err)
|
|
3153
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
3154
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
3155
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world1' })
|
|
3156
|
+
completion.stepIn()
|
|
3060
3157
|
})
|
|
3061
|
-
|
|
3062
3158
|
sget({
|
|
3063
3159
|
method: 'GET',
|
|
3064
3160
|
url: 'http://127.0.0.1:' + fastify.server.address().port + '/second'
|
|
3065
3161
|
}, (err, response, body) => {
|
|
3066
|
-
t.
|
|
3067
|
-
t.
|
|
3068
|
-
t.
|
|
3069
|
-
t.
|
|
3162
|
+
t.assert.ifError(err)
|
|
3163
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
3164
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
3165
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world12' })
|
|
3166
|
+
completion.stepIn()
|
|
3070
3167
|
})
|
|
3168
|
+
completion.patience.then(testDone)
|
|
3071
3169
|
})
|
|
3072
3170
|
})
|
|
3073
3171
|
|
|
3074
|
-
test('onRegister hook should be called / 1', t => {
|
|
3172
|
+
test('onRegister hook should be called / 1', (t, testDone) => {
|
|
3075
3173
|
t.plan(5)
|
|
3076
3174
|
const fastify = Fastify()
|
|
3077
3175
|
|
|
3078
3176
|
fastify.addHook('onRegister', function (instance, opts, done) {
|
|
3079
|
-
t.ok(this.addHook)
|
|
3080
|
-
t.ok(instance.addHook)
|
|
3081
|
-
t.
|
|
3082
|
-
t.
|
|
3177
|
+
t.assert.ok(this.addHook)
|
|
3178
|
+
t.assert.ok(instance.addHook)
|
|
3179
|
+
t.assert.deepStrictEqual(opts, pluginOpts)
|
|
3180
|
+
t.assert.ok(!done)
|
|
3083
3181
|
})
|
|
3084
3182
|
|
|
3085
3183
|
const pluginOpts = { prefix: 'hello', custom: 'world' }
|
|
@@ -3087,16 +3185,19 @@ test('onRegister hook should be called / 1', t => {
|
|
|
3087
3185
|
done()
|
|
3088
3186
|
}, pluginOpts)
|
|
3089
3187
|
|
|
3090
|
-
fastify.ready(err => {
|
|
3188
|
+
fastify.ready(err => {
|
|
3189
|
+
t.assert.ifError(err)
|
|
3190
|
+
testDone()
|
|
3191
|
+
})
|
|
3091
3192
|
})
|
|
3092
3193
|
|
|
3093
|
-
test('onRegister hook should be called / 2', t => {
|
|
3194
|
+
test('onRegister hook should be called / 2', (t, testDone) => {
|
|
3094
3195
|
t.plan(7)
|
|
3095
3196
|
const fastify = Fastify()
|
|
3096
3197
|
|
|
3097
3198
|
fastify.addHook('onRegister', function (instance) {
|
|
3098
|
-
t.ok(this.addHook)
|
|
3099
|
-
t.ok(instance.addHook)
|
|
3199
|
+
t.assert.ok(this.addHook)
|
|
3200
|
+
t.assert.ok(instance.addHook)
|
|
3100
3201
|
})
|
|
3101
3202
|
|
|
3102
3203
|
fastify.register((instance, opts, done) => {
|
|
@@ -3111,11 +3212,12 @@ test('onRegister hook should be called / 2', t => {
|
|
|
3111
3212
|
})
|
|
3112
3213
|
|
|
3113
3214
|
fastify.ready(err => {
|
|
3114
|
-
t.
|
|
3215
|
+
t.assert.ifError(err)
|
|
3216
|
+
testDone()
|
|
3115
3217
|
})
|
|
3116
3218
|
})
|
|
3117
3219
|
|
|
3118
|
-
test('onRegister hook should be called / 3', t => {
|
|
3220
|
+
test('onRegister hook should be called / 3', (t, testDone) => {
|
|
3119
3221
|
t.plan(4)
|
|
3120
3222
|
const fastify = Fastify()
|
|
3121
3223
|
|
|
@@ -3129,24 +3231,25 @@ test('onRegister hook should be called / 3', t => {
|
|
|
3129
3231
|
instance.data.push(1)
|
|
3130
3232
|
instance.register((instance, opts, done) => {
|
|
3131
3233
|
instance.data.push(2)
|
|
3132
|
-
t.
|
|
3234
|
+
t.assert.deepStrictEqual(instance.data, [1, 2])
|
|
3133
3235
|
done()
|
|
3134
3236
|
})
|
|
3135
|
-
t.
|
|
3237
|
+
t.assert.deepStrictEqual(instance.data, [1])
|
|
3136
3238
|
done()
|
|
3137
3239
|
})
|
|
3138
3240
|
|
|
3139
3241
|
fastify.register((instance, opts, done) => {
|
|
3140
|
-
t.
|
|
3242
|
+
t.assert.deepStrictEqual(instance.data, [])
|
|
3141
3243
|
done()
|
|
3142
3244
|
})
|
|
3143
3245
|
|
|
3144
3246
|
fastify.ready(err => {
|
|
3145
|
-
t.
|
|
3247
|
+
t.assert.ifError(err)
|
|
3248
|
+
testDone()
|
|
3146
3249
|
})
|
|
3147
3250
|
})
|
|
3148
3251
|
|
|
3149
|
-
test('onRegister hook should be called (encapsulation)', t => {
|
|
3252
|
+
test('onRegister hook should be called (encapsulation)', (t, testDone) => {
|
|
3150
3253
|
t.plan(1)
|
|
3151
3254
|
const fastify = Fastify()
|
|
3152
3255
|
|
|
@@ -3156,17 +3259,18 @@ test('onRegister hook should be called (encapsulation)', t => {
|
|
|
3156
3259
|
plugin[Symbol.for('skip-override')] = true
|
|
3157
3260
|
|
|
3158
3261
|
fastify.addHook('onRegister', (instance, opts) => {
|
|
3159
|
-
t.fail('This should not be called')
|
|
3262
|
+
t.assert.fail('This should not be called')
|
|
3160
3263
|
})
|
|
3161
3264
|
|
|
3162
3265
|
fastify.register(plugin)
|
|
3163
3266
|
|
|
3164
3267
|
fastify.ready(err => {
|
|
3165
|
-
t.
|
|
3268
|
+
t.assert.ifError(err)
|
|
3269
|
+
testDone()
|
|
3166
3270
|
})
|
|
3167
3271
|
})
|
|
3168
3272
|
|
|
3169
|
-
test('early termination, onRequest', t => {
|
|
3273
|
+
test('early termination, onRequest', (t, testDone) => {
|
|
3170
3274
|
t.plan(3)
|
|
3171
3275
|
|
|
3172
3276
|
const app = Fastify()
|
|
@@ -3177,17 +3281,18 @@ test('early termination, onRequest', t => {
|
|
|
3177
3281
|
})
|
|
3178
3282
|
|
|
3179
3283
|
app.get('/', (req, reply) => {
|
|
3180
|
-
t.fail('should not happen')
|
|
3284
|
+
t.assert.fail('should not happen')
|
|
3181
3285
|
})
|
|
3182
3286
|
|
|
3183
3287
|
app.inject('/', function (err, res) {
|
|
3184
|
-
t.
|
|
3185
|
-
t.
|
|
3186
|
-
t.
|
|
3288
|
+
t.assert.ifError(err)
|
|
3289
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
3290
|
+
t.assert.strictEqual(res.body.toString(), 'hello world')
|
|
3291
|
+
testDone()
|
|
3187
3292
|
})
|
|
3188
3293
|
})
|
|
3189
3294
|
|
|
3190
|
-
test('reply.send should throw if undefined error is thrown', t => {
|
|
3295
|
+
test('reply.send should throw if undefined error is thrown', (t, testDone) => {
|
|
3191
3296
|
/* eslint prefer-promise-reject-errors: ["error", {"allowEmptyReject": true}] */
|
|
3192
3297
|
|
|
3193
3298
|
t.plan(3)
|
|
@@ -3205,18 +3310,19 @@ test('reply.send should throw if undefined error is thrown', t => {
|
|
|
3205
3310
|
method: 'GET',
|
|
3206
3311
|
url: '/'
|
|
3207
3312
|
}, (err, res) => {
|
|
3208
|
-
t.
|
|
3209
|
-
t.
|
|
3210
|
-
t.
|
|
3313
|
+
t.assert.ifError(err)
|
|
3314
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
3315
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
3211
3316
|
error: 'Internal Server Error',
|
|
3212
3317
|
code: 'FST_ERR_SEND_UNDEFINED_ERR',
|
|
3213
3318
|
message: 'Undefined error has occurred',
|
|
3214
3319
|
statusCode: 500
|
|
3215
3320
|
})
|
|
3321
|
+
testDone()
|
|
3216
3322
|
})
|
|
3217
3323
|
})
|
|
3218
3324
|
|
|
3219
|
-
test('reply.send should throw if undefined error is thrown at preParsing hook', t => {
|
|
3325
|
+
test('reply.send should throw if undefined error is thrown at preParsing hook', (t, testDone) => {
|
|
3220
3326
|
t.plan(3)
|
|
3221
3327
|
const fastify = Fastify()
|
|
3222
3328
|
|
|
@@ -3232,18 +3338,19 @@ test('reply.send should throw if undefined error is thrown at preParsing hook',
|
|
|
3232
3338
|
method: 'GET',
|
|
3233
3339
|
url: '/'
|
|
3234
3340
|
}, (err, res) => {
|
|
3235
|
-
t.
|
|
3236
|
-
t.
|
|
3237
|
-
t.
|
|
3341
|
+
t.assert.ifError(err)
|
|
3342
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
3343
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
3238
3344
|
error: 'Internal Server Error',
|
|
3239
3345
|
code: 'FST_ERR_SEND_UNDEFINED_ERR',
|
|
3240
3346
|
message: 'Undefined error has occurred',
|
|
3241
3347
|
statusCode: 500
|
|
3242
3348
|
})
|
|
3349
|
+
testDone()
|
|
3243
3350
|
})
|
|
3244
3351
|
})
|
|
3245
3352
|
|
|
3246
|
-
test('reply.send should throw if undefined error is thrown at onSend hook', t => {
|
|
3353
|
+
test('reply.send should throw if undefined error is thrown at onSend hook', (t, testDone) => {
|
|
3247
3354
|
t.plan(3)
|
|
3248
3355
|
const fastify = Fastify()
|
|
3249
3356
|
|
|
@@ -3259,24 +3366,25 @@ test('reply.send should throw if undefined error is thrown at onSend hook', t =>
|
|
|
3259
3366
|
method: 'GET',
|
|
3260
3367
|
url: '/'
|
|
3261
3368
|
}, (err, res) => {
|
|
3262
|
-
t.
|
|
3263
|
-
t.
|
|
3264
|
-
t.
|
|
3369
|
+
t.assert.ifError(err)
|
|
3370
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
3371
|
+
t.assert.deepStrictEqual(JSON.parse(res.payload), {
|
|
3265
3372
|
error: 'Internal Server Error',
|
|
3266
3373
|
code: 'FST_ERR_SEND_UNDEFINED_ERR',
|
|
3267
3374
|
message: 'Undefined error has occurred',
|
|
3268
3375
|
statusCode: 500
|
|
3269
3376
|
})
|
|
3377
|
+
testDone()
|
|
3270
3378
|
})
|
|
3271
3379
|
})
|
|
3272
3380
|
|
|
3273
|
-
test('onTimeout should be triggered', t => {
|
|
3381
|
+
test('onTimeout should be triggered', (t, testDone) => {
|
|
3274
3382
|
t.plan(6)
|
|
3275
3383
|
const fastify = Fastify({ connectionTimeout: 500 })
|
|
3276
|
-
t.
|
|
3384
|
+
t.after(() => { fastify.close() })
|
|
3277
3385
|
|
|
3278
3386
|
fastify.addHook('onTimeout', function (req, res, done) {
|
|
3279
|
-
t.ok('called', 'onTimeout')
|
|
3387
|
+
t.assert.ok('called', 'onTimeout')
|
|
3280
3388
|
done()
|
|
3281
3389
|
})
|
|
3282
3390
|
|
|
@@ -3289,32 +3397,36 @@ test('onTimeout should be triggered', t => {
|
|
|
3289
3397
|
})
|
|
3290
3398
|
|
|
3291
3399
|
fastify.listen({ port: 0 }, (err, address) => {
|
|
3292
|
-
t.
|
|
3400
|
+
t.assert.ifError(err)
|
|
3293
3401
|
|
|
3402
|
+
const completion = waitForCb({ steps: 2 })
|
|
3294
3403
|
sget({
|
|
3295
3404
|
method: 'GET',
|
|
3296
3405
|
url: address
|
|
3297
3406
|
}, (err, response, body) => {
|
|
3298
|
-
t.
|
|
3299
|
-
t.
|
|
3407
|
+
t.assert.ifError(err)
|
|
3408
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
3409
|
+
completion.stepIn()
|
|
3300
3410
|
})
|
|
3301
3411
|
sget({
|
|
3302
3412
|
method: 'GET',
|
|
3303
3413
|
url: `${address}/timeout`
|
|
3304
3414
|
}, (err, response, body) => {
|
|
3305
|
-
t.
|
|
3306
|
-
t.
|
|
3415
|
+
t.assert.ok(err, Error)
|
|
3416
|
+
t.assert.strictEqual(err.message, 'socket hang up')
|
|
3417
|
+
completion.stepIn()
|
|
3307
3418
|
})
|
|
3419
|
+
completion.patience.then(testDone)
|
|
3308
3420
|
})
|
|
3309
3421
|
})
|
|
3310
3422
|
|
|
3311
|
-
test('onTimeout should be triggered and socket _meta is set', t => {
|
|
3423
|
+
test('onTimeout should be triggered and socket _meta is set', (t, testDone) => {
|
|
3312
3424
|
t.plan(6)
|
|
3313
3425
|
const fastify = Fastify({ connectionTimeout: 500 })
|
|
3314
|
-
t.
|
|
3426
|
+
t.after(() => { fastify.close() })
|
|
3315
3427
|
|
|
3316
3428
|
fastify.addHook('onTimeout', function (req, res, done) {
|
|
3317
|
-
t.ok('called', 'onTimeout')
|
|
3429
|
+
t.assert.ok('called', 'onTimeout')
|
|
3318
3430
|
done()
|
|
3319
3431
|
})
|
|
3320
3432
|
|
|
@@ -3328,22 +3440,26 @@ test('onTimeout should be triggered and socket _meta is set', t => {
|
|
|
3328
3440
|
})
|
|
3329
3441
|
|
|
3330
3442
|
fastify.listen({ port: 0 }, (err, address) => {
|
|
3331
|
-
t.
|
|
3443
|
+
t.assert.ifError(err)
|
|
3332
3444
|
|
|
3445
|
+
const completion = waitForCb({ steps: 2 })
|
|
3333
3446
|
sget({
|
|
3334
3447
|
method: 'GET',
|
|
3335
3448
|
url: address
|
|
3336
3449
|
}, (err, response, body) => {
|
|
3337
|
-
t.
|
|
3338
|
-
t.
|
|
3450
|
+
t.assert.ifError(err)
|
|
3451
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
3452
|
+
completion.stepIn()
|
|
3339
3453
|
})
|
|
3340
3454
|
sget({
|
|
3341
3455
|
method: 'GET',
|
|
3342
3456
|
url: `${address}/timeout`
|
|
3343
3457
|
}, (err, response, body) => {
|
|
3344
|
-
t.
|
|
3345
|
-
t.
|
|
3458
|
+
t.assert.ok(err, Error)
|
|
3459
|
+
t.assert.strictEqual(err.message, 'socket hang up')
|
|
3460
|
+
completion.stepIn()
|
|
3346
3461
|
})
|
|
3462
|
+
completion.patience.then(testDone)
|
|
3347
3463
|
})
|
|
3348
3464
|
})
|
|
3349
3465
|
|
|
@@ -3352,7 +3468,7 @@ test('registering invalid hooks should throw an error', async t => {
|
|
|
3352
3468
|
|
|
3353
3469
|
const fastify = Fastify()
|
|
3354
3470
|
|
|
3355
|
-
t.throws(() => {
|
|
3471
|
+
t.assert.throws(() => {
|
|
3356
3472
|
fastify.route({
|
|
3357
3473
|
method: 'GET',
|
|
3358
3474
|
path: '/invalidHook',
|
|
@@ -3361,9 +3477,11 @@ test('registering invalid hooks should throw an error', async t => {
|
|
|
3361
3477
|
return 'hello world'
|
|
3362
3478
|
}
|
|
3363
3479
|
})
|
|
3364
|
-
},
|
|
3480
|
+
}, {
|
|
3481
|
+
message: 'onRequest hook should be a function, instead got [object Undefined]'
|
|
3482
|
+
})
|
|
3365
3483
|
|
|
3366
|
-
t.throws(() => {
|
|
3484
|
+
t.assert.throws(() => {
|
|
3367
3485
|
fastify.route({
|
|
3368
3486
|
method: 'GET',
|
|
3369
3487
|
path: '/invalidHook',
|
|
@@ -3372,7 +3490,7 @@ test('registering invalid hooks should throw an error', async t => {
|
|
|
3372
3490
|
return 'hello world'
|
|
3373
3491
|
}
|
|
3374
3492
|
})
|
|
3375
|
-
},
|
|
3493
|
+
}, { message: 'onRequest hook should be a function, instead got [object Null]' })
|
|
3376
3494
|
|
|
3377
3495
|
// undefined is ok
|
|
3378
3496
|
fastify.route({
|
|
@@ -3384,7 +3502,7 @@ test('registering invalid hooks should throw an error', async t => {
|
|
|
3384
3502
|
}
|
|
3385
3503
|
})
|
|
3386
3504
|
|
|
3387
|
-
t.throws(() => {
|
|
3505
|
+
t.assert.throws(() => {
|
|
3388
3506
|
fastify.addHook('onRoute', (routeOptions) => {
|
|
3389
3507
|
routeOptions.onSend = [undefined]
|
|
3390
3508
|
})
|
|
@@ -3392,35 +3510,39 @@ test('registering invalid hooks should throw an error', async t => {
|
|
|
3392
3510
|
fastify.get('/', function (request, reply) {
|
|
3393
3511
|
reply.send('hello world')
|
|
3394
3512
|
})
|
|
3395
|
-
},
|
|
3513
|
+
}, { message: 'onSend hook should be a function, instead got [object Undefined]' })
|
|
3396
3514
|
})
|
|
3397
3515
|
|
|
3398
|
-
test('onRequestAbort should be triggered', t => {
|
|
3516
|
+
test('onRequestAbort should be triggered', (t, testDone) => {
|
|
3399
3517
|
const fastify = Fastify()
|
|
3400
3518
|
let order = 0
|
|
3401
3519
|
|
|
3402
3520
|
t.plan(7)
|
|
3403
|
-
t.
|
|
3521
|
+
t.after(() => fastify.close())
|
|
3522
|
+
|
|
3523
|
+
const completion = waitForCb({ steps: 2 })
|
|
3524
|
+
completion.patience.then(testDone)
|
|
3404
3525
|
|
|
3405
3526
|
fastify.addHook('onRequestAbort', function (req, done) {
|
|
3406
|
-
t.
|
|
3407
|
-
t.ok(req.pendingResolve, 'request has pendingResolve')
|
|
3527
|
+
t.assert.strictEqual(++order, 1, 'called in hook')
|
|
3528
|
+
t.assert.ok(req.pendingResolve, 'request has pendingResolve')
|
|
3408
3529
|
req.pendingResolve()
|
|
3530
|
+
completion.stepIn()
|
|
3409
3531
|
done()
|
|
3410
3532
|
})
|
|
3411
3533
|
|
|
3412
3534
|
fastify.addHook('onError', function hook (request, reply, error, done) {
|
|
3413
|
-
t.fail('onError should not be called')
|
|
3535
|
+
t.assert.fail('onError should not be called')
|
|
3414
3536
|
done()
|
|
3415
3537
|
})
|
|
3416
3538
|
|
|
3417
3539
|
fastify.addHook('onSend', function hook (request, reply, payload, done) {
|
|
3418
|
-
t.
|
|
3540
|
+
t.assert.strictEqual(payload, '{"hello":"world"}', 'onSend should be called')
|
|
3419
3541
|
done(null, payload)
|
|
3420
3542
|
})
|
|
3421
3543
|
|
|
3422
3544
|
fastify.addHook('onResponse', function hook (request, reply, done) {
|
|
3423
|
-
t.fail('onResponse should not be called')
|
|
3545
|
+
t.assert.fail('onResponse should not be called')
|
|
3424
3546
|
done()
|
|
3425
3547
|
})
|
|
3426
3548
|
|
|
@@ -3428,21 +3550,22 @@ test('onRequestAbort should be triggered', t => {
|
|
|
3428
3550
|
method: 'GET',
|
|
3429
3551
|
path: '/',
|
|
3430
3552
|
async handler (request, reply) {
|
|
3431
|
-
t.
|
|
3553
|
+
t.assert.ok('handler called')
|
|
3432
3554
|
let resolvePromise
|
|
3433
3555
|
const promise = new Promise(resolve => { resolvePromise = resolve })
|
|
3434
3556
|
request.pendingResolve = resolvePromise
|
|
3435
3557
|
await promise
|
|
3436
|
-
t.
|
|
3558
|
+
t.assert.ok('handler promise resolved')
|
|
3437
3559
|
return { hello: 'world' }
|
|
3438
3560
|
},
|
|
3439
3561
|
async onRequestAbort (req) {
|
|
3440
|
-
t.
|
|
3562
|
+
t.assert.strictEqual(++order, 2, 'called in route')
|
|
3563
|
+
completion.stepIn()
|
|
3441
3564
|
}
|
|
3442
3565
|
})
|
|
3443
3566
|
|
|
3444
3567
|
fastify.listen({ port: 0 }, err => {
|
|
3445
|
-
t.
|
|
3568
|
+
t.assert.ifError(err)
|
|
3446
3569
|
|
|
3447
3570
|
const socket = connect(fastify.server.address().port)
|
|
3448
3571
|
|
|
@@ -3452,17 +3575,21 @@ test('onRequestAbort should be triggered', t => {
|
|
|
3452
3575
|
})
|
|
3453
3576
|
})
|
|
3454
3577
|
|
|
3455
|
-
test('onRequestAbort should support encapsulation', t => {
|
|
3578
|
+
test('onRequestAbort should support encapsulation', (t, testDone) => {
|
|
3456
3579
|
const fastify = Fastify()
|
|
3457
3580
|
let order = 0
|
|
3458
3581
|
let child
|
|
3459
3582
|
|
|
3460
3583
|
t.plan(6)
|
|
3461
|
-
t.
|
|
3584
|
+
t.after(() => fastify.close())
|
|
3585
|
+
|
|
3586
|
+
const completion = waitForCb({ steps: 2 })
|
|
3587
|
+
completion.patience.then(testDone)
|
|
3462
3588
|
|
|
3463
3589
|
fastify.addHook('onRequestAbort', function (req, done) {
|
|
3464
|
-
t.
|
|
3465
|
-
t.
|
|
3590
|
+
t.assert.strictEqual(++order, 1, 'called in root')
|
|
3591
|
+
t.assert.deepStrictEqual(this.pluginName, child.pluginName)
|
|
3592
|
+
completion.stepIn()
|
|
3466
3593
|
done()
|
|
3467
3594
|
})
|
|
3468
3595
|
|
|
@@ -3470,8 +3597,9 @@ test('onRequestAbort should support encapsulation', t => {
|
|
|
3470
3597
|
child = _child
|
|
3471
3598
|
|
|
3472
3599
|
fastify.addHook('onRequestAbort', async function (req) {
|
|
3473
|
-
t.
|
|
3474
|
-
t.
|
|
3600
|
+
t.assert.strictEqual(++order, 2, 'called in child')
|
|
3601
|
+
t.assert.deepStrictEqual(this.pluginName, child.pluginName)
|
|
3602
|
+
completion.stepIn()
|
|
3475
3603
|
})
|
|
3476
3604
|
|
|
3477
3605
|
child.route({
|
|
@@ -3482,13 +3610,13 @@ test('onRequestAbort should support encapsulation', t => {
|
|
|
3482
3610
|
return { hello: 'world' }
|
|
3483
3611
|
},
|
|
3484
3612
|
async onRequestAbort (_req) {
|
|
3485
|
-
t.
|
|
3613
|
+
t.assert.strictEqual(++order, 3, 'called in route')
|
|
3486
3614
|
}
|
|
3487
3615
|
})
|
|
3488
3616
|
})
|
|
3489
3617
|
|
|
3490
3618
|
fastify.listen({ port: 0 }, err => {
|
|
3491
|
-
t.
|
|
3619
|
+
t.assert.ifError(err)
|
|
3492
3620
|
|
|
3493
3621
|
const socket = connect(fastify.server.address().port)
|
|
3494
3622
|
|
|
@@ -3498,14 +3626,17 @@ test('onRequestAbort should support encapsulation', t => {
|
|
|
3498
3626
|
})
|
|
3499
3627
|
})
|
|
3500
3628
|
|
|
3501
|
-
test('onRequestAbort should handle errors / 1', t => {
|
|
3629
|
+
test('onRequestAbort should handle errors / 1', (t, testDone) => {
|
|
3502
3630
|
const fastify = Fastify()
|
|
3503
3631
|
|
|
3504
3632
|
t.plan(2)
|
|
3505
|
-
t.
|
|
3633
|
+
t.after(() => fastify.close())
|
|
3506
3634
|
|
|
3507
3635
|
fastify.addHook('onRequestAbort', function (req, done) {
|
|
3508
|
-
process.nextTick(() =>
|
|
3636
|
+
process.nextTick(() => {
|
|
3637
|
+
t.assert.ok('should pass')
|
|
3638
|
+
testDone()
|
|
3639
|
+
})
|
|
3509
3640
|
done(new Error('KABOOM!'))
|
|
3510
3641
|
})
|
|
3511
3642
|
|
|
@@ -3519,7 +3650,7 @@ test('onRequestAbort should handle errors / 1', t => {
|
|
|
3519
3650
|
})
|
|
3520
3651
|
|
|
3521
3652
|
fastify.listen({ port: 0 }, err => {
|
|
3522
|
-
t.
|
|
3653
|
+
t.assert.ifError(err)
|
|
3523
3654
|
|
|
3524
3655
|
const socket = connect(fastify.server.address().port)
|
|
3525
3656
|
|
|
@@ -3529,14 +3660,17 @@ test('onRequestAbort should handle errors / 1', t => {
|
|
|
3529
3660
|
})
|
|
3530
3661
|
})
|
|
3531
3662
|
|
|
3532
|
-
test('onRequestAbort should handle errors / 2', t => {
|
|
3663
|
+
test('onRequestAbort should handle errors / 2', (t, testDone) => {
|
|
3533
3664
|
const fastify = Fastify()
|
|
3534
3665
|
|
|
3535
3666
|
t.plan(2)
|
|
3536
|
-
t.
|
|
3667
|
+
t.after(() => fastify.close())
|
|
3537
3668
|
|
|
3538
3669
|
fastify.addHook('onRequestAbort', function (req, done) {
|
|
3539
|
-
process.nextTick(() =>
|
|
3670
|
+
process.nextTick(() => {
|
|
3671
|
+
t.assert.ok('should pass')
|
|
3672
|
+
testDone()
|
|
3673
|
+
})
|
|
3540
3674
|
throw new Error('KABOOM!')
|
|
3541
3675
|
})
|
|
3542
3676
|
|
|
@@ -3550,7 +3684,7 @@ test('onRequestAbort should handle errors / 2', t => {
|
|
|
3550
3684
|
})
|
|
3551
3685
|
|
|
3552
3686
|
fastify.listen({ port: 0 }, err => {
|
|
3553
|
-
t.
|
|
3687
|
+
t.assert.ifError(err)
|
|
3554
3688
|
|
|
3555
3689
|
const socket = connect(fastify.server.address().port)
|
|
3556
3690
|
|
|
@@ -3560,14 +3694,17 @@ test('onRequestAbort should handle errors / 2', t => {
|
|
|
3560
3694
|
})
|
|
3561
3695
|
})
|
|
3562
3696
|
|
|
3563
|
-
test('onRequestAbort should handle async errors / 1', t => {
|
|
3697
|
+
test('onRequestAbort should handle async errors / 1', (t, testDone) => {
|
|
3564
3698
|
const fastify = Fastify()
|
|
3565
3699
|
|
|
3566
3700
|
t.plan(2)
|
|
3567
|
-
t.
|
|
3701
|
+
t.after(() => fastify.close())
|
|
3568
3702
|
|
|
3569
3703
|
fastify.addHook('onRequestAbort', async function (req) {
|
|
3570
|
-
process.nextTick(() =>
|
|
3704
|
+
process.nextTick(() => {
|
|
3705
|
+
t.assert.ok('should pass')
|
|
3706
|
+
testDone()
|
|
3707
|
+
})
|
|
3571
3708
|
throw new Error('KABOOM!')
|
|
3572
3709
|
})
|
|
3573
3710
|
|
|
@@ -3581,7 +3718,7 @@ test('onRequestAbort should handle async errors / 1', t => {
|
|
|
3581
3718
|
})
|
|
3582
3719
|
|
|
3583
3720
|
fastify.listen({ port: 0 }, err => {
|
|
3584
|
-
t.
|
|
3721
|
+
t.assert.ifError(err)
|
|
3585
3722
|
|
|
3586
3723
|
const socket = connect(fastify.server.address().port)
|
|
3587
3724
|
|
|
@@ -3591,14 +3728,18 @@ test('onRequestAbort should handle async errors / 1', t => {
|
|
|
3591
3728
|
})
|
|
3592
3729
|
})
|
|
3593
3730
|
|
|
3594
|
-
test('onRequestAbort should handle async errors / 2', t => {
|
|
3731
|
+
test('onRequestAbort should handle async errors / 2', (t, testDone) => {
|
|
3595
3732
|
const fastify = Fastify()
|
|
3596
3733
|
|
|
3597
3734
|
t.plan(2)
|
|
3598
|
-
t.
|
|
3735
|
+
t.after(() => fastify.close())
|
|
3599
3736
|
|
|
3600
3737
|
fastify.addHook('onRequestAbort', async function (req) {
|
|
3601
|
-
process.nextTick(() =>
|
|
3738
|
+
process.nextTick(() => {
|
|
3739
|
+
t.assert.ok('should pass')
|
|
3740
|
+
testDone()
|
|
3741
|
+
})
|
|
3742
|
+
|
|
3602
3743
|
return Promise.reject()
|
|
3603
3744
|
})
|
|
3604
3745
|
|
|
@@ -3612,7 +3753,7 @@ test('onRequestAbort should handle async errors / 2', t => {
|
|
|
3612
3753
|
})
|
|
3613
3754
|
|
|
3614
3755
|
fastify.listen({ port: 0 }, err => {
|
|
3615
|
-
t.
|
|
3756
|
+
t.assert.ifError(err)
|
|
3616
3757
|
|
|
3617
3758
|
const socket = connect(fastify.server.address().port)
|
|
3618
3759
|
|