corrade-nucleus-nucleons – Blame information for rev 41

Subversion Repositories:
Rev:
Rev Author Line No. Line
41 office 1 'use strict';
2  
3 /*global lifecycle: true*/
4  
5 QUnit.module('read', lifecycle);
6  
7 QUnit.test('simple value', function (assert) {
8 assert.expect(1);
9 document.cookie = 'c=v';
10 assert.strictEqual(Cookies.get('c'), 'v', 'should return value');
11 });
12  
13 QUnit.test('empty value', function (assert) {
14 assert.expect(1);
15 // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
16 // resulted in a bug while reading such a cookie.
17 Cookies.set('c', '');
18 assert.strictEqual(Cookies.get('c'), '', 'should return value');
19 });
20  
21 QUnit.test('not existing', function (assert) {
22 assert.expect(1);
23 assert.strictEqual(Cookies.get('whatever'), undefined, 'return undefined');
24 });
25  
26 // github.com/carhartl/jquery-cookie/issues/50
27 QUnit.test('equality sign in cookie value', function (assert) {
28 assert.expect(1);
29 Cookies.set('c', 'foo=bar');
30 assert.strictEqual(Cookies.get('c'), 'foo=bar', 'should include the entire value');
31 });
32  
33 // github.com/carhartl/jquery-cookie/issues/215
34 QUnit.test('percent character in cookie value', function (assert) {
35 assert.expect(1);
36 document.cookie = 'bad=foo%';
37 assert.strictEqual(Cookies.get('bad'), 'foo%', 'should read the percent character');
38 });
39  
40 QUnit.test('percent character in cookie value mixed with encoded values', function (assert) {
41 assert.expect(1);
42 document.cookie = 'bad=foo%bar%22baz%bax%3D';
43 assert.strictEqual(Cookies.get('bad'), 'foo%bar"baz%bax=', 'should read the percent character');
44 });
45  
46 // github.com/carhartl/jquery-cookie/pull/88
47 // github.com/carhartl/jquery-cookie/pull/117
48 QUnit.test('malformed cookie value in IE', function (assert) {
49 assert.expect(1);
50 var done = assert.async();
51 // Sandbox in an iframe so that we can poke around with document.cookie.
52 var iframe = document.createElement('iframe');
53 iframe.src = 'malformed_cookie.html';
54 addEvent(iframe, 'load', function () {
55 if (iframe.contentWindow.ok) {
56 assert.strictEqual(iframe.contentWindow.testValue, 'two', 'reads all cookie values, skipping duplicate occurences of "; "');
57 } else {
58 // Skip the test where we can't stub document.cookie using
59 // Object.defineProperty. Seems to work fine in
60 // Chrome, Firefox and IE 8+.
61 assert.ok(true, 'N/A');
62 }
63 done();
64 });
65 document.body.appendChild(iframe);
66 });
67  
68 // github.com/js-cookie/js-cookie/pull/171
69 QUnit.test('missing leading semicolon', function (assert) {
70 assert.expect(1);
71 var done = assert.async();
72 // Sandbox in an iframe so that we can poke around with document.cookie.
73 var iframe = document.createElement('iframe');
74 var loadedSuccessfully = true;
75 iframe.src = 'missing_semicolon.html';
76  
77 addEvent(iframe, 'load', function () {
78 iframe.contentWindow.onerror = function () {
79 loadedSuccessfully = false;
80 };
81 assert.strictEqual(loadedSuccessfully, true, 'can\'t throw Object is not a function error');
82 done();
83 });
84 document.body.appendChild(iframe);
85 });
86  
87 QUnit.test('Call to read all when there are cookies', function (assert) {
88 Cookies.set('c', 'v');
89 Cookies.set('foo', 'bar');
90 assert.deepEqual(Cookies.get(), { c: 'v', foo: 'bar' }, 'returns object containing all cookies');
91 });
92  
93 QUnit.test('Call to read all when there are no cookies at all', function (assert) {
94 assert.deepEqual(Cookies.get(), {}, 'returns empty object');
95 });
96  
97 QUnit.test('RFC 6265 - reading cookie-octet enclosed in DQUOTE', function (assert) {
98 assert.expect(1);
99 document.cookie = 'c="v"';
100 assert.strictEqual(Cookies.get('c'), 'v', 'should simply ignore quoted strings');
101 });
102  
103 // github.com/js-cookie/js-cookie/issues/196
104 QUnit.test('Call to read cookie when there is another unrelated cookie with malformed encoding in the name', function (assert) {
105 assert.expect(2);
106 document.cookie = 'BS%BS=1';
107 document.cookie = 'c=v';
108 assert.strictEqual(Cookies.get('c'), 'v', 'should not throw a URI malformed exception when retrieving a single cookie');
109 assert.deepEqual(Cookies.get(), { c: 'v' }, 'should not throw a URI malformed exception when retrieving all cookies');
110 document.cookie = 'BS%BS=1; expires=Thu, 01 Jan 1970 00:00:00 GMT';
111 });
112  
113 // github.com/js-cookie/js-cookie/pull/62
114 QUnit.test('Call to read cookie when there is another unrelated cookie with malformed encoding in the value', function (assert) {
115 assert.expect(2);
116 document.cookie = 'invalid=%A1';
117 document.cookie = 'c=v';
118 assert.strictEqual(Cookies.get('c'), 'v', 'should not throw a URI malformed exception when retrieving a single cookie');
119 assert.deepEqual(Cookies.get(), { c: 'v' }, 'should not throw a URI malformed exception when retrieving all cookies');
120 Cookies.withConverter(unescape).remove('invalid');
121 });
122  
123 // github.com/js-cookie/js-cookie/issues/145
124 QUnit.test('Call to read cookie when passing an Object Literal as the second argument', function (assert) {
125 assert.expect(1);
126 Cookies.get('name', { path: '' });
127 assert.strictEqual(document.cookie, '', 'should not create a cookie');
128 });
129  
130 // github.com/js-cookie/js-cookie/issues/238
131 QUnit.test('Call to read cookie when there is a window.json variable globally', function (assert) {
132 assert.expect(1);
133 window.json = true;
134 Cookies.set('boolean', true);
135 assert.strictEqual(typeof Cookies.get('boolean'), 'string', 'should not change the returned type');
136 // IE 6-8 throw an exception if trying to delete a window property
137 // See stackoverflow.com/questions/1073414/deleting-a-window-property-in-ie/1824228
138 try {
139 delete window.json;
140 } catch (e) {}
141 });
142  
143 QUnit.module('write', lifecycle);
144  
145 QUnit.test('String primitive', function (assert) {
146 assert.expect(1);
147 Cookies.set('c', 'v');
148 assert.strictEqual(Cookies.get('c'), 'v', 'should write value');
149 });
150  
151 QUnit.test('String object', function (assert) {
152 assert.expect(1);
153 Cookies.set('c', new String('v'));
154 assert.strictEqual(Cookies.get('c'), 'v', 'should write value');
155 });
156  
157 QUnit.test('value "[object Object]"', function (assert) {
158 assert.expect(1);
159 Cookies.set('c', '[object Object]');
160 assert.strictEqual(Cookies.get('c'), '[object Object]', 'should write value');
161 });
162  
163 QUnit.test('number', function (assert) {
164 assert.expect(1);
165 Cookies.set('c', 1234);
166 assert.strictEqual(Cookies.get('c'), '1234', 'should write value');
167 });
168  
169 QUnit.test('null', function (assert) {
170 assert.expect(1);
171 Cookies.set('c', null);
172 assert.strictEqual(Cookies.get('c'), 'null', 'should write value');
173 });
174  
175 QUnit.test('undefined', function (assert) {
176 assert.expect(1);
177 Cookies.set('c', undefined);
178 assert.strictEqual(Cookies.get('c'), 'undefined', 'should write value');
179 });
180  
181 QUnit.test('expires option as days from now', function (assert) {
182 assert.expect(1);
183 var sevenDaysFromNow = new Date();
184 sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 21);
185 var expected = 'expires=' + sevenDaysFromNow.toUTCString();
186 var actual = Cookies.set('c', 'v', { expires: 21 });
187 assert.ok(actual.indexOf(expected) !== -1, quoted(actual) + ' includes ' + quoted(expected));
188 });
189  
190 // github.com/carhartl/jquery-cookie/issues/246
191 QUnit.test('expires option as fraction of a day', function (assert) {
192 assert.expect(1);
193  
194 var findValueForAttributeName = function (createdCookie, attributeName) {
195 var pairs = createdCookie.split('; ');
196 var foundAttributeValue;
197 pairs.forEach(function (pair) {
198 if (pair.split('=')[0] === attributeName) {
199 foundAttributeValue = pair.split('=')[1];
200 }
201 });
202 return foundAttributeValue;
203 };
204 var now = new Date();
205 var stringifiedDate = findValueForAttributeName(Cookies.set('c', 'v', { expires: 0.5 }), 'expires');
206 var expires = new Date(stringifiedDate);
207  
208 // When we were using Date.setDate() fractions have been ignored
209 // and expires resulted in the current date. Allow 1000 milliseconds
210 // difference for execution time because new Date() can be different,
211 // even when it's run synchronously.
212 // See https://github.com/js-cookie/js-cookie/commit/ecb597b65e4c477baa2b30a2a5a67fdaee9870ea#commitcomment-20146048.
213 var assertion = expires.getTime() > now.getTime() + 1000;
214 var message = quoted(expires.getTime()) + ' should be greater than ' + quoted(now.getTime());
215 assert.ok(assertion, message);
216 });
217  
218 QUnit.test('expires option as Date instance', function (assert) {
219 assert.expect(1);
220 var sevenDaysFromNow = new Date();
221 sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
222 var expected = 'expires=' + sevenDaysFromNow.toUTCString();
223 var actual = Cookies.set('c', 'v', { expires: sevenDaysFromNow });
224 assert.ok(actual.indexOf(expected) !== -1, quoted(actual) + ' includes ' + quoted(expected));
225 });
226  
227 QUnit.test('return value', function (assert) {
228 assert.expect(1);
229 var expected = 'c=v';
230 var actual = Cookies.set('c', 'v').substring(0, expected.length);
231 assert.strictEqual(actual, expected, 'should return written cookie string');
232 });
233  
234 QUnit.test('default path attribute', function (assert) {
235 assert.expect(1);
236 assert.ok(Cookies.set('c', 'v').match(/path=\//), 'should read the default path');
237 });
238  
239 QUnit.test('API for changing defaults', function (assert) {
240 assert.expect(3);
241  
242 Cookies.defaults.path = '/foo';
243 assert.ok(Cookies.set('c', 'v').match(/path=\/foo/), 'should use attributes from defaults');
244 Cookies.remove('c', { path: '/foo' });
245  
246 assert.ok(Cookies.set('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'attributes argument has precedence');
247 Cookies.remove('c', { path: '/bar' });
248  
249 delete Cookies.defaults.path;
250 assert.ok(Cookies.set('c', 'v').match(/path=\//), 'should roll back to the default path');
251 });
252  
253 QUnit.test('true secure value', function (assert) {
254 assert.expect(1);
255 var expected = 'c=v; path=/; secure';
256 var actual = Cookies.set('c', 'v', {secure: true});
257 assert.strictEqual(actual, expected, 'should add secure attribute');
258 });
259  
260 // github.com/js-cookie/js-cookie/pull/54
261 QUnit.test('false secure value', function (assert) {
262 assert.expect(1);
263 var expected = 'c=v; path=/';
264 var actual = Cookies.set('c', 'v', {secure: false});
265 assert.strictEqual(actual, expected, 'false should not modify path in cookie string');
266 });
267  
268 // github.com/js-cookie/js-cookie/issues/276
269 QUnit.test('unofficial attribute', function (assert) {
270 assert.expect(1);
271 var expected = 'c=v; path=/; unofficial=anything';
272 var actual = Cookies.set('c', 'v', {
273 unofficial: 'anything'
274 });
275 assert.strictEqual(expected, actual, 'should write the cookie string with unofficial attribute');
276 });
277  
278 QUnit.test('undefined attribute value', function (assert) {
279 assert.expect(5);
280 assert.strictEqual(Cookies.set('c', 'v', {
281 expires: undefined
282 }), 'c=v; path=/', 'should not write undefined expires attribute');
283 assert.strictEqual(Cookies.set('c', 'v', {
284 path: undefined
285 }), 'c=v', 'should not write undefined path attribute');
286 assert.strictEqual(Cookies.set('c', 'v', {
287 domain: undefined
288 }), 'c=v; path=/', 'should not write undefined domain attribute');
289 assert.strictEqual(Cookies.set('c', 'v', {
290 secure: undefined
291 }), 'c=v; path=/', 'should not write undefined secure attribute');
292 assert.strictEqual(Cookies.set('c', 'v', {
293 unofficial: undefined
294 }), 'c=v; path=/', 'should not write undefined unofficial attribute');
295 });
296  
297 QUnit.module('remove', lifecycle);
298  
299 QUnit.test('deletion', function (assert) {
300 assert.expect(1);
301 Cookies.set('c', 'v');
302 Cookies.remove('c');
303 assert.strictEqual(document.cookie, '', 'should delete the cookie');
304 });
305  
306 QUnit.test('with attributes', function (assert) {
307 assert.expect(1);
308 var attributes = { path: '/' };
309 Cookies.set('c', 'v', attributes);
310 Cookies.remove('c', attributes);
311 assert.strictEqual(document.cookie, '', 'should delete the cookie');
312 });
313  
314 QUnit.test('passing attributes reference', function (assert) {
315 assert.expect(1);
316 var attributes = { path: '/' };
317 Cookies.set('c', 'v', attributes);
318 Cookies.remove('c', attributes);
319 assert.deepEqual(attributes, { path: '/' }, 'won\'t alter attributes object');
320 });
321  
322 QUnit.module('converters', lifecycle);
323  
324 // github.com/carhartl/jquery-cookie/pull/166
325 QUnit.test('provide a way for decoding characters encoded by the escape function', function (assert) {
326 assert.expect(1);
327 document.cookie = 'c=%u5317%u4eac';
328 assert.strictEqual(Cookies.withConverter(unescape).get('c'), '北京', 'should convert chinese characters correctly');
329 });
330  
331 QUnit.test('should decode a malformed char that matches the decodeURIComponent regex', function (assert) {
332 assert.expect(1);
333 document.cookie = 'c=%E3';
334 var cookies = Cookies.withConverter(unescape);
335 assert.strictEqual(cookies.get('c'), 'ã', 'should convert the character correctly');
336 cookies.remove('c', {
337 path: ''
338 });
339 });
340  
341 QUnit.test('should be able to conditionally decode a single malformed cookie', function (assert) {
342 assert.expect(4);
343 var cookies = Cookies.withConverter(function (value, name) {
344 if (name === 'escaped') {
345 return unescape(value);
346 }
347 });
348  
349 document.cookie = 'escaped=%u5317';
350 assert.strictEqual(cookies.get('escaped'), '北', 'should use a custom method for escaped cookie');
351  
352 document.cookie = 'encoded=%E4%BA%AC';
353 assert.strictEqual(cookies.get('encoded'), '京', 'should use the default encoding for the rest');
354  
355 assert.deepEqual(cookies.get(), {
356 escaped: '北',
357 encoded: '京'
358 }, 'should retrieve everything');
359  
360 Object.keys(cookies.get()).forEach(function (name) {
361 cookies.remove(name, {
362 path: ''
363 });
364 });
365 assert.strictEqual(document.cookie, '', 'should remove everything');
366 });
367  
368 // github.com/js-cookie/js-cookie/issues/70
369 QUnit.test('should be able to create a write decoder', function (assert) {
370 assert.expect(1);
371 Cookies.withConverter({
372 write: function (value) {
373 return value.replace('+', '%2B');
374 }
375 }).set('c', '+');
376 assert.strictEqual(document.cookie, 'c=%2B', 'should call the write converter');
377 });
378  
379 QUnit.test('should be able to use read and write decoder', function (assert) {
380 assert.expect(1);
381 document.cookie = 'c=%2B';
382 var cookies = Cookies.withConverter({
383 read: function (value) {
384 return value.replace('%2B', '+');
385 }
386 });
387 assert.strictEqual(cookies.get('c'), '+', 'should call the read converter');
388 });
389  
390 QUnit.module('JSON handling', lifecycle);
391  
392 QUnit.test('Number', function (assert) {
393 assert.expect(2);
394 Cookies.set('c', 1);
395 assert.strictEqual(Cookies.getJSON('c'), 1, 'should handle a Number');
396 assert.strictEqual(Cookies.get('c'), '1', 'should return a String');
397 });
398  
399 QUnit.test('Boolean', function (assert) {
400 assert.expect(2);
401 Cookies.set('c', true);
402 assert.strictEqual(Cookies.getJSON('c'), true, 'should handle a Boolean');
403 assert.strictEqual(Cookies.get('c'), 'true', 'should return a Boolean');
404 });
405  
406 QUnit.test('Array Literal', function (assert) {
407 assert.expect(2);
408 Cookies.set('c', ['v']);
409 assert.deepEqual(Cookies.getJSON('c'), ['v'], 'should handle Array Literal');
410 assert.strictEqual(Cookies.get('c'), '["v"]', 'should return a String');
411 });
412  
413 QUnit.test('Array Constructor', function (assert) {
414 /*jshint -W009 */
415 assert.expect(2);
416 var value = new Array();
417 value[0] = 'v';
418 Cookies.set('c', value);
419 assert.deepEqual(Cookies.getJSON('c'), ['v'], 'should handle Array Constructor');
420 assert.strictEqual(Cookies.get('c'), '["v"]', 'should return a String');
421 });
422  
423 QUnit.test('Object Literal', function (assert) {
424 assert.expect(2);
425 Cookies.set('c', {k: 'v'});
426 assert.deepEqual(Cookies.getJSON('c'), {k: 'v'}, 'should handle Object Literal');
427 assert.strictEqual(Cookies.get('c'), '{"k":"v"}', 'should return a String');
428 });
429  
430 QUnit.test('Object Constructor', function (assert) {
431 /*jshint -W010 */
432 assert.expect(2);
433 var value = new Object();
434 value.k = 'v';
435 Cookies.set('c', value);
436 assert.deepEqual(Cookies.getJSON('c'), {k: 'v'}, 'should handle Object Constructor');
437 assert.strictEqual(Cookies.get('c'), '{"k":"v"}', 'should return a String');
438 });
439  
440 QUnit.test('Use String(value) for unsupported objects that do not stringify into JSON', function (assert) {
441 assert.expect(2);
442 Cookies.set('date', new Date(2015, 4, 13, 0, 0, 0, 0));
443 assert.strictEqual(Cookies.get('date').indexOf('"'), -1, 'should not quote the stringified Date object');
444 assert.strictEqual(Cookies.getJSON('date').indexOf('"'), -1, 'should not quote the stringified Date object');
445 });
446  
447 QUnit.test('Call to read all cookies with mixed json', function (assert) {
448 Cookies.set('c', { foo: 'bar' });
449 Cookies.set('c2', 'v');
450 assert.deepEqual(Cookies.getJSON(), { c: { foo: 'bar' }, c2: 'v' }, 'returns JSON parsed cookies');
451 assert.deepEqual(Cookies.get(), { c: '{"foo":"bar"}', c2: 'v' }, 'returns unparsed cookies');
452 });
453  
454 QUnit.module('noConflict', lifecycle);
455  
456 QUnit.test('do not conflict with existent globals', function (assert) {
457 assert.expect(2);
458 var Cookies = window.Cookies.noConflict();
459 Cookies.set('c', 'v');
460 assert.strictEqual(Cookies.get('c'), 'v', 'should work correctly');
461 assert.strictEqual(window.Cookies, 'existent global', 'should restore the original global');
462 window.Cookies = Cookies;
463 });