react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / tough-cookie / test / cookie_to_json_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('Cookie.toJSON()')39.addBatch({40"JSON": {41"serialization": {42topic: function() {43var c = Cookie.parse('alpha=beta; Domain=example.com; Path=/foo; Expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly');44return JSON.stringify(c);45},46"gives a string": function(str) {47assert.equal(typeof str, "string");48},49"date is in ISO format": function(str) {50assert.match(str, /"expires":"2038-01-19T03:14:07\.000Z"/, 'expires is in ISO format');51}52},53"deserialization": {54topic: function() {55var json = '{"key":"alpha","value":"beta","domain":"example.com","path":"/foo","expires":"2038-01-19T03:14:07.000Z","httpOnly":true,"lastAccessed":2000000000123}';56return Cookie.fromJSON(json);57},58"works": function(c) {59assert.ok(c);60},61"key": function(c) { assert.equal(c.key, "alpha") },62"value": function(c) { assert.equal(c.value, "beta") },63"domain": function(c) { assert.equal(c.domain, "example.com") },64"path": function(c) { assert.equal(c.path, "/foo") },65"httpOnly": function(c) { assert.strictEqual(c.httpOnly, true) },66"secure": function(c) { assert.strictEqual(c.secure, false) },67"hostOnly": function(c) { assert.strictEqual(c.hostOnly, null) },68"expires is a date object": function(c) {69assert.equal(c.expires.getTime(), 2147483647000);70},71"lastAccessed is a date object": function(c) {72assert.equal(c.lastAccessed.getTime(), 2000000000123);73},74"creation defaulted": function(c) {75assert.ok(c.creation.getTime());76}77},78"null deserialization": {79topic: function() {80return Cookie.fromJSON(null);81},82"is null": function(cookie) {83assert.equal(cookie,null);84}85}86},87"expiry deserialization": {88"Infinity": {89topic: Cookie.fromJSON.bind(null, '{"expires":"Infinity"}'),90"is infinite": function(c) {91assert.strictEqual(c.expires, "Infinity");92assert.equal(c.expires, Infinity);93}94}95},96"maxAge serialization": {97topic: function() {98return function(toSet) {99var c = new Cookie();100c.key = 'foo'; c.value = 'bar';101c.setMaxAge(toSet);102return JSON.stringify(c);103};104},105"zero": {106topic: function(f) { return f(0) },107"looks good": function(str) {108assert.match(str, /"maxAge":0/);109}110},111"Infinity": {112topic: function(f) { return f(Infinity) },113"looks good": function(str) {114assert.match(str, /"maxAge":"Infinity"/);115}116},117"-Infinity": {118topic: function(f) { return f(-Infinity) },119"looks good": function(str) {120assert.match(str, /"maxAge":"-Infinity"/);121}122},123"null": {124topic: function(f) { return f(null) },125"looks good": function(str) {126assert.match(str, /"maxAge":null/);127}128}129},130"maxAge deserialization": {131"number": {132topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":123}'),133"is the number": function(c) {134assert.strictEqual(c.maxAge, 123);135}136},137"null": {138topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":null}'),139"is null": function(c) {140assert.strictEqual(c.maxAge, null);141}142},143"less than zero": {144topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":-123}'),145"is -123": function(c) {146assert.strictEqual(c.maxAge, -123);147}148},149"Infinity": {150topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":"Infinity"}'),151"is inf-as-string": function(c) {152assert.strictEqual(c.maxAge, "Infinity");153}154},155"-Infinity": {156topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":"-Infinity"}'),157"is inf-as-string": function(c) {158assert.strictEqual(c.maxAge, "-Infinity");159}160}161}162})163.export(module);164165166