Skip to content

Commit

Permalink
fix: refactor requestContextPlugin.spec.js to use response headers in…
Browse files Browse the repository at this point in the history
…stead of JSON body (#176)
  • Loading branch information
shawshankkumar authored Nov 18, 2024
1 parent a9c2df2 commit 59c967f
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/requestContextPlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
})

0 comments on commit 59c967f

Please sign in to comment.