|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('tap') |
| 4 | +const Fastify = require('fastify') |
| 5 | +const fastifyWebsocket = require('..') |
| 6 | + |
| 7 | +function buildFastify (t) { |
| 8 | + const fastify = Fastify() |
| 9 | + t.teardown(() => { fastify.close() }) |
| 10 | + fastify.register(fastifyWebsocket) |
| 11 | + return fastify |
| 12 | +} |
| 13 | + |
| 14 | +test('routes correctly the message', async (t) => { |
| 15 | + const fastify = buildFastify(t) |
| 16 | + const message = 'hi from client' |
| 17 | + |
| 18 | + let _resolve |
| 19 | + const promise = new Promise((resolve) => { _resolve = resolve }) |
| 20 | + |
| 21 | + fastify.register( |
| 22 | + async function (instance) { |
| 23 | + instance.get('/ws', { websocket: true }, function (conn) { |
| 24 | + conn.once('data', chunk => { |
| 25 | + _resolve(chunk.toString()) |
| 26 | + }) |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + await fastify.ready() |
| 31 | + const ws = await fastify.injectWS('/ws') |
| 32 | + ws.send(message) |
| 33 | + t.same(await promise, message) |
| 34 | + ws.terminate() |
| 35 | +}) |
| 36 | + |
| 37 | +test('redirect on / if no path specified', async (t) => { |
| 38 | + const fastify = buildFastify(t) |
| 39 | + const message = 'hi from client' |
| 40 | + |
| 41 | + let _resolve |
| 42 | + const promise = new Promise((resolve) => { _resolve = resolve }) |
| 43 | + |
| 44 | + fastify.register( |
| 45 | + async function (instance) { |
| 46 | + instance.get('/', { websocket: true }, function (conn) { |
| 47 | + conn.once('data', chunk => { |
| 48 | + _resolve(chunk.toString()) |
| 49 | + }) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + await fastify.ready() |
| 54 | + const ws = await fastify.injectWS() |
| 55 | + ws.send(message) |
| 56 | + t.same(await promise, message) |
| 57 | + ws.terminate() |
| 58 | +}) |
| 59 | + |
| 60 | +test('routes correctly the message between two routes', async (t) => { |
| 61 | + const fastify = buildFastify(t) |
| 62 | + const message = 'hi from client' |
| 63 | + |
| 64 | + let _resolve |
| 65 | + let _reject |
| 66 | + const promise = new Promise((resolve, reject) => { _resolve = resolve; _reject = reject }) |
| 67 | + |
| 68 | + fastify.register( |
| 69 | + async function (instance) { |
| 70 | + instance.get('/ws', { websocket: true }, function (conn) { |
| 71 | + conn.once('data', () => { |
| 72 | + _reject('wrong-route') |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + instance.get('/ws-2', { websocket: true }, function (conn) { |
| 77 | + conn.once('data', chunk => { |
| 78 | + _resolve(chunk.toString()) |
| 79 | + }) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + await fastify.ready() |
| 84 | + const ws = await fastify.injectWS('/ws-2') |
| 85 | + ws.send(message) |
| 86 | + t.same(await promise, message) |
| 87 | + ws.terminate() |
| 88 | +}) |
| 89 | + |
| 90 | +test('use the upgrade context to upgrade if there is some hook', async (t) => { |
| 91 | + const fastify = buildFastify(t) |
| 92 | + const message = 'hi from client' |
| 93 | + |
| 94 | + let _resolve |
| 95 | + const promise = new Promise((resolve) => { _resolve = resolve }) |
| 96 | + |
| 97 | + fastify.register( |
| 98 | + async function (instance) { |
| 99 | + instance.addHook('preValidation', async (request, reply) => { |
| 100 | + if (request.headers['api-key'] !== 'some-random-key') { |
| 101 | + return reply.code(401).send() |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + instance.get('/', { websocket: true }, function (conn) { |
| 106 | + conn.once('data', chunk => { |
| 107 | + _resolve(chunk.toString()) |
| 108 | + }) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + await fastify.ready() |
| 113 | + const ws = await fastify.injectWS('/', { headers: { 'api-key': 'some-random-key' } }) |
| 114 | + ws.send(message) |
| 115 | + t.same(await promise, message) |
| 116 | + ws.terminate() |
| 117 | +}) |
| 118 | + |
| 119 | +test('rejects if the websocket is not upgraded', async (t) => { |
| 120 | + const fastify = buildFastify(t) |
| 121 | + |
| 122 | + fastify.register( |
| 123 | + async function (instance) { |
| 124 | + instance.addHook('preValidation', async (request, reply) => { |
| 125 | + return reply.code(401).send() |
| 126 | + }) |
| 127 | + |
| 128 | + instance.get('/', { websocket: true }, function (conn) { |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + await fastify.ready() |
| 133 | + t.rejects(fastify.injectWS('/'), 'Unexpected server response: 401') |
| 134 | +}) |
0 commit comments