From 53811a7fd480bad09a424f34e3f4491ff0f3b4e3 Mon Sep 17 00:00:00 2001 From: Dylan Lamont Date: Sun, 28 Apr 2024 05:06:25 +1000 Subject: [PATCH] fix: only sets req.raw.body if body defined, aligning behaviour with node:http --- index.js | 2 +- test/enhance-request.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 085737d..0cddcf1 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) 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..3ea07c6 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 be defined', (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) + } + ) + }) +})