-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplitio-api-binding.test.js
381 lines (359 loc) · 12.2 KB
/
splitio-api-binding.test.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
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
'use strict'
const { expect } = require('chai')
const nock = require('nock')
const sinon = require('sinon')
const {
getSegmentNamesInUse,
SplitioApiBinding
} = require('./splitio-api-binding')
const MOCK_SPLITIO_API_URI = 'https://mock-sdk.split.io/api'
const MOCK_SPLITIO_API_KEY = 'mock-key'
describe('lib.splitio-api-binding', () => {
describe('SplitioApiBinding', () => {
let splitioApiMock
beforeEach(() => {
splitioApiMock = new SplitioApiBinding({
splitioApiUri: MOCK_SPLITIO_API_URI,
splitioApiKey: MOCK_SPLITIO_API_KEY
})
})
describe('httpGet', () => {
it('returns a successful response', async () => {
nock(MOCK_SPLITIO_API_URI)
.get('/mock')
.query({
since: -1
})
.reply(200, 'Success!')
const results = await splitioApiMock.httpGet({
path: 'mock',
since: -1
})
expect(results).equals('Success!')
})
it('throws an error on a non-200 response', async () => {
nock(MOCK_SPLITIO_API_URI)
.get('/mock')
.query({
since: -1
})
.reply(401, { message: 'Unauthorized' })
try {
await splitioApiMock.httpGet({
path: 'mock',
since: -1
})
throw new Error('did not throw')
} catch (err) {
expect(err.statusCode).equals(401)
expect(err.message).equals('Unauthorized')
}
})
it('throws an error when no response is received', async () => {
nock(MOCK_SPLITIO_API_URI)
.get('/mock')
.query({
since: -1
})
.replyWithError('Error')
try {
await splitioApiMock.httpGet({
path: 'mock',
since: -1
})
throw new Error('did not throw')
} catch (err) {
expect(err).to.be.an.instanceof(Error)
}
})
})
describe('getAllChanges', () => {
const mockChanges0 = [{ bar: 'bar' }]
const mockChanges1 = [{ baz: 'baz' }]
const mockChanges2 = [{ zoo: 'zoo' }]
it('returns all changes and the since value', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.httpGet = requestStub
requestStub.onCall(0).resolves({ changes: mockChanges0, till: 0 })
requestStub.onCall(1).resolves({ changes: mockChanges1, till: 1 })
requestStub.onCall(2).resolves({ changes: mockChanges2, till: 2 })
requestStub.onCall(3).resolves({ changes: mockChanges2, till: 2 })
const changes = await splitioApiBinding.getAllChanges({
path: '/mock',
maxNumRequests: 10
})
expect(changes).deep.equals({
allChanges: [
{ changes: mockChanges0, till: 0 },
{ changes: mockChanges1, till: 1 },
{ changes: mockChanges2, till: 2 }
],
since: 2
})
})
it('polls until the max number of requests is reached', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.httpGet = requestStub
requestStub.onCall(0).resolves({ changes: mockChanges0, till: 0 })
requestStub.onCall(1).resolves({ changes: mockChanges1, till: 1 })
requestStub.onCall(2).resolves({ changes: mockChanges2, till: 2 })
requestStub.onCall(3).resolves({ changes: mockChanges2, till: 2 })
const changes = await splitioApiBinding.getAllChanges({
path: '/mock',
maxNumRequests: 2
})
expect(changes).deep.equals({
allChanges: [
{ changes: mockChanges0, till: 0 },
{ changes: mockChanges1, till: 1 }
],
since: 1
})
})
it('throws an error on an error from httpGet', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.httpGet = requestStub
requestStub.onCall(0).resolves({ changes: mockChanges0, till: 0 })
requestStub.onCall(1).rejects('Some Error')
try {
await splitioApiBinding.getAllChanges({ path: '/mock' })
throw new Error('did not throw')
} catch (err) {
expect(err).to.be.an.instanceof(Error)
}
})
})
describe('getSplits', () => {
const mockSplit0 = { name: 'mockSplit0', status: 'bar' }
const mockSplit1 = { name: 'mockSplit1', status: 'baz' }
const mockSplit1Updated = { name: 'mockSplit1', status: 'zoo' }
const mockSplit2 = { name: 'mockSplit2', status: 'ARCHIVED' }
const mockSplit3 = { name: 'mockSplit3', status: 'zaz' }
const mockSplit3Updated = { name: 'mockSplit3', status: 'ARCHIVED' }
it('returns all active splits and the since value', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub.onCall(0).resolves({
allChanges: [
{ splits: [mockSplit0], till: 0 },
{ splits: [mockSplit1, mockSplit2], till: 1 }
],
since: 1
})
const splits = await splitioApiBinding.getSplits({ path: '/splitChanges' })
expect(splits).deep.equals({
splits: {
mockSplit0,
mockSplit1
},
since: 1
})
})
it('overwrites an existing split object with the latest data from a subsequent request', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub.onCall(0).resolves({
allChanges: [
{ splits: [mockSplit0, mockSplit1], till: 0 },
{ splits: [mockSplit1Updated, mockSplit3], till: 1 }
],
since: 1
})
const splits = await splitioApiBinding.getSplits({ path: '/splitChanges' })
expect(splits).deep.equals({
splits: {
mockSplit0,
mockSplit1: mockSplit1Updated,
mockSplit3
},
since: 1
})
})
it('does not save a split that was archived after the initial request to GET /splitChanges', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub.onCall(0).resolves({
allChanges: [
{ splits: [mockSplit0, mockSplit3], till: 0 },
{ splits: [mockSplit3Updated], till: 1 }
],
since: 1
})
const splits = await splitioApiBinding.getSplits({ path: '/splitChanges' })
expect(splits).deep.equals({
splits: {
mockSplit0
},
since: 1
})
})
it('throws an error on an error from getAllChanges', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub.onCall(0).rejects('Some Error')
try {
await splitioApiBinding.getSplits()
throw new Error('did not throw')
} catch (err) {
expect(err).to.be.an.instanceof(Error)
}
})
})
describe('getSegment', () => {
it('returns segment data', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub
.withArgs({ path: '/segmentChanges/bar' })
.resolves({
allChanges: [
{ name: 'bar', added: ['bar1', 'bar2', 'bar3'], removed: [] },
{ name: 'bar', added: [], removed: ['bar2'] }
]
})
const segment = await splitioApiBinding.getSegment({ name: 'bar' })
expect(segment).deep.equals(
{ name: 'bar', added: ['bar1', 'bar3'] }
)
})
})
describe('getSegmentsForSplits', () => {
it('returns segments data and the count of splits using segments', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const mockSplits = [
{
conditions: [
{
matcherGroup: {
matchers: [
{
matcherType: 'IN_SEGMENT',
userDefinedSegmentMatcherData: {
segmentName: 'foo'
}
}
]
}
}
]
},
{
conditions: [
{
matcherGroup: {
matchers: [
{
matcherType: 'NOT_IN_SEGMENT',
userDefinedSegmentMatcherData: {
segmentName: 'boo'
}
}
]
}
}
]
},
{
conditions: [
{
matcherGroup: {
matchers: [
{
matcherType: 'IN_SEGMENT',
userDefinedSegmentMatcherData: {
segmentName: 'bar'
}
}
]
}
}
]
}
]
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub
.withArgs({ path: '/segmentChanges/foo' })
.resolves({
allChanges: [
{ name: 'foo', added: ['foo1', 'foo2'], removed: [] },
{ name: 'foo', added: [], removed: [] }
]
})
requestStub
.withArgs({ path: '/segmentChanges/bar' })
.resolves({
allChanges: [
{ name: 'bar', added: ['bar1', 'bar2', 'bar3'], removed: [] },
{ name: 'bar', added: [], removed: ['bar2'] }
]
})
const { segments, usingSegmentsCount } = await splitioApiBinding.getSegmentsForSplits({ splits: mockSplits })
expect(segments).deep.equals({
foo: {
name: 'foo',
added: ['foo1', 'foo2']
},
bar: {
name: 'bar',
added: ['bar1', 'bar3']
}
})
expect(usingSegmentsCount).equals(2)
})
it('throws an error on an error from getAllChanges', async () => {
const splitioApiBinding = new SplitioApiBinding({ splitioApiKey: 'foo' })
const requestStub = sinon.stub()
splitioApiBinding.getAllChanges = requestStub
requestStub.onCall(0).rejects('Some Error')
try {
await splitioApiBinding.getSegmentsForSplits({})
throw new Error('did not throw')
} catch (err) {
expect(err).to.be.an.instanceof(Error)
}
})
})
})
describe('getSegmentNamesInUse', () => {
it('returns the names of any segments in use by splits', () => {
const splitConditions = [
{
conditionType: 'foo',
matcherGroup: {
matchers: [
{
matcherType: 'WHITELIST',
userDefinedSegmentMatcherData: null
}
]
}
},
{
conditionType: 'bar',
matcherGroup: {
matchers: [
{
matcherType: 'IN_SEGMENT',
userDefinedSegmentMatcherData: {
segmentName: 'test-segment'
}
}
]
}
}
]
const segmentNames = getSegmentNamesInUse({ splitConditions })
expect(segmentNames.size).equals(1)
expect(segmentNames.has('test-segment')).equals(true)
})
})
})