diff --git a/src/code-templates/libraries/request-context/src/request-id/express/middleware.ts b/src/code-templates/libraries/request-context/src/request-id/express/middleware.ts index ea45b3c6..3634fd83 100644 --- a/src/code-templates/libraries/request-context/src/request-id/express/middleware.ts +++ b/src/code-templates/libraries/request-context/src/request-id/express/middleware.ts @@ -14,6 +14,8 @@ export function addRequestId( res: ServerResponse, next: () => void ) { + if (process.env.SKIP_ADD_REQUEST_ID_MIDDLEWARE === 'true') return next(); + let requestId = req.headers[REQUEST_ID_HEADER]; if (!requestId) { diff --git a/src/code-templates/libraries/request-context/test/request-context.test.ts b/src/code-templates/libraries/request-context/test/request-context.test.ts index 9c9770f4..89e03822 100644 --- a/src/code-templates/libraries/request-context/test/request-context.test.ts +++ b/src/code-templates/libraries/request-context/test/request-context.test.ts @@ -1,5 +1,5 @@ -import sinon from 'sinon'; import { context } from '../index'; +import sinon from 'sinon'; describe('request-context', () => { test('When instantiating a new context with initial values, then should get back the context', () => { diff --git a/src/code-templates/libraries/request-context/test/request-id-express-middleware.test.ts b/src/code-templates/libraries/request-context/test/request-id-express-middleware.test.ts index c4879aed..d41b5f5e 100644 --- a/src/code-templates/libraries/request-context/test/request-id-express-middleware.test.ts +++ b/src/code-templates/libraries/request-context/test/request-id-express-middleware.test.ts @@ -2,6 +2,7 @@ import express, { Express } from 'express'; import { Server } from 'http'; import { AddressInfo } from 'net'; import axios from 'axios'; +import sinon from 'sinon'; import { addRequestIdExpressMiddleware, context } from '../index'; import { REQUEST_ID_HEADER } from '../src/request-id/constant'; @@ -36,6 +37,8 @@ describe('Request ID express middleware', () => { currentServer = undefined; } + + sinon.restore(); }); describe('when the request ID already exists in the request header', () => { @@ -245,4 +248,28 @@ describe('Request ID express middleware', () => { }, }); }); + + test('when request ID middleware should be omited', async () => { + // Arrange + const client = await setupExpressServer((app) => { + app.use(addRequestIdExpressMiddleware); + + app.get('/', (req, res) => { + res.send({}); + }); + }); + sinon + .stub(process, 'env') + .value({ SKIP_ADD_REQUEST_ID_MIDDLEWARE: 'true' }); + + // Act + const response = await client.get('/'); + + // Assert + expect(response).toMatchObject({ + status: 200, + data: {}, + headers: {}, + }); + }); });