|
| 1 | +module('encoding', lifecycle); |
| 2 | + |
| 3 | +test('Handling quotes in the cookie value for read and write', function () { |
| 4 | + expect(3); |
| 5 | + |
| 6 | + Cookies.set('quote', '"'); |
| 7 | + strictEqual(Cookies.get('quote'), '"', 'should print the quote character'); |
| 8 | + |
| 9 | + Cookies.set('without-last', '"content'); |
| 10 | + strictEqual(Cookies.get('without-last'), '"content', 'should print the quote character'); |
| 11 | + |
| 12 | + Cookies.set('without-first', 'content"'); |
| 13 | + strictEqual(Cookies.get('without-first'), 'content"', 'should print the quote character'); |
| 14 | +}); |
| 15 | + |
| 16 | +test('RFC 6265 - reading cookie-octet enclosed in DQUOTE', function () { |
| 17 | + expect(1); |
| 18 | + document.cookie = 'c="v"'; |
| 19 | + strictEqual(Cookies.get('c'), 'v', 'should decode the quotes'); |
| 20 | +}); |
| 21 | + |
| 22 | +test('RFC 6265 - unallowed characters in cookie value', function () { |
| 23 | + expect(9); |
| 24 | + |
| 25 | + Cookies.set('whitespace', ' '); |
| 26 | + strictEqual(Cookies.get('whitespace'), ' ', 'should handle the whitespace character'); |
| 27 | + strictEqual(document.cookie, 'whitespace=%20', 'whitespace is not allowed, need to encode'); |
| 28 | + Cookies.remove('whitespace'); |
| 29 | + |
| 30 | + Cookies.set('comma', ','); |
| 31 | + strictEqual(Cookies.get('comma'), ',', 'should handle the comma character'); |
| 32 | + strictEqual(document.cookie, 'comma=%2C', 'comma is not allowed, need to encode'); |
| 33 | + Cookies.remove('comma'); |
| 34 | + |
| 35 | + Cookies.set('semicolon', ';'); |
| 36 | + strictEqual(Cookies.get('semicolon'), ';', 'should handle the semicolon character'); |
| 37 | + strictEqual(document.cookie, 'semicolon=%3B', 'semicolon is not allowed, need to encode'); |
| 38 | + Cookies.remove('semicolon'); |
| 39 | + |
| 40 | + Cookies.set('backslash', '\\'); |
| 41 | + strictEqual(Cookies.get('backslash'), '\\', 'should handle the backslash character'); |
| 42 | + strictEqual(document.cookie, 'backslash=%5C', 'backslash is not allowed, need to encode'); |
| 43 | + Cookies.remove('backslash'); |
| 44 | + |
| 45 | + Cookies.set('multiple', '" ,;\\" ,;\\'); |
| 46 | + strictEqual(Cookies.get('multiple'), '" ,;\\" ,;\\', 'should handle multiple special characters'); |
| 47 | + Cookies.remove('multiple'); |
| 48 | +}); |
| 49 | + |
| 50 | +test('RFC 6265 - sharp is allowed in cookie value', function () { |
| 51 | + expect(2); |
| 52 | + Cookies.set('c', '#'); |
| 53 | + strictEqual(Cookies.get('c'), '#', 'should handle the sharp character'); |
| 54 | + strictEqual(document.cookie, 'c=#', 'sharp is allowed, should not encode'); |
| 55 | +}); |
| 56 | + |
| 57 | +test('RFC 6265 - dollar sign is allowed in cookie value', function () { |
| 58 | + expect(2); |
| 59 | + Cookies.set('c', '$'); |
| 60 | + strictEqual(Cookies.get('c'), '$', 'should handle the dollar sign character'); |
| 61 | + strictEqual(document.cookie, 'c=$', 'dollar sign is allowed, should not encode'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('RFC 6265 - percent is allowed in cookie value', function () { |
| 65 | + expect(2); |
| 66 | + Cookies.set('c', '%'); |
| 67 | + strictEqual(Cookies.get('c'), '%', 'should handle the percent character'); |
| 68 | + strictEqual(document.cookie, 'c=%25', 'percent is allowed, but encode to escape'); |
| 69 | +}); |
| 70 | + |
| 71 | +test('RFC 6265 - ampersand is allowed in cookie value', function () { |
| 72 | + expect(2); |
| 73 | + Cookies.set('c', '&'); |
| 74 | + strictEqual(Cookies.get('c'), '&', 'should handle the ampersand character'); |
| 75 | + strictEqual(document.cookie, 'c=&', 'ampersand is allowed, should not encode'); |
| 76 | +}); |
| 77 | + |
| 78 | +// github.com/carhartl/jquery-cookie/pull/62 |
| 79 | +test('RFC 6265 - plus is allowed in cookie value', function () { |
| 80 | + expect(2); |
| 81 | + Cookies.set('c', '+'); |
| 82 | + strictEqual(Cookies.get('c'), '+', 'should handle the plus character'); |
| 83 | + strictEqual(document.cookie, 'c=+', 'plus is allowed, should not encode'); |
| 84 | +}); |
| 85 | + |
| 86 | +test('RFC 6265 - colon is allowed in cookie value', function () { |
| 87 | + expect(2); |
| 88 | + Cookies.set('c', ':'); |
| 89 | + strictEqual(Cookies.get('c'), ':', 'should handle the colon character'); |
| 90 | + strictEqual(document.cookie, 'c=:', 'colon is allowed, should not encode'); |
| 91 | +}); |
| 92 | + |
| 93 | +test('RFC 6265 - less-than is allowed in cookie value', function () { |
| 94 | + expect(2); |
| 95 | + Cookies.set('c', '<'); |
| 96 | + strictEqual(Cookies.get('c'), '<', 'should handle the less-than character'); |
| 97 | + strictEqual(document.cookie, 'c=<', 'less-than is allowed, should not encode'); |
| 98 | +}); |
| 99 | + |
| 100 | +test('RFC 6265 - greater-than is allowed in cookie value', function () { |
| 101 | + expect(2); |
| 102 | + Cookies.set('c', '>'); |
| 103 | + strictEqual(Cookies.get('c'), '>', 'should handle the greater-than character'); |
| 104 | + strictEqual(document.cookie, 'c=>', 'greater-than is allowed, should not encode'); |
| 105 | +}); |
| 106 | + |
| 107 | +test('RFC 6265 - equal sign is allowed in cookie value', function () { |
| 108 | + expect(2); |
| 109 | + Cookies.set('c', '='); |
| 110 | + strictEqual(Cookies.get('c'), '=', 'should handle the equal sign character'); |
| 111 | + strictEqual(document.cookie, 'c==', 'equal sign is allowed, should not encode'); |
| 112 | +}); |
| 113 | + |
| 114 | +test('RFC 6265 - slash is allowed in cookie value', function () { |
| 115 | + expect(2); |
| 116 | + Cookies.set('c', '/'); |
| 117 | + strictEqual(Cookies.get('c'), '/', 'should handle the slash character'); |
| 118 | + strictEqual(document.cookie, 'c=/', 'slash is allowed, should not encode'); |
| 119 | +}); |
| 120 | + |
| 121 | +test('RFC 6265 - question mark is allowed in cookie value', function () { |
| 122 | + expect(2); |
| 123 | + Cookies.set('c', '?'); |
| 124 | + strictEqual(Cookies.get('c'), '?', 'should handle the question mark character'); |
| 125 | + strictEqual(document.cookie, 'c=?', 'question mark is allowed, should not encode'); |
| 126 | +}); |
| 127 | + |
| 128 | +test('RFC 6265 - at is allowed in cookie value', function () { |
| 129 | + expect(2); |
| 130 | + Cookies.set('c', '@'); |
| 131 | + strictEqual(Cookies.get('c'), '@', 'should handle the at character'); |
| 132 | + strictEqual(document.cookie, 'c=@', 'at is allowed, should not encode'); |
| 133 | +}); |
| 134 | + |
| 135 | +test('RFC 6265 - opening square bracket is allowed in cookie value', function () { |
| 136 | + expect(2); |
| 137 | + Cookies.set('c', '['); |
| 138 | + strictEqual(Cookies.get('c'), '[', 'should handle the opening square bracket character'); |
| 139 | + strictEqual(document.cookie, 'c=[', 'opening square bracket is allowed, should not encode'); |
| 140 | +}); |
| 141 | + |
| 142 | +test('RFC 6265 - closing square bracket is allowed in cookie value', function () { |
| 143 | + expect(2); |
| 144 | + Cookies.set('c', ']'); |
| 145 | + strictEqual(Cookies.get('c'), ']', 'should handle the closing square bracket character'); |
| 146 | + strictEqual(document.cookie, 'c=]', 'closing square bracket is allowed, should not encode'); |
| 147 | +}); |
| 148 | + |
| 149 | +test('RFC 6265 - caret is allowed in cookie value', function () { |
| 150 | + expect(2); |
| 151 | + Cookies.set('c', '^'); |
| 152 | + strictEqual(Cookies.get('c'), '^', 'should handle the caret character'); |
| 153 | + strictEqual(document.cookie, 'c=^', 'caret is allowed, should not encode'); |
| 154 | +}); |
| 155 | + |
| 156 | +test('RFC 6265 - grave accent is allowed in cookie value', function () { |
| 157 | + expect(2); |
| 158 | + Cookies.set('c', '`'); |
| 159 | + strictEqual(Cookies.get('c'), '`', 'should handle the grave accent character'); |
| 160 | + strictEqual(document.cookie, 'c=`', 'grave accent is allowed, should not encode'); |
| 161 | +}); |
| 162 | + |
| 163 | +test('RFC 6265 - opening curly bracket is allowed in cookie value', function () { |
| 164 | + expect(2); |
| 165 | + Cookies.set('c', '{'); |
| 166 | + strictEqual(Cookies.get('c'), '{', 'should handle the opening curly bracket character'); |
| 167 | + strictEqual(document.cookie, 'c={', 'opening curly bracket is allowed, should not encode'); |
| 168 | +}); |
| 169 | + |
| 170 | +test('RFC 6265 - closing curly bracket is allowed in cookie value', function () { |
| 171 | + expect(2); |
| 172 | + Cookies.set('c', '}'); |
| 173 | + strictEqual(Cookies.get('c'), '}', 'should handle the closing curly bracket character'); |
| 174 | + strictEqual(document.cookie, 'c=}', 'closing curly bracket is allowed, should not encode'); |
| 175 | +}); |
| 176 | + |
| 177 | +test('RFC 6265 - pipe is allowed in cookie value', function () { |
| 178 | + expect(2); |
| 179 | + Cookies.set('c', '|'); |
| 180 | + strictEqual(Cookies.get('c'), '|', 'should handle the pipe character'); |
| 181 | + strictEqual(document.cookie, 'c=|', 'pipe is allowed, should not encode'); |
| 182 | +}); |
| 183 | + |
| 184 | +test('RFC 6265 - unallowed characters in cookie-name', function () { |
| 185 | + expect(38); |
| 186 | + |
| 187 | + Cookies.set('(', 'v'); |
| 188 | + strictEqual(Cookies.get('('), 'v', 'should handle the opening parens character'); |
| 189 | + strictEqual(document.cookie, '%28=v', 'opening parens is not allowed, need to encode'); |
| 190 | + Cookies.remove('('); |
| 191 | + |
| 192 | + Cookies.set(')', 'v'); |
| 193 | + strictEqual(Cookies.get(')'), 'v', 'should handle the closing parens character'); |
| 194 | + strictEqual(document.cookie, '%29=v', 'closing parens is not allowed, need to encode'); |
| 195 | + Cookies.remove(')'); |
| 196 | + |
| 197 | + Cookies.set('<', 'v'); |
| 198 | + strictEqual(Cookies.get('<'), 'v', 'should handle the less-than character'); |
| 199 | + strictEqual(document.cookie, '%3C=v', 'less-than is not allowed, need to encode'); |
| 200 | + Cookies.remove('<'); |
| 201 | + |
| 202 | + Cookies.set('>', 'v'); |
| 203 | + strictEqual(Cookies.get('>'), 'v', 'should handle the greater-than character'); |
| 204 | + strictEqual(document.cookie, '%3E=v', 'greater-than is not allowed, need to encode'); |
| 205 | + Cookies.remove('>'); |
| 206 | + |
| 207 | + Cookies.set('@', 'v'); |
| 208 | + strictEqual(Cookies.get('@'), 'v', 'should handle the at character'); |
| 209 | + strictEqual(document.cookie, '%40=v', 'at is not allowed, need to encode'); |
| 210 | + Cookies.remove('@'); |
| 211 | + |
| 212 | + Cookies.set(',', 'v'); |
| 213 | + strictEqual(Cookies.get(','), 'v', 'should handle the comma character'); |
| 214 | + strictEqual(document.cookie, '%2C=v', 'comma is not allowed, need to encode'); |
| 215 | + Cookies.remove(','); |
| 216 | + |
| 217 | + Cookies.set(';', 'v'); |
| 218 | + strictEqual(Cookies.get(';'), 'v', 'should handle the semicolon character'); |
| 219 | + strictEqual(document.cookie, '%3B=v', 'semicolon is not allowed, need to encode'); |
| 220 | + Cookies.remove(';'); |
| 221 | + |
| 222 | + Cookies.set(':', 'v'); |
| 223 | + strictEqual(Cookies.get(':'), 'v', 'should handle the colon character'); |
| 224 | + strictEqual(document.cookie, '%3A=v', 'colon is not allowed, need to encode'); |
| 225 | + Cookies.remove(':'); |
| 226 | + |
| 227 | + Cookies.set('\\', 'v'); |
| 228 | + strictEqual(Cookies.get('\\'), 'v', 'should handle the backslash character'); |
| 229 | + strictEqual(document.cookie, '%5C=v', 'backslash is not allowed, need to encode'); |
| 230 | + Cookies.remove('\\'); |
| 231 | + |
| 232 | + Cookies.set('"', 'v'); |
| 233 | + strictEqual(Cookies.get('"'), 'v', 'should handle the double quote character'); |
| 234 | + strictEqual(document.cookie, '%22=v', 'double quote is not allowed, need to encode'); |
| 235 | + Cookies.remove('"'); |
| 236 | + |
| 237 | + Cookies.set('/', 'v'); |
| 238 | + strictEqual(Cookies.get('/'), 'v', 'should handle the slash character'); |
| 239 | + strictEqual(document.cookie, '%2F=v', 'slash is not allowed, need to encode'); |
| 240 | + Cookies.remove('/'); |
| 241 | + |
| 242 | + Cookies.set('[', 'v'); |
| 243 | + strictEqual(Cookies.get('['), 'v', 'should handle the opening square brackets character'); |
| 244 | + strictEqual(document.cookie, '%5B=v', 'opening square brackets is not allowed, need to encode'); |
| 245 | + Cookies.remove('['); |
| 246 | + |
| 247 | + Cookies.set(']', 'v'); |
| 248 | + strictEqual(Cookies.get(']'), 'v', 'should handle the closing square brackets character'); |
| 249 | + strictEqual(document.cookie, '%5D=v', 'closing square brackets is not allowed, need to encode'); |
| 250 | + Cookies.remove(']'); |
| 251 | + |
| 252 | + Cookies.set('?', 'v'); |
| 253 | + strictEqual(Cookies.get('?'), 'v', 'should handle the question mark character'); |
| 254 | + strictEqual(document.cookie, '%3F=v', 'question mark is not allowed, need to encode'); |
| 255 | + Cookies.remove('?'); |
| 256 | + |
| 257 | + Cookies.set('=', 'v'); |
| 258 | + strictEqual(Cookies.get('='), 'v', 'should handle the equal sign character'); |
| 259 | + strictEqual(document.cookie, '%3D=v', 'equal sign is not allowed, need to encode'); |
| 260 | + Cookies.remove('='); |
| 261 | + |
| 262 | + Cookies.set('{', 'v'); |
| 263 | + strictEqual(Cookies.get('{'), 'v', 'should handle the opening curly brackets character'); |
| 264 | + strictEqual(document.cookie, '%7B=v', 'opening curly brackets is not allowed, need to encode'); |
| 265 | + Cookies.remove('{'); |
| 266 | + |
| 267 | + Cookies.set('}', 'v'); |
| 268 | + strictEqual(Cookies.get('}'), 'v', 'should handle the closing curly brackets character'); |
| 269 | + strictEqual(document.cookie, '%7D=v', 'closing curly brackets is not allowed, need to encode'); |
| 270 | + Cookies.remove('}'); |
| 271 | + |
| 272 | + Cookies.set(' ', 'v'); |
| 273 | + strictEqual(Cookies.get(' '), 'v', 'should handle the horizontal tab character'); |
| 274 | + strictEqual(document.cookie, '%09=v', 'horizontal tab is not allowed, need to encode'); |
| 275 | + Cookies.remove(' '); |
| 276 | + |
| 277 | + Cookies.set(' ', 'v'); |
| 278 | + strictEqual(Cookies.get(' '), 'v', 'should handle whitespace character'); |
| 279 | + strictEqual(document.cookie, '%20=v', 'whitespace is not allowed, need to encode'); |
| 280 | + Cookies.remove(' '); |
| 281 | +}); |
| 282 | + |
| 283 | +test('cookie-name - 2 bytes characters', function () { |
| 284 | + expect(2); |
| 285 | + |
| 286 | + Cookies.set('ã', 'v'); |
| 287 | + strictEqual(Cookies.get('ã'), 'v', 'should handle the ã character'); |
| 288 | + strictEqual(document.cookie, '%C3%A3=v', 'should encode the ã character'); |
| 289 | + Cookies.remove('ã'); |
| 290 | +}); |
| 291 | + |
| 292 | +test('cookie-name - 3 bytes characters', function () { |
| 293 | + expect(2); |
| 294 | + |
| 295 | + Cookies.set('₯', 'v'); |
| 296 | + strictEqual(Cookies.get('₯'), 'v', 'should handle the ₯ character'); |
| 297 | + strictEqual(document.cookie, '%E2%82%AF=v', 'should encode the ₯ character'); |
| 298 | + Cookies.remove('₯'); |
| 299 | +}); |
| 300 | + |
| 301 | +test('cookie-name - 4 bytes characters', function () { |
| 302 | + expect(2); |
| 303 | + |
| 304 | + Cookies.set('𩸽', 'v'); |
| 305 | + strictEqual(Cookies.get('𩸽'), 'v', 'should_handle the 𩸽 character'); |
| 306 | + strictEqual(document.cookie, '%F0%A9%B8%BD=v', 'should encode the 𩸽 character'); |
| 307 | + Cookies.remove('𩸽'); |
| 308 | +}); |
| 309 | + |
| 310 | +test('cookie-value - 2 bytes characters', function () { |
| 311 | + expect(2); |
| 312 | + |
| 313 | + Cookies.set('c', 'ã'); |
| 314 | + strictEqual(Cookies.get('c'), 'ã', 'should handle the ã character'); |
| 315 | + strictEqual(document.cookie, 'c=%C3%A3', 'should encode the ã character'); |
| 316 | + Cookies.remove('c'); |
| 317 | +}); |
| 318 | + |
| 319 | +test('cookie-value - 3 bytes characters', function () { |
| 320 | + expect(2); |
| 321 | + |
| 322 | + Cookies.set('c', '₯'); |
| 323 | + strictEqual(Cookies.get('c'), '₯', 'should handle the ₯ character'); |
| 324 | + strictEqual(document.cookie, 'c=%E2%82%AF', 'should encode the ₯ character'); |
| 325 | + Cookies.remove('c'); |
| 326 | +}); |
| 327 | + |
| 328 | +test('cookie-value - 4 bytes characters', function () { |
| 329 | + expect(2); |
| 330 | + |
| 331 | + Cookies.set('c', '𩸽'); |
| 332 | + strictEqual(Cookies.get('c'), '𩸽', 'should handle the 𩸽 character'); |
| 333 | + strictEqual(document.cookie, 'c=%F0%A9%B8%BD', 'should encode the 𩸽 character'); |
| 334 | + Cookies.remove('c'); |
| 335 | +}); |
0 commit comments