From 8c7508e5d4e664e4d06b3e258568eba73dc615ed Mon Sep 17 00:00:00 2001 From: Dylan Lamont <38599829+didley@users.noreply.github.com> Date: Thu, 2 May 2024 00:58:06 +1000 Subject: [PATCH] Only sets req.raw.body if body defined, aligning behaviour with node:http (#201) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: only sets req.raw.body if body defined, aligning behaviour with node:http * force ci --------- Co-authored-by: Gürgün Dayıoğlu --- 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..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) + } + ) + }) +})