strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5const sget = require('simple-get').concat6 7test('The request id header key can be customized', async (t) => {8 t.plan(2)9 const REQUEST_ID = '42'10 11 const fastify = Fastify({12 requestIdHeader: 'my-custom-request-id'13 })14 15 fastify.get('/', (req, reply) => {16 t.assert.strictEqual(req.id, REQUEST_ID)17 reply.send({ id: req.id })18 })19 20 const response = await fastify.inject({ method: 'GET', url: '/', headers: { 'my-custom-request-id': REQUEST_ID } })21 const body = await response.json()22 t.assert.strictEqual(body.id, REQUEST_ID)23})24 25test('The request id header key can be customized', async (t) => {26 t.plan(2)27 const REQUEST_ID = '42'28 29 const fastify = Fastify({30 requestIdHeader: 'my-custom-request-id'31 })32 33 fastify.get('/', (req, reply) => {34 t.assert.strictEqual(req.id, REQUEST_ID)35 reply.send({ id: req.id })36 })37 38 const response = await fastify.inject({ method: 'GET', url: '/', headers: { 'MY-CUSTOM-REQUEST-ID': REQUEST_ID } })39 const body = await response.json()40 t.assert.strictEqual(body.id, REQUEST_ID)41})42 43test('The request id header key can be customized', (t, done) => {44 t.plan(4)45 const REQUEST_ID = '42'46 47 const fastify = Fastify({48 requestIdHeader: 'my-custom-request-id'49 })50 51 fastify.get('/', (req, reply) => {52 t.assert.strictEqual(req.id, REQUEST_ID)53 reply.send({ id: req.id })54 })55 56 t.after(() => fastify.close())57 58 fastify.listen({ port: 0 }, (err, address) => {59 t.assert.ifError(err)60 61 sget({62 method: 'GET',63 url: address,64 headers: {65 'my-custom-request-id': REQUEST_ID66 }67 }, (err, response, body) => {68 t.assert.ifError(err)69 t.assert.strictEqual(body.toString(), `{"id":"${REQUEST_ID}"}`)70 done()71 })72 })73})74 75test('The request id header key can be customized', (t, done) => {76 t.plan(4)77 const REQUEST_ID = '42'78 79 const fastify = Fastify({80 requestIdHeader: 'my-custom-request-id'81 })82 83 fastify.get('/', (req, reply) => {84 t.assert.strictEqual(req.id, REQUEST_ID)85 reply.send({ id: req.id })86 })87 88 t.after(() => fastify.close())89 90 fastify.listen({ port: 0 }, (err, address) => {91 t.assert.ifError(err)92 93 sget({94 method: 'GET',95 url: address,96 headers: {97 'MY-CUSTOM-REQUEST-ID': REQUEST_ID98 }99 }, (err, response, body) => {100 t.assert.ifError(err)101 t.assert.strictEqual(body.toString(), `{"id":"${REQUEST_ID}"}`)102 done()103 })104 })105})106 107test('The request id header key can be customized', (t, done) => {108 t.plan(4)109 const REQUEST_ID = '42'110 111 const fastify = Fastify({112 requestIdHeader: 'MY-CUSTOM-REQUEST-ID'113 })114 115 fastify.get('/', (req, reply) => {116 t.assert.strictEqual(req.id, REQUEST_ID)117 reply.send({ id: req.id })118 })119 120 t.after(() => fastify.close())121 122 fastify.listen({ port: 0 }, (err, address) => {123 t.assert.ifError(err)124 125 sget({126 method: 'GET',127 url: address,128 headers: {129 'MY-CUSTOM-REQUEST-ID': REQUEST_ID130 }131 }, (err, response, body) => {132 t.assert.ifError(err)133 t.assert.strictEqual(body.toString(), `{"id":"${REQUEST_ID}"}`)134 done()135 })136 })137})138 