react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / test / parsing_test.js
81146 views/*!1* Copyright (c) 2015, Salesforce.com, Inc.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions are met:6*7* 1. Redistributions of source code must retain the above copyright notice,8* this list of conditions and the following disclaimer.9*10* 2. Redistributions in binary form must reproduce the above copyright notice,11* this list of conditions and the following disclaimer in the documentation12* and/or other materials provided with the distribution.13*14* 3. Neither the name of Salesforce.com nor the names of its contributors may15* be used to endorse or promote products derived from this software without16* specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE22* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/3031'use strict';32var vows = require('vows');33var assert = require('assert');34var tough = require('../lib/cookie');35var Cookie = tough.Cookie;3637vows38.describe('Parsing')39.addBatch({40"simple": {41topic: function() {42return Cookie.parse('a=bcd') || null;43},44"parsed": function(c) { assert.ok(c) },45"key": function(c) { assert.equal(c.key, 'a') },46"value": function(c) { assert.equal(c.value, 'bcd') },47"no path": function(c) { assert.equal(c.path, null) },48"no domain": function(c) { assert.equal(c.domain, null) },49"no extensions": function(c) { assert.ok(!c.extensions) }50},51"with expiry": {52topic: function() {53return Cookie.parse('a=bcd; Expires=Tue, 18 Oct 2011 07:05:03 GMT') || null;54},55"parsed": function(c) { assert.ok(c) },56"key": function(c) { assert.equal(c.key, 'a') },57"value": function(c) { assert.equal(c.value, 'bcd') },58"has expires": function(c) {59assert.ok(c.expires !== Infinity, 'expiry is infinite when it shouldn\'t be');60assert.equal(c.expires.getTime(), 1318921503000);61}62},63"with expiry and path": {64topic: function() {65return Cookie.parse('abc="xyzzy!"; Expires=Tue, 18 Oct 2011 07:05:03 GMT; Path=/aBc') || null;66},67"parsed": function(c) { assert.ok(c) },68"key": function(c) { assert.equal(c.key, 'abc') },69"value": function(c) { assert.equal(c.value, '"xyzzy!"') },70"has expires": function(c) {71assert.ok(c.expires !== Infinity, 'expiry is infinite when it shouldn\'t be');72assert.equal(c.expires.getTime(), 1318921503000);73},74"has path": function(c) { assert.equal(c.path, '/aBc'); },75"no httponly or secure": function(c) {76assert.ok(!c.httpOnly);77assert.ok(!c.secure);78}79},80"with everything": {81topic: function() {82return Cookie.parse('abc="xyzzy!"; Expires=Tue, 18 Oct 2011 07:05:03 GMT; Path=/aBc; Domain=example.com; Secure; HTTPOnly; Max-Age=1234; Foo=Bar; Baz') || null;83},84"parsed": function(c) { assert.ok(c) },85"key": function(c) { assert.equal(c.key, 'abc') },86"value": function(c) { assert.equal(c.value, '"xyzzy!"') },87"has expires": function(c) {88assert.ok(c.expires !== Infinity, 'expiry is infinite when it shouldn\'t be');89assert.equal(c.expires.getTime(), 1318921503000);90},91"has path": function(c) { assert.equal(c.path, '/aBc'); },92"has domain": function(c) { assert.equal(c.domain, 'example.com'); },93"has httponly": function(c) { assert.equal(c.httpOnly, true); },94"has secure": function(c) { assert.equal(c.secure, true); },95"has max-age": function(c) { assert.equal(c.maxAge, 1234); },96"has extensions": function(c) {97assert.ok(c.extensions);98assert.equal(c.extensions[0], 'Foo=Bar');99assert.equal(c.extensions[1], 'Baz');100}101},102"invalid expires": function() {103var c = Cookie.parse("a=b; Expires=xyzzy");104assert.ok(c);105assert.equal(c.expires, Infinity);106},107"zero max-age": function() {108var c = Cookie.parse("a=b; Max-Age=0");109assert.ok(c);110assert.equal(c.maxAge, 0);111},112"negative max-age": function() {113var c = Cookie.parse("a=b; Max-Age=-1");114assert.ok(c);115assert.equal(c.maxAge, -1);116},117"empty domain": function() {118var c = Cookie.parse("a=b; domain=");119assert.ok(c);120assert.equal(c.domain, null);121},122"dot domain": function() {123var c = Cookie.parse("a=b; domain=.");124assert.ok(c);125assert.equal(c.domain, null);126},127"uppercase domain": function() {128var c = Cookie.parse("a=b; domain=EXAMPLE.COM");129assert.ok(c);130assert.equal(c.domain, 'example.com');131},132"trailing dot in domain": {133topic: function() {134return Cookie.parse("a=b; Domain=example.com.", true) || null;135},136"has the domain": function(c) { assert.equal(c.domain,"example.com.") },137"but doesn't validate": function(c) { assert.equal(c.validate(),false) }138},139"empty path": function() {140var c = Cookie.parse("a=b; path=");141assert.ok(c);142assert.equal(c.path, null);143},144"no-slash path": function() {145var c = Cookie.parse("a=b; path=xyzzy");146assert.ok(c);147assert.equal(c.path, null);148},149"trailing semi-colons after path": {150topic: function () {151return [152"a=b; path=/;",153"c=d;;;;"154];155},156"strips semi-colons": function (t) {157var c1 = Cookie.parse(t[0]);158var c2 = Cookie.parse(t[1]);159assert.ok(c1);160assert.ok(c2);161assert.equal(c1.path, '/');162}163},164"secure-with-value": function() {165var c = Cookie.parse("a=b; Secure=xyzzy");166assert.ok(c);167assert.equal(c.secure, true);168},169"httponly-with-value": function() {170var c = Cookie.parse("a=b; HttpOnly=xyzzy");171assert.ok(c);172assert.equal(c.httpOnly, true);173},174"garbage": {175topic: function() {176return Cookie.parse("\x08", true) || null;177},178"doesn't parse": function(c) { assert.equal(c,null) }179},180"public suffix domain": {181topic: function() {182return Cookie.parse("a=b; domain=kyoto.jp", true) || null;183},184"parses fine": function(c) {185assert.ok(c);186assert.equal(c.domain, 'kyoto.jp');187},188"but fails validation": function(c) {189assert.ok(c);190assert.ok(!c.validate());191}192},193"public suffix foonet.net": {194"top level": {195topic: function() {196return Cookie.parse("a=b; domain=foonet.net") || null;197},198"parses and is valid": function(c) {199assert.ok(c);200assert.equal(c.domain, 'foonet.net');201assert.ok(c.validate());202}203},204"www": {205topic: function() {206return Cookie.parse("a=b; domain=www.foonet.net") || null;207},208"parses and is valid": function(c) {209assert.ok(c);210assert.equal(c.domain, 'www.foonet.net');211assert.ok(c.validate());212}213},214"with a dot": {215topic: function() {216return Cookie.parse("a=b; domain=.foonet.net") || null;217},218"parses and is valid": function(c) {219assert.ok(c);220assert.equal(c.domain, 'foonet.net');221assert.ok(c.validate());222}223}224},225"Ironically, Google 'GAPS' cookie has very little whitespace": {226topic: function() {227return Cookie.parse("GAPS=1:A1aaaaAaAAa1aaAaAaaAAAaaa1a11a:aaaAaAaAa-aaaA1-;Path=/;Expires=Thu, 17-Apr-2014 02:12:29 GMT;Secure;HttpOnly");228},229"parsed": function(c) { assert.ok(c) },230"key": function(c) { assert.equal(c.key, 'GAPS') },231"value": function(c) { assert.equal(c.value, '1:A1aaaaAaAAa1aaAaAaaAAAaaa1a11a:aaaAaAaAa-aaaA1-') },232"path": function(c) {233assert.notEqual(c.path, '/;Expires'); // BUG234assert.equal(c.path, '/');235},236"expires": function(c) {237assert.notEqual(c.expires, Infinity);238assert.equal(c.expires.getTime(), 1397700749000);239},240"secure": function(c) { assert.ok(c.secure) },241"httponly": function(c) { assert.ok(c.httpOnly) }242},243"lots of equal signs": {244topic: function() {245return Cookie.parse("queryPref=b=c&d=e; Path=/f=g; Expires=Thu, 17 Apr 2014 02:12:29 GMT; HttpOnly");246},247"parsed": function(c) { assert.ok(c) },248"key": function(c) { assert.equal(c.key, 'queryPref') },249"value": function(c) { assert.equal(c.value, 'b=c&d=e') },250"path": function(c) {251assert.equal(c.path, '/f=g');252},253"expires": function(c) {254assert.notEqual(c.expires, Infinity);255assert.equal(c.expires.getTime(), 1397700749000);256},257"httponly": function(c) { assert.ok(c.httpOnly) }258},259"spaces in value": {260topic: function() {261return Cookie.parse('a=one two three',false) || null;262},263"parsed": function(c) { assert.ok(c) },264"key": function(c) { assert.equal(c.key, 'a') },265"value": function(c) { assert.equal(c.value, 'one two three') },266"no path": function(c) { assert.equal(c.path, null) },267"no domain": function(c) { assert.equal(c.domain, null) },268"no extensions": function(c) { assert.ok(!c.extensions) }269},270"quoted spaces in value": {271topic: function() {272return Cookie.parse('a="one two three"',false) || null;273},274"parsed": function(c) { assert.ok(c) },275"key": function(c) { assert.equal(c.key, 'a') },276"value": function(c) { assert.equal(c.value, '"one two three"') },277"no path": function(c) { assert.equal(c.path, null) },278"no domain": function(c) { assert.equal(c.domain, null) },279"no extensions": function(c) { assert.ok(!c.extensions) }280},281"non-ASCII in value": {282topic: function() {283return Cookie.parse('farbe=weiß',false) || null;284},285"parsed": function(c) { assert.ok(c) },286"key": function(c) { assert.equal(c.key, 'farbe') },287"value": function(c) { assert.equal(c.value, 'weiß') },288"no path": function(c) { assert.equal(c.path, null) },289"no domain": function(c) { assert.equal(c.domain, null) },290"no extensions": function(c) { assert.ok(!c.extensions) }291}292})293.export(module);294295296