forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websockets_spec.js
329 lines (259 loc) · 9.02 KB
/
websockets_spec.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
require('../spec_helper')
const ws = require('ws')
const httpsProxyAgent = require('https-proxy-agent')
const evilDns = require('evil-dns')
const Promise = require('bluebird')
const socketIo = require(`@packages/socket/lib/browser`)
const httpsServer = require(`@packages/https-proxy/test/helpers/https_server`)
const config = require(`../../lib/config`)
const { ServerBase } = require(`../../lib/server-base`)
const { SocketE2E } = require(`../../lib/socket-e2e`)
const { Automation } = require(`../../lib/automation`)
const Fixtures = require('@tooling/system-tests')
const { createRoutes } = require(`../../lib/routes`)
const { getCtx } = require(`../../lib/makeDataContext`)
const cyPort = 12345
const otherPort = 55551
const wsPort = 20000
const wssPort = 8443
let ctx
describe('Web Sockets', () => {
require('mocha-banner').register()
beforeEach(async function () {
ctx = getCtx()
Fixtures.scaffold()
this.idsPath = Fixtures.projectPath('ids')
await ctx.actions.project.setCurrentProjectAndTestingTypeForTestSetup(this.idsPath)
return ctx.lifecycleManager.getFullInitialConfig({ port: cyPort })
.then((cfg) => {
this.cfg = cfg
this.ws = new ws.Server({ port: wsPort })
this.server = new ServerBase()
return this.server.open(this.cfg, {
SocketCtor: SocketE2E,
createRoutes,
testingType: 'e2e',
getCurrentBrowser: () => null,
})
.then(async () => {
const automationStub = {
use: () => { },
}
await this.server.startWebsockets(automationStub, config, {})
return httpsServer.start(wssPort)
}).then((httpsSrv) => {
this.wss = new ws.Server({ server: httpsSrv })
})
})
})
afterEach(function () {
Fixtures.remove()
evilDns.clear()
this.ws.close()
this.wss.close()
return Promise.join(
this.server.close(),
httpsServer.stop(),
)
})
context('proxying external websocket requests', () => {
it('sends back ECONNRESET when error upgrading', function (done) {
const agent = new httpsProxyAgent(`http://localhost:${cyPort}`)
this.server.remoteStates.set(`http://localhost:${otherPort}`)
const client = new ws(`ws://localhost:${otherPort}`, {
agent,
})
return client.on('error', (err) => {
expect(err.code).to.eq('ECONNRESET')
expect(err.message).to.eq('socket hang up')
return done()
})
})
it('proxies https messages', function (done) {
const agent = new httpsProxyAgent(`http://localhost:${cyPort}`, {
})
this.wss.on('connection', (c) => {
return c.on('message', (msg) => {
return c.send(`response:${msg}`)
})
})
const client = new ws(`wss://localhost:${wssPort}`, {
rejectUnauthorized: false,
agent,
})
client.on('message', (data) => {
expect(data).to.eq('response:foo')
return done()
})
return client.on('open', () => {
return client.send('foo')
})
})
it('proxies http messages through http proxy', function (done) {
// force node into legit proxy mode like a browser
const agent = new httpsProxyAgent(`http://localhost:${cyPort}`)
this.server.remoteStates.set(`http://localhost:${wsPort}`)
this.ws.on('connection', (c) => {
return c.on('message', (msg) => {
return c.send(`response:${msg}`)
})
})
const client = new ws(`ws://localhost:${wsPort}`, {
agent,
})
client.on('message', (data) => {
expect(data).to.eq('response:foo')
return done()
})
return client.on('open', () => {
return client.send('foo')
})
})
it('proxies https messages through http', function (done) {
// force node into legit proxy mode like a browser
const agent = new httpsProxyAgent({
host: 'localhost',
port: cyPort,
rejectUnauthorized: false,
})
this.server.remoteStates.set(`https://localhost:${wssPort}`)
this.wss.on('connection', (c) => {
return c.on('message', (msg) => {
return c.send(`response:${msg}`)
})
})
const client = new ws(`wss://localhost:${wssPort}`, {
agent,
})
client.on('message', (data) => {
expect(data).to.eq('response:foo')
return done()
})
return client.on('open', () => {
return client.send('foo')
})
})
it('proxies through subdomain by using host header', function (done) {
// we specifically only allow remote connections
// to ws.foobar.com since that is where the websocket
// server is mounted and this tests that we make
// a connection to the right host instead of the
// origin (which isnt ws.foobar.com)
nock.enableNetConnect('ws.foobar.com')
evilDns.add('ws.foobar.com', '127.0.0.1')
// force node into legit proxy mode like a browser
const agent = new httpsProxyAgent({
host: 'localhost',
port: cyPort,
rejectUnauthorized: false,
})
this.server.remoteStates.set(`https://foobar.com:${wssPort}`)
this.wss.on('connection', (c) => {
return c.on('message', (msg) => {
return c.send(`response:${msg}`)
})
})
const client = new ws(`wss://ws.foobar.com:${wssPort}`, {
agent,
})
client.on('message', (data) => {
expect(data).to.eq('response:foo')
return done()
})
return client.on('open', () => {
return client.send('foo')
})
})
})
context('socket.io handling', () => {
beforeEach(function () {
this.automation = new Automation({
cyNamespace: this.cfg.namespace,
cookieNamespace: this.cfg.socketIoCookie,
screenshotsFolder: this.cfg.screenshotsFolder,
})
return this.server.startWebsockets(this.automation, this.cfg, {})
})
const testSocketIo = function (wsUrl, beforeFn) {
context('behind Cy proxy', () => {
beforeEach(function (done) {
// force node into legit proxy mode like a browser
const agent = new httpsProxyAgent(`http://localhost:${cyPort}`)
if (beforeFn != null) {
beforeFn.call(this)
}
this.wsClient = socketIo.client(wsUrl || this.cfg.proxyUrl, {
agent,
path: this.cfg.socketIoRoute,
transports: ['websocket'],
rejectUnauthorized: false,
})
return this.wsClient.on('connect', () => {
return done()
})
})
afterEach(function () {
return this.wsClient.disconnect()
})
it('continues to handle socket.io requests just fine', function (done) {
return this.wsClient.emit('backend:request', 'get:fixture', 'example.json', {}, (data) => {
expect(data.response).to.deep.eq({ foo: 'bar' })
return done()
})
})
})
context('without Cy proxy', () => {
beforeEach(function () {
return (beforeFn != null ? beforeFn.call(this) : undefined)
})
afterEach(function () {
return this.wsClient.disconnect()
})
it('fails to connect via websocket', function (done) {
this.wsClient = socketIo.client(wsUrl || this.cfg.proxyUrl, {
path: this.cfg.socketIoRoute,
transports: ['websocket'],
rejectUnauthorized: false,
reconnection: false,
})
this.wsClient.on('connect', () => {
return done(new Error('should not have been able to connect'))
})
return this.wsClient.io.on('error', () => {
return done()
})
})
// TODO: this test will currently fail because we allow polling in development mode
// for webkit support. Restore this test before WebKit is available in production.
// it('fails to connect via polling', function (done) {
// this.wsClient = socketIo.client(wsUrl || this.cfg.proxyUrl, {
// path: this.cfg.socketIoRoute,
// transports: ['polling'],
// rejectUnauthorized: false,
// reconnection: false,
// })
// this.wsClient.on('connect', () => {
// return done(new Error('should not have been able to connect'))
// })
// return this.wsClient.io.on('error', () => {
// return done()
// })
// })
})
}
context('http', () => {
return testSocketIo()
})
context('when http superDomain has been set', () => {
return testSocketIo(`http://localhost:${otherPort}`, function () {
return this.server.remoteStates.set(`http://localhost:${otherPort}`)
})
})
context('when https superDomain has been set', () => {
return testSocketIo(`http://localhost:${wssPort}`, function () {
return this.server.remoteStates.set(`http://localhost:${wssPort}`)
})
})
})
})