Skip to content

Commit b00d176

Browse files
tiswrtSwizz
andauthored
adds test to PR #34 - next callback should use the response payload (#49)
* next callback should use the response payload * add test Co-authored-by: Swizz <[email protected]>
1 parent 69cb12b commit b00d176

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function etagOnSend (req, res, payload, next) {
4848
{ id: etag, segment: this.cacheSegment },
4949
true,
5050
res._etagLife,
51-
next
51+
(err) => next(err, payload)
5252
)
5353
}
5454

test/cache.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,36 @@ test('etag cache life is customizable', (t) => {
138138
.on('error', t.threw)
139139
})
140140
})
141+
142+
test('returns response payload', (t) => {
143+
t.plan(1)
144+
const instance = fastify()
145+
instance.register(plugin)
146+
147+
instance.get('/one', (req, reply) => {
148+
reply
149+
.etag('123456', 300)
150+
.send({ hello: 'world' })
151+
})
152+
153+
instance.listen(0, (err) => {
154+
if (err) t.threw(err)
155+
instance.server.unref()
156+
const portNum = instance.server.address().port
157+
const opts = {
158+
host: '127.0.0.1',
159+
port: portNum,
160+
path: '/one'
161+
}
162+
http
163+
.get(opts, (res) => {
164+
let payload = ''
165+
res.on('data', (chunk) => {
166+
payload += chunk
167+
}).on('end', () => {
168+
t.same(JSON.parse(payload), { hello: 'world' })
169+
}).on('error', t.threw)
170+
})
171+
.on('error', t.threw)
172+
})
173+
})

0 commit comments

Comments
 (0)