From 4ccea8668705b7074853d87ac436583bfd838363 Mon Sep 17 00:00:00 2001 From: Dylan Lamont Date: Sun, 28 Apr 2024 05:06:25 +1000 Subject: [PATCH 1/2] 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..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..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) + } + ) + }) +}) From d0c4573fffa278163e4399d938b4170d3baf0f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 1 May 2024 10:47:50 +0200 Subject: [PATCH 2/2] force ci --- test/enhance-request.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/enhance-request.test.js b/test/enhance-request.test.js index 3ea07c6..8206d82 100644 --- a/test/enhance-request.test.js +++ b/test/enhance-request.test.js @@ -84,7 +84,7 @@ 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) => { +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)