Skip to content

Commit

Permalink
Only sets req.raw.body if body defined, aligning behaviour with node:…
Browse files Browse the repository at this point in the history
…http (#201)

* 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 <hey@gurgun.day>
  • Loading branch information
didley and gurgunday authored May 1, 2024
1 parent 64a8609 commit 8c7508e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions test/enhance-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
})
})

0 comments on commit 8c7508e

Please sign in to comment.