diff --git a/index.js b/index.js index 085737d..32e3862 100644 --- a/index.js +++ b/index.js @@ -67,9 +67,9 @@ function fastifyMiddie (fastify, options, next) { req.raw.ip = req.ip req.raw.ips = req.ips req.raw.log = req.log - req.raw.body = req.body req.raw.query = req.query reply.raw.log = req.log + if (req.body !== undefined) req.raw.body = req.body this[kMiddie].run(req.raw, reply.raw, next) } else { next() diff --git a/test/enhance-request.test.js b/test/enhance-request.test.js index 91c85bb..8206d82 100644 --- a/test/enhance-request.test.js +++ b/test/enhance-request.test.js @@ -83,3 +83,30 @@ test('Should not enhance the Node.js core request/response objects when there ar ) }) }) + +test('If the enhanced response body is undefined, the body key should not exist', (t) => { + t.plan(3) + const fastify = Fastify() + t.teardown(fastify.close) + + fastify.register(middiePlugin).after(() => { + fastify.use(cors()) + fastify.use((req, res, next) => { + t.equal('body' in req, false) + next() + }) + }) + + fastify.listen({ port: 0 }, (err, address) => { + t.error(err) + sget( + { + method: 'POST', + url: `${address}?foo=bar` + }, + (err, res, data) => { + t.error(err) + } + ) + }) +})