|
| 1 | +/** |
| 2 | + * This code was originally copied from the 'cookie` module at v0.5.0 and was simplified for our use case. |
| 3 | + * https://github.com/jshttp/cookie/blob/a0c84147aab6266bdb3996cf4062e93907c0b0fc/test/parse.js |
| 4 | + * It had the following license: |
| 5 | + * |
| 6 | + * (The MIT License) |
| 7 | + * |
| 8 | + * Copyright (c) 2012-2014 Roman Shtylman <[email protected]> |
| 9 | + * Copyright (c) 2015 Douglas Christopher Wilson <[email protected]> |
| 10 | + * |
| 11 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 12 | + * a copy of this software and associated documentation files (the |
| 13 | + * 'Software'), to deal in the Software without restriction, including |
| 14 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 15 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 16 | + * permit persons to whom the Software is furnished to do so, subject to |
| 17 | + * the following conditions: |
| 18 | + * |
| 19 | + * The above copyright notice and this permission notice shall be |
| 20 | + * included in all copies or substantial portions of the Software. |
| 21 | + * |
| 22 | + * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| 23 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 24 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 25 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 26 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 27 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 28 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | + */ |
| 30 | + |
| 31 | +import { parseCookie } from '../src/cookie'; |
| 32 | + |
| 33 | +describe('parseCookie(str)', function () { |
| 34 | + it('should parse cookie string to object', function () { |
| 35 | + expect(parseCookie('foo=bar')).toEqual({ foo: 'bar' }); |
| 36 | + expect(parseCookie('foo=123')).toEqual({ foo: '123' }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should ignore OWS', function () { |
| 40 | + expect(parseCookie('FOO = bar; baz = raz')).toEqual({ FOO: 'bar', baz: 'raz' }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should parse cookie with empty value', function () { |
| 44 | + expect(parseCookie('foo= ; bar=')).toEqual({ foo: '', bar: '' }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should URL-decode values', function () { |
| 48 | + expect(parseCookie('foo="bar=123456789&name=Magic+Mouse"')).toEqual({ foo: 'bar=123456789&name=Magic+Mouse' }); |
| 49 | + |
| 50 | + expect(parseCookie('email=%20%22%2c%3b%2f')).toEqual({ email: ' ",;/' }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return original value on escape error', function () { |
| 54 | + expect(parseCookie('foo=%1;bar=bar')).toEqual({ foo: '%1', bar: 'bar' }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should ignore cookies without value', function () { |
| 58 | + expect(parseCookie('foo=bar;fizz ; buzz')).toEqual({ foo: 'bar' }); |
| 59 | + expect(parseCookie(' fizz; foo= bar')).toEqual({ foo: 'bar' }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should ignore duplicate cookies', function () { |
| 63 | + expect(parseCookie('foo=%1;bar=bar;foo=boo')).toEqual({ foo: '%1', bar: 'bar' }); |
| 64 | + expect(parseCookie('foo=false;bar=bar;foo=tre')).toEqual({ foo: 'false', bar: 'bar' }); |
| 65 | + expect(parseCookie('foo=;bar=bar;foo=boo')).toEqual({ foo: '', bar: 'bar' }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments