Dateigrösse: 9.26 kb
1 // Required for exposing test results to the Sauce Labs API. 2 // Can be removed when the following issue is fixed: 3 // https://github.com/axemclion/grunt-saucelabs/issues/84 4 QUnit.done(function (details) { 5 window.global_test_results = details; 6 }); 7 8 9 var lifecycle = { 10 teardown: function () { 11 $.cookie.defaults = {}; 12 delete $.cookie.raw; 13 delete $.cookie.json; 14 $.each($.cookie(), $.removeCookie); 15 } 16 }; 17 18 19 module('read', lifecycle); 20 21 test('simple value', function () { 22 expect(1); 23 document.cookie = 'c=v'; 24 strictEqual($.cookie('c'), 'v', 'should return value'); 25 }); 26 27 test('empty value', function () { 28 expect(1); 29 // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which 30 // resulted in a bug while reading such a cookie. 31 $.cookie('c', ''); 32 strictEqual($.cookie('c'), '', 'should return value'); 33 }); 34 35 test('not existing', function () { 36 expect(1); 37 strictEqual($.cookie('whatever'), undefined, 'return undefined'); 38 }); 39 40 test('RFC 2068 quoted string', function () { 41 expect(1); 42 document.cookie = 'c="v@address.com\\"\\\\\\""'; 43 strictEqual($.cookie('c'), 'v@address.com"\\"', 'should decode RFC 2068 quoted string'); 44 }); 45 46 test('decode', function () { 47 expect(1); 48 document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v'); 49 strictEqual($.cookie(' c'), ' v', 'should decode key and value'); 50 }); 51 52 test('decode pluses to space for server side written cookie', function () { 53 expect(1); 54 document.cookie = 'c=foo+bar'; 55 strictEqual($.cookie('c'), 'foo bar', 'should convert pluses back to space'); 56 }); 57 58 test('raw = true', function () { 59 expect(2); 60 $.cookie.raw = true; 61 62 document.cookie = 'c=%20v'; 63 strictEqual($.cookie('c'), '%20v', 'should not decode value'); 64 65 // see https://github.com/carhartl/jquery-cookie/issues/50 66 $.cookie('c', 'foo=bar'); 67 strictEqual($.cookie('c'), 'foo=bar', 'should include the entire value'); 68 }); 69 70 test('json = true', function () { 71 expect(1); 72 73 if ('JSON' in window) { 74 $.cookie.json = true; 75 $.cookie('c', { foo: 'bar' }); 76 deepEqual($.cookie('c'), { foo: 'bar' }, 'should parse JSON'); 77 } else { 78 ok(true); 79 } 80 }); 81 82 test('not existing with json = true', function () { 83 expect(1); 84 85 if ('JSON' in window) { 86 $.cookie.json = true; 87 strictEqual($.cookie('whatever'), undefined, "won't throw exception"); 88 } else { 89 ok(true); 90 } 91 }); 92 93 test('string with json = true', function () { 94 expect(1); 95 96 if ('JSON' in window) { 97 $.cookie.json = true; 98 $.cookie('c', 'v'); 99 strictEqual($.cookie('c'), 'v', 'should return value'); 100 } else { 101 ok(true); 102 } 103 }); 104 105 test('invalid JSON string with json = true', function () { 106 expect(1); 107 108 if ('JSON' in window) { 109 $.cookie('c', 'v'); 110 $.cookie.json = true; 111 strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined"); 112 } else { 113 ok(true); 114 } 115 }); 116 117 test('invalid URL encoding', function () { 118 expect(1); 119 document.cookie = 'bad=foo%'; 120 strictEqual($.cookie('bad'), undefined, "won't throw exception, returns undefined"); 121 // Delete manually here because it requires raw === true... 122 $.cookie.raw = true; 123 $.removeCookie('bad'); 124 }); 125 126 asyncTest('malformed cookie value in IE (#88, #117)', function () { 127 expect(1); 128 // Sandbox in an iframe so that we can poke around with document.cookie. 129 var iframe = $('<iframe src="malformed_cookie.html"></iframe>')[0]; 130 $(iframe).on('load', function () { 131 start(); 132 if (iframe.contentWindow.ok) { 133 strictEqual(iframe.contentWindow.testValue, 'two', 'reads all cookie values, skipping duplicate occurences of "; "'); 134 } else { 135 // Skip the test where we can't stub document.cookie using 136 // Object.defineProperty. Seems to work fine in 137 // Chrome, Firefox and IE 8+. 138 ok(true, 'N/A'); 139 } 140 }); 141 document.body.appendChild(iframe); 142 }); 143 144 test('Call to read all when there are cookies', function () { 145 $.cookie('c', 'v'); 146 $.cookie('foo', 'bar'); 147 deepEqual($.cookie(), { c: 'v', foo: 'bar' }, 'returns object containing all cookies'); 148 }); 149 150 test('Call to read all when there are no cookies at all', function () { 151 deepEqual($.cookie(), {}, 'returns empty object'); 152 }); 153 154 test('Call to read all with json: true', function () { 155 $.cookie.json = true; 156 $.cookie('c', { foo: 'bar' }); 157 deepEqual($.cookie(), { c: { foo: 'bar' } }, 'returns JSON parsed cookies'); 158 }); 159 160 test('Call to read all with a badly encoded cookie', function () { 161 expect(1); 162 document.cookie = 'bad=foo%'; 163 document.cookie = 'good=foo'; 164 deepEqual($.cookie(), { good: 'foo' }, 'returns object containing all decodable cookies'); 165 // Delete manually here because it requires raw === true... 166 $.cookie.raw = true; 167 $.removeCookie('bad'); 168 }); 169 170 171 module('write', lifecycle); 172 173 test('String primitive', function () { 174 expect(1); 175 $.cookie('c', 'v'); 176 strictEqual($.cookie('c'), 'v', 'should write value'); 177 }); 178 179 test('String object', function () { 180 expect(1); 181 $.cookie('c', new String('v')); 182 strictEqual($.cookie('c'), 'v', 'should write value'); 183 }); 184 185 test('value "[object Object]"', function () { 186 expect(1); 187 $.cookie('c', '[object Object]'); 188 strictEqual($.cookie('c'), '[object Object]', 'should write value'); 189 }); 190 191 test('number', function () { 192 expect(1); 193 $.cookie('c', 1234); 194 strictEqual($.cookie('c'), '1234', 'should write value'); 195 }); 196 197 test('null', function () { 198 expect(1); 199 $.cookie('c', null); 200 strictEqual($.cookie('c'), 'null', 'should write value'); 201 }); 202 203 test('undefined', function () { 204 expect(1); 205 $.cookie('c', undefined); 206 strictEqual($.cookie('c'), 'undefined', 'should write value'); 207 }); 208 209 test('expires option as days from now', function () { 210 expect(1); 211 var sevenDaysFromNow = new Date(); 212 sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 21); 213 strictEqual($.cookie('c', 'v', { expires: 21 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(), 214 'should write the cookie string with expires'); 215 }); 216 217 test('expires option as fraction of a day', function () { 218 expect(1); 219 220 var now = new Date().getTime(); 221 var expires = Date.parse($.cookie('c', 'v', { expires: 0.5 }).replace(/.+expires=/, '')); 222 223 // When we were using Date.setDate() fractions have been ignored 224 // and expires resulted in the current date. Allow 1000 milliseconds 225 // difference for execution time. 226 ok(expires > now + 1000, 'should write expires attribute with the correct date'); 227 }); 228 229 test('expires option as Date instance', function () { 230 expect(1); 231 var sevenDaysFromNow = new Date(); 232 sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7); 233 strictEqual($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(), 234 'should write the cookie string with expires'); 235 }); 236 237 test('return value', function () { 238 expect(1); 239 strictEqual($.cookie('c', 'v'), 'c=v', 'should return written cookie string'); 240 }); 241 242 test('defaults', function () { 243 expect(2); 244 $.cookie.defaults.path = '/foo'; 245 ok($.cookie('c', 'v').match(/path=\/foo/), 'should use options from defaults'); 246 ok($.cookie('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'options argument has precedence'); 247 }); 248 249 test('raw = true', function () { 250 expect(1); 251 $.cookie.raw = true; 252 strictEqual($.cookie('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode'); 253 // Delete manually here because it requires raw === true... 254 $.removeCookie('c[1]'); 255 }); 256 257 test('json = true', function () { 258 expect(1); 259 $.cookie.json = true; 260 261 if ('JSON' in window) { 262 $.cookie('c', { foo: 'bar' }); 263 strictEqual(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON'); 264 } else { 265 ok(true); 266 } 267 }); 268 269 270 module('removeCookie', lifecycle); 271 272 test('deletion', function () { 273 expect(1); 274 $.cookie('c', 'v'); 275 $.removeCookie('c'); 276 strictEqual(document.cookie, '', 'should delete the cookie'); 277 }); 278 279 test('when sucessfully deleted', function () { 280 expect(1); 281 $.cookie('c', 'v'); 282 strictEqual($.removeCookie('c'), true, 'returns true'); 283 }); 284 285 test('when cookie does not exist', function () { 286 expect(1); 287 strictEqual($.removeCookie('c'), true, 'returns true'); 288 }); 289 290 test('when deletion failed', function () { 291 expect(1); 292 $.cookie('c', 'v'); 293 294 var originalCookie = $.cookie; 295 $.cookie = function () { 296 // Stub deletion... 297 if (arguments.length === 1) { 298 return originalCookie.apply(null, arguments); 299 } 300 }; 301 302 strictEqual($.removeCookie('c'), false, 'returns false'); 303 304 $.cookie = originalCookie; 305 }); 306 307 test('with options', function () { 308 expect(1); 309 var options = { path: '/' }; 310 $.cookie('c', 'v', options); 311 $.removeCookie('c', options); 312 strictEqual(document.cookie, '', 'should delete the cookie'); 313 }); 314 315 test('passing options reference', function () { 316 expect(1); 317 var options = { path: '/' }; 318 $.cookie('c', 'v', options); 319 $.removeCookie('c', options); 320 deepEqual(options, { path: '/' }, "won't alter options object"); 321 }); 322 323 test('[] used in name', function () { 324 expect(1); 325 $.cookie.raw = true; 326 document.cookie = 'c[1]=foo'; 327 $.removeCookie('c[1]'); 328 strictEqual(document.cookie, '', 'delete the cookie'); 329 }); 330 331 332 module('conversion', lifecycle); 333 334 test('read converter', function() { 335 expect(1); 336 $.cookie('c', '1'); 337 strictEqual($.cookie('c', Number), 1, 'converts read value'); 338 }); 339 340 test('read converter with raw = true', function() { 341 expect(1); 342 $.cookie.raw = true; 343 $.cookie('c', '1'); 344 strictEqual($.cookie('c', Number), 1, 'does not decode, but converts read value'); 345 }); 346