|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const gateway = require('../index') |
| 4 | +const WebSocket = require('faye-websocket') |
| 5 | +const http = require('http') |
| 6 | + |
| 7 | +/* global describe, it */ |
| 8 | +const expect = require('chai').expect |
| 9 | + |
| 10 | +describe('ws-proxy', () => { |
| 11 | + let gw, echoServer |
| 12 | + |
| 13 | + it('initialize', async () => { |
| 14 | + echoServer = http.createServer() |
| 15 | + echoServer.on('upgrade', (request, socket, body) => { |
| 16 | + if (WebSocket.isWebSocket(request)) { |
| 17 | + const ws = new WebSocket(request, socket, body) |
| 18 | + |
| 19 | + ws.on('message', (event) => { |
| 20 | + ws.send(JSON.stringify({ |
| 21 | + data: event.data, |
| 22 | + url: request.url |
| 23 | + })) |
| 24 | + }) |
| 25 | + } |
| 26 | + }) |
| 27 | + echoServer.listen(3000) |
| 28 | + |
| 29 | + gw = gateway({ |
| 30 | + routes: [{ |
| 31 | + proxyType: 'websocket', |
| 32 | + prefix: '/', |
| 33 | + target: 'ws://127.0.0.1:3000' |
| 34 | + }, { |
| 35 | + proxyType: 'websocket', |
| 36 | + prefix: '/echo', |
| 37 | + target: 'ws://127.0.0.1:3000' |
| 38 | + }, { |
| 39 | + proxyType: 'websocket', |
| 40 | + prefix: '/echo-auth', |
| 41 | + target: 'ws://127.0.0.1:3000', |
| 42 | + hooks: { |
| 43 | + onOpen (ws, searchParams) { |
| 44 | + if (searchParams.get('accessToken') !== '12345') { |
| 45 | + const err = new Error('Unauthorized') |
| 46 | + err.closeEventCode = 4401 |
| 47 | + |
| 48 | + throw err |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + }, { |
| 53 | + proxyType: 'websocket', |
| 54 | + prefix: '/echo-params', |
| 55 | + target: 'ws://127.0.0.1:3000', |
| 56 | + hooks: { |
| 57 | + onOpen (ws, searchParams) { |
| 58 | + searchParams.set('x-token', 'abc') |
| 59 | + } |
| 60 | + } |
| 61 | + }] |
| 62 | + }) |
| 63 | + |
| 64 | + await gw.start(8080) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should echo using default prefix', (done) => { |
| 68 | + const ws = new WebSocket.Client('ws://127.0.0.1:8080') |
| 69 | + const msg = 'hello' |
| 70 | + |
| 71 | + ws.on('message', (event) => { |
| 72 | + const { data } = JSON.parse(event.data) |
| 73 | + expect(data).equals('hello') |
| 74 | + |
| 75 | + ws.close() |
| 76 | + done() |
| 77 | + }) |
| 78 | + |
| 79 | + ws.send(msg) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should echo', (done) => { |
| 83 | + const ws = new WebSocket.Client('ws://127.0.0.1:8080/echo') |
| 84 | + const msg = 'hello' |
| 85 | + |
| 86 | + ws.on('message', (event) => { |
| 87 | + const { data } = JSON.parse(event.data) |
| 88 | + expect(data).equals('hello') |
| 89 | + |
| 90 | + ws.close() |
| 91 | + done() |
| 92 | + }) |
| 93 | + |
| 94 | + ws.send(msg) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should fail auth', (done) => { |
| 98 | + const ws = new WebSocket.Client('ws://127.0.0.1:8080/echo-auth?accessToken=2') |
| 99 | + ws.on('close', (event) => { |
| 100 | + done() |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should pass auth', (done) => { |
| 105 | + const ws = new WebSocket.Client('ws://127.0.0.1:8080/echo-auth?accessToken=12345') |
| 106 | + const msg = 'hello' |
| 107 | + |
| 108 | + ws.on('message', (event) => { |
| 109 | + const { data } = JSON.parse(event.data) |
| 110 | + expect(data).equals('hello') |
| 111 | + |
| 112 | + ws.close() |
| 113 | + done() |
| 114 | + }) |
| 115 | + |
| 116 | + ws.send(msg) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should rewrite search params', (done) => { |
| 120 | + const ws = new WebSocket.Client('ws://127.0.0.1:8080/echo-params') |
| 121 | + const msg = 'hello' |
| 122 | + |
| 123 | + ws.on('message', (event) => { |
| 124 | + const { url } = JSON.parse(event.data) |
| 125 | + expect(url).contains('?x-token=abc') |
| 126 | + |
| 127 | + ws.close() |
| 128 | + done() |
| 129 | + }) |
| 130 | + |
| 131 | + ws.send(msg) |
| 132 | + }) |
| 133 | + |
| 134 | + it('shutdown', async () => { |
| 135 | + await gw.close() |
| 136 | + echoServer.close() |
| 137 | + }) |
| 138 | +}) |
0 commit comments