-
Notifications
You must be signed in to change notification settings - Fork 821
/
websocket.spec.ts
126 lines (107 loc) · 3.89 KB
/
websocket.spec.ts
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
import * as http from 'http';
import * as WebSocket from 'ws';
import { Server as WebSocketServer } from 'ws';
import * as getPort from 'get-port';
import { createProxyMiddleware, createApp } from './test-kit';
import type { RequestHandler } from '../../src/types';
/********************************************************************
* - Not possible to use `supertest` to test WebSockets
* - Make sure to use different port for each test to avoid flakiness
********************************************************************/
describe('E2E WebSocket proxy', () => {
let proxyServer: http.Server;
let ws: WebSocket;
let wss: WebSocketServer;
let proxyMiddleware: RequestHandler;
let WS_SERVER_PORT: number;
let SERVER_PORT: number;
beforeEach(async () => {
WS_SERVER_PORT = await getPort();
SERVER_PORT = await getPort();
wss = new WebSocketServer({ port: WS_SERVER_PORT });
wss.on('connection', (websocket) => {
websocket.on('message', (data, isBinary) => {
const message = isBinary ? data : data.toString();
websocket.send(message); // echo received message
});
});
});
beforeEach(() => {
proxyMiddleware = createProxyMiddleware('/', {
target: `http://localhost:${WS_SERVER_PORT}`,
ws: true,
pathRewrite: { '^/socket': '' },
});
});
afterEach(async () => {
return Promise.all([
new Promise((resolve) => proxyServer.close(resolve)),
new Promise((resolve) => wss.close(resolve)),
new Promise((resolve) => resolve(ws.close())),
]);
});
describe('option.ws', () => {
beforeEach(async () => {
proxyServer = createApp(proxyMiddleware).listen(SERVER_PORT);
// quick & dirty Promise version of http.get (don't care about correctness)
const get = async (uri) => new Promise((resolve, reject) => http.get(uri, resolve));
// need to make a normal http request, so http-proxy-middleware can catch the upgrade request
await get(`http://localhost:${SERVER_PORT}/`);
// do a second http request to make sure only 1 listener subscribes to upgrade request
await get(`http://localhost:${SERVER_PORT}/`);
return new Promise((resolve) => {
ws = new WebSocket(`ws://localhost:${SERVER_PORT}/socket`);
ws.on('open', resolve);
});
});
it('should proxy to path', (done) => {
ws.on('message', (data, isBinary) => {
const message = isBinary ? data : data.toString();
expect(message).toBe('foobar');
done();
});
ws.send('foobar');
});
});
describe('option.ws with external server "upgrade"', () => {
beforeEach((done) => {
proxyServer = createApp(proxyMiddleware).listen(SERVER_PORT);
proxyServer.on('upgrade', proxyMiddleware.upgrade);
ws = new WebSocket(`ws://localhost:${SERVER_PORT}/socket`);
ws.on('open', done);
});
it('should proxy to path', (done) => {
ws.on('message', (data, isBinary) => {
const message = isBinary ? data : data.toString();
expect(message).toBe('foobar');
done();
});
ws.send('foobar');
});
});
describe('with router and pathRewrite', () => {
beforeEach(() => {
// override
proxyServer = createApp(
// cSpell:ignore notworkinghost
createProxyMiddleware('ws://notworkinghost:6789', {
router: { '/socket': `ws://localhost:${WS_SERVER_PORT}` },
pathRewrite: { '^/socket': '' },
})
).listen(SERVER_PORT);
proxyServer.on('upgrade', proxyMiddleware.upgrade);
});
beforeEach((done) => {
ws = new WebSocket(`ws://localhost:${SERVER_PORT}/socket`);
ws.on('open', done);
});
it('should proxy to path', (done) => {
ws.on('message', (data, isBinary) => {
const message = isBinary ? data : data.toString();
expect(message).toBe('foobar');
done();
});
ws.send('foobar');
});
});
});