Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from alessioalex/master
Implemented cookies - closes issue 82: #82
- Loading branch information
Showing
8 changed files
with
330 additions
and
2 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
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 |
---|---|---|
|
@@ -16,4 +16,4 @@ module.exports = function () { | |
s[8] = s[13] = s[18] = s[23] = '-'; | ||
|
||
return s.join(''); | ||
} | ||
} |
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,57 @@ | ||
/*! | ||
* Tobi - Cookie | ||
* Copyright(c) 2010 LearnBoost <dev@learnboost.com> | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var url = require('url'); | ||
|
||
/** | ||
* Initialize a new `Cookie` with the given cookie `str` and `req`. | ||
* | ||
* @param {String} str | ||
* @param {IncomingRequest} req | ||
* @api private | ||
*/ | ||
|
||
var Cookie = exports = module.exports = function Cookie(str, req) { | ||
this.str = str; | ||
|
||
// First key is the name | ||
this.name = str.substr(0, str.indexOf('=')); | ||
|
||
// Map the key/val pairs | ||
str.split(/ *; */).reduce(function(obj, pair){ | ||
pair = pair.split(/ *= */); | ||
obj[pair[0]] = pair[1] || true; | ||
return obj; | ||
}, this); | ||
|
||
// Assign value | ||
this.value = this[this.name]; | ||
|
||
// Expires | ||
this.expires = this.expires | ||
? new Date(this.expires) | ||
: Infinity; | ||
|
||
// Default or trim path | ||
this.path = this.path | ||
? this.path.trim() | ||
: url.parse(req.url).pathname; | ||
}; | ||
|
||
/** | ||
* Return the original cookie string. | ||
* | ||
* @return {String} | ||
* @api public | ||
*/ | ||
|
||
Cookie.prototype.toString = function(){ | ||
return this.str; | ||
}; |
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,72 @@ | ||
/*! | ||
* Tobi - CookieJar | ||
* Copyright(c) 2010 LearnBoost <dev@learnboost.com> | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var url = require('url'); | ||
|
||
/** | ||
* Initialize a new `CookieJar`. | ||
* | ||
* @api private | ||
*/ | ||
|
||
var CookieJar = exports = module.exports = function CookieJar() { | ||
this.cookies = []; | ||
}; | ||
|
||
/** | ||
* Add the given `cookie` to the jar. | ||
* | ||
* @param {Cookie} cookie | ||
* @api private | ||
*/ | ||
|
||
CookieJar.prototype.add = function(cookie){ | ||
this.cookies = this.cookies.filter(function(c){ | ||
// Avoid duplication (same path, same name) | ||
return !(c.name == cookie.name && c.path == cookie.path); | ||
}); | ||
this.cookies.push(cookie); | ||
}; | ||
|
||
/** | ||
* Get cookies for the given `req`. | ||
* | ||
* @param {IncomingRequest} req | ||
* @return {Array} | ||
* @api private | ||
*/ | ||
|
||
CookieJar.prototype.get = function(req){ | ||
var path = url.parse(req.url).pathname | ||
, now = new Date | ||
, specificity = {}; | ||
return this.cookies.filter(function(cookie){ | ||
if (0 == path.indexOf(cookie.path) && now < cookie.expires | ||
&& cookie.path.length > (specificity[cookie.name] || 0)) | ||
return specificity[cookie.name] = cookie.path.length; | ||
}); | ||
}; | ||
|
||
/** | ||
* Return Cookie string for the given `req`. | ||
* | ||
* @param {IncomingRequest} req | ||
* @return {String} | ||
* @api private | ||
*/ | ||
|
||
CookieJar.prototype.cookieString = function(req){ | ||
var cookies = this.get(req); | ||
if (cookies.length) { | ||
return cookies.map(function(cookie){ | ||
return cookie.name + '=' + cookie.value; | ||
}).join('; '); | ||
} | ||
}; |