Aktueller Stand
This commit is contained in:
73
backend/node_modules/fastify/test/internals/logger.test.js
generated
vendored
73
backend/node_modules/fastify/test/internals/logger.test.js
generated
vendored
@@ -1,34 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
const t = require('tap')
|
||||
const test = t.test
|
||||
const { test } = require('node:test')
|
||||
const Fastify = require('../..')
|
||||
const loggerUtils = require('../../lib/logger')
|
||||
const loggerUtils = require('../../lib/logger-factory')
|
||||
const { serializers } = require('../../lib/logger-pino')
|
||||
|
||||
test('time resolution', t => {
|
||||
t.plan(2)
|
||||
t.equal(typeof loggerUtils.now, 'function')
|
||||
t.equal(typeof loggerUtils.now(), 'number')
|
||||
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 => {
|
||||
test('The logger should add a unique id for every request', (t, done) => {
|
||||
const ids = []
|
||||
|
||||
const fastify = Fastify()
|
||||
fastify.get('/', (req, reply) => {
|
||||
t.ok(req.id)
|
||||
t.assert.ok(req.id)
|
||||
reply.send({ id: req.id })
|
||||
})
|
||||
|
||||
fastify.listen({ port: 0 }, err => {
|
||||
t.error(err)
|
||||
t.assert.ifError(err)
|
||||
const queue = new Queue()
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.add(checkId)
|
||||
}
|
||||
queue.add(() => {
|
||||
fastify.close()
|
||||
t.end()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -37,24 +37,24 @@ test('The logger should add a unique id for every request', t => {
|
||||
method: 'GET',
|
||||
url: 'http://localhost:' + fastify.server.address().port
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.assert.ifError(err)
|
||||
const payload = JSON.parse(res.payload)
|
||||
t.ok(ids.indexOf(payload.id) === -1, 'the id should not be duplicated')
|
||||
t.assert.ok(ids.indexOf(payload.id) === -1, 'the id should not be duplicated')
|
||||
ids.push(payload.id)
|
||||
done()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
test('The logger should reuse request id header for req.id', t => {
|
||||
test('The logger should not reuse request id header for req.id', (t, done) => {
|
||||
const fastify = Fastify()
|
||||
fastify.get('/', (req, reply) => {
|
||||
t.ok(req.id)
|
||||
t.assert.ok(req.id)
|
||||
reply.send({ id: req.id })
|
||||
})
|
||||
|
||||
fastify.listen({ port: 0 }, err => {
|
||||
t.error(err)
|
||||
t.assert.ifError(err)
|
||||
|
||||
fastify.inject({
|
||||
method: 'GET',
|
||||
@@ -63,11 +63,40 @@ test('The logger should reuse request id header for req.id', t => {
|
||||
'Request-Id': 'request-id-1'
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
t.assert.ifError(err)
|
||||
const payload = JSON.parse(res.payload)
|
||||
t.ok(payload.id === 'request-id-1', 'the request id from the header should be returned')
|
||||
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()
|
||||
t.end()
|
||||
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()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -108,26 +137,26 @@ test('The logger should error if both stream and file destination are given', t
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
t.equal(err.code, 'FST_ERR_LOG_INVALID_DESTINATION')
|
||||
t.equal(err.message, 'Cannot specify both logger.stream and logger.file options')
|
||||
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 = loggerUtils.serializers.req({
|
||||
const serialized = serializers.req({
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
socket: undefined,
|
||||
headers: {}
|
||||
})
|
||||
|
||||
t.same(serialized, {
|
||||
t.assert.deepStrictEqual(serialized, {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
version: undefined,
|
||||
hostname: undefined,
|
||||
host: undefined,
|
||||
remoteAddress: undefined,
|
||||
remotePort: undefined
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user