Skip to content

Commit f30973c

Browse files
authored
test: migrate request logger to node test runner (#6058)
1 parent 0a9aa87 commit f30973c

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

test/logger/request.test.js

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22

33
const stream = require('node:stream')
44

5-
const t = require('tap')
5+
const t = require('node:test')
66
const split = require('split2')
77

88
const Fastify = require('../../fastify')
99
const helper = require('../helper')
1010
const { on } = stream
1111
const { request } = require('./logger-test-utils')
12+
const { partialDeepStrictEqual } = require('../toolkit')
1213

13-
t.test('request', (t) => {
14-
t.setTimeout(60000)
15-
14+
t.test('request', { timeout: 60000 }, async (t) => {
1615
let localhost
1716

1817
t.plan(7)
1918
t.before(async function () {
2019
[localhost] = await helper.getLoopbackHost()
2120
})
2221

23-
t.test('The request id header key can be customized', async (t) => {
22+
await t.test('The request id header key can be customized', async (t) => {
2423
const lines = ['incoming request', 'some log message', 'request completed']
2524
t.plan(lines.length * 2 + 2)
2625
const REQUEST_ID = '42'
@@ -30,26 +29,26 @@ t.test('request', (t) => {
3029
logger: { stream, level: 'info' },
3130
requestIdHeader: 'my-custom-request-id'
3231
})
33-
t.teardown(fastify.close.bind(fastify))
32+
t.after(() => fastify.close())
3433

3534
fastify.get('/', (req, reply) => {
36-
t.equal(req.id, REQUEST_ID)
35+
t.assert.strictEqual(req.id, REQUEST_ID)
3736
req.log.info('some log message')
3837
reply.send({ id: req.id })
3938
})
4039

4140
const response = await fastify.inject({ method: 'GET', url: '/', headers: { 'my-custom-request-id': REQUEST_ID } })
4241
const body = await response.json()
43-
t.equal(body.id, REQUEST_ID)
42+
t.assert.strictEqual(body.id, REQUEST_ID)
4443

4544
for await (const [line] of on(stream, 'data')) {
46-
t.equal(line.reqId, REQUEST_ID)
47-
t.equal(line.msg, lines.shift(), 'message is set')
45+
t.assert.strictEqual(line.reqId, REQUEST_ID)
46+
t.assert.strictEqual(line.msg, lines.shift(), 'message is set')
4847
if (lines.length === 0) break
4948
}
5049
})
5150

52-
t.test('The request id header key can be ignored', async (t) => {
51+
await t.test('The request id header key can be ignored', async (t) => {
5352
const lines = ['incoming request', 'some log message', 'request completed']
5453
t.plan(lines.length * 2 + 2)
5554
const REQUEST_ID = 'ignore-me'
@@ -59,33 +58,33 @@ t.test('request', (t) => {
5958
logger: { stream, level: 'info' },
6059
requestIdHeader: false
6160
})
62-
t.teardown(fastify.close.bind(fastify))
61+
t.after(() => fastify.close())
6362

6463
fastify.get('/', (req, reply) => {
65-
t.equal(req.id, 'req-1')
64+
t.assert.strictEqual(req.id, 'req-1')
6665
req.log.info('some log message')
6766
reply.send({ id: req.id })
6867
})
6968
const response = await fastify.inject({ method: 'GET', url: '/', headers: { 'request-id': REQUEST_ID } })
7069
const body = await response.json()
71-
t.equal(body.id, 'req-1')
70+
t.assert.strictEqual(body.id, 'req-1')
7271

7372
for await (const [line] of on(stream, 'data')) {
74-
t.equal(line.reqId, 'req-1')
75-
t.equal(line.msg, lines.shift(), 'message is set')
73+
t.assert.strictEqual(line.reqId, 'req-1')
74+
t.assert.strictEqual(line.msg, lines.shift(), 'message is set')
7675
if (lines.length === 0) break
7776
}
7877
})
7978

80-
t.test('The request id header key can be customized along with a custom id generator', async (t) => {
79+
await t.test('The request id header key can be customized along with a custom id generator', async (t) => {
8180
const REQUEST_ID = '42'
8281
const matches = [
83-
{ reqId: REQUEST_ID, msg: /incoming request/ },
84-
{ reqId: REQUEST_ID, msg: /some log message/ },
85-
{ reqId: REQUEST_ID, msg: /request completed/ },
86-
{ reqId: 'foo', msg: /incoming request/ },
87-
{ reqId: 'foo', msg: /some log message 2/ },
88-
{ reqId: 'foo', msg: /request completed/ }
82+
{ reqId: REQUEST_ID, msg: 'incoming request' },
83+
{ reqId: REQUEST_ID, msg: 'some log message' },
84+
{ reqId: REQUEST_ID, msg: 'request completed' },
85+
{ reqId: 'foo', msg: 'incoming request' },
86+
{ reqId: 'foo', msg: 'some log message 2' },
87+
{ reqId: 'foo', msg: 'request completed' }
8988
]
9089
t.plan(matches.length + 4)
9190

@@ -97,47 +96,47 @@ t.test('request', (t) => {
9796
return 'foo'
9897
}
9998
})
100-
t.teardown(fastify.close.bind(fastify))
99+
t.after(() => fastify.close())
101100

102101
fastify.get('/one', (req, reply) => {
103-
t.equal(req.id, REQUEST_ID)
102+
t.assert.strictEqual(req.id, REQUEST_ID)
104103
req.log.info('some log message')
105104
reply.send({ id: req.id })
106105
})
107106

108107
fastify.get('/two', (req, reply) => {
109-
t.equal(req.id, 'foo')
108+
t.assert.strictEqual(req.id, 'foo')
110109
req.log.info('some log message 2')
111110
reply.send({ id: req.id })
112111
})
113112

114113
{
115114
const response = await fastify.inject({ method: 'GET', url: '/one', headers: { 'my-custom-request-id': REQUEST_ID } })
116115
const body = await response.json()
117-
t.equal(body.id, REQUEST_ID)
116+
t.assert.strictEqual(body.id, REQUEST_ID)
118117
}
119118

120119
{
121120
const response = await fastify.inject({ method: 'GET', url: '/two' })
122121
const body = await response.json()
123-
t.equal(body.id, 'foo')
122+
t.assert.strictEqual(body.id, 'foo')
124123
}
125124

126125
for await (const [line] of on(stream, 'data')) {
127-
t.match(line, matches.shift())
126+
t.assert.ok(partialDeepStrictEqual(line, matches.shift()))
128127
if (matches.length === 0) break
129128
}
130129
})
131130

132-
t.test('The request id header key can be ignored along with a custom id generator', async (t) => {
131+
await t.test('The request id header key can be ignored along with a custom id generator', async (t) => {
133132
const REQUEST_ID = 'ignore-me'
134133
const matches = [
135-
{ reqId: 'foo', msg: /incoming request/ },
136-
{ reqId: 'foo', msg: /some log message/ },
137-
{ reqId: 'foo', msg: /request completed/ },
138-
{ reqId: 'foo', msg: /incoming request/ },
139-
{ reqId: 'foo', msg: /some log message 2/ },
140-
{ reqId: 'foo', msg: /request completed/ }
134+
{ reqId: 'foo', msg: 'incoming request' },
135+
{ reqId: 'foo', msg: 'some log message' },
136+
{ reqId: 'foo', msg: 'request completed' },
137+
{ reqId: 'foo', msg: 'incoming request' },
138+
{ reqId: 'foo', msg: 'some log message 2' },
139+
{ reqId: 'foo', msg: 'request completed' }
141140
]
142141
t.plan(matches.length + 4)
143142

@@ -149,44 +148,44 @@ t.test('request', (t) => {
149148
return 'foo'
150149
}
151150
})
152-
t.teardown(fastify.close.bind(fastify))
151+
t.after(() => fastify.close())
153152

154153
fastify.get('/one', (req, reply) => {
155-
t.equal(req.id, 'foo')
154+
t.assert.strictEqual(req.id, 'foo')
156155
req.log.info('some log message')
157156
reply.send({ id: req.id })
158157
})
159158

160159
fastify.get('/two', (req, reply) => {
161-
t.equal(req.id, 'foo')
160+
t.assert.strictEqual(req.id, 'foo')
162161
req.log.info('some log message 2')
163162
reply.send({ id: req.id })
164163
})
165164

166165
{
167166
const response = await fastify.inject({ method: 'GET', url: '/one', headers: { 'request-id': REQUEST_ID } })
168167
const body = await response.json()
169-
t.equal(body.id, 'foo')
168+
t.assert.strictEqual(body.id, 'foo')
170169
}
171170

172171
{
173172
const response = await fastify.inject({ method: 'GET', url: '/two' })
174173
const body = await response.json()
175-
t.equal(body.id, 'foo')
174+
t.assert.strictEqual(body.id, 'foo')
176175
}
177176

178177
for await (const [line] of on(stream, 'data')) {
179-
t.match(line, matches.shift())
178+
t.assert.ok(partialDeepStrictEqual(line, matches.shift()))
180179
if (matches.length === 0) break
181180
}
182181
})
183182

184-
t.test('The request id log label can be changed', async (t) => {
183+
await t.test('The request id log label can be changed', async (t) => {
185184
const REQUEST_ID = '42'
186185
const matches = [
187-
{ traceId: REQUEST_ID, msg: /incoming request/ },
188-
{ traceId: REQUEST_ID, msg: /some log message/ },
189-
{ traceId: REQUEST_ID, msg: /request completed/ }
186+
{ traceId: REQUEST_ID, msg: 'incoming request' },
187+
{ traceId: REQUEST_ID, msg: 'some log message' },
188+
{ traceId: REQUEST_ID, msg: 'request completed' }
190189
]
191190
t.plan(matches.length + 2)
192191

@@ -196,33 +195,27 @@ t.test('request', (t) => {
196195
requestIdHeader: 'my-custom-request-id',
197196
requestIdLogLabel: 'traceId'
198197
})
199-
t.teardown(fastify.close.bind(fastify))
198+
t.after(() => fastify.close())
200199

201200
fastify.get('/one', (req, reply) => {
202-
t.equal(req.id, REQUEST_ID)
201+
t.assert.strictEqual(req.id, REQUEST_ID)
203202
req.log.info('some log message')
204203
reply.send({ id: req.id })
205204
})
206205

207206
{
208207
const response = await fastify.inject({ method: 'GET', url: '/one', headers: { 'my-custom-request-id': REQUEST_ID } })
209208
const body = await response.json()
210-
t.equal(body.id, REQUEST_ID)
209+
t.assert.strictEqual(body.id, REQUEST_ID)
211210
}
212211

213212
for await (const [line] of on(stream, 'data')) {
214-
t.match(line, matches.shift())
213+
t.assert.ok(partialDeepStrictEqual(line, matches.shift()))
215214
if (matches.length === 0) break
216215
}
217216
})
218217

219-
t.test('should redact the authorization header if so specified', async (t) => {
220-
const lines = [
221-
{ msg: /Server listening at/ },
222-
{ req: { headers: { authorization: '[Redacted]' } }, msg: 'incoming request' },
223-
{ res: { statusCode: 200 }, msg: 'request completed' }
224-
]
225-
t.plan(lines.length + 3)
218+
await t.test('should redact the authorization header if so specified', async (t) => {
226219
const stream = split(JSON.parse)
227220
const fastify = Fastify({
228221
logger: {
@@ -243,15 +236,22 @@ t.test('request', (t) => {
243236
}
244237
}
245238
})
246-
t.teardown(fastify.close.bind(fastify))
239+
t.after(() => fastify.close())
247240

248241
fastify.get('/', function (req, reply) {
249-
t.same(req.headers.authorization, 'Bearer abcde')
242+
t.assert.deepStrictEqual(req.headers.authorization, 'Bearer abcde')
250243
reply.send({ hello: 'world' })
251244
})
252245

253246
await fastify.ready()
254-
await fastify.listen({ port: 0, host: localhost })
247+
const server = await fastify.listen({ port: 0, host: localhost })
248+
249+
const lines = [
250+
{ msg: `Server listening at ${server}` },
251+
{ req: { headers: { authorization: '[Redacted]' } }, msg: 'incoming request' },
252+
{ res: { statusCode: 200 }, msg: 'request completed' }
253+
]
254+
t.plan(lines.length + 3)
255255

256256
await request({
257257
method: 'GET',
@@ -262,17 +262,17 @@ t.test('request', (t) => {
262262
authorization: 'Bearer abcde'
263263
}
264264
}, function (response, body) {
265-
t.equal(response.statusCode, 200)
266-
t.same(body, JSON.stringify({ hello: 'world' }))
265+
t.assert.strictEqual(response.statusCode, 200)
266+
t.assert.deepStrictEqual(body, JSON.stringify({ hello: 'world' }))
267267
})
268268

269269
for await (const [line] of on(stream, 'data')) {
270-
t.match(line, lines.shift())
270+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
271271
if (lines.length === 0) break
272272
}
273273
})
274274

275-
t.test('should not throw error when serializing custom req', (t) => {
275+
await t.test('should not throw error when serializing custom req', (t) => {
276276
t.plan(1)
277277

278278
const lines = []
@@ -283,10 +283,10 @@ t.test('request', (t) => {
283283
}
284284
})
285285
const fastify = Fastify({ logger: { level: 'info', stream: dest } })
286-
t.teardown(fastify.close.bind(fastify))
286+
t.after(() => fastify.close())
287287

288288
fastify.log.info({ req: {} })
289289

290-
t.same(lines[0].req, {})
290+
t.assert.deepStrictEqual(lines[0].req, {})
291291
})
292292
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy