@fastify/express 4.0.1 → 4.0.3
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 +13 -3
- package/LICENSE +4 -1
- package/README.md +25 -12
- package/eslint.config.js +6 -0
- package/index.js +8 -1
- package/package.json +42 -14
- package/test/404s.test.js +122 -153
- package/test/application.test.js +56 -88
- package/test/basic.test.js +210 -337
- package/test/enhance-request.test.js +60 -83
- package/test/form-data.test.js +58 -72
- package/test/hooks.test.js +19 -21
- package/test/middleware.test.js +259 -214
- package/types/index.d.ts +2 -2
- package/types/index.test-d.ts +3 -3
package/test/basic.test.js
CHANGED
|
@@ -1,370 +1,291 @@
|
|
|
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)
|
|
19
18
|
.after(() => { fastify.use(cors()) })
|
|
20
19
|
|
|
21
|
-
fastify.get('/', async (
|
|
20
|
+
fastify.get('/', async () => {
|
|
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())
|
|
47
38
|
|
|
48
|
-
fastify.get('/', async (
|
|
39
|
+
fastify.get('/', async () => {
|
|
49
40
|
return { hello: 'world' }
|
|
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()
|
|
75
57
|
fastify.use(cors())
|
|
76
58
|
|
|
77
|
-
fastify.get('/', async (
|
|
59
|
+
fastify.get('/', async () => {
|
|
78
60
|
return { hello: 'world' }
|
|
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)
|
|
104
76
|
.after(() => { fastify.use('/cors', cors()) })
|
|
105
77
|
|
|
106
|
-
fastify.get('/cors/hello', async (
|
|
78
|
+
fastify.get('/cors/hello', async () => {
|
|
107
79
|
return { hello: 'world' }
|
|
108
80
|
})
|
|
109
81
|
|
|
110
|
-
fastify.get('/', async (
|
|
82
|
+
fastify.get('/', async () => {
|
|
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, data) => {
|
|
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
|
|
|
174
|
-
|
|
133
|
+
t.after(() => fastify.close())
|
|
134
|
+
|
|
135
|
+
fastify.register((instance, _opts, next) => {
|
|
175
136
|
instance.register(expressPlugin)
|
|
176
137
|
.after(() => { instance.use(middleware) })
|
|
177
138
|
|
|
178
|
-
instance.get('/plugin', (
|
|
139
|
+
instance.get('/plugin', (_req, reply) => {
|
|
179
140
|
reply.send('ok')
|
|
180
141
|
})
|
|
181
142
|
|
|
182
143
|
next()
|
|
183
144
|
})
|
|
184
145
|
|
|
185
|
-
fastify.get('/', (
|
|
146
|
+
fastify.get('/', (_req, reply) => {
|
|
186
147
|
reply.send('ok')
|
|
187
148
|
})
|
|
188
149
|
|
|
189
|
-
fastify.listen({ port: 0 }
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
url: address
|
|
194
|
-
}, (err, res, data) => {
|
|
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
|
-
function middleware (
|
|
201
|
-
t.fail('Shuld not be called')
|
|
155
|
+
function middleware () {
|
|
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
|
-
fastify.register((instance,
|
|
169
|
+
fastify.register((instance, _opts, next) => {
|
|
213
170
|
instance.use(middleware)
|
|
214
|
-
instance.get('/plugin', (
|
|
171
|
+
instance.get('/plugin', (_req, reply) => {
|
|
215
172
|
reply.send('ok')
|
|
216
173
|
})
|
|
217
174
|
|
|
218
175
|
next()
|
|
219
176
|
})
|
|
220
177
|
|
|
221
|
-
fastify.get('/', (
|
|
178
|
+
fastify.get('/', (_req, reply) => {
|
|
222
179
|
reply.send('ok')
|
|
223
180
|
})
|
|
224
181
|
|
|
225
|
-
fastify.listen({ port: 0 }
|
|
226
|
-
t.error(err)
|
|
227
|
-
sget({
|
|
228
|
-
method: 'GET',
|
|
229
|
-
url: address
|
|
230
|
-
}, (err, res, data) => {
|
|
231
|
-
t.error(err)
|
|
232
|
-
fastify.close()
|
|
233
|
-
})
|
|
234
|
-
})
|
|
182
|
+
const address = await fastify.listen({ port: 0 })
|
|
235
183
|
|
|
236
|
-
|
|
237
|
-
|
|
184
|
+
const result = await fetch(address)
|
|
185
|
+
t.assert.ok(result.ok)
|
|
186
|
+
|
|
187
|
+
function middleware () {
|
|
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
|
|
|
250
|
-
fastify.register((instance,
|
|
201
|
+
fastify.register((instance, _opts, next) => {
|
|
251
202
|
instance.use(cors())
|
|
252
|
-
instance.get('/plugin', (
|
|
203
|
+
instance.get('/plugin', (_req, reply) => {
|
|
253
204
|
reply.send('ok')
|
|
254
205
|
})
|
|
255
206
|
|
|
256
207
|
next()
|
|
257
208
|
})
|
|
258
209
|
|
|
259
|
-
fastify.get('/', (
|
|
210
|
+
fastify.get('/', (_req, reply) => {
|
|
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, data) => {
|
|
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(() => {
|
|
296
232
|
fastify.use(middleware1)
|
|
297
233
|
})
|
|
298
234
|
|
|
299
|
-
fastify.register((instance,
|
|
235
|
+
fastify.register((instance, _opts, next) => {
|
|
300
236
|
instance.use(middleware2)
|
|
301
|
-
instance.get('/plugin', (
|
|
237
|
+
instance.get('/plugin', (_req, reply) => {
|
|
302
238
|
reply.send('ok')
|
|
303
239
|
})
|
|
304
240
|
|
|
305
241
|
next()
|
|
306
242
|
})
|
|
307
243
|
|
|
308
|
-
fastify.get('/', (
|
|
244
|
+
fastify.get('/', (_req, reply) => {
|
|
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, data) => {
|
|
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
|
-
function middleware1 (
|
|
257
|
+
function middleware1 (_req, res, next) {
|
|
337
258
|
res.setHeader('x-middleware-1', true)
|
|
338
259
|
next()
|
|
339
260
|
}
|
|
340
261
|
|
|
341
|
-
function middleware2 (
|
|
262
|
+
function middleware2 (_req, res, next) {
|
|
342
263
|
res.setHeader('x-middleware-2', true)
|
|
343
264
|
next()
|
|
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(() => {
|
|
356
277
|
fastify.use(middleware1)
|
|
357
278
|
})
|
|
358
279
|
|
|
359
|
-
fastify.register((instance,
|
|
280
|
+
fastify.register((instance, _opts, next) => {
|
|
360
281
|
instance.use(middleware2)
|
|
361
|
-
instance.get('/', (
|
|
282
|
+
instance.get('/', (_req, reply) => {
|
|
362
283
|
reply.send('ok')
|
|
363
284
|
})
|
|
364
285
|
|
|
365
|
-
instance.register((i,
|
|
286
|
+
instance.register((i, _opts, next) => {
|
|
366
287
|
i.use(middleware3)
|
|
367
|
-
i.get('/nested', (
|
|
288
|
+
i.get('/nested', (_req, reply) => {
|
|
368
289
|
reply.send('ok')
|
|
369
290
|
})
|
|
370
291
|
|
|
@@ -374,68 +295,48 @@ test('Encapsulation support / 5', t => {
|
|
|
374
295
|
next()
|
|
375
296
|
}, { prefix: '/plugin' })
|
|
376
297
|
|
|
377
|
-
fastify.get('/', (
|
|
298
|
+
fastify.get('/', (_req, reply) => {
|
|
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, data) => {
|
|
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
|
-
}, (err, res, data) => {
|
|
410
|
-
t.error(err)
|
|
411
|
-
t.match(res.headers, {
|
|
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')
|
|
416
312
|
|
|
417
|
-
|
|
313
|
+
const result3 = await fetch(address)
|
|
314
|
+
t.assert.deepStrictEqual(result3.headers.get('x-middleware-1'), 'true')
|
|
315
|
+
|
|
316
|
+
function middleware1 (_req, res, next) {
|
|
418
317
|
res.setHeader('x-middleware-1', true)
|
|
419
318
|
next()
|
|
420
319
|
}
|
|
421
320
|
|
|
422
|
-
function middleware2 (
|
|
321
|
+
function middleware2 (_req, res, next) {
|
|
423
322
|
res.setHeader('x-middleware-2', true)
|
|
424
323
|
next()
|
|
425
324
|
}
|
|
426
325
|
|
|
427
|
-
function middleware3 (
|
|
326
|
+
function middleware3 (_req, res, next) {
|
|
428
327
|
res.setHeader('x-middleware-3', true)
|
|
429
328
|
next()
|
|
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(() => {
|
|
@@ -445,43 +346,39 @@ test('Middleware chain', t => {
|
|
|
445
346
|
.use(middleware3)
|
|
446
347
|
})
|
|
447
348
|
|
|
448
|
-
fastify.get('/', async (
|
|
349
|
+
fastify.get('/', async () => {
|
|
449
350
|
return { hello: 'world' }
|
|
450
351
|
})
|
|
451
352
|
|
|
452
|
-
fastify.listen({ port: 0 }
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
url: address
|
|
457
|
-
}, (err, res, data) => {
|
|
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
|
-
function middleware1 (
|
|
464
|
-
t.
|
|
358
|
+
function middleware1 (_req, _res, next) {
|
|
359
|
+
t.assert.deepStrictEqual(order.shift(), 1)
|
|
465
360
|
next()
|
|
466
361
|
}
|
|
467
362
|
|
|
468
|
-
function middleware2 (
|
|
469
|
-
t.
|
|
363
|
+
function middleware2 (_req, _res, next) {
|
|
364
|
+
t.assert.deepStrictEqual(order.shift(), 2)
|
|
470
365
|
next()
|
|
471
366
|
}
|
|
472
367
|
|
|
473
|
-
function middleware3 (
|
|
474
|
-
t.
|
|
368
|
+
function middleware3 (_req, _res, next) {
|
|
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(() => {
|
|
@@ -491,48 +388,43 @@ test('Middleware chain (with errors) / 1', t => {
|
|
|
491
388
|
.use(middleware3)
|
|
492
389
|
})
|
|
493
390
|
|
|
494
|
-
fastify.get('/', async (
|
|
391
|
+
fastify.get('/', async () => {
|
|
495
392
|
return { hello: 'world' }
|
|
496
393
|
})
|
|
497
394
|
|
|
498
|
-
fastify.listen({ port: 0 }
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
url: address
|
|
503
|
-
}, (err, res, data) => {
|
|
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
|
-
function middleware1 (
|
|
511
|
-
t.
|
|
400
|
+
function middleware1 (_req, _res, next) {
|
|
401
|
+
t.assert.deepStrictEqual(order.shift(), 1)
|
|
512
402
|
next(new Error('middleware1'))
|
|
513
403
|
}
|
|
514
404
|
|
|
515
|
-
function middleware2 (err,
|
|
516
|
-
t.
|
|
517
|
-
t.
|
|
405
|
+
function middleware2 (err, _req, _res, next) {
|
|
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
|
-
function middleware3 (err,
|
|
522
|
-
t.
|
|
523
|
-
t.
|
|
411
|
+
function middleware3 (err, _req, _res, next) {
|
|
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
|
|
|
534
|
-
|
|
535
|
-
|
|
424
|
+
t.after(() => fastify.close())
|
|
425
|
+
|
|
426
|
+
fastify.setErrorHandler((err, _req, reply) => {
|
|
427
|
+
t.assert.deepStrictEqual(err.message, 'middleware2')
|
|
536
428
|
reply.send(err)
|
|
537
429
|
})
|
|
538
430
|
|
|
@@ -545,43 +437,38 @@ test('Middleware chain (with errors) / 2', t => {
|
|
|
545
437
|
.use(middleware3)
|
|
546
438
|
})
|
|
547
439
|
|
|
548
|
-
fastify.get('/', async (
|
|
440
|
+
fastify.get('/', async () => {
|
|
549
441
|
return { hello: 'world' }
|
|
550
442
|
})
|
|
551
443
|
|
|
552
|
-
fastify.listen({ port: 0 }
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
url: address
|
|
557
|
-
}, (err, res, data) => {
|
|
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
|
-
function middleware1 (
|
|
565
|
-
t.
|
|
449
|
+
function middleware1 (_req, _res, next) {
|
|
450
|
+
t.assert.deepStrictEqual(order.shift(), 1)
|
|
566
451
|
next(new Error('middleware1'))
|
|
567
452
|
}
|
|
568
453
|
|
|
569
|
-
function middleware2 (err,
|
|
570
|
-
t.
|
|
571
|
-
t.
|
|
454
|
+
function middleware2 (err, _req, _res, next) {
|
|
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
|
-
function middleware3 (
|
|
576
|
-
t.fail('We should not be here')
|
|
460
|
+
function middleware3 () {
|
|
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(() => {
|
|
@@ -590,83 +477,69 @@ test('Send a response from a middleware', t => {
|
|
|
590
477
|
.use(middleware2)
|
|
591
478
|
})
|
|
592
479
|
|
|
593
|
-
fastify.addHook('preValidation', (
|
|
594
|
-
t.fail('We should not be here')
|
|
480
|
+
fastify.addHook('preValidation', () => {
|
|
481
|
+
t.assert.fail('We should not be here')
|
|
595
482
|
})
|
|
596
483
|
|
|
597
|
-
fastify.addHook('preParsing', (
|
|
598
|
-
t.fail('We should not be here')
|
|
484
|
+
fastify.addHook('preParsing', () => {
|
|
485
|
+
t.assert.fail('We should not be here')
|
|
599
486
|
})
|
|
600
487
|
|
|
601
|
-
fastify.addHook('preHandler', (
|
|
602
|
-
t.fail('We should not be here')
|
|
488
|
+
fastify.addHook('preHandler', () => {
|
|
489
|
+
t.assert.fail('We should not be here')
|
|
603
490
|
})
|
|
604
491
|
|
|
605
|
-
fastify.addHook('onSend', (
|
|
606
|
-
t.ok('called')
|
|
492
|
+
fastify.addHook('onSend', (_req, _reply, payload, next) => {
|
|
493
|
+
t.assert.ok('called')
|
|
607
494
|
next(null, payload)
|
|
608
495
|
})
|
|
609
496
|
|
|
610
|
-
fastify.addHook('onResponse', (
|
|
611
|
-
t.ok('called')
|
|
497
|
+
fastify.addHook('onResponse', (_req, _reply, next) => {
|
|
498
|
+
t.assert.ok('called')
|
|
612
499
|
next()
|
|
613
500
|
})
|
|
614
501
|
|
|
615
|
-
fastify.get('/', (
|
|
616
|
-
t.fail('We should not be here')
|
|
502
|
+
fastify.get('/', () => {
|
|
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
|
-
function middleware1 (
|
|
511
|
+
function middleware1 (_req, res) {
|
|
633
512
|
res.send({ hello: 'world' })
|
|
634
513
|
}
|
|
635
514
|
|
|
636
|
-
function middleware2 (
|
|
637
|
-
t.fail('We should not be here')
|
|
515
|
+
function middleware2 () {
|
|
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
|
|
|
648
|
-
fastify.register((instance,
|
|
649
|
-
instance.use('/world', (
|
|
527
|
+
fastify.register((instance, _opts, next) => {
|
|
528
|
+
instance.use('/world', (_req, res, next) => {
|
|
650
529
|
res.setHeader('x-foo', 'bar')
|
|
651
530
|
next()
|
|
652
531
|
})
|
|
653
532
|
|
|
654
|
-
instance.get('/world', (
|
|
533
|
+
instance.get('/world', (_req, reply) => {
|
|
655
534
|
reply.send({ hello: 'world' })
|
|
656
535
|
})
|
|
657
536
|
|
|
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
|
})
|