From e0ca9c440c3d8e160132f75dca748edec6418830 Mon Sep 17 00:00:00 2001 From: Krishanu Singh Date: Sun, 5 Sep 2021 03:27:43 +0530 Subject: [PATCH] fixed unresponsive server #3283 (#3285) --- lib/reply.js | 2 +- test/reply-error.test.js | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/lib/reply.js b/lib/reply.js index 935e80ee76..29a2be4a3c 100644 --- a/lib/reply.js +++ b/lib/reply.js @@ -377,8 +377,8 @@ function preserializeHookEnd (err, request, reply, payload) { } function onSendHook (reply, payload) { - reply[kReplySent] = true if (reply.context.onSend !== null) { + reply[kReplySent] = true onSendHookRunner( reply.context.onSend, reply.request, diff --git a/test/reply-error.test.js b/test/reply-error.test.js index fa4ec8fb0a..7af3224b23 100644 --- a/test/reply-error.test.js +++ b/test/reply-error.test.js @@ -525,3 +525,74 @@ test('should trigger error handlers if a sync route throws any non-error object' t.equal(reply.statusCode, 500) t.equal(JSON.parse(reply.body).foo, 'bar') }) + +test('setting content-type on reply object should not hang the server case 1', t => { + t.plan(2) + const fastify = Fastify() + + fastify.get('/', (req, reply) => { + reply + .code(200) + .headers({ 'content-type': 'text/plain; charset=utf-32' }) + .send(JSON.stringify({ bar: 'foo', baz: 'foobar' })) + }) + + fastify.inject({ + url: '/', + method: 'GET' + }, (err, res) => { + t.error(err) + t.equal(res.statusCode, 200) + }) +}) + +test('setting content-type on reply object should not hang the server case 2', async t => { + t.plan(1) + const fastify = Fastify() + + fastify.get('/', (req, reply) => { + reply + .code(200) + .headers({ 'content-type': 'text/plain; charset=utf-8' }) + .send({ bar: 'foo', baz: 'foobar' }) + }) + + try { + await fastify.listen(0) + const res = await fastify.inject({ + url: '/', + method: 'GET' + }) + t.same({ + error: 'Internal Server Error', + message: 'Attempted to send payload of invalid type \'object\'. Expected a string or Buffer.', + statusCode: 500, + code: 'FST_ERR_REP_INVALID_PAYLOAD_TYPE' + }, + res.json()) + } catch (error) { + t.error(error) + } finally { + await fastify.close() + } +}) + +test('setting content-type on reply object should not hang the server case 3', t => { + t.plan(2) + const fastify = Fastify() + + fastify.get('/', (req, reply) => { + reply + .code(200) + .headers({ 'content-type': 'application/json' }) + .send({ bar: 'foo', baz: 'foobar' }) + }) + + fastify.inject({ + url: '/', + method: 'GET' + }, (err, res) => { + t.error(err) + t.equal(res.statusCode, 200) + }) +})