Skip to content

Demonstrate issue #78 through unit tests and an intermediate library #79

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

Closed
Closed
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
77 changes: 75 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"homepage": "https://github.com/skonves/express-http-context#readme",
"funding": "https://github.com/skonves/express-http-context?sponsor=1",
"devDependencies": {
"@oliverlockwood/express-http-context-intermediate-library": "0.0.5-original-library",
"express": "^4.21.2",
"jest": "^29.7.0",
"supertest": "^7.0.0"
Expand Down
60 changes: 60 additions & 0 deletions tests/express-test-harness.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const express = require('express');
const { init, REQUEST_ID_CONTEXT_KEY, REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME } = require('@oliverlockwood/express-http-context-intermediate-library');
const supertest = require('supertest');

const httpContext = require('../index');
Expand Down Expand Up @@ -222,4 +223,63 @@ describe('express-http-context', function () {
done();
});
});

it('maintains unique value when the library is depended upon both directly and transitively', async () => {

// ARRANGE
const app = express();

// this function in the test library makes the following two calls:
// 1. app.use(middleware) and
// 2. httpContext.set(REQUEST_ID_CONTEXT_KEY, <a unique id>)
// as can be seen in https://github.com/oliverlockwood/express-http-context-intermediate-library/blob/original-express-http-context/src/index.ts#L13-L19
init(app);

app.get('/', ((req, res) => {
httpContext.set('value', req.query['value']);

res.status(200).json({
fred: '123',
value: req.query['value'],
valueFromContext: httpContext.get('value'),
requestId: httpContext.get(REQUEST_ID_CONTEXT_KEY)
});
}));

const request = supertest(app);

// ACT
const [response1, response2] = await Promise.all([
request.get('/').query({ value: 'value1' }),
request.get('/').query({ value: 'value2' }),
])

console.log(response1.body);
console.log(response1.headers);
console.log(response2.body);
console.log(response2.headers);

expect(response1.body.value).toBe('value1');
expect(response2.body.value).toBe('value2');

expect(response1.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]?.length).toBe(21);
expect(response2.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]?.length).toBe(21);

// This is the specific example I had flagged in the Github issue - where
// setting something into the httpContext in a common library, but it's
// unusable from within the application code.
expect(response1.body.requestId).toBe(response1.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]);
expect(response2.body.requestId).toBe(response2.header[REQUEST_ID_IN_RESPONSE_HTTP_HEADER_NAME]);

// These operations also fail, I suspect, because neither of the set/get
// functions are usable, because the directly imported AsyncLocalStorage has
// not been initialised by a call to `app.use(middleware)` within our code
// here. Effectively this is another manifestation of the same bug -
// showing that although the middleware *has* already been initialised in
// Express request handler chain, it is not usable because the
// AsyncLocalStorage context is not identical for all usages of the
// `express-http-context` library code.
expect(response1.body.valueFromContext).toBe('value1');
expect(response2.body.valueFromContext).toBe('value2');
});
});