@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/404s.test.js
CHANGED
|
@@ -1,150 +1,139 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const fp = require('fastify-plugin')
|
|
5
5
|
const Fastify = require('fastify')
|
|
6
|
-
const sget = require('simple-get').concat
|
|
7
6
|
const expressPlugin = require('../index')
|
|
8
7
|
|
|
9
|
-
test('run hooks and middleware on default 404', t => {
|
|
10
|
-
t.plan(
|
|
8
|
+
test('run hooks and middleware on default 404', async t => {
|
|
9
|
+
t.plan(6)
|
|
11
10
|
|
|
12
11
|
const fastify = Fastify()
|
|
13
12
|
|
|
14
13
|
fastify
|
|
15
14
|
.register(expressPlugin)
|
|
16
15
|
.after(() => {
|
|
17
|
-
fastify.use(function (
|
|
18
|
-
t.
|
|
16
|
+
fastify.use(function (_req, _res, next) {
|
|
17
|
+
t.assert.ok('middleware called')
|
|
19
18
|
next()
|
|
20
19
|
})
|
|
21
20
|
})
|
|
22
21
|
|
|
23
|
-
fastify.addHook('onRequest', function (
|
|
24
|
-
t.
|
|
22
|
+
fastify.addHook('onRequest', function (_req, _res, next) {
|
|
23
|
+
t.assert.ok('onRequest called')
|
|
25
24
|
next()
|
|
26
25
|
})
|
|
27
26
|
|
|
28
|
-
fastify.addHook('preHandler', function (
|
|
29
|
-
t.
|
|
27
|
+
fastify.addHook('preHandler', function (_request, _reply, next) {
|
|
28
|
+
t.assert.ok('preHandler called')
|
|
30
29
|
next()
|
|
31
30
|
})
|
|
32
31
|
|
|
33
|
-
fastify.addHook('onSend', function (
|
|
34
|
-
t.
|
|
32
|
+
fastify.addHook('onSend', function (_request, _reply, _payload, next) {
|
|
33
|
+
t.assert.ok('onSend called')
|
|
35
34
|
next()
|
|
36
35
|
})
|
|
37
36
|
|
|
38
|
-
fastify.addHook('onResponse', function (
|
|
39
|
-
t.
|
|
37
|
+
fastify.addHook('onResponse', function (_request, _reply, next) {
|
|
38
|
+
t.assert.ok('onResponse called')
|
|
40
39
|
next()
|
|
41
40
|
})
|
|
42
41
|
|
|
43
|
-
fastify.get('/', function (
|
|
42
|
+
fastify.get('/', function (_req, reply) {
|
|
44
43
|
reply.send({ hello: 'world' })
|
|
45
44
|
})
|
|
46
45
|
|
|
47
|
-
t.
|
|
46
|
+
t.after(() => fastify.close())
|
|
48
47
|
|
|
49
|
-
fastify.listen({ port: 0 }
|
|
50
|
-
t.error(err)
|
|
48
|
+
const address = await fastify.listen({ port: 0 })
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
body: JSON.stringify({ hello: 'world' }),
|
|
56
|
-
headers: { 'Content-Type': 'application/json' }
|
|
57
|
-
}, (err, response, body) => {
|
|
58
|
-
t.error(err)
|
|
59
|
-
t.equal(response.statusCode, 404)
|
|
60
|
-
})
|
|
50
|
+
const result = await fetch(address, {
|
|
51
|
+
method: 'PUT',
|
|
52
|
+
body: JSON.stringify({ hello: 'world' }),
|
|
61
53
|
})
|
|
54
|
+
|
|
55
|
+
t.assert.deepStrictEqual(result.status, 404)
|
|
62
56
|
})
|
|
63
57
|
|
|
64
|
-
test('run non-encapsulated plugin hooks and middleware on default 404', t => {
|
|
65
|
-
t.plan(
|
|
58
|
+
test('run non-encapsulated plugin hooks and middleware on default 404', async t => {
|
|
59
|
+
t.plan(6)
|
|
66
60
|
|
|
67
61
|
const fastify = Fastify()
|
|
68
|
-
t.
|
|
62
|
+
t.after(() => fastify.close())
|
|
69
63
|
fastify.register(expressPlugin)
|
|
70
64
|
|
|
71
|
-
fastify.register(fp(function (instance,
|
|
72
|
-
instance.addHook('onRequest', function (
|
|
73
|
-
t.
|
|
65
|
+
fastify.register(fp(function (instance, _options, next) {
|
|
66
|
+
instance.addHook('onRequest', function (_req, _res, next) {
|
|
67
|
+
t.assert.ok('onRequest called')
|
|
74
68
|
next()
|
|
75
69
|
})
|
|
76
70
|
|
|
77
|
-
instance.use(function (
|
|
78
|
-
t.
|
|
71
|
+
instance.use(function (_req, _res, next) {
|
|
72
|
+
t.assert.ok('middleware called')
|
|
79
73
|
next()
|
|
80
74
|
})
|
|
81
75
|
|
|
82
|
-
instance.addHook('preHandler', function (
|
|
83
|
-
t.
|
|
76
|
+
instance.addHook('preHandler', function (_request, _reply, next) {
|
|
77
|
+
t.assert.ok('preHandler called')
|
|
84
78
|
next()
|
|
85
79
|
})
|
|
86
80
|
|
|
87
|
-
instance.addHook('onSend', function (
|
|
88
|
-
t.
|
|
81
|
+
instance.addHook('onSend', function (_request, _reply, _payload, next) {
|
|
82
|
+
t.assert.ok('onSend called')
|
|
89
83
|
next()
|
|
90
84
|
})
|
|
91
85
|
|
|
92
|
-
instance.addHook('onResponse', function (
|
|
93
|
-
t.
|
|
86
|
+
instance.addHook('onResponse', function (_request, _reply, next) {
|
|
87
|
+
t.assert.ok('onResponse called')
|
|
94
88
|
next()
|
|
95
89
|
})
|
|
96
90
|
|
|
97
91
|
next()
|
|
98
92
|
}))
|
|
99
93
|
|
|
100
|
-
fastify.get('/', function (
|
|
94
|
+
fastify.get('/', function (_req, reply) {
|
|
101
95
|
reply.send({ hello: 'world' })
|
|
102
96
|
})
|
|
103
97
|
|
|
104
|
-
fastify.listen({ port: 0 }
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
body: JSON.stringify({ hello: 'world' }),
|
|
110
|
-
headers: { 'Content-Type': 'application/json' }
|
|
111
|
-
}, (err, response, body) => {
|
|
112
|
-
t.error(err)
|
|
113
|
-
t.equal(response.statusCode, 404)
|
|
114
|
-
})
|
|
98
|
+
const address = await fastify.listen({ port: 0 })
|
|
99
|
+
|
|
100
|
+
const result = await fetch(address, {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
body: JSON.stringify({ hello: 'world' }),
|
|
115
103
|
})
|
|
104
|
+
t.assert.deepStrictEqual(result.status, 404)
|
|
116
105
|
})
|
|
117
106
|
|
|
118
|
-
test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
|
|
119
|
-
t.plan(
|
|
107
|
+
test('run non-encapsulated plugin hooks and middleware on custom 404', async t => {
|
|
108
|
+
t.plan(12)
|
|
120
109
|
|
|
121
110
|
const fastify = Fastify()
|
|
122
|
-
t.
|
|
111
|
+
t.after(() => fastify.close())
|
|
123
112
|
fastify.register(expressPlugin)
|
|
124
113
|
|
|
125
|
-
const plugin = fp((instance,
|
|
126
|
-
instance.addHook('onRequest', function (
|
|
127
|
-
t.
|
|
114
|
+
const plugin = fp((instance, _opts, next) => {
|
|
115
|
+
instance.addHook('onRequest', function (_req, _res, next) {
|
|
116
|
+
t.assert.ok('onRequest called')
|
|
128
117
|
next()
|
|
129
118
|
})
|
|
130
119
|
|
|
131
|
-
instance.use(function (
|
|
132
|
-
t.
|
|
120
|
+
instance.use(function (_req, _res, next) {
|
|
121
|
+
t.assert.ok('middleware called')
|
|
133
122
|
next()
|
|
134
123
|
})
|
|
135
124
|
|
|
136
|
-
instance.addHook('preHandler', function (
|
|
137
|
-
t.
|
|
125
|
+
instance.addHook('preHandler', function (_request, _reply, next) {
|
|
126
|
+
t.assert.ok('preHandler called')
|
|
138
127
|
next()
|
|
139
128
|
})
|
|
140
129
|
|
|
141
|
-
instance.addHook('onSend', function (
|
|
142
|
-
t.
|
|
130
|
+
instance.addHook('onSend', function (_request, _reply, _payload, next) {
|
|
131
|
+
t.assert.ok('onSend called')
|
|
143
132
|
next()
|
|
144
133
|
})
|
|
145
134
|
|
|
146
|
-
instance.addHook('onResponse', function (
|
|
147
|
-
t.
|
|
135
|
+
instance.addHook('onResponse', function (_request, _reply, next) {
|
|
136
|
+
t.assert.ok('onResponse called')
|
|
148
137
|
next()
|
|
149
138
|
})
|
|
150
139
|
|
|
@@ -153,186 +142,166 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', t => {
|
|
|
153
142
|
|
|
154
143
|
fastify.register(plugin)
|
|
155
144
|
|
|
156
|
-
fastify.get('/', function (
|
|
145
|
+
fastify.get('/', function (_req, reply) {
|
|
157
146
|
reply.send({ hello: 'world' })
|
|
158
147
|
})
|
|
159
148
|
|
|
160
|
-
fastify.setNotFoundHandler(function (
|
|
149
|
+
fastify.setNotFoundHandler(function (_req, reply) {
|
|
161
150
|
reply.code(404).send('this was not found')
|
|
162
151
|
})
|
|
163
152
|
|
|
164
153
|
fastify.register(plugin) // Registering plugin after handler also works
|
|
165
154
|
|
|
166
|
-
fastify.listen({ port: 0 }
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
t.error(err)
|
|
173
|
-
t.equal(body.toString(), 'this was not found')
|
|
174
|
-
t.equal(response.statusCode, 404)
|
|
175
|
-
})
|
|
176
|
-
})
|
|
155
|
+
const address = await fastify.listen({ port: 0 })
|
|
156
|
+
|
|
157
|
+
const result = await fetch(address + '/not-found')
|
|
158
|
+
|
|
159
|
+
t.assert.deepStrictEqual(await result.text(), 'this was not found')
|
|
160
|
+
t.assert.deepStrictEqual(result.status, 404)
|
|
177
161
|
})
|
|
178
162
|
|
|
179
|
-
test('run hooks and middleware with encapsulated 404', t => {
|
|
180
|
-
t.plan(
|
|
163
|
+
test('run hooks and middleware with encapsulated 404', async t => {
|
|
164
|
+
t.plan(11)
|
|
181
165
|
|
|
182
166
|
const fastify = Fastify()
|
|
183
167
|
|
|
184
168
|
fastify
|
|
185
169
|
.register(expressPlugin)
|
|
186
170
|
.after(() => {
|
|
187
|
-
fastify.use(function (
|
|
188
|
-
t.
|
|
171
|
+
fastify.use(function (_req, _res, next) {
|
|
172
|
+
t.assert.ok('middleware called')
|
|
189
173
|
next()
|
|
190
174
|
})
|
|
191
175
|
})
|
|
192
176
|
|
|
193
|
-
fastify.addHook('onRequest', function (
|
|
194
|
-
t.
|
|
177
|
+
fastify.addHook('onRequest', function (_req, _res, next) {
|
|
178
|
+
t.assert.ok('onRequest called')
|
|
195
179
|
next()
|
|
196
180
|
})
|
|
197
181
|
|
|
198
|
-
fastify.addHook('preHandler', function (
|
|
199
|
-
t.
|
|
182
|
+
fastify.addHook('preHandler', function (_request, _reply, next) {
|
|
183
|
+
t.assert.ok('preHandler called')
|
|
200
184
|
next()
|
|
201
185
|
})
|
|
202
186
|
|
|
203
|
-
fastify.addHook('onSend', function (
|
|
204
|
-
t.
|
|
187
|
+
fastify.addHook('onSend', function (_request, _reply, _payload, next) {
|
|
188
|
+
t.assert.ok('onSend called')
|
|
205
189
|
next()
|
|
206
190
|
})
|
|
207
191
|
|
|
208
|
-
fastify.addHook('onResponse', function (
|
|
209
|
-
t.
|
|
192
|
+
fastify.addHook('onResponse', function (_request, _reply, next) {
|
|
193
|
+
t.assert.ok('onResponse called')
|
|
210
194
|
next()
|
|
211
195
|
})
|
|
212
196
|
|
|
213
|
-
fastify.register(function (f,
|
|
214
|
-
f.setNotFoundHandler(function (
|
|
197
|
+
fastify.register(function (f, _opts, next) {
|
|
198
|
+
f.setNotFoundHandler(function (_req, reply) {
|
|
215
199
|
reply.code(404).send('this was not found 2')
|
|
216
200
|
})
|
|
217
201
|
|
|
218
|
-
f.addHook('onRequest', function (
|
|
219
|
-
t.
|
|
202
|
+
f.addHook('onRequest', function (_req, _res, next) {
|
|
203
|
+
t.assert.ok('onRequest 2 called')
|
|
220
204
|
next()
|
|
221
205
|
})
|
|
222
206
|
|
|
223
|
-
f.use(function (
|
|
224
|
-
t.
|
|
207
|
+
f.use(function (_req, _res, next) {
|
|
208
|
+
t.assert.ok('middleware 2 called')
|
|
225
209
|
next()
|
|
226
210
|
})
|
|
227
211
|
|
|
228
|
-
f.addHook('preHandler', function (
|
|
229
|
-
t.
|
|
212
|
+
f.addHook('preHandler', function (_request, _reply, next) {
|
|
213
|
+
t.assert.ok('preHandler 2 called')
|
|
230
214
|
next()
|
|
231
215
|
})
|
|
232
216
|
|
|
233
|
-
f.addHook('onSend', function (
|
|
234
|
-
t.
|
|
217
|
+
f.addHook('onSend', function (_request, _reply, _payload, next) {
|
|
218
|
+
t.assert.ok('onSend 2 called')
|
|
235
219
|
next()
|
|
236
220
|
})
|
|
237
221
|
|
|
238
|
-
f.addHook('onResponse', function (
|
|
239
|
-
t.
|
|
222
|
+
f.addHook('onResponse', function (_request, _reply, next) {
|
|
223
|
+
t.assert.ok('onResponse 2 called')
|
|
240
224
|
next()
|
|
241
225
|
})
|
|
242
226
|
|
|
243
227
|
next()
|
|
244
228
|
}, { prefix: '/test' })
|
|
245
229
|
|
|
246
|
-
t.
|
|
230
|
+
t.after(() => fastify.close())
|
|
247
231
|
|
|
248
|
-
fastify.listen({ port: 0 }
|
|
249
|
-
t.error(err)
|
|
232
|
+
const address = await fastify.listen({ port: 0 })
|
|
250
233
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
body: JSON.stringify({ hello: 'world' }),
|
|
255
|
-
headers: { 'Content-Type': 'application/json' }
|
|
256
|
-
}, (err, response, body) => {
|
|
257
|
-
t.error(err)
|
|
258
|
-
t.equal(response.statusCode, 404)
|
|
259
|
-
})
|
|
234
|
+
const result = await fetch(address + '/test', {
|
|
235
|
+
method: 'PUT',
|
|
236
|
+
body: JSON.stringify({ hello: 'world' }),
|
|
260
237
|
})
|
|
238
|
+
|
|
239
|
+
t.assert.deepStrictEqual(result.status, 404)
|
|
261
240
|
})
|
|
262
241
|
|
|
263
|
-
test('run middlewares on default 404', t => {
|
|
264
|
-
t.plan(
|
|
242
|
+
test('run middlewares on default 404', async t => {
|
|
243
|
+
t.plan(2)
|
|
265
244
|
|
|
266
245
|
const fastify = Fastify()
|
|
267
246
|
fastify
|
|
268
247
|
.register(expressPlugin)
|
|
269
248
|
.after(() => {
|
|
270
|
-
fastify.use(function (
|
|
271
|
-
t.
|
|
249
|
+
fastify.use(function (_req, _res, next) {
|
|
250
|
+
t.assert.ok('middleware called')
|
|
272
251
|
next()
|
|
273
252
|
})
|
|
274
253
|
})
|
|
275
254
|
|
|
276
|
-
fastify.get('/', function (
|
|
255
|
+
fastify.get('/', function (_req, reply) {
|
|
277
256
|
reply.send({ hello: 'world' })
|
|
278
257
|
})
|
|
279
258
|
|
|
280
|
-
t.
|
|
259
|
+
t.after(() => fastify.close())
|
|
281
260
|
|
|
282
|
-
fastify.listen({ port: 0 }
|
|
283
|
-
t.error(err)
|
|
261
|
+
const address = await fastify.listen({ port: 0 })
|
|
284
262
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
body: JSON.stringify({ hello: 'world' }),
|
|
289
|
-
headers: { 'Content-Type': 'application/json' }
|
|
290
|
-
}, (err, response, body) => {
|
|
291
|
-
t.error(err)
|
|
292
|
-
t.equal(response.statusCode, 404)
|
|
293
|
-
})
|
|
263
|
+
const result = await fetch(address, {
|
|
264
|
+
method: 'PUT',
|
|
265
|
+
body: JSON.stringify({ hello: 'world' }),
|
|
294
266
|
})
|
|
267
|
+
|
|
268
|
+
t.assert.deepStrictEqual(result.status, 404)
|
|
295
269
|
})
|
|
296
270
|
|
|
297
|
-
test('run middlewares with encapsulated 404', t => {
|
|
298
|
-
t.plan(
|
|
271
|
+
test('run middlewares with encapsulated 404', async t => {
|
|
272
|
+
t.plan(3)
|
|
299
273
|
|
|
300
274
|
const fastify = Fastify()
|
|
301
275
|
fastify
|
|
302
276
|
.register(expressPlugin)
|
|
303
277
|
.after(() => {
|
|
304
|
-
fastify.use(function (
|
|
305
|
-
t.
|
|
278
|
+
fastify.use(function (_req, _res, next) {
|
|
279
|
+
t.assert.ok('middleware called')
|
|
306
280
|
next()
|
|
307
281
|
})
|
|
308
282
|
})
|
|
309
283
|
|
|
310
|
-
fastify.register(function (f,
|
|
311
|
-
f.setNotFoundHandler(function (
|
|
284
|
+
fastify.register(function (f, _opts, next) {
|
|
285
|
+
f.setNotFoundHandler(function (_req, reply) {
|
|
312
286
|
reply.code(404).send('this was not found 2')
|
|
313
287
|
})
|
|
314
288
|
|
|
315
|
-
f.use(function (
|
|
316
|
-
t.
|
|
289
|
+
f.use(function (_req, _res, next) {
|
|
290
|
+
t.assert.ok('middleware 2 called')
|
|
317
291
|
next()
|
|
318
292
|
})
|
|
319
293
|
|
|
320
294
|
next()
|
|
321
295
|
}, { prefix: '/test' })
|
|
322
296
|
|
|
323
|
-
t.
|
|
297
|
+
t.after(() => fastify.close())
|
|
324
298
|
|
|
325
|
-
fastify.listen({ port: 0 }
|
|
326
|
-
t.error(err)
|
|
299
|
+
const address = await fastify.listen({ port: 0 })
|
|
327
300
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
body: JSON.stringify({ hello: 'world' }),
|
|
332
|
-
headers: { 'Content-Type': 'application/json' }
|
|
333
|
-
}, (err, response, body) => {
|
|
334
|
-
t.error(err)
|
|
335
|
-
t.equal(response.statusCode, 404)
|
|
336
|
-
})
|
|
301
|
+
const result = await fetch(address + '/test', {
|
|
302
|
+
method: 'PUT',
|
|
303
|
+
body: JSON.stringify({ hello: 'world' }),
|
|
337
304
|
})
|
|
305
|
+
|
|
306
|
+
t.assert.deepStrictEqual(result.status, 404)
|
|
338
307
|
})
|