Permalink
Browse files

support cookieDecoder - thanks to coopernurse for the patch

  • Loading branch information...
1 parent 0959b14 commit 8865efc9469d27249c8c20c710565b45beeb1411 @caolan committed Oct 4, 2010
Showing with 24 additions and 11 deletions.
  1. +18 −11 lib/cookie-sessions.js
  2. +6 −0 test/test-cookie-sessions.js
View
@@ -148,18 +148,25 @@ exports.checkLength = function(str){
};
exports.readCookies = function(req){
- // Extracts the cookies from a request object.
- var cookie = req.headers.cookie;
- if(!cookie){
- return {};
+ // if "cookieDecoder" is in use, then req.cookies
+ // will already contain the parsed cookies
+ if (req.cookies) {
+ return req.cookies;
+ }
+ else {
+ // Extracts the cookies from a request object.
+ var cookie = req.headers.cookie;
+ if(!cookie){
+ return {};
+ }
+ var parts = cookie.split(/\s*;\s*/g).map(function(x){
+ return x.split('=');
+ });
+ return parts.reduce(function(a, x){
+ a[unescape(x[0])] = unescape(x[1]);
+ return a;
+ }, {});
}
- var parts = cookie.split(/\s*;\s*/g).map(function(x){
- return x.split('=');
- });
- return parts.reduce(function(a, x){
- a[unescape(x[0])] = unescape(x[1]);
- return a;
- }, {});
};
exports.readSession = function(key, secret, timeout, req){
@@ -250,6 +250,12 @@ exports['readCookies no cookie in headers'] = function(test){
test.done();
};
+exports['readCookies from Connect cookieDecoder'] = function(test){
+ var req = {headers: {}, cookies: {'test':'cookie'}};
+ test.same(sessions.readCookies(req), {'test': 'cookie'});
+ test.done();
+};
+
exports['readSession'] = function(test){
test.expect(5);
var readCookies = sessions.readCookies;

0 comments on commit 8865efc

Please sign in to comment.