Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"lint": "standard | snazzy",
"unit": "tap \"test/*.js\"",
"unit": "tap --100 \"test/*.js\"",
"test:js": "npm run lint && npm run unit",
"test:ts": "tsd",
"test": "npm run lint && npm run test:js && npm run test:ts"
Expand Down
55 changes: 55 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,58 @@ test('Should expose fastify instance to websocket per-route handler', t => {
})
})
})

test('should call `destroy` when exception is thrown inside async handler', t => {
t.plan(2)
const fastify = Fastify()

t.tearDown(() => fastify.close())

fastify.register(fastifyWebsocket)
fastify.get('/ws', { websocket: true }, async function wsHandler (conn, req) {
conn.on('error', err => {
t.equal(err.message, 'something wrong')
t.end()
})
throw new Error('something wrong')
})

fastify.listen(0, err => {
t.error(err)
const ws = new WebSocket(
'ws://localhost:' + (fastify.server.address()).port + '/ws'
)
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })

client.on('error', (_) => { })
t.tearDown(client.destroy.bind(client))
})
})

test('register a non websocket route', t => {
t.plan(2)
const fastify = Fastify()

t.tearDown(() => fastify.close())

fastify.register(fastifyWebsocket)
fastify.get('/ws', function handler (req, reply) {
reply.send({ hello: 'world' })
})

fastify.listen(0, err => {
t.error(err)
get('http://localhost:' + (fastify.server.address()).port + '/ws', function (response) {
let data = ''

response.on('data', (chunk) => {
data += chunk
})

response.on('end', () => {
t.equal(data, '{"hello":"world"}')
t.end()
})
})
})
})