Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added request id omit flag #303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*
* **Important:** this should be your first middleware
*/
export function addRequestId(

Check failure on line 12 in src/code-templates/libraries/request-context/src/request-id/express/middleware.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Expected to return a value at the end of function 'addRequestId'

Check failure on line 12 in src/code-templates/libraries/request-context/src/request-id/express/middleware.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Expected to return a value at the end of function 'addRequestId'
req: IncomingMessage,
res: ServerResponse,
next: () => void
) {
if (process.env.SKIP_ADD_REQUEST_ID_MIDDLEWARE === 'true') return next();

let requestId = req.headers[REQUEST_ID_HEADER];

if (!requestId) {
Expand All @@ -29,7 +31,7 @@
// Append to the current context
currentContext.requestId = requestId;
next();
return;

Check failure on line 34 in src/code-templates/libraries/request-context/src/request-id/express/middleware.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Function 'addRequestId' expected a return value

Check failure on line 34 in src/code-templates/libraries/request-context/src/request-id/express/middleware.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Function 'addRequestId' expected a return value
}

context().run({ requestId }, next);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sinon from 'sinon';
import { context } from '../index';
import sinon from 'sinon';

Check failure on line 2 in src/code-templates/libraries/request-context/test/request-context.test.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

`sinon` import should occur before import of `../index`

Check failure on line 2 in src/code-templates/libraries/request-context/test/request-context.test.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

`sinon` import should occur before import of `../index`

describe('request-context', () => {
test('When instantiating a new context with initial values, then should get back the context', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -36,6 +37,8 @@ describe('Request ID express middleware', () => {

currentServer = undefined;
}

sinon.restore();
});

describe('when the request ID already exists in the request header', () => {
Expand Down Expand Up @@ -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: {},
});
});
});
Loading