Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed unresponsive server #3283 #3285

Merged
merged 4 commits into from Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
climba03003 marked this conversation as resolved.
Show resolved Hide resolved
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)
})
})