@fastify/express 4.0.2 → 4.0.4
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/.github/dependabot.yml +1 -1
- package/.github/workflows/ci.yml +12 -2
- package/LICENSE +4 -1
- package/README.md +3 -3
- package/index.js +5 -1
- package/package.json +5 -9
- package/test/404s.test.js +83 -114
- package/test/application.test.js +48 -80
- package/test/basic.test.js +157 -284
- package/test/enhance-request.test.js +76 -79
- package/test/form-data.test.js +57 -69
- package/test/hooks.test.js +15 -17
- package/test/middleware.test.js +228 -183
package/test/basic.test.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('fastify')
|
|
5
|
-
const sget = require('simple-get').concat
|
|
6
5
|
const cors = require('cors')
|
|
7
6
|
const passport = require('passport')
|
|
8
7
|
const Strategy = require('passport-http-bearer').Strategy
|
|
9
8
|
|
|
10
9
|
const expressPlugin = require('../index')
|
|
11
10
|
|
|
12
|
-
test('Should support connect style middlewares', t => {
|
|
13
|
-
t.plan(
|
|
11
|
+
test('Should support connect style middlewares', async t => {
|
|
12
|
+
t.plan(2)
|
|
14
13
|
const fastify = Fastify()
|
|
15
|
-
t.
|
|
14
|
+
t.after(() => fastify.close())
|
|
16
15
|
|
|
17
16
|
fastify
|
|
18
17
|
.register(expressPlugin)
|
|
@@ -22,25 +21,17 @@ test('Should support connect style middlewares', t => {
|
|
|
22
21
|
return { hello: 'world' }
|
|
23
22
|
})
|
|
24
23
|
|
|
25
|
-
fastify.listen({ port: 0 }
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}, (err, res, data) => {
|
|
31
|
-
t.error(err)
|
|
32
|
-
t.match(res.headers, {
|
|
33
|
-
'access-control-allow-origin': '*'
|
|
34
|
-
})
|
|
35
|
-
t.same(JSON.parse(data), { hello: 'world' })
|
|
36
|
-
})
|
|
37
|
-
})
|
|
24
|
+
const address = await fastify.listen({ port: 0 })
|
|
25
|
+
|
|
26
|
+
const result = await fetch(address)
|
|
27
|
+
t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
|
|
28
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
38
29
|
})
|
|
39
30
|
|
|
40
31
|
test('Should support connect style middlewares (async await)', async t => {
|
|
41
|
-
t.plan(
|
|
32
|
+
t.plan(2)
|
|
42
33
|
const fastify = Fastify()
|
|
43
|
-
t.
|
|
34
|
+
t.after(() => fastify.close())
|
|
44
35
|
|
|
45
36
|
await fastify.register(expressPlugin)
|
|
46
37
|
fastify.use(cors())
|
|
@@ -50,25 +41,16 @@ test('Should support connect style middlewares (async await)', async t => {
|
|
|
50
41
|
})
|
|
51
42
|
|
|
52
43
|
const address = await fastify.listen({ port: 0 })
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}, (err, res, data) => {
|
|
58
|
-
t.error(err)
|
|
59
|
-
t.match(res.headers, {
|
|
60
|
-
'access-control-allow-origin': '*'
|
|
61
|
-
})
|
|
62
|
-
t.same(JSON.parse(data), { hello: 'world' })
|
|
63
|
-
resolve()
|
|
64
|
-
})
|
|
65
|
-
})
|
|
44
|
+
|
|
45
|
+
const result = await fetch(address)
|
|
46
|
+
t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
|
|
47
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
66
48
|
})
|
|
67
49
|
|
|
68
50
|
test('Should support connect style middlewares (async await after)', async t => {
|
|
69
|
-
t.plan(
|
|
51
|
+
t.plan(2)
|
|
70
52
|
const fastify = Fastify()
|
|
71
|
-
t.
|
|
53
|
+
t.after(() => fastify.close())
|
|
72
54
|
|
|
73
55
|
fastify.register(expressPlugin)
|
|
74
56
|
await fastify.after()
|
|
@@ -79,25 +61,15 @@ test('Should support connect style middlewares (async await after)', async t =>
|
|
|
79
61
|
})
|
|
80
62
|
|
|
81
63
|
const address = await fastify.listen({ port: 0 })
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
url: address
|
|
86
|
-
}, (err, res, data) => {
|
|
87
|
-
t.error(err)
|
|
88
|
-
t.match(res.headers, {
|
|
89
|
-
'access-control-allow-origin': '*'
|
|
90
|
-
})
|
|
91
|
-
t.same(JSON.parse(data), { hello: 'world' })
|
|
92
|
-
resolve()
|
|
93
|
-
})
|
|
94
|
-
})
|
|
64
|
+
const result = await fetch(address)
|
|
65
|
+
t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
|
|
66
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
95
67
|
})
|
|
96
68
|
|
|
97
|
-
test('Should support per path middlewares', t => {
|
|
98
|
-
t.plan(
|
|
69
|
+
test('Should support per path middlewares', async t => {
|
|
70
|
+
t.plan(2)
|
|
99
71
|
const fastify = Fastify()
|
|
100
|
-
t.
|
|
72
|
+
t.after(() => fastify.close())
|
|
101
73
|
|
|
102
74
|
fastify
|
|
103
75
|
.register(expressPlugin)
|
|
@@ -111,66 +83,55 @@ test('Should support per path middlewares', t => {
|
|
|
111
83
|
return { hello: 'world' }
|
|
112
84
|
})
|
|
113
85
|
|
|
114
|
-
fastify.listen({ port: 0 }
|
|
115
|
-
t.error(err)
|
|
116
|
-
sget({
|
|
117
|
-
method: 'GET',
|
|
118
|
-
url: address + '/cors/hello'
|
|
119
|
-
}, (err, res) => {
|
|
120
|
-
t.error(err)
|
|
121
|
-
t.match(res.headers, {
|
|
122
|
-
'access-control-allow-origin': '*'
|
|
123
|
-
})
|
|
124
|
-
})
|
|
86
|
+
const address = await fastify.listen({ port: 0 })
|
|
125
87
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
t.notOk(res.headers['access-control-allow-origin'])
|
|
132
|
-
})
|
|
133
|
-
})
|
|
88
|
+
const result = await fetch(address + '/cors/hello')
|
|
89
|
+
t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
|
|
90
|
+
|
|
91
|
+
const result2 = await fetch(address)
|
|
92
|
+
t.assert.deepStrictEqual(result2.headers.get('access-control-allow-origin'), null)
|
|
134
93
|
})
|
|
135
94
|
|
|
136
|
-
test('Should support complex middlewares', t => {
|
|
137
|
-
t.plan(
|
|
95
|
+
test('Should support complex middlewares', async t => {
|
|
96
|
+
t.plan(3)
|
|
138
97
|
|
|
139
98
|
const fastify = Fastify()
|
|
140
99
|
|
|
141
100
|
passport.use(new Strategy((token, cb) => {
|
|
142
|
-
t.
|
|
101
|
+
t.assert.deepStrictEqual(token, '123456789')
|
|
143
102
|
cb(null, { token })
|
|
144
103
|
}))
|
|
145
104
|
|
|
105
|
+
t.after(() => fastify.close())
|
|
106
|
+
|
|
146
107
|
fastify
|
|
147
108
|
.register(expressPlugin)
|
|
148
109
|
.after(() => { fastify.use(passport.authenticate('bearer', { session: false })) })
|
|
110
|
+
|
|
149
111
|
fastify
|
|
150
112
|
.get('/', (req, reply) => {
|
|
151
|
-
t.
|
|
113
|
+
t.assert.deepStrictEqual(req.raw.user, { token: '123456789' })
|
|
152
114
|
reply.send('ok')
|
|
153
115
|
})
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
fastify.close()
|
|
165
|
-
})
|
|
166
|
-
})
|
|
116
|
+
|
|
117
|
+
const address = await fastify.listen({ port: 0 })
|
|
118
|
+
|
|
119
|
+
const result = await fetch(address, {
|
|
120
|
+
headers: {
|
|
121
|
+
authorization: 'Bearer 123456789'
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
t.assert.ok(result.ok)
|
|
167
126
|
})
|
|
168
127
|
|
|
169
|
-
test('Encapsulation support / 1', t => {
|
|
170
|
-
t.plan(
|
|
128
|
+
test('Encapsulation support / 1', async t => {
|
|
129
|
+
t.plan(1)
|
|
171
130
|
|
|
172
131
|
const fastify = Fastify()
|
|
173
132
|
|
|
133
|
+
t.after(() => fastify.close())
|
|
134
|
+
|
|
174
135
|
fastify.register((instance, _opts, next) => {
|
|
175
136
|
instance.register(expressPlugin)
|
|
176
137
|
.after(() => { instance.use(middleware) })
|
|
@@ -186,27 +147,23 @@ test('Encapsulation support / 1', t => {
|
|
|
186
147
|
reply.send('ok')
|
|
187
148
|
})
|
|
188
149
|
|
|
189
|
-
fastify.listen({ port: 0 }
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
url: address
|
|
194
|
-
}, (err) => {
|
|
195
|
-
t.error(err)
|
|
196
|
-
fastify.close()
|
|
197
|
-
})
|
|
198
|
-
})
|
|
150
|
+
const address = await fastify.listen({ port: 0 })
|
|
151
|
+
|
|
152
|
+
const result = await fetch(address)
|
|
153
|
+
t.assert.ok(result.ok)
|
|
199
154
|
|
|
200
155
|
function middleware () {
|
|
201
|
-
t.fail('Shuld not be called')
|
|
156
|
+
t.assert.fail('Shuld not be called')
|
|
202
157
|
}
|
|
203
158
|
})
|
|
204
159
|
|
|
205
|
-
test('Encapsulation support / 2', t => {
|
|
206
|
-
t.plan(
|
|
160
|
+
test('Encapsulation support / 2', async t => {
|
|
161
|
+
t.plan(1)
|
|
207
162
|
|
|
208
163
|
const fastify = Fastify()
|
|
209
164
|
|
|
165
|
+
t.after(() => fastify.close())
|
|
166
|
+
|
|
210
167
|
fastify.register(expressPlugin)
|
|
211
168
|
|
|
212
169
|
fastify.register((instance, _opts, next) => {
|
|
@@ -222,28 +179,22 @@ test('Encapsulation support / 2', t => {
|
|
|
222
179
|
reply.send('ok')
|
|
223
180
|
})
|
|
224
181
|
|
|
225
|
-
fastify.listen({ port: 0 }
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
url: address
|
|
230
|
-
}, (err) => {
|
|
231
|
-
t.error(err)
|
|
232
|
-
fastify.close()
|
|
233
|
-
})
|
|
234
|
-
})
|
|
182
|
+
const address = await fastify.listen({ port: 0 })
|
|
183
|
+
|
|
184
|
+
const result = await fetch(address)
|
|
185
|
+
t.assert.ok(result.ok)
|
|
235
186
|
|
|
236
187
|
function middleware () {
|
|
237
|
-
t.fail('Shuld not be called')
|
|
188
|
+
t.assert.fail('Shuld not be called')
|
|
238
189
|
}
|
|
239
190
|
})
|
|
240
191
|
|
|
241
|
-
test('Encapsulation support / 3', t => {
|
|
242
|
-
t.plan(
|
|
192
|
+
test('Encapsulation support / 3', async t => {
|
|
193
|
+
t.plan(2)
|
|
243
194
|
|
|
244
195
|
const fastify = Fastify()
|
|
245
196
|
|
|
246
|
-
t.
|
|
197
|
+
t.after(() => fastify.close())
|
|
247
198
|
|
|
248
199
|
fastify.register(expressPlugin)
|
|
249
200
|
|
|
@@ -260,36 +211,21 @@ test('Encapsulation support / 3', t => {
|
|
|
260
211
|
reply.send('ok')
|
|
261
212
|
})
|
|
262
213
|
|
|
263
|
-
fastify.listen({ port: 0 }
|
|
264
|
-
t.error(err)
|
|
265
|
-
sget({
|
|
266
|
-
method: 'GET',
|
|
267
|
-
url: address + '/plugin'
|
|
268
|
-
}, (err, res) => {
|
|
269
|
-
t.error(err)
|
|
270
|
-
t.match(res.headers, {
|
|
271
|
-
'access-control-allow-origin': '*'
|
|
272
|
-
})
|
|
273
|
-
})
|
|
214
|
+
const address = await fastify.listen({ port: 0 })
|
|
274
215
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
t.notMatch(res.headers, {
|
|
281
|
-
'access-control-allow-origin': '*'
|
|
282
|
-
})
|
|
283
|
-
})
|
|
284
|
-
})
|
|
216
|
+
const result = await fetch(address + '/plugin')
|
|
217
|
+
t.assert.deepStrictEqual(result.headers.get('access-control-allow-origin'), '*')
|
|
218
|
+
|
|
219
|
+
const result2 = await fetch(address)
|
|
220
|
+
t.assert.deepStrictEqual(result2.headers.get('access-control-allow-origin'), null)
|
|
285
221
|
})
|
|
286
222
|
|
|
287
|
-
test('Encapsulation support / 4', t => {
|
|
288
|
-
t.plan(
|
|
223
|
+
test('Encapsulation support / 4', async t => {
|
|
224
|
+
t.plan(3)
|
|
289
225
|
|
|
290
226
|
const fastify = Fastify()
|
|
291
227
|
|
|
292
|
-
t.
|
|
228
|
+
t.after(() => fastify.close())
|
|
293
229
|
|
|
294
230
|
fastify.register(expressPlugin)
|
|
295
231
|
fastify.after(() => {
|
|
@@ -309,29 +245,14 @@ test('Encapsulation support / 4', t => {
|
|
|
309
245
|
reply.send('ok')
|
|
310
246
|
})
|
|
311
247
|
|
|
312
|
-
fastify.listen({ port: 0 }
|
|
313
|
-
t.error(err)
|
|
314
|
-
sget({
|
|
315
|
-
method: 'GET',
|
|
316
|
-
url: address + '/plugin'
|
|
317
|
-
}, (err, res) => {
|
|
318
|
-
t.error(err)
|
|
319
|
-
t.match(res.headers, {
|
|
320
|
-
'x-middleware-1': 'true',
|
|
321
|
-
'x-middleware-2': 'true'
|
|
322
|
-
})
|
|
323
|
-
})
|
|
248
|
+
const address = await fastify.listen({ port: 0 })
|
|
324
249
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
'x-middleware-1': 'true'
|
|
332
|
-
})
|
|
333
|
-
})
|
|
334
|
-
})
|
|
250
|
+
const result = await fetch(address + '/plugin')
|
|
251
|
+
t.assert.deepStrictEqual(result.headers.get('x-middleware-1'), 'true')
|
|
252
|
+
t.assert.deepStrictEqual(result.headers.get('x-middleware-2'), 'true')
|
|
253
|
+
|
|
254
|
+
const result2 = await fetch(address)
|
|
255
|
+
t.assert.deepStrictEqual(result2.headers.get('x-middleware-1'), 'true')
|
|
335
256
|
|
|
336
257
|
function middleware1 (_req, res, next) {
|
|
337
258
|
res.setHeader('x-middleware-1', true)
|
|
@@ -344,12 +265,12 @@ test('Encapsulation support / 4', t => {
|
|
|
344
265
|
}
|
|
345
266
|
})
|
|
346
267
|
|
|
347
|
-
test('Encapsulation support / 5', t => {
|
|
348
|
-
t.plan(
|
|
268
|
+
test('Encapsulation support / 5', async t => {
|
|
269
|
+
t.plan(6)
|
|
349
270
|
|
|
350
271
|
const fastify = Fastify()
|
|
351
272
|
|
|
352
|
-
t.
|
|
273
|
+
t.after(() => fastify.close())
|
|
353
274
|
|
|
354
275
|
fastify.register(expressPlugin)
|
|
355
276
|
fastify.after(() => {
|
|
@@ -378,41 +299,19 @@ test('Encapsulation support / 5', t => {
|
|
|
378
299
|
reply.send('ok')
|
|
379
300
|
})
|
|
380
301
|
|
|
381
|
-
fastify.listen({ port: 0 }
|
|
382
|
-
t.error(err)
|
|
383
|
-
sget({
|
|
384
|
-
method: 'GET',
|
|
385
|
-
url: address + '/plugin/nested'
|
|
386
|
-
}, (err, res) => {
|
|
387
|
-
t.error(err)
|
|
388
|
-
t.match(res.headers, {
|
|
389
|
-
'x-middleware-1': 'true',
|
|
390
|
-
'x-middleware-2': 'true',
|
|
391
|
-
'x-middleware-3': 'true'
|
|
392
|
-
})
|
|
393
|
-
})
|
|
302
|
+
const address = await fastify.listen({ port: 0 })
|
|
394
303
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
t.error(err)
|
|
400
|
-
t.match(res.headers, {
|
|
401
|
-
'x-middleware-1': 'true',
|
|
402
|
-
'x-middleware-2': 'true'
|
|
403
|
-
})
|
|
404
|
-
})
|
|
304
|
+
const result = await fetch(address + '/plugin/nested')
|
|
305
|
+
t.assert.deepStrictEqual(result.headers.get('x-middleware-1'), 'true')
|
|
306
|
+
t.assert.deepStrictEqual(result.headers.get('x-middleware-2'), 'true')
|
|
307
|
+
t.assert.deepStrictEqual(result.headers.get('x-middleware-3'), 'true')
|
|
405
308
|
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
'x-middleware-1': 'true'
|
|
413
|
-
})
|
|
414
|
-
})
|
|
415
|
-
})
|
|
309
|
+
const result2 = await fetch(address + '/plugin')
|
|
310
|
+
t.assert.deepStrictEqual(result2.headers.get('x-middleware-1'), 'true')
|
|
311
|
+
t.assert.deepStrictEqual(result2.headers.get('x-middleware-2'), 'true')
|
|
312
|
+
|
|
313
|
+
const result3 = await fetch(address)
|
|
314
|
+
t.assert.deepStrictEqual(result3.headers.get('x-middleware-1'), 'true')
|
|
416
315
|
|
|
417
316
|
function middleware1 (_req, res, next) {
|
|
418
317
|
res.setHeader('x-middleware-1', true)
|
|
@@ -430,12 +329,14 @@ test('Encapsulation support / 5', t => {
|
|
|
430
329
|
}
|
|
431
330
|
})
|
|
432
331
|
|
|
433
|
-
test('Middleware chain', t => {
|
|
434
|
-
t.plan(
|
|
332
|
+
test('Middleware chain', async t => {
|
|
333
|
+
t.plan(4)
|
|
435
334
|
|
|
436
335
|
const order = [1, 2, 3]
|
|
437
336
|
const fastify = Fastify()
|
|
438
337
|
|
|
338
|
+
t.after(() => fastify.close())
|
|
339
|
+
|
|
439
340
|
fastify
|
|
440
341
|
.register(expressPlugin)
|
|
441
342
|
.after(() => {
|
|
@@ -449,39 +350,35 @@ test('Middleware chain', t => {
|
|
|
449
350
|
return { hello: 'world' }
|
|
450
351
|
})
|
|
451
352
|
|
|
452
|
-
fastify.listen({ port: 0 }
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
url: address
|
|
457
|
-
}, (err) => {
|
|
458
|
-
t.error(err)
|
|
459
|
-
fastify.close()
|
|
460
|
-
})
|
|
461
|
-
})
|
|
353
|
+
const address = await fastify.listen({ port: 0 })
|
|
354
|
+
|
|
355
|
+
const result = await fetch(address)
|
|
356
|
+
t.assert.ok(result.ok)
|
|
462
357
|
|
|
463
358
|
function middleware1 (_req, _res, next) {
|
|
464
|
-
t.
|
|
359
|
+
t.assert.deepStrictEqual(order.shift(), 1)
|
|
465
360
|
next()
|
|
466
361
|
}
|
|
467
362
|
|
|
468
363
|
function middleware2 (_req, _res, next) {
|
|
469
|
-
t.
|
|
364
|
+
t.assert.deepStrictEqual(order.shift(), 2)
|
|
470
365
|
next()
|
|
471
366
|
}
|
|
472
367
|
|
|
473
368
|
function middleware3 (_req, _res, next) {
|
|
474
|
-
t.
|
|
369
|
+
t.assert.deepStrictEqual(order.shift(), 3)
|
|
475
370
|
next()
|
|
476
371
|
}
|
|
477
372
|
})
|
|
478
373
|
|
|
479
|
-
test('Middleware chain (with errors) / 1', t => {
|
|
480
|
-
t.plan(
|
|
374
|
+
test('Middleware chain (with errors) / 1', async t => {
|
|
375
|
+
t.plan(6)
|
|
481
376
|
|
|
482
377
|
const order = [1, 2, 3]
|
|
483
378
|
const fastify = Fastify()
|
|
484
379
|
|
|
380
|
+
t.after(() => fastify.close())
|
|
381
|
+
|
|
485
382
|
fastify
|
|
486
383
|
.register(expressPlugin)
|
|
487
384
|
.after(() => {
|
|
@@ -495,44 +392,39 @@ test('Middleware chain (with errors) / 1', t => {
|
|
|
495
392
|
return { hello: 'world' }
|
|
496
393
|
})
|
|
497
394
|
|
|
498
|
-
fastify.listen({ port: 0 }
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
url: address
|
|
503
|
-
}, (err, res) => {
|
|
504
|
-
t.error(err)
|
|
505
|
-
t.equal(res.statusCode, 500)
|
|
506
|
-
fastify.close()
|
|
507
|
-
})
|
|
508
|
-
})
|
|
395
|
+
const address = await fastify.listen({ port: 0 })
|
|
396
|
+
|
|
397
|
+
const result = await fetch(address)
|
|
398
|
+
t.assert.deepStrictEqual(result.status, 500)
|
|
509
399
|
|
|
510
400
|
function middleware1 (_req, _res, next) {
|
|
511
|
-
t.
|
|
401
|
+
t.assert.deepStrictEqual(order.shift(), 1)
|
|
512
402
|
next(new Error('middleware1'))
|
|
513
403
|
}
|
|
514
404
|
|
|
515
405
|
function middleware2 (err, _req, _res, next) {
|
|
516
|
-
t.
|
|
517
|
-
t.
|
|
406
|
+
t.assert.deepStrictEqual(err.message, 'middleware1')
|
|
407
|
+
t.assert.deepStrictEqual(order.shift(), 2)
|
|
518
408
|
next(new Error('middleware2'))
|
|
519
409
|
}
|
|
520
410
|
|
|
521
411
|
function middleware3 (err, _req, _res, next) {
|
|
522
|
-
t.
|
|
523
|
-
t.
|
|
412
|
+
t.assert.deepStrictEqual(err.message, 'middleware2')
|
|
413
|
+
t.assert.deepStrictEqual(order.shift(), 3)
|
|
524
414
|
next(new Error('kaboom'))
|
|
525
415
|
}
|
|
526
416
|
})
|
|
527
417
|
|
|
528
|
-
test('Middleware chain (with errors) / 2', t => {
|
|
529
|
-
t.plan(
|
|
418
|
+
test('Middleware chain (with errors) / 2', async t => {
|
|
419
|
+
t.plan(5)
|
|
530
420
|
|
|
531
421
|
const order = [1, 2]
|
|
532
422
|
const fastify = Fastify()
|
|
533
423
|
|
|
424
|
+
t.after(() => fastify.close())
|
|
425
|
+
|
|
534
426
|
fastify.setErrorHandler((err, _req, reply) => {
|
|
535
|
-
t.
|
|
427
|
+
t.assert.deepStrictEqual(err.message, 'middleware2')
|
|
536
428
|
reply.send(err)
|
|
537
429
|
})
|
|
538
430
|
|
|
@@ -549,39 +441,34 @@ test('Middleware chain (with errors) / 2', t => {
|
|
|
549
441
|
return { hello: 'world' }
|
|
550
442
|
})
|
|
551
443
|
|
|
552
|
-
fastify.listen({ port: 0 }
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
url: address
|
|
557
|
-
}, (err, res) => {
|
|
558
|
-
t.error(err)
|
|
559
|
-
t.equal(res.statusCode, 500)
|
|
560
|
-
fastify.close()
|
|
561
|
-
})
|
|
562
|
-
})
|
|
444
|
+
const address = await fastify.listen({ port: 0 })
|
|
445
|
+
|
|
446
|
+
const result = await fetch(address)
|
|
447
|
+
t.assert.deepStrictEqual(result.status, 500)
|
|
563
448
|
|
|
564
449
|
function middleware1 (_req, _res, next) {
|
|
565
|
-
t.
|
|
450
|
+
t.assert.deepStrictEqual(order.shift(), 1)
|
|
566
451
|
next(new Error('middleware1'))
|
|
567
452
|
}
|
|
568
453
|
|
|
569
454
|
function middleware2 (err, _req, _res, next) {
|
|
570
|
-
t.
|
|
571
|
-
t.
|
|
455
|
+
t.assert.deepStrictEqual(err.message, 'middleware1')
|
|
456
|
+
t.assert.deepStrictEqual(order.shift(), 2)
|
|
572
457
|
next(new Error('middleware2'))
|
|
573
458
|
}
|
|
574
459
|
|
|
575
460
|
function middleware3 () {
|
|
576
|
-
t.fail('We should not be here')
|
|
461
|
+
t.assert.fail('We should not be here')
|
|
577
462
|
}
|
|
578
463
|
})
|
|
579
464
|
|
|
580
|
-
test('Send a response from a middleware', t => {
|
|
581
|
-
t.plan(
|
|
465
|
+
test('Send a response from a middleware', async t => {
|
|
466
|
+
t.plan(3)
|
|
582
467
|
|
|
583
468
|
const fastify = Fastify()
|
|
584
469
|
|
|
470
|
+
t.after(() => fastify.close())
|
|
471
|
+
|
|
585
472
|
fastify
|
|
586
473
|
.register(expressPlugin)
|
|
587
474
|
.after(() => {
|
|
@@ -591,57 +478,49 @@ test('Send a response from a middleware', t => {
|
|
|
591
478
|
})
|
|
592
479
|
|
|
593
480
|
fastify.addHook('preValidation', () => {
|
|
594
|
-
t.fail('We should not be here')
|
|
481
|
+
t.assert.fail('We should not be here')
|
|
595
482
|
})
|
|
596
483
|
|
|
597
484
|
fastify.addHook('preParsing', () => {
|
|
598
|
-
t.fail('We should not be here')
|
|
485
|
+
t.assert.fail('We should not be here')
|
|
599
486
|
})
|
|
600
487
|
|
|
601
488
|
fastify.addHook('preHandler', () => {
|
|
602
|
-
t.fail('We should not be here')
|
|
489
|
+
t.assert.fail('We should not be here')
|
|
603
490
|
})
|
|
604
491
|
|
|
605
492
|
fastify.addHook('onSend', (_req, _reply, payload, next) => {
|
|
606
|
-
t.ok('called')
|
|
493
|
+
t.assert.ok('called')
|
|
607
494
|
next(null, payload)
|
|
608
495
|
})
|
|
609
496
|
|
|
610
497
|
fastify.addHook('onResponse', (_req, _reply, next) => {
|
|
611
|
-
t.ok('called')
|
|
498
|
+
t.assert.ok('called')
|
|
612
499
|
next()
|
|
613
500
|
})
|
|
614
501
|
|
|
615
502
|
fastify.get('/', () => {
|
|
616
|
-
t.fail('We should not be here')
|
|
503
|
+
t.assert.fail('We should not be here')
|
|
617
504
|
})
|
|
618
505
|
|
|
619
|
-
fastify.listen({ port: 0 }
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
url: address,
|
|
624
|
-
json: true
|
|
625
|
-
}, (err, _res, data) => {
|
|
626
|
-
t.error(err)
|
|
627
|
-
t.same(data, { hello: 'world' })
|
|
628
|
-
fastify.close()
|
|
629
|
-
})
|
|
630
|
-
})
|
|
506
|
+
const address = await fastify.listen({ port: 0 })
|
|
507
|
+
|
|
508
|
+
const result = await fetch(address)
|
|
509
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
631
510
|
|
|
632
511
|
function middleware1 (_req, res) {
|
|
633
512
|
res.send({ hello: 'world' })
|
|
634
513
|
}
|
|
635
514
|
|
|
636
515
|
function middleware2 () {
|
|
637
|
-
t.fail('We should not be here')
|
|
516
|
+
t.assert.fail('We should not be here')
|
|
638
517
|
}
|
|
639
518
|
})
|
|
640
519
|
|
|
641
|
-
test('Should support plugin level prefix', t => {
|
|
642
|
-
t.plan(
|
|
520
|
+
test('Should support plugin level prefix', async t => {
|
|
521
|
+
t.plan(2)
|
|
643
522
|
const fastify = Fastify()
|
|
644
|
-
t.
|
|
523
|
+
t.after(() => fastify.close())
|
|
645
524
|
|
|
646
525
|
fastify.register(expressPlugin)
|
|
647
526
|
|
|
@@ -658,15 +537,9 @@ test('Should support plugin level prefix', t => {
|
|
|
658
537
|
next()
|
|
659
538
|
}, { prefix: '/hello' })
|
|
660
539
|
|
|
661
|
-
fastify.listen({ port: 0 }
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
}, (err, res, data) => {
|
|
667
|
-
t.error(err)
|
|
668
|
-
t.equal(res.headers['x-foo'], 'bar')
|
|
669
|
-
t.same(JSON.parse(data), { hello: 'world' })
|
|
670
|
-
})
|
|
671
|
-
})
|
|
540
|
+
const address = await fastify.listen({ port: 0 })
|
|
541
|
+
|
|
542
|
+
const result = await fetch(address + '/hello/world')
|
|
543
|
+
t.assert.deepStrictEqual(result.headers.get('x-foo'), 'bar')
|
|
544
|
+
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
672
545
|
})
|