forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_spec.js
More file actions
400 lines (320 loc) · 11 KB
/
server_spec.js
File metadata and controls
400 lines (320 loc) · 11 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
require('../spec_helper')
const _ = require('lodash')
const os = require('os')
const express = require('express')
const Promise = require('bluebird')
const { connect } = require('@packages/network')
const { setupFullConfigWithDefaults } = require('@packages/config')
const { ServerBase } = require(`../../lib/server-base`)
const { SocketE2E } = require(`../../lib/socket-e2e`)
const fileServer = require(`../../lib/file_server`)
const ensureUrl = require(`../../lib/util/ensure-url`)
const { getCtx } = require('@packages/data-context')
const morganFn = function () {}
mockery.registerMock('morgan', () => {
return morganFn
})
describe('lib/server', () => {
beforeEach(function () {
this.server = new ServerBase()
return setupFullConfigWithDefaults({ projectRoot: '/foo/bar/', config: { supportFile: false } }, getCtx().file.getFilesByGlob)
.then((cfg) => {
this.config = cfg
})
})
context('#close', () => {
it('resolves true successfully bailing out early', function () {
return this.server.close().then((res) => {
expect(res[0]).to.be.true
})
})
})
})
// TODO: Figure out correct configuration to run these tests and/or which ones we need to keep.
// The introduction of server-base/socket-base and the `ensureProp` function made unit testing
// the server difficult.
describe.skip('lib/server', () => {
beforeEach(function () {
this.fileServer = {
close () {},
port () {
return 1111
},
}
sinon.stub(fileServer, 'create').returns(this.fileServer)
return setupFullConfigWithDefaults({ projectRoot: '/foo/bar/' }, getCtx().file.getFilesByGlob)
.then((cfg) => {
this.config = cfg
this.server = new ServerBase()
this.oldFileServer = this.server._fileServer
this.server._fileServer = this.fileServer
})
})
afterEach(function () {
return this.server && this.server.close()
})
context('#createExpressApp', () => {
beforeEach(function () {
this.use = sinon.spy(express.application, 'use')
})
it('instantiates express instance without morgan', function () {
const app = this.server.createExpressApp({ morgan: false })
expect(app.get('view engine')).to.eq('html')
expect(this.use).not.to.be.calledWith(morganFn)
})
it('requires morgan if true', function () {
this.server.createExpressApp({ morgan: true })
expect(this.use).to.be.calledWith(morganFn)
})
})
context('#open', () => {
beforeEach(function () {
return sinon.stub(this.server, 'createServer').resolves()
})
it('calls #createExpressApp with morgan', function () {
sinon.spy(this.server, 'createExpressApp')
_.extend(this.config, { port: 54321, morgan: false })
return this.server.open(this.config)
.then(() => {
expect(this.server.createExpressApp).to.be.calledWithMatch({ morgan: false })
})
})
it('calls #createServer with port', function () {
_.extend(this.config, { port: 54321 })
const obj = {}
sinon.stub(this.server, 'createRoutes')
sinon.stub(this.server, 'createExpressApp').returns(obj)
return this.server.open(this.config)
.then(() => {
expect(this.server.createServer).to.be.calledWith(obj, this.config)
})
})
it('calls #createRoutes with app + config', function () {
const app = {}
const project = {}
const onError = sinon.spy()
sinon.stub(this.server, 'createRoutes')
sinon.stub(this.server, 'createExpressApp').returns(app)
return this.server.open(this.config, project, onError)
.then(() => {
expect(this.server.createRoutes).to.be.called
expect(this.server.createRoutes.lastCall.args[0].app).to.equal(app)
expect(this.server.createRoutes.lastCall.args[0].config).to.equal(this.config)
expect(this.server.createRoutes.lastCall.args[0].project).to.equal(project)
expect(this.server.createRoutes.lastCall.args[0].onError).to.equal(onError)
})
})
it('calls #createServer with port + fileServerFolder + socketIoRoute + app', function () {
const obj = {}
sinon.stub(this.server, 'createRoutes')
sinon.stub(this.server, 'createExpressApp').returns(obj)
return this.server.open(this.config)
.then(() => {
expect(this.server.createServer).to.be.calledWith(obj, this.config)
})
})
})
context('#createServer', () => {
beforeEach(function () {
this.port = 54321
this.app = this.server.createExpressApp({ morgan: true })
})
it('isListening=true', function () {
return this.server.createServer(this.app, { port: this.port })
.then(() => {
expect(this.server.isListening).to.be.true
})
})
it('resolves with http server port', function () {
return this.server.createServer(this.app, { port: this.port })
.spread((port) => {
expect(port).to.eq(this.port)
})
})
it('all servers listen only on localhost and no other interface', function () {
fileServer.create.restore()
this.server._fileServer = this.oldFileServer
const interfaces = _.flatten(_.values(os.networkInterfaces()))
const nonLoopback = interfaces.find((iface) => {
return (iface.family === 'IPv4') && (iface.address !== '127.0.0.1')
})
// verify that we can connect to `port` over loopback
// and not over another configured IPv4 address
const tryOnlyLoopbackConnect = (port) => {
return Promise.all([
connect.byPortAndAddress(port, '127.0.0.1'),
connect.byPortAndAddress(port, nonLoopback)
.then(() => {
throw new Error(`Shouldn't be able to connect on ${nonLoopback.address}:${port}`)
}).catch({ errno: 'ECONNREFUSED' }, () => {}),
])
}
return this.server.createServer(this.app, {})
.spread((port) => {
return Promise.map(
[
port,
this.server._fileServer.port(),
this.server._httpsProxy._sniPort,
],
tryOnlyLoopbackConnect,
)
})
})
it('resolves with warning if cannot connect to baseUrl', function () {
sinon.stub(ensureUrl, 'isListening').rejects()
return this.server.createServer(this.app, { port: this.port, baseUrl: `http://localhost:${this.port}` })
.spread((port, warning) => {
expect(warning.type).to.eq('CANNOT_CONNECT_BASE_URL_WARNING')
expect(warning.message).to.include(this.port)
})
})
context('errors', () => {
it('rejects with portInUse', function () {
return this.server.createServer(this.app, { port: this.port })
.then(() => {
return this.server.createServer(this.app, { port: this.port })
}).then(() => {
throw new Error('should have failed but didn\'t')
}).catch((err) => {
expect(err.type).to.eq('PORT_IN_USE_SHORT')
expect(err.message).to.include(this.port)
})
})
})
})
context('#end', () => {
it('calls this._socket.end', function () {
const socket = sinon.stub({
end () {},
close () {},
})
this.server._socket = socket
this.server.end()
expect(socket.end).to.be.called
})
it('is noop without this._socket', function () {
return this.server.end()
})
})
context('#startWebsockets', () => {
beforeEach(function () {
this.startListening = sinon.stub(SocketE2E.prototype, 'startListening')
})
it('sets _socket and calls _socket#startListening', function () {
return this.server.open(this.config)
.then(() => {
const arg2 = {}
this.server.startWebsockets(1, 2, arg2)
expect(this.startListening).to.be.calledWith(this.server.getHttpServer(), 1, 2, arg2)
})
})
})
context('#reset', () => {
beforeEach(function () {
return this.server.open(this.config)
.then(() => {
this.buffers = this.server._networkProxy.http
return sinon.stub(this.buffers, 'reset')
})
})
it('resets the buffers', function () {
this.server.reset()
expect(this.buffers.reset).to.be.called
})
it('sets the domain to the previous base url if set', function () {
this.server._baseUrl = 'http://localhost:3000'
this.server.reset()
expect(this.server._remoteStrategy).to.equal('http')
})
it('sets the domain to <root> if not set', function () {
this.server.reset()
expect(this.server._remoteStrategy).to.equal('file')
})
})
context('#close', () => {
it('returns a promise', function () {
expect(this.server.close()).to.be.instanceof(Promise)
})
it('calls close on this.server', function () {
return this.server.open(this.config)
.then(() => {
return this.server.close()
})
})
it('isListening=false', function () {
return this.server.open(this.config)
.then(() => {
return this.server.close()
}).then(() => {
expect(this.server.isListening).to.be.false
})
})
it('calls close on this._socket', function () {
this.server._socket = { close: sinon.spy() }
return this.server.close()
.then(() => {
expect(this.server._socket.close).to.be.calledOnce
})
})
})
context('#proxyWebsockets', () => {
beforeEach(function () {
this.proxy = sinon.stub({
ws () {},
on () {},
})
this.socket = sinon.stub({ end () {} })
this.head = {}
})
it('is noop if req.url startsWith socketIoRoute', function () {
const socket = {
remotePort: 12345,
remoteAddress: '127.0.0.1',
}
this.server._socketAllowed.add({
localPort: socket.remotePort,
once: _.noop,
})
const noop = this.server.proxyWebsockets(this.proxy, '/foo', {
url: '/foobarbaz',
socket,
})
expect(noop).to.be.undefined
})
it('calls proxy.ws with hostname + port', function () {
this.server.remoteStates.set('https://www.google.com')
const req = {
connection: {
encrypted: true,
},
url: '/',
headers: {
host: 'www.google.com',
},
}
this.server.proxyWebsockets(this.proxy, '/foo', req, this.socket, this.head)
expect(this.proxy.ws).to.be.calledWithMatch(req, this.socket, this.head, {
secure: false,
target: {
host: 'www.google.com',
port: '443',
protocol: 'https:',
},
})
})
it('ends the socket if its writable and there is no __cypress.remoteHost', function () {
const req = {
url: '/',
headers: {
cookie: 'foo=bar',
},
}
this.server.proxyWebsockets(this.proxy, '/foo', req, this.socket, this.head)
expect(this.socket.end).not.to.be.called
this.socket.writable = true
this.server.proxyWebsockets(this.proxy, '/foo', req, this.socket, this.head)
expect(this.socket.end).to.be.called
})
})
})