diff options
Diffstat (limited to 'deps/npm/node_modules/request/node_modules/tough-cookie/test.js')
-rw-r--r-- | deps/npm/node_modules/request/node_modules/tough-cookie/test.js | 323 |
1 files changed, 304 insertions, 19 deletions
diff --git a/deps/npm/node_modules/request/node_modules/tough-cookie/test.js b/deps/npm/node_modules/request/node_modules/tough-cookie/test.js index 3cce815e76..5cbf536ca2 100644 --- a/deps/npm/node_modules/request/node_modules/tough-cookie/test.js +++ b/deps/npm/node_modules/request/node_modules/tough-cookie/test.js @@ -18,7 +18,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ - +'use strict'; var vows = require('vows'); var assert = require('assert'); var async = require('async'); @@ -31,7 +31,7 @@ var CookieJar = tough.CookieJar; function dateVows(table) { var theVows = { }; - var keys = Object.keys(table).forEach(function(date) { + Object.keys(table).forEach(function(date) { var expect = table[date]; theVows[date] = function() { var got = tough.parseDate(date) ? 'valid' : 'invalid'; @@ -69,7 +69,7 @@ function defaultPathVows(table) { } var atNow = Date.now(); -function at(offset) { return {now: new Date(atNow+offset)} } +function at(offset) { return {now: new Date(atNow+offset)}; } vows.describe('Cookie Jar') .addBatch({ @@ -135,8 +135,8 @@ vows.describe('Cookie Jar') "doesn't validate": function(c) { assert.ok(!c.validate()); }, - "to string": function(c) { - assert.equal(c.toString(), 'a="beta gamma"'); + "'garbage in, garbage out'": function(c) { + assert.equal(c.toString(), 'a=beta gamma'); }, }, "with an empty value and HttpOnly": { @@ -277,7 +277,7 @@ vows.describe('Cookie Jar') assert.ok(c.isPersistent()); }, "default TTL": { - topic: function() { return new Cookie() }, + topic: function() { return new Cookie(); }, "is Infinite-future": function(c) { assert.equal(c.TTL(), Infinity) }, "is a 'session' cookie": function(c) { assert.ok(!c.isPersistent()) }, }, @@ -507,7 +507,64 @@ vows.describe('Cookie Jar') assert.equal(c.expires.getTime(), 1397700749000); }, "httponly": function(c) { assert.ok(c.httpOnly) }, - } + }, + "spaces in value": { + "strict": { + topic: function() { + return Cookie.parse('a=one two three',true) || null; + }, + "did not parse": function(c) { assert.isNull(c) }, + }, + "non-strict": { + topic: function() { + return Cookie.parse('a=one two three',false) || null; + }, + "parsed": function(c) { assert.ok(c) }, + "key": function(c) { assert.equal(c.key, 'a') }, + "value": function(c) { assert.equal(c.value, 'one two three') }, + "no path": function(c) { assert.equal(c.path, null) }, + "no domain": function(c) { assert.equal(c.domain, null) }, + "no extensions": function(c) { assert.ok(!c.extensions) }, + }, + }, + "quoted spaces in value": { + "strict": { + topic: function() { + return Cookie.parse('a="one two three"',true) || null; + }, + "did not parse": function(c) { assert.isNull(c) }, + }, + "non-strict": { + topic: function() { + return Cookie.parse('a="one two three"',false) || null; + }, + "parsed": function(c) { assert.ok(c) }, + "key": function(c) { assert.equal(c.key, 'a') }, + "value": function(c) { assert.equal(c.value, 'one two three') }, + "no path": function(c) { assert.equal(c.path, null) }, + "no domain": function(c) { assert.equal(c.domain, null) }, + "no extensions": function(c) { assert.ok(!c.extensions) }, + } + }, + "non-ASCII in value": { + "strict": { + topic: function() { + return Cookie.parse('farbe=weiß',true) || null; + }, + "did not parse": function(c) { assert.isNull(c) }, + }, + "non-strict": { + topic: function() { + return Cookie.parse('farbe=weiß',false) || null; + }, + "parsed": function(c) { assert.ok(c) }, + "key": function(c) { assert.equal(c.key, 'farbe') }, + "value": function(c) { assert.equal(c.value, 'weiß') }, + "no path": function(c) { assert.equal(c.path, null) }, + "no domain": function(c) { assert.equal(c.domain, null) }, + "no extensions": function(c) { assert.ok(!c.extensions) }, + }, + }, } }) .addBatch({ @@ -706,6 +763,20 @@ vows.describe('Cookie Jar') assert.equal(c.domain, 'example.com'); }, }, + "Setting a sub-path cookie on a super-domain": { + topic: function() { + var cj = new CookieJar(); + var c = Cookie.parse("a=b; Domain=example.com; Path=/subpath"); + assert.strictEqual(c.hostOnly, null); + assert.instanceOf(c.creation, Date); + assert.strictEqual(c.lastAccessed, null); + c.creation = new Date(Date.now()-10000); + cj.setCookie(c, 'http://www.example.com/index.html', this.callback); + }, + "domain is super-domain": function(c) { assert.equal(c.domain, 'example.com') }, + "path is /subpath": function(c) { assert.equal(c.path, '/subpath') }, + "path was NOT derived": function(c) { assert.strictEqual(c.pathIsDefault, null) }, + }, "Setting HttpOnly cookie over non-HTTP API": { topic: function() { var cj = new CookieJar(); @@ -755,10 +826,13 @@ vows.describe('Cookie Jar') }); }, "setup ok": function(err,cj,results) { - assert.ok(1); + assert.ok(!err); + assert.ok(cj); + assert.ok(results); }, "then retrieving for http://nodejs.org": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('http://nodejs.org',this.callback); }, "get a nodejs cookie": function(cookies) { @@ -768,7 +842,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for https://example.com": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('https://example.com',{secure:true},this.callback); }, "get a secure example cookie with others": function(cookies) { @@ -777,7 +852,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for https://example.com (missing options)": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('https://example.com',this.callback); }, "get a secure example cookie with others": function(cookies) { @@ -786,7 +862,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://example.com": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('http://example.com',this.callback); }, "get a bunch of cookies": function(cookies) { @@ -795,7 +872,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://EXAMPlE.com": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('http://EXAMPlE.com',this.callback); }, "get a bunch of cookies": function(cookies) { @@ -804,7 +882,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://example.com, non-HTTP": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('http://example.com',{http:false},this.callback); }, "get a bunch of cookies": function(cookies) { @@ -813,7 +892,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://example.com/foo/bar": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('http://example.com/foo/bar',this.callback); }, "get a bunch of cookies": function(cookies) { @@ -822,7 +902,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://example.com as a string": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookieString('http://example.com',this.callback); }, "get a single string": function(cookieHeader) { @@ -830,7 +911,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://example.com as a set-cookie header": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getSetCookieStrings('http://example.com',this.callback); }, "get a single string": function(cookieHeaders) { @@ -841,7 +923,8 @@ vows.describe('Cookie Jar') }, }, "then retrieving for http://www.example.com/": { - topic: function(cj,results) { + topic: function(cj,oldResults) { + assert.ok(oldResults); cj.getCookies('http://www.example.com/foo/bar',this.callback); }, "get a bunch of cookies": function(cookies) { @@ -909,6 +992,7 @@ vows.describe('Cookie Jar') topic: function() { var cb = this.callback; var next = function (err,c) { + c = null; return cb(err,cj); }; var cj = new CookieJar(); @@ -916,11 +1000,16 @@ vows.describe('Cookie Jar') }, "initial cookie is set": function(err,cj) { assert.ok(!err); + assert.ok(cj); }, "but when trying to overwrite": { topic: function(cj) { var cb = this.callback; - cj.setCookie('k=12; Domain=example.ca; Path=/','http://example.ca',{http:false},function(err,c) {cb(null,err)}); + var next = function(err,c) { + c = null; + cb(null,err); + }; + cj.setCookie('k=12; Domain=example.ca; Path=/','http://example.ca',{http:false},next); }, "it's an error": function(err) { assert.ok(err); @@ -1337,4 +1426,200 @@ vows.describe('Cookie Jar') }, } }) +.addBatch({ + "remove cookies": { + topic: function() { + var jar = new CookieJar(); + var cookie = Cookie.parse("a=b; Domain=example.com; Path=/"); + var cookie2 = Cookie.parse("a=b; Domain=foo.com; Path=/"); + var cookie3 = Cookie.parse("foo=bar; Domain=foo.com; Path=/"); + jar.setCookie(cookie, 'http://example.com/index.html', function(){}); + jar.setCookie(cookie2, 'http://foo.com/index.html', function(){}); + jar.setCookie(cookie3, 'http://foo.com/index.html', function(){}); + return jar; + }, + "all from matching domain": function(jar){ + jar.store.removeCookies('example.com',null, function(err) { + assert(err == null); + + jar.store.findCookies('example.com', null, function(err, cookies){ + assert(err == null); + assert(cookies != null); + assert(cookies.length === 0, 'cookie was not removed'); + }); + + jar.store.findCookies('foo.com', null, function(err, cookies){ + assert(err == null); + assert(cookies != null); + assert(cookies.length === 2, 'cookies should not have been removed'); + }); + }); + }, + "from cookie store matching domain and key": function(jar){ + jar.store.removeCookie('foo.com', '/', 'foo', function(err) { + assert(err == null); + + jar.store.findCookies('foo.com', null, function(err, cookies){ + assert(err == null); + assert(cookies != null); + assert(cookies.length === 1, 'cookie was not removed correctly'); + assert(cookies[0].key === 'a', 'wrong cookie was removed'); + }); + }); + } + } +}) +.addBatch({ + "Synchronous CookieJar": { + "setCookieSync": { + topic: function() { + var jar = new CookieJar(); + var cookie = Cookie.parse("a=b; Domain=example.com; Path=/"); + cookie = jar.setCookieSync(cookie, 'http://example.com/index.html'); + return cookie; + }, + "returns a copy of the cookie": function(cookie) { + assert.instanceOf(cookie, Cookie); + } + }, + + "setCookieSync strict parse error": { + topic: function() { + var jar = new CookieJar(); + var opts = { strict: true }; + try { + jar.setCookieSync("farbe=weiß", 'http://example.com/index.html', opts); + return false; + } catch (e) { + return e; + } + }, + "throws the error": function(err) { + assert.instanceOf(err, Error); + assert.equal(err.message, "Cookie failed to parse"); + } + }, + + "getCookiesSync": { + topic: function() { + var jar = new CookieJar(); + var url = 'http://example.com/index.html'; + jar.setCookieSync("a=b; Domain=example.com; Path=/", url); + jar.setCookieSync("c=d; Domain=example.com; Path=/", url); + return jar.getCookiesSync(url); + }, + "returns the cookie array": function(err, cookies) { + assert.ok(!err); + assert.ok(Array.isArray(cookies)); + assert.lengthOf(cookies, 2); + cookies.forEach(function(cookie) { + assert.instanceOf(cookie, Cookie); + }); + } + }, + + "getCookieStringSync": { + topic: function() { + var jar = new CookieJar(); + var url = 'http://example.com/index.html'; + jar.setCookieSync("a=b; Domain=example.com; Path=/", url); + jar.setCookieSync("c=d; Domain=example.com; Path=/", url); + return jar.getCookieStringSync(url); + }, + "returns the cookie header string": function(err, str) { + assert.ok(!err); + assert.typeOf(str, 'string'); + } + }, + + "getSetCookieStringsSync": { + topic: function() { + var jar = new CookieJar(); + var url = 'http://example.com/index.html'; + jar.setCookieSync("a=b; Domain=example.com; Path=/", url); + jar.setCookieSync("c=d; Domain=example.com; Path=/", url); + return jar.getSetCookieStringsSync(url); + }, + "returns the cookie header string": function(err, headers) { + assert.ok(!err); + assert.ok(Array.isArray(headers)); + assert.lengthOf(headers, 2); + headers.forEach(function(header) { + assert.typeOf(header, 'string'); + }); + } + }, + } +}) +.addBatch({ + "Synchronous API on async CookieJar": { + topic: function() { + return new tough.Store(); + }, + "setCookieSync": { + topic: function(store) { + var jar = new CookieJar(store); + try { + jar.setCookieSync("a=b", 'http://example.com/index.html'); + return false; + } catch(e) { + return e; + } + }, + "fails": function(err) { + assert.instanceOf(err, Error); + assert.equal(err.message, + 'CookieJar store is not synchronous; use async API instead.'); + } + }, + "getCookiesSync": { + topic: function(store) { + var jar = new CookieJar(store); + try { + jar.getCookiesSync('http://example.com/index.html'); + return false; + } catch(e) { + return e; + } + }, + "fails": function(err) { + assert.instanceOf(err, Error); + assert.equal(err.message, + 'CookieJar store is not synchronous; use async API instead.'); + } + }, + "getCookieStringSync": { + topic: function(store) { + var jar = new CookieJar(store); + try { + jar.getCookieStringSync('http://example.com/index.html'); + return false; + } catch(e) { + return e; + } + }, + "fails": function(err) { + assert.instanceOf(err, Error); + assert.equal(err.message, + 'CookieJar store is not synchronous; use async API instead.'); + } + }, + "getSetCookieStringsSync": { + topic: function(store) { + var jar = new CookieJar(store); + try { + jar.getSetCookieStringsSync('http://example.com/index.html'); + return false; + } catch(e) { + return e; + } + }, + "fails": function(err) { + assert.instanceOf(err, Error); + assert.equal(err.message, + 'CookieJar store is not synchronous; use async API instead.'); + } + }, + } +}) .export(module); |