diff --git a/plugin.js b/plugin.js index 3ce02fe..ccd8619 100644 --- a/plugin.js +++ b/plugin.js @@ -48,7 +48,7 @@ function etagOnSend (req, res, payload, next) { { id: etag, segment: this.cacheSegment }, true, res._etagLife, - next + (err) => next(err, payload) ) } diff --git a/test/cache.test.js b/test/cache.test.js index 24a9c6c..841636c 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -138,3 +138,36 @@ test('etag cache life is customizable', (t) => { .on('error', t.threw) }) }) + +test('returns response payload', (t) => { + t.plan(1) + const instance = fastify() + instance.register(plugin) + + instance.get('/one', (req, reply) => { + reply + .etag('123456', 300) + .send({ hello: 'world' }) + }) + + instance.listen(0, (err) => { + if (err) t.threw(err) + instance.server.unref() + const portNum = instance.server.address().port + const opts = { + host: '127.0.0.1', + port: portNum, + path: '/one' + } + http + .get(opts, (res) => { + let payload = '' + res.on('data', (chunk) => { + payload += chunk + }).on('end', () => { + t.same(JSON.parse(payload), { hello: 'world' }) + }).on('error', t.threw) + }) + .on('error', t.threw) + }) +})