-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (102 loc) · 3.3 KB
/
index.js
File metadata and controls
112 lines (102 loc) · 3.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
import { SplitFactory } from '@splitsoftware/splitio';
const DEFAULT_ERROR_MARGIN = 50; // 0.05 secs
/**
* Assert if an `actual` and `expected` numeric values are nearlyEqual.
*
* @param {number} actual actual time lapse in millis
* @param {number} expected expected time lapse in millis
* @param {number} epsilon error margin in millis
* @returns {boolean} whether the absolute difference is minor to epsilon value or not
*/
export function nearlyEqual(actual, expected, epsilon = DEFAULT_ERROR_MARGIN) {
const diff = Math.abs(actual - expected);
return diff <= epsilon;
}
/**
* mock the basic behaviour for `/segmentChanges` endpoint:
* - when `?since=-1`, it returns the given segment `keys` in `added` list.
* - otherwise, it returns empty `added` and `removed` lists, and the same since and till values.
*
* @param {Object} fetchMock see http://www.wheresrhys.co.uk/fetch-mock
* @param {string|RegExp|...} matcher see http://www.wheresrhys.co.uk/fetch-mock/#api-mockingmock_matcher
* @param {string[]} keys array of segment keys to fetch
* @param {number} changeNumber optional changeNumber
*/
export function mockSegmentChanges(fetchMock, matcher, keys, changeNumber = 1457552620999) {
fetchMock.get(matcher, function (url) {
const since = parseInt(url.split('=').pop());
const name = url.split('?')[0].split('/').pop();
return {
status: 200, body: {
'name': name,
'added': since === -1 ? keys : [],
'removed': [],
'since': since,
'till': since === -1 ? changeNumber : since,
}
};
});
}
export function hasNoCacheHeader(fetchMockOpts) {
return fetchMockOpts.headers['Cache-Control'] === 'no-cache';
}
const eventsEndpointMatcher = /^\/(testImpressions|metrics|events)/;
const authEndpointMatcher = /^\/v2\/auth/;
const streamingEndpointMatcher = /^\/(sse|event-stream)/;
/**
* Switch URLs servers based on target.
* Only used for testing purposes.
*
* @param {Object} settings settings object
* @param {String} target url path
* @return {String} completed url
*/
export function url(settings, target) {
if (eventsEndpointMatcher.test(target)) {
return `${settings.urls.events}${target}`;
}
if (authEndpointMatcher.test(target)) {
return `${settings.urls.auth}${target}`;
}
if (streamingEndpointMatcher.test(target)) {
return `${settings.urls.streaming}${target}`;
}
return `${settings.urls.sdk}${target}`;
}
const getRedisConfig = (redisPort) => ({
core: {
authorizationKey: 'SOME SDK KEY' // in consumer mode, SDK key is only used to track and log warning regarding duplicated SDK instances
},
mode: 'consumer',
storage: {
type: 'REDIS',
prefix: 'REDIS_NODE_UT',
options: {
url: `redis://localhost:${redisPort}/0`
}
},
sync: {
impressionsMode: 'DEBUG'
},
startup: {
readyTimeout: 36000 // 10hs
}
});
const config = {
core: {
authorizationKey: 'localhost'
},
features: './split.yaml',
}
/**
* get a Split client in localhost mode for testing purposes
*/
export function getLocalHostSplitClient() {
return SplitFactory(config).client();
}
export function getRedisSplitClient(redisPort) {
return SplitFactory(getRedisConfig(redisPort)).client();
}
export function getSplitFactory() {
return SplitFactory(config);
}