Skip to content

Commit 8a8f2d5

Browse files
authored
Add test for POST request with body (#11)
1 parent 24f70df commit 8a8f2d5

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"dependencies": {
2222
"asynchronous-local-storage": "^1.0.1",
23-
"fastify-plugin": "^2.2.0"
23+
"fastify-plugin": "^2.3.0"
2424
},
2525
"peerDependencies": {
2626
"fastify": "^3.0.0"
@@ -31,7 +31,7 @@
3131
"eslint": "^7.6.0",
3232
"eslint-config-prettier": "^6.11.0",
3333
"eslint-plugin-prettier": "^3.1.4",
34-
"jest": "^26.2.2",
34+
"jest": "^26.4.0",
3535
"prettier": "^2.0.5",
3636
"tsd": "^0.13.1",
3737
"typescript": "3.9.7"

test/requestContextPlugin.spec.js

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fastify = require('fastify')
22
const { fastifyRequestContextPlugin } = require('../lib/requestContextPlugin')
33
const { TestService } = require('./internal/testService')
44

5-
function initApp(endpoint) {
5+
function initAppGet(endpoint) {
66
const app = fastify({ logger: true })
77
app.register(fastifyRequestContextPlugin)
88

@@ -11,13 +11,22 @@ function initApp(endpoint) {
1111
return app.ready()
1212
}
1313

14+
function initAppPost(endpoint) {
15+
const app = fastify({ logger: true })
16+
app.register(fastifyRequestContextPlugin)
17+
18+
app.post('/', endpoint)
19+
20+
return app.ready()
21+
}
22+
1423
describe('requestContextPlugin', () => {
1524
let app
1625
afterEach(() => {
1726
return app.close()
1827
})
1928

20-
it('correctly preserves values within single request', () => {
29+
it('correctly preserves values within single GET request', () => {
2130
expect.assertions(2)
2231

2332
let testService
@@ -50,7 +59,7 @@ describe('requestContextPlugin', () => {
5059
}
5160
}
5261

53-
initApp(route).then((_app) => {
62+
initAppGet(route).then((_app) => {
5463
app = _app
5564
testService = new TestService(app)
5665
const response1Promise = app
@@ -89,4 +98,77 @@ describe('requestContextPlugin', () => {
8998
return promiseRequest2
9099
})
91100
})
101+
102+
it('correctly preserves values within single POST request', () => {
103+
expect.assertions(2)
104+
105+
let testService
106+
let responseCounter = 0
107+
return new Promise((resolveResponsePromise) => {
108+
const promiseRequest2 = new Promise((resolveRequest2Promise) => {
109+
const promiseRequest1 = new Promise((resolveRequest1Promise) => {
110+
const route = (req, reply) => {
111+
function prepareReply() {
112+
testService.processRequest(requestId).then(() => {
113+
const storedValue = req.requestContext.get('testKey')
114+
reply.status(204).send({
115+
storedValue,
116+
})
117+
})
118+
}
119+
120+
const requestId = Number.parseInt(req.body.requestId)
121+
req.requestContext.set('testKey', `testValue${requestId}`)
122+
123+
// We don't want to read values until both requests wrote their values to see if there is a racing condition
124+
if (requestId === 1) {
125+
resolveRequest1Promise()
126+
return promiseRequest2.then(prepareReply)
127+
}
128+
129+
if (requestId === 2) {
130+
resolveRequest2Promise()
131+
return promiseRequest1.then(prepareReply)
132+
}
133+
}
134+
135+
initAppPost(route).then((_app) => {
136+
app = _app
137+
testService = new TestService(app)
138+
const response1Promise = app
139+
.inject()
140+
.post('/')
141+
.body({ requestId: 1 })
142+
.end()
143+
.then((response) => {
144+
expect(response.json().storedValue).toBe('testValue1')
145+
responseCounter++
146+
if (responseCounter === 2) {
147+
resolveResponsePromise()
148+
}
149+
})
150+
151+
const response2Promise = app
152+
.inject()
153+
.post('/')
154+
.body({ requestId: 2 })
155+
.end()
156+
.then((response) => {
157+
expect(response.json().storedValue).toBe('testValue2')
158+
responseCounter++
159+
if (responseCounter === 2) {
160+
resolveResponsePromise()
161+
}
162+
})
163+
164+
return Promise.all([response1Promise, response2Promise])
165+
})
166+
})
167+
168+
return promiseRequest1
169+
})
170+
171+
return promiseRequest2
172+
})
173+
})
92174
})

0 commit comments

Comments
 (0)