Skip to content

Commit

Permalink
fixed cookies parsing, updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afanasy committed Dec 12, 2011
1 parent 5a37199 commit 4882e51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tests/test-cookie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 str = 'sid="s543qactge.wKE61E01Bs%2BKhzmxrwrnug="; path=/; httpOnly; expires=Sat, 04 Dec 2010 23:27:28 GMT';
var cookie = new Cookie(str);

// test .toString()
Expand All @@ -17,7 +17,7 @@ assert.equal(cookie.httpOnly, true);
assert.equal(cookie.name, 'sid');

// test .value
assert.equal(cookie.value, 's543qactge.wKE61E01Bs%2BKhzmxrwrnug');
assert.equal(cookie.value, '"s543qactge.wKE61E01Bs%2BKhzmxrwrnug="');

// test .expires
assert.equal(cookie.expires instanceof Date, true);
Expand Down
15 changes: 10 additions & 5 deletions vendor/cookie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ var Cookie = exports = module.exports = function Cookie(str, req) {
this.str = str;

// First key is the name
this.name = str.substr(0, str.indexOf('='));
this.name = str.substr(0, str.indexOf('=')).trim();

// Map the key/val pairs
str.split(/ *; */).reduce(function(obj, pair){
pair = pair.split(/ *= */);
obj[pair[0].toLowerCase()] = pair[1] || true;
return obj;
var p = pair.indexOf('=');
if(p > 0)
obj[pair.substring(0, p).trim()] = pair.substring(p + 1).trim();
else
obj[pair.trim()] = true;
return obj;
}, this);

// Assign value
Expand All @@ -40,7 +43,9 @@ var Cookie = exports = module.exports = function Cookie(str, req) {
: Infinity;

// Default or trim path
this.path = this.path || '/';
this.path = this.path
? this.path.trim(): req
? url.parse(req.url).pathname: '/';
};

/**
Expand Down

0 comments on commit 4882e51

Please sign in to comment.