Spaces:
Runtime error
Runtime error
File size: 3,484 Bytes
23ac194 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | '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 (socket) {
socket.once('message', 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 (socket) {
socket.once('message', 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 (socket) {
socket.once('message', () => {
_reject('wrong-route')
})
})
instance.get('/ws-2', { websocket: true }, function (socket) {
socket.once('message', 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 (socket) {
socket.once('message', 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 () {
})
})
await fastify.ready()
t.rejects(fastify.injectWS('/'), 'Unexpected server response: 401')
})
|