@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/test/404s.test.js CHANGED
@@ -1,150 +1,139 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
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)
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 (req, res, next) {
18
- t.pass('middleware called')
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 (req, res, next) {
24
- t.pass('onRequest called')
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 (request, reply, next) {
29
- t.pass('preHandler called')
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 (request, reply, payload, next) {
34
- t.pass('onSend called')
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 (request, reply, next) {
39
- t.pass('onResponse called')
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 (req, reply) {
42
+ fastify.get('/', function (_req, reply) {
44
43
  reply.send({ hello: 'world' })
45
44
  })
46
45
 
47
- t.teardown(fastify.close.bind(fastify))
46
+ t.after(() => fastify.close())
48
47
 
49
- fastify.listen({ port: 0 }, err => {
50
- t.error(err)
48
+ const address = await fastify.listen({ port: 0 })
51
49
 
52
- sget({
53
- method: 'PUT',
54
- url: 'http://localhost:' + fastify.server.address().port,
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(8)
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.teardown(fastify.close)
62
+ t.after(() => fastify.close())
69
63
  fastify.register(expressPlugin)
70
64
 
71
- fastify.register(fp(function (instance, options, next) {
72
- instance.addHook('onRequest', function (req, res, next) {
73
- t.pass('onRequest called')
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 (req, res, next) {
78
- t.pass('middleware called')
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 (request, reply, next) {
83
- t.pass('preHandler called')
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 (request, reply, payload, next) {
88
- t.pass('onSend called')
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 (request, reply, next) {
93
- t.pass('onResponse called')
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 (req, reply) {
94
+ fastify.get('/', function (_req, reply) {
101
95
  reply.send({ hello: 'world' })
102
96
  })
103
97
 
104
- fastify.listen({ port: 0 }, (err, address) => {
105
- t.error(err)
106
- sget({
107
- method: 'POST',
108
- url: address,
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(14)
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.teardown(fastify.close)
111
+ t.after(() => fastify.close())
123
112
  fastify.register(expressPlugin)
124
113
 
125
- const plugin = fp((instance, opts, next) => {
126
- instance.addHook('onRequest', function (req, res, next) {
127
- t.pass('onRequest called')
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 (req, res, next) {
132
- t.pass('middleware called')
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 (request, reply, next) {
137
- t.pass('preHandler called')
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 (request, reply, payload, next) {
142
- t.pass('onSend called')
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 (request, reply, next) {
147
- t.pass('onResponse called')
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 (req, reply) {
145
+ fastify.get('/', function (_req, reply) {
157
146
  reply.send({ hello: 'world' })
158
147
  })
159
148
 
160
- fastify.setNotFoundHandler(function (req, reply) {
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 }, (err, address) => {
167
- t.error(err)
168
- sget({
169
- method: 'GET',
170
- url: address + '/not-found'
171
- }, (err, response, body) => {
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(13)
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 (req, res, next) {
188
- t.pass('middleware called')
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 (req, res, next) {
194
- t.pass('onRequest called')
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 (request, reply, next) {
199
- t.pass('preHandler called')
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 (request, reply, payload, next) {
204
- t.pass('onSend called')
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 (request, reply, next) {
209
- t.pass('onResponse called')
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, opts, next) {
214
- f.setNotFoundHandler(function (req, reply) {
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 (req, res, next) {
219
- t.pass('onRequest 2 called')
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 (req, res, next) {
224
- t.pass('middleware 2 called')
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 (request, reply, next) {
229
- t.pass('preHandler 2 called')
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 (request, reply, payload, next) {
234
- t.pass('onSend 2 called')
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 (request, reply, next) {
239
- t.pass('onResponse 2 called')
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.teardown(fastify.close.bind(fastify))
230
+ t.after(() => fastify.close())
247
231
 
248
- fastify.listen({ port: 0 }, err => {
249
- t.error(err)
232
+ const address = await fastify.listen({ port: 0 })
250
233
 
251
- sget({
252
- method: 'PUT',
253
- url: 'http://localhost:' + fastify.server.address().port + '/test',
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(4)
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 (req, res, next) {
271
- t.pass('middleware called')
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 (req, reply) {
255
+ fastify.get('/', function (_req, reply) {
277
256
  reply.send({ hello: 'world' })
278
257
  })
279
258
 
280
- t.teardown(fastify.close.bind(fastify))
259
+ t.after(() => fastify.close())
281
260
 
282
- fastify.listen({ port: 0 }, err => {
283
- t.error(err)
261
+ const address = await fastify.listen({ port: 0 })
284
262
 
285
- sget({
286
- method: 'PUT',
287
- url: 'http://localhost:' + fastify.server.address().port,
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(5)
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 (req, res, next) {
305
- t.pass('middleware called')
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, opts, next) {
311
- f.setNotFoundHandler(function (req, reply) {
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 (req, res, next) {
316
- t.pass('middleware 2 called')
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.teardown(fastify.close.bind(fastify))
297
+ t.after(() => fastify.close())
324
298
 
325
- fastify.listen({ port: 0 }, err => {
326
- t.error(err)
299
+ const address = await fastify.listen({ port: 0 })
327
300
 
328
- sget({
329
- method: 'PUT',
330
- url: 'http://localhost:' + fastify.server.address().port + '/test',
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
  })