-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathremote_states.spec.ts
More file actions
360 lines (303 loc) · 10.3 KB
/
remote_states.spec.ts
File metadata and controls
360 lines (303 loc) · 10.3 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
require('../spec_helper')
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import chaiSubset from 'chai-subset'
import sinonChai from '@cypress/sinon-chai'
import Sinon from 'sinon'
import { OriginBehavior } from '@packages/network-tools'
import { RemoteStates, DEFAULT_DOMAIN_NAME } from '../../lib/remote_states'
chai.use(chaiAsPromised)
chai.use(chaiSubset)
chai.use(sinonChai)
describe('remote states', () => {
const serverPorts = {
server: 3030,
fileServer: 3030,
}
const remoteStatesServerPorts = () => {
return serverPorts
}
let remoteStates: RemoteStates
let documentDomainInjection: Sinon.SinonStubbedInstance<OriginBehavior>
beforeEach(() => {
documentDomainInjection = Sinon.createStubInstance(OriginBehavior)
// While the behavior of this class is partially determined by DocumentDomainInjection,
// it's not necessary to test multiple permutations of its getOriginKey - as long as it's
// returning an appropriate origin key, this class will behave as expected.
documentDomainInjection.getOrigin.callsFake((url) => {
return new URL(url).origin
})
remoteStates = new RemoteStates(remoteStatesServerPorts, documentDomainInjection)
// set the initial state
remoteStates.set('http://localhost:3500')
})
context('#get', () => {
it('returns the remote state for an origin when a matching origin key is returned from DocumentDomainInjection', function () {
documentDomainInjection.getOrigin.returns('http://localhost:3500')
const state = remoteStates.get('http://localhost:3500/foobar')
expect(state).to.deep.equal({
auth: undefined,
origin: 'http://localhost:3500',
strategy: 'http',
domainName: 'localhost',
fileServer: null,
props: {
port: '3500',
domain: '',
tld: 'localhost',
subdomain: null,
protocol: 'http:',
},
})
})
it('returns undefined when the remote state is not found', function () {
const state = remoteStates.get('http://notfound.com')
expect(state).to.be.undefined
})
it('changing returned state does not mutate remote state', function () {
const originalState = remoteStates.get('http://localhost:3500/foobar')
expect(originalState).to.deep.equal({
auth: undefined,
origin: 'http://localhost:3500',
strategy: 'http',
domainName: 'localhost',
fileServer: null,
props: {
port: '3500',
domain: '',
tld: 'localhost',
subdomain: null,
protocol: 'http:',
},
})
originalState.auth = { username: 'u', password: 'p' }
const currentState = remoteStates.get('http://localhost:3500/foobar')
expect(currentState).to.deep.equal({
auth: undefined,
origin: 'http://localhost:3500',
strategy: 'http',
domainName: 'localhost',
fileServer: null,
props: {
port: '3500',
domain: '',
tld: 'localhost',
subdomain: null,
protocol: 'http:',
},
})
})
})
context('#getPrimary', () => {
it('returns the primary when there is only the primary in remote states', function () {
const state = remoteStates.getPrimary()
expect(state).to.deep.equal({
auth: undefined,
origin: 'http://localhost:3500',
strategy: 'http',
domainName: 'localhost',
fileServer: null,
props: {
port: '3500',
domain: '',
tld: 'localhost',
subdomain: null,
protocol: 'http:',
},
})
})
it('returns the primary when there are multiple remote states', function () {
remoteStates.set('https://staging.google.com/foo/bar', {}, false)
const state = remoteStates.getPrimary()
expect(state).to.deep.equal({
auth: undefined,
origin: 'http://localhost:3500',
strategy: 'http',
domainName: 'localhost',
fileServer: null,
props: {
port: '3500',
domain: '',
tld: 'localhost',
subdomain: null,
protocol: 'http:',
},
})
})
})
context('#isPrimarySuperDomainOrigin', () => {
it('returns true when the requested url is the primary origin', function () {
const isPrimarySuperDomainOrigin = remoteStates.isPrimarySuperDomainOrigin('http://localhost:3500')
expect(isPrimarySuperDomainOrigin).to.be.true
})
it('returns false when the requested url is not the primary origin', function () {
remoteStates.set('https://google.com', {}, false)
const isPrimarySuperDomainOrigin = remoteStates.isPrimarySuperDomainOrigin('http://google.com')
expect(isPrimarySuperDomainOrigin).to.be.false
})
})
context('#reset', () => {
it('resets the origin stack and remote states to the primary', function () {
remoteStates.set('https://google.com', {}, false)
expect(remoteStates.get('https://google.com')).to.not.be.undefined
remoteStates.reset()
expect(remoteStates.get('https://google.com')).to.be.undefined
})
})
context('#current', () => {
it('returns the remote state for the current origin in the stack', function () {
remoteStates.set('https://google.com', {})
remoteStates.set('https://staging.google.com/foo/bar', {}, false)
const state = remoteStates.current()
expect(state).to.deep.equal({
auth: undefined,
origin: 'https://staging.google.com',
strategy: 'http',
domainName: 'google.com',
fileServer: null,
props: {
port: '443',
domain: 'google',
tld: 'com',
subdomain: 'staging',
protocol: 'https:',
},
})
})
})
context('#set', () => {
it('sets primary state and origin when isPrimarySuperDomainOrigin is true', function () {
expect(remoteStates.isPrimarySuperDomainOrigin('http://localhost:3500')).to.be.true
const state = remoteStates.set('https://staging.google.com/foo/bar', {}, true)
expect(state).to.deep.equal({
auth: undefined,
origin: 'https://staging.google.com',
strategy: 'http',
domainName: 'google.com',
fileServer: null,
props: {
port: '443',
domain: 'google',
tld: 'com',
subdomain: 'staging',
protocol: 'https:',
},
})
expect(remoteStates.get('https://staging.google.com')).to.deep.equal(state)
expect(remoteStates.isPrimarySuperDomainOrigin('https://staging.google.com')).to.be.true
})
it('sets a secondary state when isPrimarySuperDomainOrigin is false', function () {
expect(remoteStates.isPrimarySuperDomainOrigin('http://localhost:3500')).to.be.true
const state = remoteStates.set('https://staging.google.com/foo/bar', {}, false)
expect(state).to.deep.equal({
auth: undefined,
origin: 'https://staging.google.com',
strategy: 'http',
domainName: 'google.com',
fileServer: null,
props: {
port: '443',
domain: 'google',
tld: 'com',
subdomain: 'staging',
protocol: 'https:',
},
})
expect(remoteStates.get('https://staging.google.com')).to.deep.equal(state)
expect(remoteStates.isPrimarySuperDomainOrigin('http://localhost:3500')).to.be.true
expect(remoteStates.isPrimarySuperDomainOrigin('https://staging.google.com')).to.be.false
})
it('sets port to 443 when omitted and https:', function () {
const state = remoteStates.set('https://staging.google.com/foo/bar')
expect(state).to.deep.equal({
auth: undefined,
origin: 'https://staging.google.com',
strategy: 'http',
domainName: 'google.com',
fileServer: null,
props: {
port: '443',
domain: 'google',
tld: 'com',
subdomain: 'staging',
protocol: 'https:',
},
})
})
it('sets port to 80 when omitted and http:', function () {
const state = remoteStates.set('http://staging.google.com/foo/bar')
expect(state).to.deep.equal({
auth: undefined,
origin: 'http://staging.google.com',
strategy: 'http',
domainName: 'google.com',
fileServer: null,
props: {
port: '80',
domain: 'google',
tld: 'com',
subdomain: 'staging',
protocol: 'http:',
},
})
})
it('sets host + port to localhost', function () {
const state = remoteStates.set('http://localhost:4200/a/b?q=1#asdf')
expect(state).to.deep.equal({
auth: undefined,
origin: 'http://localhost:4200',
strategy: 'http',
domainName: 'localhost',
fileServer: null,
props: {
port: '4200',
domain: '',
tld: 'localhost',
subdomain: null,
protocol: 'http:',
},
})
})
it('sets local file', function () {
const state = remoteStates.set('/index.html')
expect(state).to.deep.equal({
auth: undefined,
origin: `http://${DEFAULT_DOMAIN_NAME}:${serverPorts.server}`,
strategy: 'file',
domainName: DEFAULT_DOMAIN_NAME,
fileServer: `http://${DEFAULT_DOMAIN_NAME}:${serverPorts.fileServer}`,
props: null,
})
})
it('sets <root>', function () {
const state = remoteStates.set('<root>')
expect(state).to.deep.equal({
auth: undefined,
origin: `http://${DEFAULT_DOMAIN_NAME}:${serverPorts.server}`,
strategy: 'file',
domainName: DEFAULT_DOMAIN_NAME,
fileServer: `http://${DEFAULT_DOMAIN_NAME}:${serverPorts.fileServer}`,
props: null,
})
})
it('sets the remote state when passed a state object', function () {
const state = {
auth: undefined,
origin: 'http://www.foobar.com',
strategy: 'http',
domainName: 'foobar.com',
fileServer: null,
props: {
port: '80',
domain: 'foobar',
tld: 'com',
subdomain: 'www',
protocol: 'http:',
},
}
remoteStates.set(state)
const actualState = remoteStates.get('http://www.foobar.com')
expect(actualState).to.deep.equal(state)
})
})
})