react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / test / api_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;373839var atNow = Date.now();4041function at(offset) {42return {now: new Date(atNow + offset)};43}4445vows46.describe('API')47.addBatch({48"All defined": function () {49assert.ok(Cookie);50assert.ok(CookieJar);51}52})53.addBatch({54"Constructor": {55topic: function () {56return new Cookie({57key: 'test',58value: 'b',59maxAge: 6060});61},62'check for key property': function (c) {63assert.ok(c);64assert.equal(c.key, 'test');65},66'check for value property': function (c) {67assert.equal(c.value, 'b');68},69'check for maxAge': function (c) {70assert.equal(c.maxAge, 60);71},72'check for default values for unspecified properties': function (c) {73assert.equal(c.expires, "Infinity");74assert.equal(c.secure, false);75assert.equal(c.httpOnly, false);76}77}78})79.addBatch({80"expiry option": {81topic: function () {82var cb = this.callback;83var cj = new CookieJar();84cj.setCookie('near=expiry; Domain=example.com; Path=/; Max-Age=1', 'http://www.example.com', at(-1), function (err, cookie) {8586cb(err, {cj: cj, cookie: cookie});87});88},89"set the cookie": function (t) {90assert.ok(t.cookie, "didn't set?!");91assert.equal(t.cookie.key, 'near');92},93"then, retrieving": {94topic: function (t) {95var cb = this.callback;96setTimeout(function () {97t.cj.getCookies('http://www.example.com', {http: true, expire: false}, function (err, cookies) {98t.cookies = cookies;99cb(err, t);100});101}, 2000);102},103"got the cookie": function (t) {104assert.lengthOf(t.cookies, 1);105assert.equal(t.cookies[0].key, 'near');106}107}108}109})110.addBatch({111"allPaths option": {112topic: function () {113var cj = new CookieJar();114var tasks = [];115tasks.push(cj.setCookie.bind(cj, 'nopath_dom=qq; Path=/; Domain=example.com', 'http://example.com', {}));116tasks.push(cj.setCookie.bind(cj, 'path_dom=qq; Path=/foo; Domain=example.com', 'http://example.com', {}));117tasks.push(cj.setCookie.bind(cj, 'nopath_host=qq; Path=/', 'http://www.example.com', {}));118tasks.push(cj.setCookie.bind(cj, 'path_host=qq; Path=/foo', 'http://www.example.com', {}));119tasks.push(cj.setCookie.bind(cj, 'other=qq; Path=/', 'http://other.example.com/', {}));120tasks.push(cj.setCookie.bind(cj, 'other2=qq; Path=/foo', 'http://other.example.com/foo', {}));121var cb = this.callback;122async.parallel(tasks, function (err, results) {123cb(err, {cj: cj, cookies: results});124});125},126"all set": function (t) {127assert.equal(t.cookies.length, 6);128assert.ok(t.cookies.every(function (c) {129return !!c130}));131},132"getting without allPaths": {133topic: function (t) {134var cb = this.callback;135var cj = t.cj;136cj.getCookies('http://www.example.com/', {}, function (err, cookies) {137cb(err, {cj: cj, cookies: cookies});138});139},140"found just two cookies": function (t) {141assert.equal(t.cookies.length, 2);142},143"all are path=/": function (t) {144assert.ok(t.cookies.every(function (c) {145return c.path === '/'146}));147},148"no 'other' cookies": function (t) {149assert.ok(!t.cookies.some(function (c) {150return (/^other/).test(c.name)151}));152}153},154"getting without allPaths for /foo": {155topic: function (t) {156var cb = this.callback;157var cj = t.cj;158cj.getCookies('http://www.example.com/foo', {}, function (err, cookies) {159cb(err, {cj: cj, cookies: cookies});160});161},162"found four cookies": function (t) {163assert.equal(t.cookies.length, 4);164},165"no 'other' cookies": function (t) {166assert.ok(!t.cookies.some(function (c) {167return (/^other/).test(c.name)168}));169}170},171"getting with allPaths:true": {172topic: function (t) {173var cb = this.callback;174var cj = t.cj;175cj.getCookies('http://www.example.com/', {allPaths: true}, function (err, cookies) {176cb(err, {cj: cj, cookies: cookies});177});178},179"found four cookies": function (t) {180assert.equal(t.cookies.length, 4);181},182"no 'other' cookies": function (t) {183assert.ok(!t.cookies.some(function (c) {184return (/^other/).test(c.name)185}));186}187}188}189})190.addBatch({191"Remove cookies": {192topic: function () {193var jar = new CookieJar();194var cookie = Cookie.parse("a=b; Domain=example.com; Path=/");195var cookie2 = Cookie.parse("a=b; Domain=foo.com; Path=/");196var cookie3 = Cookie.parse("foo=bar; Domain=foo.com; Path=/");197jar.setCookie(cookie, 'http://example.com/index.html', function () {198});199jar.setCookie(cookie2, 'http://foo.com/index.html', function () {200});201jar.setCookie(cookie3, 'http://foo.com/index.html', function () {202});203return jar;204},205"all from matching domain": function (jar) {206jar.store.removeCookies('example.com', null, function (err) {207assert(err == null);208209jar.store.findCookies('example.com', null, function (err, cookies) {210assert(err == null);211assert(cookies != null);212assert(cookies.length === 0, 'cookie was not removed');213});214215jar.store.findCookies('foo.com', null, function (err, cookies) {216assert(err == null);217assert(cookies != null);218assert(cookies.length === 2, 'cookies should not have been removed');219});220});221},222"from cookie store matching domain and key": function (jar) {223jar.store.removeCookie('foo.com', '/', 'foo', function (err) {224assert(err == null);225226jar.store.findCookies('foo.com', null, function (err, cookies) {227assert(err == null);228assert(cookies != null);229assert(cookies.length === 1, 'cookie was not removed correctly');230assert(cookies[0].key === 'a', 'wrong cookie was removed');231});232});233}234}235})236.addBatch({237"Synchronous CookieJar": {238"setCookieSync": {239topic: function () {240var jar = new CookieJar();241var cookie = Cookie.parse("a=b; Domain=example.com; Path=/");242cookie = jar.setCookieSync(cookie, 'http://example.com/index.html');243return cookie;244},245"returns a copy of the cookie": function (cookie) {246assert.instanceOf(cookie, Cookie);247}248},249"getCookiesSync": {250topic: function () {251var jar = new CookieJar();252var url = 'http://example.com/index.html';253jar.setCookieSync("a=b; Domain=example.com; Path=/", url);254jar.setCookieSync("c=d; Domain=example.com; Path=/", url);255return jar.getCookiesSync(url);256},257"returns the cookie array": function (err, cookies) {258assert.ok(!err);259assert.ok(Array.isArray(cookies));260assert.lengthOf(cookies, 2);261cookies.forEach(function (cookie) {262assert.instanceOf(cookie, Cookie);263});264}265},266267"getCookieStringSync": {268topic: function () {269var jar = new CookieJar();270var url = 'http://example.com/index.html';271jar.setCookieSync("a=b; Domain=example.com; Path=/", url);272jar.setCookieSync("c=d; Domain=example.com; Path=/", url);273return jar.getCookieStringSync(url);274},275"returns the cookie header string": function (err, str) {276assert.ok(!err);277assert.typeOf(str, 'string');278}279},280281"getSetCookieStringsSync": {282topic: function () {283var jar = new CookieJar();284var url = 'http://example.com/index.html';285jar.setCookieSync("a=b; Domain=example.com; Path=/", url);286jar.setCookieSync("c=d; Domain=example.com; Path=/", url);287return jar.getSetCookieStringsSync(url);288},289"returns the cookie header string": function (err, headers) {290assert.ok(!err);291assert.ok(Array.isArray(headers));292assert.lengthOf(headers, 2);293headers.forEach(function (header) {294assert.typeOf(header, 'string');295});296}297}298}299})300.addBatch({301"Synchronous API on async CookieJar": {302topic: function () {303return new tough.Store();304},305"setCookieSync": {306topic: function (store) {307var jar = new CookieJar(store);308try {309jar.setCookieSync("a=b", 'http://example.com/index.html');310return false;311} catch (e) {312return e;313}314},315"fails": function (err) {316assert.instanceOf(err, Error);317assert.equal(err.message,318'CookieJar store is not synchronous; use async API instead.');319}320},321"getCookiesSync": {322topic: function (store) {323var jar = new CookieJar(store);324try {325jar.getCookiesSync('http://example.com/index.html');326return false;327} catch (e) {328return e;329}330},331"fails": function (err) {332assert.instanceOf(err, Error);333assert.equal(err.message,334'CookieJar store is not synchronous; use async API instead.');335}336},337"getCookieStringSync": {338topic: function (store) {339var jar = new CookieJar(store);340try {341jar.getCookieStringSync('http://example.com/index.html');342return false;343} catch (e) {344return e;345}346},347"fails": function (err) {348assert.instanceOf(err, Error);349assert.equal(err.message,350'CookieJar store is not synchronous; use async API instead.');351}352},353"getSetCookieStringsSync": {354topic: function (store) {355var jar = new CookieJar(store);356try {357jar.getSetCookieStringsSync('http://example.com/index.html');358return false;359} catch (e) {360return e;361}362},363"fails": function (err) {364assert.instanceOf(err, Error);365assert.equal(err.message,366'CookieJar store is not synchronous; use async API instead.');367}368}369}370})371.export(module);372373374