forked from carhartl/jquery-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
199 lines (158 loc) · 5.56 KB
/
test.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
var before = {
setup: function () {
cookies = document.cookie.split('; ')
for (var i = 0, c; (c = (cookies)[i]) && (c = c.split('=')[0]); i++) {
document.cookie = c + '=; expires=' + new Date(0).toUTCString();
}
$.cookie.defaults = {};
delete $.cookie.raw;
delete $.cookie.json;
}
};
module('read', before);
test('simple value', 1, function () {
document.cookie = 'c=v';
equal($.cookie('c'), 'v', 'should return value');
});
test('empty value', 1, function () {
// 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', '');
equal($.cookie('c'), '', 'should return value');
});
test('not existing', 1, function () {
equal($.cookie('whatever'), null, 'should return null');
});
test('decode', 1, function () {
document.cookie = encodeURIComponent(' c') + '=' + encodeURIComponent(' v');
equal($.cookie(' c'), ' v', 'should decode key and value');
});
test('decode pluses to space for server side written cookie', 1, function () {
document.cookie = 'c=foo+bar'
equal($.cookie('c'), 'foo bar', 'should convert pluses back to space');
});
test('[] used in name', 1, function () {
document.cookie = 'c[999]=foo';
equal($.cookie('c[999]'), 'foo', 'should return value');
});
test('raw: true', 2, function () {
$.cookie.raw = true;
document.cookie = 'c=%20v';
equal($.cookie('c'), '%20v', 'should not decode value');
// see https://github.com/carhartl/jquery-cookie/issues/50
$.cookie('c', 'foo=bar');
equal($.cookie('c'), 'foo=bar', 'should include the entire value');
});
test('json: true', 1, function () {
$.cookie.json = true;
if ('JSON' in window) {
document.cookie = 'c=' + JSON.stringify({ foo: 'bar' });
deepEqual($.cookie('c'), { foo: 'bar'}, 'should parse JSON');
} else {
ok(true);
}
});
asyncTest('malformed cookie value in IE (#88, #117)', 1, function() {
// Sandbox in an iframe so that we can poke around with document.cookie.
var iframe = document.createElement('iframe');
iframe.onload = function() {
start();
if (iframe.contentWindow.ok) {
equal(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');
}
};
iframe.src = '/sandbox.html';
document.body.appendChild(iframe);
});
module('write', before);
test('String primitive', 1, function () {
$.cookie('c', 'v');
equal($.cookie('c'), 'v', 'should write value');
});
test('String object', 1, function () {
$.cookie('c', new String('v'));
equal($.cookie('c'), 'v', 'should write value');
});
test('value "[object Object]"', 1, function () {
$.cookie('c', '[object Object]');
equal($.cookie('c'), '[object Object]', 'should write value');
});
test('number', 1, function () {
$.cookie('c', 1234);
equal($.cookie('c'), '1234', 'should write value');
});
test('expires option as days from now', 1, function() {
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
equal($.cookie('c', 'v', { expires: 7 }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
});
test('expires option as Date instance', 1, function() {
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
equal($.cookie('c', 'v', { expires: sevenDaysFromNow }), 'c=v; expires=' + sevenDaysFromNow.toUTCString(),
'should write the cookie string with expires');
});
test('invalid expires option (in the past)', 1, function() {
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
$.cookie('c', 'v', { expires: yesterday });
equal($.cookie('c'), null, 'should not save already expired cookie');
});
test('return value', 1, function () {
equal($.cookie('c', 'v'), 'c=v', 'should return written cookie string');
});
test('defaults', 2, function () {
$.cookie.defaults.path = '/';
ok($.cookie('c', 'v').match(/path=\//), 'should use options from defaults');
ok($.cookie('c', 'v', { path: '/foo' }).match(/path=\/foo/), 'options argument has precedence');
});
test('raw: true', 1, function () {
$.cookie.raw = true;
equal($.cookie('c', ' v').split('=')[1], ' v', 'should not encode');
});
test('json: true', 1, function () {
$.cookie.json = true;
if ('JSON' in window) {
$.cookie('c', { foo: 'bar' });
equal(document.cookie, 'c=' + encodeURIComponent(JSON.stringify({ foo: 'bar' })), 'should stringify JSON');
} else {
ok(true);
}
});
module('delete', before);
test('delete (deprecated)', 1, function () {
document.cookie = 'c=v';
$.cookie('c', null);
equal(document.cookie, '', 'should delete the cookie');
});
module('removeCookie', before);
test('delete', 1, function() {
document.cookie = 'c=v';
$.removeCookie('c');
equal(document.cookie, '', 'should delete the cookie');
});
test('return', 2, function() {
equal($.removeCookie('c'), false, "should return false if a cookie wasn't found");
document.cookie = 'c=v';
equal($.removeCookie('c'), true, 'should return true if the cookie was found');
});
test('with options', 2, function() {
var oldCookie = $.cookie;
$.cookie = function(arg0, arg1, arg2) {
if (arg1 === null) {
equal(arg2.foo, 'bar', 'should pass options when deleting cookie');
} else {
// see https://github.com/carhartl/jquery-cookie/issues/99
equal(arguments.length, 1, "should look up cookie instead of writing a new");
}
};
document.cookie = 'c=v';
$.removeCookie('c', { foo: 'bar' });
$.cookie = oldCookie;
});