-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cookies-tests.js
347 lines (289 loc) · 12.6 KB
/
cookies-tests.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
import { EJSON } from 'meteor/ejson';
import { Meteor } from 'meteor/meteor';
import { Cookies } from './cookies.js';
const helpers = {
isArray(obj) {
return Array.isArray(obj);
},
clone(obj) {
if (!this.isObject(obj)) return obj;
return this.isArray(obj) ? obj.slice() : Object.assign({}, obj);
}
};
const _helpers = ['Number', 'Object', 'Function'];
for (let i = 0; i < _helpers.length; i++) {
helpers['is' + _helpers[i]] = function (obj) {
return Object.prototype.toString.call(obj) === '[object ' + _helpers[i] + ']';
};
}
const antiCircular = (_obj) => {
const object = helpers.clone(_obj);
const cache = new Map();
return JSON.parse(JSON.stringify(object, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.get(value)) {
return void 0;
}
cache.set(value, true);
}
return value;
}));
};
if (Meteor.isClient) {
const cookies = new Cookies();
Tinytest.add('From Server to Client', test => {
test.equal(cookies.get('FORCLIENT', document.cookie), '_form_server_to_client_tests_', 'document.cookie');
test.equal(cookies.get('FORCLIENT'), '_form_server_to_client_tests_', 'js');
});
Tinytest.add('cookies: set() - empty value', test => {
const testVal = void 0;
const setRes = cookies.set('testCookieEmpty', testVal);
test.isFalse(setRes);
test.equal(cookies.get('testCookieEmpty', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testCookieEmpty'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() - String', test => {
const testVal = 'this is test value';
const setRes = cookies.set('testCookie', testVal);
test.isTrue(setRes);
test.equal(cookies.get('testCookie', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testCookie'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - Unicode', function (test) {
const testVal = '⦶';
const setRes = cookies.set('⦁', testVal);
test.isTrue(setRes);
test.isTrue(cookies.has('⦁'));
test.isFalse(cookies.has('⦁⦁⦁'));
test.equal(cookies.get('⦁', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('⦁'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - Unicode 2', function (test) {
const testVal = '小飼弾\n小飼弾';
const setRes = cookies.set('小飼弾', testVal);
test.isTrue(setRes);
test.isTrue(cookies.has('小飼弾'));
test.equal(cookies.get('小飼弾', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('小飼弾'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - Cyrillic', function (test) {
const testVal = 'йцукен\nнекуцй';
const setRes = cookies.set('фывфыв', testVal);
test.isTrue(setRes);
test.isTrue(cookies.has('фывфыв'));
test.equal(cookies.get('фывфыв', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('фывфыв'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - "Device undefined ("', function (test) {
const testVal = 'Device undefined (';
const testKey = 'device undefined';
const setRes = cookies.set(testKey, testVal);
test.isTrue(setRes);
test.isTrue(cookies.has(testKey));
test.equal(cookies.get(testKey, document.cookie), testVal, 'document.cookie');
test.equal(cookies.get(testKey), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - FALSE', test => {
const testVal = false;
const setRes = cookies.set('testFalse', testVal);
test.isTrue(setRes);
test.isTrue(cookies.has('testFalse'));
test.equal(cookies.get('testFalse', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testFalse'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - TRUE', test => {
const testVal = true;
const setRes = cookies.set('testTrue', testVal);
test.isTrue(setRes);
test.isTrue(cookies.has('testTrue'));
test.equal(cookies.get('testTrue', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testTrue'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() / has() - NULL', test => {
const testVal = null;
const setRes = cookies.set('testNull', testVal);
test.isTrue(setRes);
test.isTrue(cookies.has('testNull'));
test.equal(cookies.get('testNull', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testNull'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() - Object', test => {
const testVal = {key: '1', key2: {key1: 1, key2: false, key3: [true, false]}};
const setRes = cookies.set('testObject', testVal);
test.isTrue(setRes);
test.equal(cookies.get('testObject', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testObject'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() - Object Circle', test => {
const testVal = {key: '1', key2: {key1: 1, key2: false, key3: [true, false]}};
testVal.slef = testVal;
const setRes = cookies.set('testObject', testVal);
test.isTrue(setRes);
const _testVal = antiCircular(Object.assign({}, testVal));
test.equal(cookies.get('testObject', document.cookie), _testVal, 'document.cookie');
test.equal(cookies.get('testObject'), _testVal, 'js');
});
Tinytest.add('cookies: set() / get() - Array', test => {
const testVal = [true, false, null, {key1: 1, key2: false, key3: [true, false]}, [1, 2, 3, '4', '5']];
const setRes = cookies.set('testArray', testVal);
test.isTrue(setRes);
test.equal(cookies.get('testArray', document.cookie), testVal, 'document.cookie');
test.equal(cookies.get('testArray'), testVal, 'js');
});
Tinytest.add('cookies: set() / get() - Array Circle', test => {
const obj = {asd: 'dsa', arr: [1, 2, 3]};
obj.slef = ['a', obj, 'c'];
const testVal = [true, false, null, {key1: 1, key2: false, key3: [true, false]}, [1, 2, 3, '4', '5'], obj];
const setRes = cookies.set('testArray', testVal);
test.isTrue(setRes);
test.equal(cookies.get('testArray', document.cookie), antiCircular(testVal), 'document.cookie');
test.equal(cookies.get('testArray'), antiCircular(testVal), 'js');
});
Tinytest.add('cookies: set() / get() / has() - no key', test => {
test.isFalse(cookies.set());
test.isUndefined(cookies.get());
test.isFalse(cookies.has());
});
Tinytest.add('cookies: get() / has() - from custom string', test => {
const testVal = 'customCookieVal';
const cookie = `customCookie=${testVal}; `;
test.equal(cookies.get('customCookie', cookie), testVal);
test.equal(cookies.get('asd', cookie), undefined);
test.isTrue(cookies.has('customCookie', cookie));
test.isFalse(cookies.has('asd', cookie));
});
Tinytest.add('cookies: get() - non existent cookie', test => {
test.isUndefined(cookies.get('1234567890321-asdfghjk', document.cookie), 'document.cookie');
test.isUndefined(cookies.get('1234567890321-asdfghjk'), 'js');
});
Tinytest.add('cookies: remove() - non existent cookie', test => {
const removeRes = cookies.remove('1234567890asdfghjk');
test.isFalse(removeRes);
});
Tinytest.add('cookies: keys() / has() / remove() - String', test => {
cookies.remove();
cookies.set('testCookieOne', 'One');
cookies.set('testCookieTwo', 'Two');
test.equal(cookies.keys(), ['testCookieOne', 'testCookieTwo']);
test.isTrue(cookies.has('testCookieOne'));
test.isTrue(cookies.has('testCookieTwo'));
const removeRes = cookies.remove('testCookieOne');
test.isTrue(removeRes);
test.isFalse(cookies.has('testCookieOne'));
test.isTrue(cookies.has('testCookieTwo'));
});
Tinytest.add('cookies: remove() - all', test => {
let removeRes = cookies.remove();
test.isTrue(removeRes);
test.equal(cookies.keys(), []);
removeRes = cookies.remove();
test.isFalse(removeRes);
});
Tinytest.addAsync('cookies: send(callback) - empty', (test, next) => {
cookies.send(() => {
test.isTrue(true);
next();
});
});
Tinytest.add('Server test - see in console', () => {});
Tinytest.add('From Client to Server', () => {
cookies.set('FORSERVERTEST', '_form_client_to_server_tests_');
cookies.send();
});
Tinytest.addAsync('cookies: send(callback) - default', (test, next) => {
cookies.send(() => {
test.isTrue(true);
next();
});
});
} else {
const WebApp = require('meteor/webapp').WebApp;
const tester = (one, two, testname) => {
if(EJSON.equals(one, two)){
console.info(`[${testname}] PASSED`);
}else{
console.warn(`[${testname}] Failed!`, `Expected: ${JSON.stringify(two)}`, `Got: ${JSON.stringify(one)}`);
}
};
const cookie = new Cookies({
auto: false,
handler(cookies) {
tester(cookies.get('FORSERVERTEST'), '_form_client_to_server_tests_', 'From Client to Server [First Time should FAIL]');
(() => {
const testVal = '⦶';
const setRes = cookies.set('⦁', testVal);
tester(setRes, true, 'Unicode .set()', cookies);
tester(cookies.has('⦁'), true, 'Unicode .has()', cookies);
tester(cookies.has('⦁⦁⦁'), false, 'Unicode .has() non-existent', cookies);
tester(cookies.get('⦁'), testVal, 'Unicode .get()', cookies);
})();
(() => {
const testVal = '小飼弾\n小飼弾';
const setRes = cookies.set('小飼弾', testVal);
tester(setRes, true, 'Unicode 2 .set()', cookies);
tester(cookies.has('小飼弾'), true, 'Unicode 2 .has()', cookies);
tester(cookies.get('小飼弾'), testVal, 'Unicode 2 .get()', cookies);
})();
(() => {
const testVal = 'йцукен\nнекуцй';
const setRes = cookies.set('фывфыв', testVal);
tester(setRes, true, 'Cyrillic .set()', cookies);
tester(cookies.has('фывфыв'), true, 'Cyrillic .has()', cookies);
tester(cookies.get('фывфыв'), testVal, 'Cyrillic .get()', cookies);
})();
(() => {
const testVal = void 0;
const setRes = cookies.set('testCookieEmpty', testVal);
tester(setRes, false, 'test empty value', cookies);
tester(cookies.get('testCookieEmpty'), testVal, 'test empty value', cookies);
})();
(() => {
const testVal = 'this is test value';
const setRes = cookies.set('testCookie', testVal);
tester(setRes, true, 'cookies.set', cookies);
tester(cookies.get('testCookie'), testVal, 'cookies.get', cookies);
})();
(() => {
tester(cookies.set(), false, 'cookies.set()', cookies);
tester(cookies.get(), undefined, 'cookies.get()', cookies);
tester(cookies.has(), false, 'cookies.has()', cookies);
})();
(() => {
const testVal = 'customCookieVal';
const _cookie = `customCookie=${testVal}; `;
tester(cookies.get('customCookie', _cookie), testVal, "cookies.get('customCookie')", cookies);
tester(cookies.get('asd', _cookie), undefined, "cookies.get('asd')", cookies);
tester(cookies.has('customCookie', _cookie), true, "cookies.has('customCookie')", cookies);
tester(cookies.has('asd', _cookie), false, "cookies.has('asd')", cookies);
})();
(() => {
const removeRes = cookies.remove('1234567890asdfghjk');
tester(removeRes, false, "cookies.remove('1234567890asdfghjk')", cookies);
})();
(() => {
tester(cookies.get('1234567890asdfghjk'), undefined, "cookies.get('1234567890asdfghjk')", cookies);
})();
(() => {
cookies.remove();
cookies.set('testCookieOne', 'One');
cookies.set('testCookieTwo', 'Two');
tester(cookies.keys(), ['testCookieOne', 'testCookieTwo'], 'cookies.keys()', cookies);
tester(cookies.has('testCookieOne'), true, "cookies.has('testCookieOne')", cookies);
tester(cookies.has('testCookieTwo'), true, "cookies.has('testCookieTwo')", cookies);
const removeRes = cookies.remove('testCookieOne');
tester(removeRes, true, "cookies.remove('testCookieOne')", cookies);
tester(cookies.has('testCookieOne'), false, "cookies.has('testCookieOne')", cookies);
tester(cookies.has('testCookieTwo'), true, "cookies.has('testCookieTwo')", cookies);
})();
(() => {
let removeRes = cookies.remove();
tester(removeRes, true, 'cookies.remove()', cookies);
tester(cookies.keys(), [], 'cookies.keys()', cookies);
removeRes = cookies.remove();
tester(removeRes, false, 'cookies.remove()', cookies);
})();
cookies.set('FORCLIENT', '_form_server_to_client_tests_');
}
});
WebApp.connectHandlers.use(cookie.middleware());
}