fastify 5.3.0 → 5.3.2

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  const stream = require('node:stream')
4
4
 
5
- const t = require('tap')
5
+ const t = require('node:test')
6
6
  const split = require('split2')
7
7
  const pino = require('pino')
8
8
 
@@ -10,10 +10,9 @@ const Fastify = require('../../fastify')
10
10
  const helper = require('../helper')
11
11
  const { once, on } = stream
12
12
  const { request } = require('./logger-test-utils')
13
+ const { partialDeepStrictEqual } = require('../toolkit')
13
14
 
14
- t.test('logging', (t) => {
15
- t.setTimeout(60000)
16
-
15
+ t.test('logging', { timeout: 60000 }, async (t) => {
17
16
  let localhost
18
17
  let localhostForURL
19
18
 
@@ -23,7 +22,7 @@ t.test('logging', (t) => {
23
22
  [localhost, localhostForURL] = await helper.getLoopbackHost()
24
23
  })
25
24
 
26
- t.test('The default 404 handler logs the incoming request', async (t) => {
25
+ await t.test('The default 404 handler logs the incoming request', async (t) => {
27
26
  const lines = ['incoming request', 'Route GET:/not-found not found', 'request completed']
28
27
  t.plan(lines.length + 1)
29
28
 
@@ -34,29 +33,22 @@ t.test('logging', (t) => {
34
33
  const fastify = Fastify({
35
34
  loggerInstance
36
35
  })
37
- t.teardown(fastify.close.bind(fastify))
36
+ t.after(() => fastify.close())
38
37
 
39
38
  await fastify.ready()
40
39
 
41
40
  {
42
41
  const response = await fastify.inject({ method: 'GET', url: '/not-found' })
43
- t.equal(response.statusCode, 404)
42
+ t.assert.strictEqual(response.statusCode, 404)
44
43
  }
45
44
 
46
45
  for await (const [line] of on(stream, 'data')) {
47
- t.equal(line.msg, lines.shift())
46
+ t.assert.strictEqual(line.msg, lines.shift())
48
47
  if (lines.length === 0) break
49
48
  }
50
49
  })
51
50
 
52
- t.test('should not rely on raw request to log errors', async (t) => {
53
- const lines = [
54
- { msg: /Server listening at/ },
55
- { level: 30, msg: 'incoming request' },
56
- { res: { statusCode: 415 }, msg: 'something happened' },
57
- { res: { statusCode: 415 }, msg: 'request completed' }
58
- ]
59
- t.plan(lines.length + 1)
51
+ await t.test('should not rely on raw request to log errors', async (t) => {
60
52
  const stream = split(JSON.parse)
61
53
  const fastify = Fastify({
62
54
  logger: {
@@ -64,32 +56,31 @@ t.test('logging', (t) => {
64
56
  level: 'info'
65
57
  }
66
58
  })
67
- t.teardown(fastify.close.bind(fastify))
59
+ t.after(() => fastify.close())
68
60
  fastify.get('/error', function (req, reply) {
69
- t.ok(req.log)
61
+ t.assert.ok(req.log)
70
62
  reply.status(415).send(new Error('something happened'))
71
63
  })
72
64
 
73
65
  await fastify.ready()
74
- await fastify.listen({ port: 0, host: localhost })
66
+ const server = await fastify.listen({ port: 0, host: localhost })
67
+ const lines = [
68
+ { msg: `Server listening at ${server}` },
69
+ { level: 30, msg: 'incoming request' },
70
+ { res: { statusCode: 415 }, msg: 'something happened' },
71
+ { res: { statusCode: 415 }, msg: 'request completed' }
72
+ ]
73
+ t.plan(lines.length + 1)
75
74
 
76
75
  await request(`http://${localhostForURL}:` + fastify.server.address().port + '/error')
77
76
 
78
77
  for await (const [line] of on(stream, 'data')) {
79
- t.match(line, lines.shift())
78
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
80
79
  if (lines.length === 0) break
81
80
  }
82
81
  })
83
82
 
84
- t.test('should log the error if no error handler is defined', async (t) => {
85
- const lines = [
86
- { msg: /Server listening at/ },
87
- { msg: 'incoming request' },
88
- { level: 50, msg: 'a generic error' },
89
- { res: { statusCode: 500 }, msg: 'request completed' }
90
- ]
91
- t.plan(lines.length + 1)
92
-
83
+ await t.test('should log the error if no error handler is defined', async (t) => {
93
84
  const stream = split(JSON.parse)
94
85
  const fastify = Fastify({
95
86
  logger: {
@@ -97,32 +88,32 @@ t.test('logging', (t) => {
97
88
  level: 'info'
98
89
  }
99
90
  })
100
- t.teardown(fastify.close.bind(fastify))
91
+ t.after(() => fastify.close())
101
92
 
102
93
  fastify.get('/error', function (req, reply) {
103
- t.ok(req.log)
94
+ t.assert.ok(req.log)
104
95
  reply.send(new Error('a generic error'))
105
96
  })
106
97
 
107
98
  await fastify.ready()
108
- await fastify.listen({ port: 0, host: localhost })
99
+ const server = await fastify.listen({ port: 0, host: localhost })
100
+ const lines = [
101
+ { msg: `Server listening at ${server}` },
102
+ { msg: 'incoming request' },
103
+ { level: 50, msg: 'a generic error' },
104
+ { res: { statusCode: 500 }, msg: 'request completed' }
105
+ ]
106
+ t.plan(lines.length + 1)
109
107
 
110
108
  await request(`http://${localhostForURL}:` + fastify.server.address().port + '/error')
111
109
 
112
110
  for await (const [line] of on(stream, 'data')) {
113
- t.match(line, lines.shift())
111
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
114
112
  if (lines.length === 0) break
115
113
  }
116
114
  })
117
115
 
118
- t.test('should log as info if error status code >= 400 and < 500 if no error handler is defined', async (t) => {
119
- const lines = [
120
- { msg: /Server listening at/ },
121
- { msg: 'incoming request' },
122
- { level: 30, msg: 'a 400 error' },
123
- { res: { statusCode: 400 }, msg: 'request completed' }
124
- ]
125
- t.plan(lines.length + 1)
116
+ await t.test('should log as info if error status code >= 400 and < 500 if no error handler is defined', async (t) => {
126
117
  const stream = split(JSON.parse)
127
118
  const fastify = Fastify({
128
119
  logger: {
@@ -130,36 +121,36 @@ t.test('logging', (t) => {
130
121
  level: 'info'
131
122
  }
132
123
  })
133
- t.teardown(fastify.close.bind(fastify))
124
+ t.after(() => fastify.close())
134
125
 
135
126
  fastify.get('/400', function (req, reply) {
136
- t.ok(req.log)
127
+ t.assert.ok(req.log)
137
128
  reply.send(Object.assign(new Error('a 400 error'), { statusCode: 400 }))
138
129
  })
139
130
  fastify.get('/503', function (req, reply) {
140
- t.ok(req.log)
131
+ t.assert.ok(req.log)
141
132
  reply.send(Object.assign(new Error('a 503 error'), { statusCode: 503 }))
142
133
  })
143
134
 
144
135
  await fastify.ready()
145
- await fastify.listen({ port: 0, host: localhost })
136
+ const server = await fastify.listen({ port: 0, host: localhost })
137
+ const lines = [
138
+ { msg: `Server listening at ${server}` },
139
+ { msg: 'incoming request' },
140
+ { level: 30, msg: 'a 400 error' },
141
+ { res: { statusCode: 400 }, msg: 'request completed' }
142
+ ]
143
+ t.plan(lines.length + 1)
146
144
 
147
145
  await request(`http://${localhostForURL}:` + fastify.server.address().port + '/400')
148
146
 
149
147
  for await (const [line] of on(stream, 'data')) {
150
- t.match(line, lines.shift())
148
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
151
149
  if (lines.length === 0) break
152
150
  }
153
151
  })
154
152
 
155
- t.test('should log as error if error status code >= 500 if no error handler is defined', async (t) => {
156
- const lines = [
157
- { msg: /Server listening at/ },
158
- { msg: 'incoming request' },
159
- { level: 50, msg: 'a 503 error' },
160
- { res: { statusCode: 503 }, msg: 'request completed' }
161
- ]
162
- t.plan(lines.length + 1)
153
+ await t.test('should log as error if error status code >= 500 if no error handler is defined', async (t) => {
163
154
  const stream = split(JSON.parse)
164
155
  const fastify = Fastify({
165
156
  logger: {
@@ -167,30 +158,31 @@ t.test('logging', (t) => {
167
158
  level: 'info'
168
159
  }
169
160
  })
170
- t.teardown(fastify.close.bind(fastify))
161
+ t.after(() => fastify.close())
171
162
  fastify.get('/503', function (req, reply) {
172
- t.ok(req.log)
163
+ t.assert.ok(req.log)
173
164
  reply.send(Object.assign(new Error('a 503 error'), { statusCode: 503 }))
174
165
  })
175
166
 
176
167
  await fastify.ready()
177
- await fastify.listen({ port: 0, host: localhost })
168
+ const server = await fastify.listen({ port: 0, host: localhost })
169
+ const lines = [
170
+ { msg: `Server listening at ${server}` },
171
+ { msg: 'incoming request' },
172
+ { level: 50, msg: 'a 503 error' },
173
+ { res: { statusCode: 503 }, msg: 'request completed' }
174
+ ]
175
+ t.plan(lines.length + 1)
178
176
 
179
177
  await request(`http://${localhostForURL}:` + fastify.server.address().port + '/503')
180
178
 
181
179
  for await (const [line] of on(stream, 'data')) {
182
- t.match(line, lines.shift())
180
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
183
181
  if (lines.length === 0) break
184
182
  }
185
183
  })
186
184
 
187
- t.test('should not log the error if error handler is defined and it does not error', async (t) => {
188
- const lines = [
189
- { msg: /Server listening at/ },
190
- { level: 30, msg: 'incoming request' },
191
- { res: { statusCode: 200 }, msg: 'request completed' }
192
- ]
193
- t.plan(lines.length + 2)
185
+ await t.test('should not log the error if error handler is defined and it does not error', async (t) => {
194
186
  const stream = split(JSON.parse)
195
187
  const fastify = Fastify({
196
188
  logger: {
@@ -198,28 +190,34 @@ t.test('logging', (t) => {
198
190
  level: 'info'
199
191
  }
200
192
  })
201
- t.teardown(fastify.close.bind(fastify))
193
+ t.after(() => fastify.close())
202
194
  fastify.get('/error', function (req, reply) {
203
- t.ok(req.log)
195
+ t.assert.ok(req.log)
204
196
  reply.send(new Error('something happened'))
205
197
  })
206
198
  fastify.setErrorHandler((err, req, reply) => {
207
- t.ok(err)
199
+ t.assert.ok(err)
208
200
  reply.send('something bad happened')
209
201
  })
210
202
 
211
203
  await fastify.ready()
212
- await fastify.listen({ port: 0, host: localhost })
204
+ const server = await fastify.listen({ port: 0, host: localhost })
205
+ const lines = [
206
+ { msg: `Server listening at ${server}` },
207
+ { level: 30, msg: 'incoming request' },
208
+ { res: { statusCode: 200 }, msg: 'request completed' }
209
+ ]
210
+ t.plan(lines.length + 2)
213
211
 
214
212
  await request(`http://${localhostForURL}:` + fastify.server.address().port + '/error')
215
213
 
216
214
  for await (const [line] of on(stream, 'data')) {
217
- t.match(line, lines.shift())
215
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
218
216
  if (lines.length === 0) break
219
217
  }
220
218
  })
221
219
 
222
- t.test('reply.send logs an error if called twice in a row', async (t) => {
220
+ await t.test('reply.send logs an error if called twice in a row', async (t) => {
223
221
  const lines = [
224
222
  'incoming request',
225
223
  'request completed',
@@ -234,7 +232,7 @@ t.test('logging', (t) => {
234
232
  const fastify = Fastify({
235
233
  loggerInstance
236
234
  })
237
- t.teardown(fastify.close.bind(fastify))
235
+ t.after(() => fastify.close())
238
236
 
239
237
  fastify.get('/', (req, reply) => {
240
238
  reply.send({ hello: 'world' })
@@ -244,19 +242,19 @@ t.test('logging', (t) => {
244
242
 
245
243
  const response = await fastify.inject({ method: 'GET', url: '/' })
246
244
  const body = await response.json()
247
- t.same(body, { hello: 'world' })
245
+ t.assert.ok(partialDeepStrictEqual(body, { hello: 'world' }))
248
246
 
249
247
  for await (const [line] of on(stream, 'data')) {
250
- t.same(line.msg, lines.shift())
248
+ t.assert.strictEqual(line.msg, lines.shift())
251
249
  if (lines.length === 0) break
252
250
  }
253
251
  })
254
252
 
255
- t.test('should not log incoming request and outgoing response when disabled', async (t) => {
253
+ await t.test('should not log incoming request and outgoing response when disabled', async (t) => {
256
254
  t.plan(1)
257
255
  const stream = split(JSON.parse)
258
256
  const fastify = Fastify({ disableRequestLogging: true, logger: { level: 'info', stream } })
259
- t.teardown(fastify.close.bind(fastify))
257
+ t.after(() => fastify.close())
260
258
 
261
259
  fastify.get('/500', (req, reply) => {
262
260
  reply.code(500).send(Error('500 error'))
@@ -267,27 +265,27 @@ t.test('logging', (t) => {
267
265
  await fastify.inject({ method: 'GET', url: '/500' })
268
266
 
269
267
  // no more readable data
270
- t.equal(stream.readableLength, 0)
268
+ t.assert.strictEqual(stream.readableLength, 0)
271
269
  })
272
270
 
273
- t.test('should not log incoming request, outgoing response and route not found for 404 onBadUrl when disabled', async (t) => {
271
+ await t.test('should not log incoming request, outgoing response and route not found for 404 onBadUrl when disabled', async (t) => {
274
272
  t.plan(1)
275
273
  const stream = split(JSON.parse)
276
274
  const fastify = Fastify({ disableRequestLogging: true, logger: { level: 'info', stream } })
277
- t.teardown(fastify.close.bind(fastify))
275
+ t.after(() => fastify.close())
278
276
 
279
277
  await fastify.ready()
280
278
 
281
279
  await fastify.inject({ method: 'GET', url: '/%c0' })
282
280
 
283
281
  // no more readable data
284
- t.equal(stream.readableLength, 0)
282
+ t.assert.strictEqual(stream.readableLength, 0)
285
283
  })
286
284
 
287
- t.test('defaults to info level', async (t) => {
285
+ await t.test('defaults to info level', async (t) => {
288
286
  const lines = [
289
- { reqId: /req-/, req: { method: 'GET' }, msg: 'incoming request' },
290
- { reqId: /req-/, res: { statusCode: 200 }, msg: 'request completed' }
287
+ { req: { method: 'GET' }, msg: 'incoming request' },
288
+ { res: { statusCode: 200 }, msg: 'request completed' }
291
289
  ]
292
290
  t.plan(lines.length * 2 + 1)
293
291
  const stream = split(JSON.parse)
@@ -296,10 +294,10 @@ t.test('logging', (t) => {
296
294
  stream
297
295
  }
298
296
  })
299
- t.teardown(fastify.close.bind(fastify))
297
+ t.after(() => fastify.close())
300
298
 
301
299
  fastify.get('/', function (req, reply) {
302
- t.ok(req.log)
300
+ t.assert.ok(req.log)
303
301
  reply.send({ hello: 'world' })
304
302
  })
305
303
 
@@ -313,20 +311,13 @@ t.test('logging', (t) => {
313
311
  // we skip the non-request log
314
312
  if (typeof line.reqId !== 'string') continue
315
313
  if (id === undefined && line.reqId) id = line.reqId
316
- if (id !== undefined && line.reqId) t.equal(line.reqId, id)
317
- t.match(line, lines.shift())
314
+ if (id !== undefined && line.reqId) t.assert.strictEqual(line.reqId, id)
315
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
318
316
  if (lines.length === 0) break
319
317
  }
320
318
  })
321
319
 
322
- t.test('test log stream', async (t) => {
323
- const lines = [
324
- { msg: /^Server listening at / },
325
- { reqId: /req-/, req: { method: 'GET' }, msg: 'incoming request' },
326
- { reqId: /req-/, res: { statusCode: 200 }, msg: 'request completed' }
327
- ]
328
- t.plan(lines.length + 3)
329
-
320
+ await t.test('test log stream', async (t) => {
330
321
  const stream = split(JSON.parse)
331
322
  const fastify = Fastify({
332
323
  logger: {
@@ -334,36 +325,34 @@ t.test('logging', (t) => {
334
325
  level: 'info'
335
326
  }
336
327
  })
337
- t.teardown(fastify.close.bind(fastify))
328
+ t.after(() => fastify.close())
338
329
 
339
330
  fastify.get('/', function (req, reply) {
340
- t.ok(req.log)
331
+ t.assert.ok(req.log)
341
332
  reply.send({ hello: 'world' })
342
333
  })
343
334
 
344
335
  await fastify.ready()
345
- await fastify.listen({ port: 0, host: localhost })
336
+ const server = await fastify.listen({ port: 0, host: localhost })
337
+ const lines = [
338
+ { msg: `Server listening at ${server}` },
339
+ { req: { method: 'GET' }, msg: 'incoming request' },
340
+ { res: { statusCode: 200 }, msg: 'request completed' }
341
+ ]
342
+ t.plan(lines.length + 3)
346
343
 
347
344
  await request(`http://${localhostForURL}:` + fastify.server.address().port)
348
345
 
349
346
  let id
350
347
  for await (const [line] of on(stream, 'data')) {
351
348
  if (id === undefined && line.reqId) id = line.reqId
352
- if (id !== undefined && line.reqId) t.equal(line.reqId, id)
353
- t.match(line, lines.shift())
349
+ if (id !== undefined && line.reqId) t.assert.strictEqual(line.reqId, id)
350
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
354
351
  if (lines.length === 0) break
355
352
  }
356
353
  })
357
354
 
358
- t.test('test error log stream', async (t) => {
359
- const lines = [
360
- { msg: /^Server listening at / },
361
- { reqId: /req-/, req: { method: 'GET' }, msg: 'incoming request' },
362
- { reqId: /req-/, res: { statusCode: 500 }, msg: 'kaboom' },
363
- { reqId: /req-/, res: { statusCode: 500 }, msg: 'request completed' }
364
- ]
365
- t.plan(lines.length + 4)
366
-
355
+ await t.test('test error log stream', async (t) => {
367
356
  const stream = split(JSON.parse)
368
357
  const fastify = Fastify({
369
358
  logger: {
@@ -371,28 +360,35 @@ t.test('logging', (t) => {
371
360
  level: 'info'
372
361
  }
373
362
  })
374
- t.teardown(fastify.close.bind(fastify))
363
+ t.after(() => fastify.close())
375
364
 
376
365
  fastify.get('/error', function (req, reply) {
377
- t.ok(req.log)
366
+ t.assert.ok(req.log)
378
367
  reply.send(new Error('kaboom'))
379
368
  })
380
369
 
381
370
  await fastify.ready()
382
- await fastify.listen({ port: 0, host: localhost })
371
+ const server = await fastify.listen({ port: 0, host: localhost })
372
+ const lines = [
373
+ { msg: `Server listening at ${server}` },
374
+ { req: { method: 'GET' }, msg: 'incoming request' },
375
+ { res: { statusCode: 500 }, msg: 'kaboom' },
376
+ { res: { statusCode: 500 }, msg: 'request completed' }
377
+ ]
378
+ t.plan(lines.length + 4)
383
379
 
384
380
  await request(`http://${localhostForURL}:` + fastify.server.address().port + '/error')
385
381
 
386
382
  let id
387
383
  for await (const [line] of on(stream, 'data')) {
388
384
  if (id === undefined && line.reqId) id = line.reqId
389
- if (id !== undefined && line.reqId) t.equal(line.reqId, id)
390
- t.match(line, lines.shift())
385
+ if (id !== undefined && line.reqId) t.assert.strictEqual(line.reqId, id)
386
+ t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
391
387
  if (lines.length === 0) break
392
388
  }
393
389
  })
394
390
 
395
- t.test('should not log the error if request logging is disabled', async (t) => {
391
+ await t.test('should not log the error if request logging is disabled', async (t) => {
396
392
  t.plan(4)
397
393
 
398
394
  const stream = split(JSON.parse)
@@ -403,10 +399,10 @@ t.test('logging', (t) => {
403
399
  },
404
400
  disableRequestLogging: true
405
401
  })
406
- t.teardown(fastify.close.bind(fastify))
402
+ t.after(() => fastify.close())
407
403
 
408
404
  fastify.get('/error', function (req, reply) {
409
- t.ok(req.log)
405
+ t.assert.ok(req.log)
410
406
  reply.send(new Error('a generic error'))
411
407
  })
412
408
 
@@ -417,11 +413,11 @@ t.test('logging', (t) => {
417
413
 
418
414
  {
419
415
  const [line] = await once(stream, 'data')
420
- t.type(line.msg, 'string')
421
- t.ok(line.msg.startsWith('Server listening at'), 'message is set')
416
+ t.assert.ok(typeof line.msg === 'string')
417
+ t.assert.ok(line.msg.startsWith('Server listening at'), 'message is set')
422
418
  }
423
419
 
424
420
  // no more readable data
425
- t.equal(stream.readableLength, 0)
421
+ t.assert.strictEqual(stream.readableLength, 0)
426
422
  })
427
423
  })