react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / test / cookie_jar_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*/30'use strict';31var vows = require('vows');32var assert = require('assert');33var async = require('async');34var tough = require('../lib/cookie');35var Cookie = tough.Cookie;36var CookieJar = tough.CookieJar;3738var atNow = Date.now();3940function at(offset) {41return {now: new Date(atNow + offset)};42}4344vows45.describe('CookieJar')46.addBatch({47"Setting a basic cookie": {48topic: function () {49var cj = new CookieJar();50var c = Cookie.parse("a=b; Domain=example.com; Path=/");51assert.strictEqual(c.hostOnly, null);52assert.instanceOf(c.creation, Date);53assert.strictEqual(c.lastAccessed, null);54c.creation = new Date(Date.now() - 10000);55cj.setCookie(c, 'http://example.com/index.html', this.callback);56},57"works": function (c) {58assert.instanceOf(c, Cookie)59}, // C is for Cookie, good enough for me60"gets timestamped": function (c) {61assert.ok(c.creation);62assert.ok(Date.now() - c.creation.getTime() < 5000); // recently stamped63assert.ok(c.lastAccessed);64assert.equal(c.creation, c.lastAccessed);65assert.equal(c.TTL(), Infinity);66assert.ok(!c.isPersistent());67}68},69"Setting a no-path cookie": {70topic: function () {71var cj = new CookieJar();72var c = Cookie.parse("a=b; Domain=example.com");73assert.strictEqual(c.hostOnly, null);74assert.instanceOf(c.creation, Date);75assert.strictEqual(c.lastAccessed, null);76c.creation = new Date(Date.now() - 10000);77cj.setCookie(c, 'http://example.com/index.html', this.callback);78},79"domain": function (c) {80assert.equal(c.domain, 'example.com')81},82"path is /": function (c) {83assert.equal(c.path, '/')84},85"path was derived": function (c) {86assert.strictEqual(c.pathIsDefault, true)87}88},89"Setting a cookie already marked as host-only": {90topic: function () {91var cj = new CookieJar();92var c = Cookie.parse("a=b; Domain=example.com");93assert.strictEqual(c.hostOnly, null);94assert.instanceOf(c.creation, Date);95assert.strictEqual(c.lastAccessed, null);96c.creation = new Date(Date.now() - 10000);97c.hostOnly = true;98cj.setCookie(c, 'http://example.com/index.html', this.callback);99},100"domain": function (c) {101assert.equal(c.domain, 'example.com')102},103"still hostOnly": function (c) {104assert.strictEqual(c.hostOnly, true)105}106},107"Setting a session cookie": {108topic: function () {109var cj = new CookieJar();110var c = Cookie.parse("a=b");111assert.strictEqual(c.path, null);112cj.setCookie(c, 'http://www.example.com/dir/index.html', this.callback);113},114"works": function (c) {115assert.instanceOf(c, Cookie)116},117"gets the domain": function (c) {118assert.equal(c.domain, 'www.example.com')119},120"gets the default path": function (c) {121assert.equal(c.path, '/dir')122},123"is 'hostOnly'": function (c) {124assert.ok(c.hostOnly)125}126},127"Setting wrong domain cookie": {128topic: function () {129var cj = new CookieJar();130var c = Cookie.parse("a=b; Domain=fooxample.com; Path=/");131cj.setCookie(c, 'http://example.com/index.html', this.callback);132},133"fails": function (err, c) {134assert.ok(err.message.match(/domain/i));135assert.ok(!c);136}137},138"Setting sub-domain cookie": {139topic: function () {140var cj = new CookieJar();141var c = Cookie.parse("a=b; Domain=www.example.com; Path=/");142cj.setCookie(c, 'http://example.com/index.html', this.callback);143},144"fails": function (err, c) {145assert.ok(err.message.match(/domain/i));146assert.ok(!c);147}148},149"Setting super-domain cookie": {150topic: function () {151var cj = new CookieJar();152var c = Cookie.parse("a=b; Domain=example.com; Path=/");153cj.setCookie(c, 'http://www.app.example.com/index.html', this.callback);154},155"success": function (err, c) {156assert.ok(!err);157assert.equal(c.domain, 'example.com');158}159},160"Setting a sub-path cookie on a super-domain": {161topic: function () {162var cj = new CookieJar();163var c = Cookie.parse("a=b; Domain=example.com; Path=/subpath");164assert.strictEqual(c.hostOnly, null);165assert.instanceOf(c.creation, Date);166assert.strictEqual(c.lastAccessed, null);167c.creation = new Date(Date.now() - 10000);168cj.setCookie(c, 'http://www.example.com/index.html', this.callback);169},170"domain is super-domain": function (c) {171assert.equal(c.domain, 'example.com')172},173"path is /subpath": function (c) {174assert.equal(c.path, '/subpath')175},176"path was NOT derived": function (c) {177assert.strictEqual(c.pathIsDefault, null)178}179},180"Setting HttpOnly cookie over non-HTTP API": {181topic: function () {182var cj = new CookieJar();183var c = Cookie.parse("a=b; Domain=example.com; Path=/; HttpOnly");184cj.setCookie(c, 'http://example.com/index.html', {http: false}, this.callback);185},186"fails": function (err, c) {187assert.match(err.message, /HttpOnly/i);188assert.ok(!c);189}190}191})192.addBatch({193"Store eight cookies": {194topic: function () {195var cj = new CookieJar();196var ex = 'http://example.com/index.html';197var tasks = [];198tasks.push(function (next) {199cj.setCookie('a=1; Domain=example.com; Path=/', ex, at(0), next);200});201tasks.push(function (next) {202cj.setCookie('b=2; Domain=example.com; Path=/; HttpOnly', ex, at(1000), next);203});204tasks.push(function (next) {205cj.setCookie('c=3; Domain=example.com; Path=/; Secure', ex, at(2000), next);206});207tasks.push(function (next) { // path208cj.setCookie('d=4; Domain=example.com; Path=/foo', ex, at(3000), next);209});210tasks.push(function (next) { // host only211cj.setCookie('e=5', ex, at(4000), next);212});213tasks.push(function (next) { // other domain214cj.setCookie('f=6; Domain=nodejs.org; Path=/', 'http://nodejs.org', at(5000), next);215});216tasks.push(function (next) { // expired217cj.setCookie('g=7; Domain=example.com; Path=/; Expires=Tue, 18 Oct 2011 00:00:00 GMT', ex, at(6000), next);218});219tasks.push(function (next) { // expired via Max-Age220cj.setCookie('h=8; Domain=example.com; Path=/; Max-Age=1', ex, next);221});222var cb = this.callback;223async.parallel(tasks, function (err, results) {224setTimeout(function () {225cb(err, cj, results);226}, 2000); // so that 'h=8' expires227});228},229"setup ok": function (err, cj, results) {230assert.ok(!err);231assert.ok(cj);232assert.ok(results);233},234"then retrieving for http://nodejs.org": {235topic: function (cj, oldResults) {236assert.ok(oldResults);237cj.getCookies('http://nodejs.org', this.callback);238},239"get a nodejs cookie": function (cookies) {240assert.lengthOf(cookies, 1);241var cookie = cookies[0];242assert.equal(cookie.domain, 'nodejs.org');243}244},245"then retrieving for https://example.com": {246topic: function (cj, oldResults) {247assert.ok(oldResults);248cj.getCookies('https://example.com', {secure: true}, this.callback);249},250"get a secure example cookie with others": function (cookies) {251var names = cookies.map(function (c) {252return c.key253});254assert.deepEqual(names, ['a', 'b', 'c', 'e']);255}256},257"then retrieving for https://example.com (missing options)": {258topic: function (cj, oldResults) {259assert.ok(oldResults);260cj.getCookies('https://example.com', this.callback);261},262"get a secure example cookie with others": function (cookies) {263var names = cookies.map(function (c) {264return c.key265});266assert.deepEqual(names, ['a', 'b', 'c', 'e']);267}268},269"then retrieving for http://example.com": {270topic: function (cj, oldResults) {271assert.ok(oldResults);272cj.getCookies('http://example.com', this.callback);273},274"get a bunch of cookies": function (cookies) {275var names = cookies.map(function (c) {276return c.key277});278assert.deepEqual(names, ['a', 'b', 'e']);279}280},281"then retrieving for http://EXAMPlE.com": {282topic: function (cj, oldResults) {283assert.ok(oldResults);284cj.getCookies('http://EXAMPlE.com', this.callback);285},286"get a bunch of cookies": function (cookies) {287var names = cookies.map(function (c) {288return c.key289});290assert.deepEqual(names, ['a', 'b', 'e']);291}292},293"then retrieving for http://example.com, non-HTTP": {294topic: function (cj, oldResults) {295assert.ok(oldResults);296cj.getCookies('http://example.com', {http: false}, this.callback);297},298"get a bunch of cookies": function (cookies) {299var names = cookies.map(function (c) {300return c.key301});302assert.deepEqual(names, ['a', 'e']);303}304},305"then retrieving for http://example.com/foo/bar": {306topic: function (cj, oldResults) {307assert.ok(oldResults);308cj.getCookies('http://example.com/foo/bar', this.callback);309},310"get a bunch of cookies": function (cookies) {311var names = cookies.map(function (c) {312return c.key313});314assert.deepEqual(names, ['d', 'a', 'b', 'e']);315}316},317"then retrieving for http://example.com as a string": {318topic: function (cj, oldResults) {319assert.ok(oldResults);320cj.getCookieString('http://example.com', this.callback);321},322"get a single string": function (cookieHeader) {323assert.equal(cookieHeader, "a=1; b=2; e=5");324}325},326"then retrieving for http://example.com as a set-cookie header": {327topic: function (cj, oldResults) {328assert.ok(oldResults);329cj.getSetCookieStrings('http://example.com', this.callback);330},331"get a single string": function (cookieHeaders) {332assert.lengthOf(cookieHeaders, 3);333assert.equal(cookieHeaders[0], "a=1; Domain=example.com; Path=/");334assert.equal(cookieHeaders[1], "b=2; Domain=example.com; Path=/; HttpOnly");335assert.equal(cookieHeaders[2], "e=5; Path=/");336}337},338"then retrieving for http://www.example.com/": {339topic: function (cj, oldResults) {340assert.ok(oldResults);341cj.getCookies('http://www.example.com/foo/bar', this.callback);342},343"get a bunch of cookies": function (cookies) {344var names = cookies.map(function (c) {345return c.key346});347assert.deepEqual(names, ['d', 'a', 'b']); // note lack of 'e'348}349}350}351})352.addBatch({353"Repeated names": {354topic: function () {355var cb = this.callback;356var cj = new CookieJar();357var ex = 'http://www.example.com/';358var sc = cj.setCookie;359var tasks = [];360var now = Date.now();361tasks.push(sc.bind(cj, 'aaaa=xxxx', ex, at(0)));362tasks.push(sc.bind(cj, 'aaaa=1111; Domain=www.example.com', ex, at(1000)));363tasks.push(sc.bind(cj, 'aaaa=2222; Domain=example.com', ex, at(2000)));364tasks.push(sc.bind(cj, 'aaaa=3333; Domain=www.example.com; Path=/pathA', ex, at(3000)));365async.series(tasks, function (err, results) {366results = results.filter(function (e) {367return e !== undefined368});369cb(err, {cj: cj, cookies: results, now: now});370});371},372"all got set": function (err, t) {373assert.lengthOf(t.cookies, 4);374},375"then getting 'em back": {376topic: function (t) {377var cj = t.cj;378cj.getCookies('http://www.example.com/pathA', this.callback);379},380"there's just three": function (err, cookies) {381var vals = cookies.map(function (c) {382return c.value383});384// may break with sorting; sorting should put 3333 first due to longest path:385assert.deepEqual(vals, ['3333', '1111', '2222']);386}387}388}389})390.addBatch({391"CookieJar setCookie errors": {392"public-suffix domain": {393topic: function () {394var cj = new CookieJar();395cj.setCookie('i=9; Domain=kyoto.jp; Path=/', 'kyoto.jp', this.callback);396},397"errors": function (err, cookie) {398assert.ok(err);399assert.ok(!cookie);400assert.match(err.message, /public suffix/i);401}402},403"wrong domain": {404topic: function () {405var cj = new CookieJar();406cj.setCookie('j=10; Domain=google.com; Path=/', 'http://google.ca', this.callback);407},408"errors": function (err, cookie) {409assert.ok(err);410assert.ok(!cookie);411assert.match(err.message, /not in this host's domain/i);412}413},414"old cookie is HttpOnly": {415topic: function () {416var cb = this.callback;417var next = function (err, c) {418c = null;419return cb(err, cj);420};421var cj = new CookieJar();422cj.setCookie('k=11; Domain=example.ca; Path=/; HttpOnly', 'http://example.ca', {http: true}, next);423},424"initial cookie is set": function (err, cj) {425assert.ok(!err);426assert.ok(cj);427},428"but when trying to overwrite": {429topic: function (cj) {430var cb = this.callback;431var next = function (err, c) {432c = null;433cb(null, err);434};435cj.setCookie('k=12; Domain=example.ca; Path=/', 'http://example.ca', {http: false}, next);436},437"it's an error": function (err) {438assert.ok(err);439},440"then, checking the original": {441topic: function (ignored, cj) {442assert.ok(cj instanceof CookieJar);443cj.getCookies('http://example.ca', {http: true}, this.callback);444},445"cookie has original value": function (err, cookies) {446assert.equal(err, null);447assert.lengthOf(cookies, 1);448assert.equal(cookies[0].value, 11);449}450}451}452},453"similar to public suffix": {454topic: function () {455var cj = new CookieJar();456var url = 'http://www.foonet.net';457assert.isTrue(cj.rejectPublicSuffixes);458cj.setCookie('l=13; Domain=foonet.net; Path=/', url, this.callback);459},460"doesn't error": function (err, cookie) {461assert.ok(!err);462assert.ok(cookie);463}464}465}466})467.export(module);468469470