Spaces:
Runtime error
Runtime error
File size: 1,682 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 |
'use strict'
const { test } = require('node:test')
const diagnostics = require('node:diagnostics_channel')
const sget = require('simple-get').concat
const Fastify = require('../..')
const { getServerUrl } = require('../helper')
const Request = require('../../lib/request')
const Reply = require('../../lib/reply')
test('diagnostics channel sync events fire in expected order', (t, done) => {
t.plan(10)
let callOrder = 0
let firstEncounteredMessage
diagnostics.subscribe('tracing:fastify.request.handler:start', (msg) => {
t.assert.strictEqual(callOrder++, 0)
firstEncounteredMessage = msg
t.assert.ok(msg.request instanceof Request)
t.assert.ok(msg.reply instanceof Reply)
})
diagnostics.subscribe('tracing:fastify.request.handler:end', (msg) => {
t.assert.ok(msg.request instanceof Request)
t.assert.ok(msg.reply instanceof Reply)
t.assert.strictEqual(callOrder++, 1)
t.assert.strictEqual(msg, firstEncounteredMessage)
})
diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
t.assert.fail('should not trigger error channel')
})
const fastify = Fastify()
fastify.route({
method: 'GET',
url: '/',
handler: function (req, reply) {
reply.send({ hello: 'world' })
}
})
fastify.listen({ port: 0 }, function (err) {
if (err) t.assert.ifError(err)
t.after(() => { fastify.close() })
sget({
method: 'GET',
url: getServerUrl(fastify) + '/'
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
done()
})
})
})
|