-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closed issue 82 : handling cookies - added tests too
- Loading branch information
alessioalex
committed
Nov 12, 2011
1 parent
8869b2e
commit 8323eed
Showing
10 changed files
with
309 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var Cookie = require('../vendor/cookie') | ||
, assert = require('assert'); | ||
|
||
var str = 'sid=s543qactge.wKE61E01Bs%2BKhzmxrwrnug; path=/; httpOnly; expires=Sat, 04 Dec 2010 23:27:28 GMT'; | ||
var cookie = new Cookie(str); | ||
|
||
// test .toString() | ||
assert.equal(cookie.toString(), str); | ||
|
||
// test .path | ||
assert.equal(cookie.path, '/'); | ||
|
||
// test .httpOnly | ||
assert.equal(cookie.httpOnly, true); | ||
|
||
// test .name | ||
assert.equal(cookie.name, 'sid'); | ||
|
||
// test .value | ||
assert.equal(cookie.value, 's543qactge.wKE61E01Bs%2BKhzmxrwrnug'); | ||
|
||
// test .expires | ||
assert.equal(cookie.expires instanceof Date, true); | ||
|
||
// test .path default | ||
var cookie = new Cookie('foo=bar', { url: 'http://foo.com/bar' }); | ||
assert.equal(cookie.path, '/bar'); | ||
|
||
console.log('All tests passed'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
var Cookie = require('../vendor/cookie') | ||
, Jar = require('../vendor/cookie/jar') | ||
, assert = require('assert'); | ||
|
||
function expires(ms) { | ||
return new Date(Date.now() + ms).toUTCString(); | ||
} | ||
|
||
// test .get() expiration | ||
(function() { | ||
var jar = new Jar; | ||
var cookie = new Cookie('sid=1234; path=/; expires=' + expires(1000)); | ||
jar.add(cookie); | ||
setTimeout(function(){ | ||
var cookies = jar.get({ url: 'http://foo.com/foo' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], cookie); | ||
setTimeout(function(){ | ||
var cookies = jar.get({ url: 'http://foo.com/foo' }); | ||
assert.equal(cookies.length, 0); | ||
}, 1000); | ||
}, 5); | ||
})(); | ||
|
||
// test .get() path support | ||
(function() { | ||
var jar = new Jar; | ||
var a = new Cookie('sid=1234; path=/'); | ||
var b = new Cookie('sid=1111; path=/foo/bar'); | ||
var c = new Cookie('sid=2222; path=/'); | ||
jar.add(a); | ||
jar.add(b); | ||
jar.add(c); | ||
|
||
// should remove the duplicates | ||
assert.equal(jar.cookies.length, 2); | ||
|
||
// same name, same path, latter prevails | ||
var cookies = jar.get({ url: 'http://foo.com/' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], c); | ||
|
||
// same name, diff path, path specifity prevails, latter prevails | ||
var cookies = jar.get({ url: 'http://foo.com/foo/bar' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], b); | ||
|
||
var jar = new Jar; | ||
var a = new Cookie('sid=1111; path=/foo/bar'); | ||
var b = new Cookie('sid=1234; path=/'); | ||
jar.add(a); | ||
jar.add(b); | ||
|
||
var cookies = jar.get({ url: 'http://foo.com/foo/bar' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], a); | ||
|
||
var cookies = jar.get({ url: 'http://foo.com/' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], b); | ||
|
||
var jar = new Jar; | ||
var a = new Cookie('sid=1111; path=/foo/bar'); | ||
var b = new Cookie('sid=3333; path=/foo/bar'); | ||
var c = new Cookie('pid=3333; path=/foo/bar'); | ||
var d = new Cookie('sid=2222; path=/foo/'); | ||
var e = new Cookie('sid=1234; path=/'); | ||
jar.add(a); | ||
jar.add(b); | ||
jar.add(c); | ||
jar.add(d); | ||
jar.add(e); | ||
|
||
var cookies = jar.get({ url: 'http://foo.com/foo/bar' }); | ||
assert.equal(cookies.length, 2); | ||
assert.equal(cookies[0], b); | ||
assert.equal(cookies[1], c); | ||
|
||
var cookies = jar.get({ url: 'http://foo.com/foo/' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], d); | ||
|
||
var cookies = jar.get({ url: 'http://foo.com/' }); | ||
assert.equal(cookies.length, 1); | ||
assert.equal(cookies[0], e); | ||
})(); | ||
|
||
setTimeout(function() { | ||
console.log('All tests passed'); | ||
}, 1200); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.