Skip to content

Commit 24933e0

Browse files
committed
adding websocket tests
1 parent 8596c79 commit 24933e0

File tree

3 files changed

+147
-6
lines changed

3 files changed

+147
-6
lines changed

lib/default-hooks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const toArray = require('stream-to-array')
55
const TRANSFER_ENCODING_HEADER_NAME = 'transfer-encoding'
66

77
module.exports = {
8+
websocket: {
9+
onOpenNoOp (ws, searchParams) { }
10+
},
811
lambda: {
912
onRequestNoOp (req, res) { },
1013
onResponse (req, res, response) {

lib/ws-proxy.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict'
22

33
const WebSocket = require('faye-websocket')
4-
5-
function onOpenNoOp () {}
4+
const { onOpenNoOp } = require('./default-hooks').websocket
65

76
module.exports = (config) => {
87
const { routes, server } = config
98

10-
const prefix2route = {}
9+
const prefix2route = new Map()
1110
for (const route of routes) {
12-
prefix2route[route.prefix] = route
11+
prefix2route.set(route.prefix, route)
1312
}
1413

1514
server.on('upgrade', async (req, socket, body) => {
1615
if (WebSocket.isWebSocket(req)) {
1716
const url = new URL('http://fw' + req.url)
18-
const route = prefix2route[url.pathname]
17+
const prefix = url.pathname || '/'
1918

20-
if (route) {
19+
if (prefix2route.has(prefix)) {
20+
const route = prefix2route.get(prefix)
2121
const subProtocols = route.subProtocols || []
2222
route.hooks = route.hooks || {}
2323
const onOpen = route.hooks.onOpen || onOpenNoOp

test/ws-proxy.test.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

Comments
 (0)