Skip to content

Commit

Permalink
fixed unresponsive server #3283 (#3285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishanu230 committed Sep 4, 2021
1 parent aa9a44d commit e0ca9c4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/reply.js
Expand Up @@ -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,
Expand Down
71 changes: 71 additions & 0 deletions test/reply-error.test.js
Expand Up @@ -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)
})
})

0 comments on commit e0ca9c4

Please sign in to comment.