react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / test / domain_and_path_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;3637function matchVows(func, table) {38var theVows = {};39table.forEach(function (item) {40var str = item[0];41var dom = item[1];42var expect = item[2];43var label = str + (expect ? " matches " : " doesn't match ") + dom;44theVows[label] = function () {45assert.equal(func(str, dom), expect);46};47});48return theVows;49}5051function defaultPathVows(table) {52var theVows = {};53table.forEach(function (item) {54var str = item[0];55var expect = item[1];56var label = str + " gives " + expect;57theVows[label] = function () {58assert.equal(tough.defaultPath(str), expect);59};60});61return theVows;62}6364vows65.describe('Domain and Path')66.addBatch({67"domain normalization": {68"simple": function () {69var c = new Cookie();70c.domain = "EXAMPLE.com";71assert.equal(c.canonicalizedDomain(), "example.com");72},73"extra dots": function () {74var c = new Cookie();75c.domain = ".EXAMPLE.com";76assert.equal(c.cdomain(), "example.com");77},78"weird trailing dot": function () {79var c = new Cookie();80c.domain = "EXAMPLE.ca.";81assert.equal(c.canonicalizedDomain(), "example.ca.");82},83"weird internal dots": function () {84var c = new Cookie();85c.domain = "EXAMPLE...ca.";86assert.equal(c.canonicalizedDomain(), "example...ca.");87},88"IDN": function () {89var c = new Cookie();90c.domain = "δοκιμή.δοκιμή"; // "test.test" in greek91assert.equal(c.canonicalizedDomain(), "xn--jxalpdlp.xn--jxalpdlp");92}93}94})95.addBatch({96"Domain Match": matchVows(tough.domainMatch, [97// str, dom, expect98["example.com", "example.com", true],99["eXaMpLe.cOm", "ExAmPlE.CoM", true],100["no.ca", "yes.ca", false],101["wwwexample.com", "example.com", false],102["www.example.com", "example.com", true],103["example.com", "www.example.com", false],104["www.subdom.example.com", "example.com", true],105["www.subdom.example.com", "subdom.example.com", true],106["example.com", "example.com.", false], // RFC6265 S4.1.2.3107["192.168.0.1", "168.0.1", false], // S5.1.3 "The string is a host name"108[null, "example.com", null],109["example.com", null, null],110[null, null, null],111[undefined, undefined, null],112])113})114115.addBatch({116"default-path": defaultPathVows([117[null, "/"],118["/", "/"],119["/file", "/"],120["/dir/file", "/dir"],121["noslash", "/"],122])123})124.addBatch({125"Path-Match": matchVows(tough.pathMatch, [126// request, cookie, match127["/", "/", true],128["/dir", "/", true],129["/", "/dir", false],130["/dir/", "/dir/", true],131["/dir/file", "/dir/", true],132["/dir/file", "/dir", true],133["/directory", "/dir", false],134])135})136.addBatch({137"permuteDomain": {138"base case": {139topic: tough.permuteDomain.bind(null, 'example.com'),140"got the domain": function (list) {141assert.deepEqual(list, ['example.com']);142}143},144"two levels": {145topic: tough.permuteDomain.bind(null, 'foo.bar.example.com'),146"got three things": function (list) {147assert.deepEqual(list, ['example.com', 'bar.example.com', 'foo.bar.example.com']);148}149},150"local domain": {151topic: tough.permuteDomain.bind(null, 'foo.bar.example.localduhmain'),152"got three things": function (list) {153assert.deepEqual(list, ['example.localduhmain', 'bar.example.localduhmain', 'foo.bar.example.localduhmain']);154}155}156},157"permutePath": {158"base case": {159topic: tough.permutePath.bind(null, '/'),160"just slash": function (list) {161assert.deepEqual(list, ['/']);162}163},164"single case": {165topic: tough.permutePath.bind(null, '/foo'),166"two things": function (list) {167assert.deepEqual(list, ['/foo', '/']);168},169"path matching": function (list) {170list.forEach(function (e) {171assert.ok(tough.pathMatch('/foo', e));172});173}174},175"double case": {176topic: tough.permutePath.bind(null, '/foo/bar'),177"four things": function (list) {178assert.deepEqual(list, ['/foo/bar', '/foo', '/']);179},180"path matching": function (list) {181list.forEach(function (e) {182assert.ok(tough.pathMatch('/foo/bar', e));183});184}185},186"trailing slash": {187topic: tough.permutePath.bind(null, '/foo/bar/'),188"three things": function (list) {189assert.deepEqual(list, ['/foo/bar', '/foo', '/']);190},191"path matching": function (list) {192list.forEach(function (e) {193assert.ok(tough.pathMatch('/foo/bar/', e));194});195}196}197}198})199.export(module);200201202203