/
websocket.spec.ts
153 lines (126 loc) · 3.85 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import http from 'http';
import WebSocket from 'ws';
// tslint:disable-next-line: no-duplicate-imports
import { Server as WebSocketServer } from 'ws';
import { createServer, proxyMiddleware } from './_utils';
describe('E2E WebSocket proxy', () => {
let proxyServer;
let ws;
let wss;
let responseMessage;
let proxy;
beforeEach(() => {
proxy = proxyMiddleware('/', {
target: 'http://localhost:8000',
ws: true,
pathRewrite: { '^/socket': '' }
});
proxyServer = createServer(3000, proxy);
// @ts-ignore: Expected arguments error
wss = new WebSocketServer({ port: 8000 });
wss.on('connection', function connection(websocket) {
websocket.on('message', function incoming(message) {
websocket.send(message); // echo received message
});
});
});
describe('option.ws', () => {
beforeEach(done => {
// need to make a normal http request,
// so http-proxy-middleware can catch the upgrade request
http.get('http://localhost:3000/', () => {
// do a second http request to make
// sure only 1 listener subscribes to upgrade request
http.get('http://localhost:3000/', () => {
// @ts-ignore: Expected arguments error
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
});
});
});
});
it('should proxy to path', () => {
expect(responseMessage).toBe('foobar');
});
});
describe('option.ws with external server "upgrade"', () => {
beforeEach(done => {
proxyServer.on('upgrade', proxy.upgrade);
// @ts-ignore: Expected arguments error
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
});
});
it('should proxy to path', () => {
expect(responseMessage).toBe('foobar');
});
});
describe('option.ws with external server "upgrade" and shorthand usage', () => {
beforeEach(() => {
proxyServer.close();
// override
proxy = proxyMiddleware('ws://localhost:8000', {
pathRewrite: { '^/socket': '' }
});
proxyServer = createServer(3000, proxy);
});
beforeEach(done => {
proxyServer.on('upgrade', proxy.upgrade);
// @ts-ignore: Expected arguments error
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
});
});
it('should proxy to path', () => {
expect(responseMessage).toBe('foobar');
});
});
describe('with router and pathRewrite', () => {
beforeEach(() => {
proxyServer.close();
// override
proxy = proxyMiddleware('ws://notworkinghost:6789', {
router: { '/socket': 'ws://localhost:8000' },
pathRewrite: { '^/socket': '' }
});
proxyServer = createServer(3000, proxy);
});
beforeEach(done => {
proxyServer.on('upgrade', proxy.upgrade);
// @ts-ignore: Expected arguments error
ws = new WebSocket('ws://localhost:3000/socket');
ws.on('message', function incoming(message) {
responseMessage = message;
done();
});
ws.on('open', function open() {
ws.send('foobar');
});
});
it('should proxy to path', () => {
expect(responseMessage).toBe('foobar');
});
});
afterEach(async () => {
return new Promise(resolve => {
proxyServer.close(resolve);
wss.close();
ws = null;
});
});
});