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
|
var lifecycle = {
teardown: function () {
$.cookie.defaults = {};
delete $.cookie.raw;
delete $.cookie.json;
$.each($.cookie(), $.removeCookie);
}
};
module('read', lifecycle);
test('simple value', function () {
expect(1);
document.cookie = 'c=v';
strictEqual($.cookie('c'), 'v', 'should return value');
});
test('empty value', function () {
expect(1);
// IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, which
// resulted in a bug while reading such a cookie.
$.cookie('c', '');
strictEqual($.cookie('c'), '', 'should return value');
});
test('not existing', function () {
expect(1);
strictEqual($.cookie('whatever'), undefined, 'return undefined');
});
test('RFC 2068 quoted string', function () {
expect(1);
document.cookie = 'c="v@address.com\\"\\\\\\""';
strictEqual($.cookie('c'), 'v@address.com"\\"', 'should decode RFC 2068 quoted string');
});
test('decode', function () {
expect(1);
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
strictEqual($.cookie(' c'), ' v', 'should decode key and value');
});
test('decode pluses to space for server side written cookie', function () {
expect(1);
document.cookie = 'c=foo+bar';
strictEqual($.cookie('c'), 'foo bar', 'should convert pluses back to space');
});
test('raw = true', function () {
expect(2);
$.cookie.raw = true;
document.cookie = 'c=%20v';
strictEqual($.cookie('c'), '%20v', 'should not decode value');
// see https://github.com/carhartl/jquery-cookie/issues/50
$.cookie('c', 'foo=bar');
strictEqual($.cookie('c'), 'foo=bar', 'should include the entire value');
});
test('json = true', function () {
expect(1);
if ('JSON' in window) {
$.cookie.json = true;
$.cookie('c', { foo: 'bar' });
deepEqual($.cookie('c'), { foo: 'bar' }, 'should parse JSON');
} else {
ok(true);
}
});
test('not existing with json = true', function () {
expect(1);
if ('JSON' in window) {
$.cookie.json = true;
strictEqual($.cookie('whatever'), undefined, "won't throw exception");
} else {
ok(true);
}
});
test('string with json = true', function () {
expect(1);
if ('JSON' in window) {
$.cookie.json = true;
$.cookie('c', 'v');
strictEqual($.cookie('c'), 'v', 'should return value');
} else {
ok(true);
}
});
test('invalid JSON string with json = true', function () {
expect(1);
if ('JSON' in window) {
$.cookie('c', 'v');
$.cookie.json = true;
strictEqual($.cookie('c'), undefined, "won't throw exception, returns undefined");
} else {
ok(true);
}
});
test('invalid URL encoding', function () {
expect(1);
document.cookie = 'bad=foo%';
strictEqual($.cookie('bad'), undefined, "won't throw exception, returns undefined");
// Delete manually here because it requires raw === true...
$.cookie.raw = true;
$.removeCookie('bad');
});
asyncTest('malformed cookie value in IE (#88, #117)', function () {
expect(1);
// Sandbox in an iframe so that we can poke around with document.cookie.
var iframe = $('<iframe src="malformed_cookie.html"></iframe>')[0];
$(iframe).on('load', function () {
start();
if (iframe.contentWindow.ok) {
strictEqual(iframe.contentWindow.testValue, 'two', 'reads all cookie values, skipping duplicate occurences of "; "');
} else {
// Skip the test where we can't stub document.cookie using
// Object.defineProperty. Seems to work fine in
// Chrome, Firefox and IE 8+.
ok(true, 'N/A');
}
});
document.body.appendChild(iframe);
});
test('Call to read all when there are cookies', function () {
$.cookie('c', 'v');
$.cookie('foo', 'bar');
deepEqual($.cookie(), { c: 'v', foo: 'bar' }, 'returns object containing all cookies');
});
test('Call to read all when there are no cookies at all', function () {
deepEqual($.cookie(), {}, 'returns empty object');
});
test('Call to read all with json: true', function () {
$.cookie.json = true;
$.cookie('c', { foo: 'bar' });
deepEqual($.cookie(), { c: { foo: 'bar' } }, 'returns JSON parsed cookies');
});
test('Call to read all with a badly encoded cookie', function () {
expect(1);
document.cookie = 'bad=foo%';
document.cookie = 'good=foo';
deepEqual($.cookie(), { good: 'foo' }, 'returns object containing all decodable cookies');
// Delete manually here because it requires raw === true...
$.cookie.raw = true;
$.removeCookie('bad');
});
module('write', lifecycle);
test('String primitive', function () {
expect(1);
$.cookie('c', 'v');
strictEqual($.cookie('c'), 'v', 'should write value');
});
test('String object', function () {
expect(1);
$.cookie('c', new String('v'));
strictEqual($.cookie('c'), 'v', 'should write value');
});
test('value "[object Object]"', function () {
expect(1);
$.cookie('c', '[object Object]');
strictEqual($.cookie('c'), '[object Object]', 'should write value');
});
test('number', function () {
expect(1);
$.cookie('c', 1234);
strictEqual($.cookie('c'), '1234', 'should write value');
});
test('expires option as days from now', function () {
expect(1);
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
strictEqual($.cookie('c', 'v', { expires: 7 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
});
test('expires option as fraction of a day', function () {
expect(1);
var now = new Date().getTime();
var expires = Date.parse($.cookie('c', 'v', { expires: 0.5 }).replace(/.+expires=/, ''));
// When we were using Date.setDate() fractions have been ignored
// and expires resulted in the current date. Allow 1000 milliseconds
// difference for execution time.
ok(expires > now + 1000, 'should write expires attribute with the correct date');
});
test('expires option as Date instance', function () {
expect(1);
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
strictEqual($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
});
test('return value', function () {
expect(1);
strictEqual($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
});
test('defaults', function () {
expect(2);
$.cookie.defaults.path = '/foo';
ok($.cookie('c', 'v').match(/path=\/foo/), 'should use options from defaults');
ok($.cookie('c', 'v', { path: '/bar' }).match(/path=\/bar/), 'options argument has precedence');
});
test('raw = true', function () {
expect(1);
$.cookie.raw = true;
strictEqual($.cookie('c[1]', 'v[1]'), 'c[1]=v[1]', 'should not encode');
// Delete manually here because it requires raw === true...
$.removeCookie('c[1]');
});
test('json = true', function () {
expect(1);
$.cookie.json = true;
if ('JSON' in window) {
$.cookie('c', { foo: 'bar' });
strictEqual(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON');
} else {
ok(true);
}
});
module('removeCookie', lifecycle);
test('deletion', function () {
expect(1);
$.cookie('c', 'v');
$.removeCookie('c');
strictEqual(document.cookie, '', 'should delete the cookie');
});
test('when sucessfully deleted', function () {
expect(1);
$.cookie('c', 'v');
strictEqual($.removeCookie('c'), true, 'returns true');
});
test('when deletion failed', function () {
expect(1);
$.cookie('c', 'v');
var originalCookie = $.cookie;
$.cookie = function () {
// Stub deletion...
if (arguments.length === 1) {
return originalCookie.apply(null, arguments);
}
};
strictEqual($.removeCookie('c'), false, 'returns false');
$.cookie = originalCookie;
});
test('when cookie does not exist', function () {
expect(1);
strictEqual($.removeCookie('c'), false, 'returns false');
});
test('with options', function () {
expect(1);
var options = { path: '/' };
$.cookie('c', 'v', options);
$.removeCookie('c', options);
strictEqual(document.cookie, '', 'should delete the cookie');
});
test('passing options reference', function () {
expect(1);
var options = { path: '/' };
$.cookie('c', 'v', options);
$.removeCookie('c', options);
deepEqual(options, { path: '/' }, "won't alter options object");
});
test('[] used in name', function () {
expect(1);
$.cookie.raw = true;
document.cookie = 'c[1]=foo';
$.removeCookie('c[1]');
strictEqual(document.cookie, '', 'delete the cookie');
});
module('conversion', lifecycle);
test('read converter', function() {
expect(1);
$.cookie('c', '1');
strictEqual($.cookie('c', Number), 1, 'converts read value');
});
test('read converter with raw = true', function() {
expect(1);
$.cookie.raw = true;
$.cookie('c', '1');
strictEqual($.cookie('c', Number), 1, 'does not decode, but converts read value');
});
|