Spaces:
Runtime error
Runtime error
File size: 3,962 Bytes
23ac194 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 'use strict'
const { test } = require('node:test')
const Fastify = require('../..')
const loggerUtils = require('../../lib/logger-factory')
const { serializers } = require('../../lib/logger-pino')
test('time resolution', t => {
t.plan(2)
t.assert.strictEqual(typeof loggerUtils.now, 'function')
t.assert.strictEqual(typeof loggerUtils.now(), 'number')
})
test('The logger should add a unique id for every request', (t, done) => {
const ids = []
const fastify = Fastify()
fastify.get('/', (req, reply) => {
t.assert.ok(req.id)
reply.send({ id: req.id })
})
fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
const queue = new Queue()
for (let i = 0; i < 10; i++) {
queue.add(checkId)
}
queue.add(() => {
fastify.close()
done()
})
})
function checkId (done) {
fastify.inject({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, res) => {
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.assert.ok(ids.indexOf(payload.id) === -1, 'the id should not be duplicated')
ids.push(payload.id)
done()
})
}
})
test('The logger should not reuse request id header for req.id', (t, done) => {
const fastify = Fastify()
fastify.get('/', (req, reply) => {
t.assert.ok(req.id)
reply.send({ id: req.id })
})
fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
fastify.inject({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port,
headers: {
'Request-Id': 'request-id-1'
}
}, (err, res) => {
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.assert.ok(payload.id !== 'request-id-1', 'the request id from the header should not be returned with default configuration')
t.assert.ok(payload.id === 'req-1') // first request id when using the default configuration
fastify.close()
done()
})
})
})
test('The logger should reuse request id header for req.id if requestIdHeader is set', (t, done) => {
const fastify = Fastify({
requestIdHeader: 'request-id'
})
fastify.get('/', (req, reply) => {
t.assert.ok(req.id)
reply.send({ id: req.id })
})
fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
fastify.inject({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port,
headers: {
'Request-Id': 'request-id-1'
}
}, (err, res) => {
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.assert.ok(payload.id === 'request-id-1', 'the request id from the header should be returned')
fastify.close()
done()
})
})
})
function Queue () {
this.q = []
this.running = false
}
Queue.prototype.add = function add (job) {
this.q.push(job)
if (!this.running) this.run()
}
Queue.prototype.run = function run () {
this.running = true
const job = this.q.shift()
job(() => {
if (this.q.length) {
this.run()
} else {
this.running = false
}
})
}
test('The logger should error if both stream and file destination are given', t => {
t.plan(2)
const stream = require('node:stream').Writable
try {
Fastify({
logger: {
level: 'info',
stream,
file: '/test'
}
})
} catch (err) {
t.assert.strictEqual(err.code, 'FST_ERR_LOG_INVALID_DESTINATION')
t.assert.strictEqual(err.message, 'Cannot specify both logger.stream and logger.file options')
}
})
test('The serializer prevent fails if the request socket is undefined', t => {
t.plan(1)
const serialized = serializers.req({
method: 'GET',
url: '/',
socket: undefined,
headers: {}
})
t.assert.deepStrictEqual(serialized, {
method: 'GET',
url: '/',
version: undefined,
host: undefined,
remoteAddress: undefined,
remotePort: undefined
})
})
|