diff --git a/test/requestContextPlugin.spec.js b/test/requestContextPlugin.spec.js index 85d5514..ba8568f 100644 --- a/test/requestContextPlugin.spec.js +++ b/test/requestContextPlugin.spec.js @@ -278,4 +278,77 @@ describe('requestContextPlugin', () => { }) }) }) + + it('correctly preserves values for 204 responses', () => { + expect.assertions(2) + + let testService + let responseCounter = 0 + return new Promise((resolveResponsePromise) => { + const promiseRequest2 = new Promise((resolveRequest2Promise) => { + const promiseRequest1 = new Promise((resolveRequest1Promise) => { + const route = (req, reply) => { + function prepareReply() { + return testService.processRequest(requestId).then(() => { + const storedValue = req.requestContext.get('testKey') + reply.status(204).header('storedvalue', storedValue).send() + }) + } + + const requestId = Number.parseInt(req.query.requestId) + req.requestContext.set('testKey', `testValue${requestId}`) + + // We don't want to read values until both requests wrote their values to see if there is a racing condition + if (requestId === 1) { + resolveRequest1Promise() + return promiseRequest2.then(prepareReply) + } + + if (requestId === 2) { + resolveRequest2Promise() + return promiseRequest1.then(prepareReply) + } + } + + initAppGet(route) + .ready() + .then((_app) => { + app = _app + testService = new TestService(app) + const response1Promise = app + .inject() + .get('/') + .query({ requestId: 1 }) + .end() + .then((response) => { + expect(response.headers.storedvalue).toBe('testValue1') + responseCounter++ + if (responseCounter === 2) { + resolveResponsePromise() + } + }) + + const response2Promise = app + .inject() + .get('/') + .query({ requestId: 2 }) + .end() + .then((response) => { + expect(response.headers.storedvalue).toBe('testValue2') + responseCounter++ + if (responseCounter === 2) { + resolveResponsePromise() + } + }) + + return Promise.all([response1Promise, response2Promise]) + }) + }) + + return promiseRequest1 + }) + + return promiseRequest2 + }) + }) })