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