-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add #injectWS Now is possible to invoke an websocket handler without listening * Up-to-date documentation Added testing paragraph * Removed unused dependency * remove ws.terminate() in the plugin The user must manually close the ws in the test * add upgradeContext parameter in injectWS It allows to enhance the request made for upgrading the socket * Rejects if websocket upgrade failed * remove useless line in docs * rejects with 'Unexpected server response: <statusCode>' Implementation as close as possibile to ws connectiong error * Fix test * Fix types --------- Signed-off-by: Daniele Fedeli <[email protected]> Co-authored-by: Daniele Fedeli <[email protected]>
- Loading branch information
1 parent
a5ef710
commit cb3ce0d
Showing
5 changed files
with
270 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const Fastify = require('fastify') | ||
const fastifyWebsocket = require('..') | ||
|
||
function buildFastify (t) { | ||
const fastify = Fastify() | ||
t.teardown(() => { fastify.close() }) | ||
fastify.register(fastifyWebsocket) | ||
return fastify | ||
} | ||
|
||
test('routes correctly the message', async (t) => { | ||
const fastify = buildFastify(t) | ||
const message = 'hi from client' | ||
|
||
let _resolve | ||
const promise = new Promise((resolve) => { _resolve = resolve }) | ||
|
||
fastify.register( | ||
async function (instance) { | ||
instance.get('/ws', { websocket: true }, function (conn) { | ||
conn.once('data', chunk => { | ||
_resolve(chunk.toString()) | ||
}) | ||
}) | ||
}) | ||
|
||
await fastify.ready() | ||
const ws = await fastify.injectWS('/ws') | ||
ws.send(message) | ||
t.same(await promise, message) | ||
ws.terminate() | ||
}) | ||
|
||
test('redirect on / if no path specified', async (t) => { | ||
const fastify = buildFastify(t) | ||
const message = 'hi from client' | ||
|
||
let _resolve | ||
const promise = new Promise((resolve) => { _resolve = resolve }) | ||
|
||
fastify.register( | ||
async function (instance) { | ||
instance.get('/', { websocket: true }, function (conn) { | ||
conn.once('data', chunk => { | ||
_resolve(chunk.toString()) | ||
}) | ||
}) | ||
}) | ||
|
||
await fastify.ready() | ||
const ws = await fastify.injectWS() | ||
ws.send(message) | ||
t.same(await promise, message) | ||
ws.terminate() | ||
}) | ||
|
||
test('routes correctly the message between two routes', async (t) => { | ||
const fastify = buildFastify(t) | ||
const message = 'hi from client' | ||
|
||
let _resolve | ||
let _reject | ||
const promise = new Promise((resolve, reject) => { _resolve = resolve; _reject = reject }) | ||
|
||
fastify.register( | ||
async function (instance) { | ||
instance.get('/ws', { websocket: true }, function (conn) { | ||
conn.once('data', () => { | ||
_reject('wrong-route') | ||
}) | ||
}) | ||
|
||
instance.get('/ws-2', { websocket: true }, function (conn) { | ||
conn.once('data', chunk => { | ||
_resolve(chunk.toString()) | ||
}) | ||
}) | ||
}) | ||
|
||
await fastify.ready() | ||
const ws = await fastify.injectWS('/ws-2') | ||
ws.send(message) | ||
t.same(await promise, message) | ||
ws.terminate() | ||
}) | ||
|
||
test('use the upgrade context to upgrade if there is some hook', async (t) => { | ||
const fastify = buildFastify(t) | ||
const message = 'hi from client' | ||
|
||
let _resolve | ||
const promise = new Promise((resolve) => { _resolve = resolve }) | ||
|
||
fastify.register( | ||
async function (instance) { | ||
instance.addHook('preValidation', async (request, reply) => { | ||
if (request.headers['api-key'] !== 'some-random-key') { | ||
return reply.code(401).send() | ||
} | ||
}) | ||
|
||
instance.get('/', { websocket: true }, function (conn) { | ||
conn.once('data', chunk => { | ||
_resolve(chunk.toString()) | ||
}) | ||
}) | ||
}) | ||
|
||
await fastify.ready() | ||
const ws = await fastify.injectWS('/', { headers: { 'api-key': 'some-random-key' } }) | ||
ws.send(message) | ||
t.same(await promise, message) | ||
ws.terminate() | ||
}) | ||
|
||
test('rejects if the websocket is not upgraded', async (t) => { | ||
const fastify = buildFastify(t) | ||
|
||
fastify.register( | ||
async function (instance) { | ||
instance.addHook('preValidation', async (request, reply) => { | ||
return reply.code(401).send() | ||
}) | ||
|
||
instance.get('/', { websocket: true }, function (conn) { | ||
}) | ||
}) | ||
|
||
await fastify.ready() | ||
t.rejects(fastify.injectWS('/'), 'Unexpected server response: 401') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters