react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / test / cookie_sorting_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 tough = require('../lib/cookie');34var Cookie = tough.Cookie;3536function toKeyArray(cookies) {37return cookies.map(function (c) {38return c.key39});40}4142vows43.describe('Cookie sorting')44.addBatch({45"Cookie Sorting": {46topic: function () {47var cookies = [];48cookies.push(Cookie.parse("a=0; Domain=example.com"));49cookies.push(Cookie.parse("b=1; Domain=www.example.com"));50cookies.push(Cookie.parse("c=2; Domain=example.com; Path=/pathA"));51cookies.push(Cookie.parse("d=3; Domain=www.example.com; Path=/pathA"));52cookies.push(Cookie.parse("e=4; Domain=example.com; Path=/pathA/pathB"));53cookies.push(Cookie.parse("f=5; Domain=www.example.com; Path=/pathA/pathB"));5455// weak shuffle:56cookies = cookies.sort(function () {57return Math.random() - 0.558});5960cookies = cookies.sort(tough.cookieCompare);61return cookies;62},63"got": function (cookies) {64assert.lengthOf(cookies, 6);65assert.deepEqual(toKeyArray(cookies), ['e', 'f', 'c', 'd', 'a', 'b']);66}67}68})69.addBatch({70"Changing creation date affects sorting": {71topic: function () {72var cookies = [];73var now = Date.now();74cookies.push(Cookie.parse("a=0;"));75cookies.push(Cookie.parse("b=1;"));76cookies.push(Cookie.parse("c=2;"));7778cookies.forEach(function (cookie, idx) {79cookie.creation = new Date(now - 100 * idx);80});8182return cookies.sort(tough.cookieCompare);83},84"got": function (cookies) {85assert.deepEqual(toKeyArray(cookies), ['c', 'b', 'a']);86}87}88})89.export(module);909192