Permalink
Browse files

add support for setting multiple cookies at once

  • Loading branch information...
1 parent 22baff4 commit 2a141bb3407e1e9971e9e9f6691dc36d81ee737d @caolan committed Jul 30, 2010
Showing with 26 additions and 2 deletions.
  1. +3 −2 lib/cookie-sessions.js
  2. +23 −0 test/test-cookie-sessions.js
View
@@ -90,7 +90,7 @@ exports.readCookies = function(req){
if(!cookie){
return {};
}
- var parts = cookie.split('; ').map(function(x){
+ var parts = cookie.split(/\s*;\s*/g).map(function(x){
return x.split('=');
});
return parts.reduce(function(a, x){
@@ -138,7 +138,8 @@ exports.filter = function(settings){
// response so that the timestamp is up to date, and the session
// does not expire unless the user is inactive.
headers['Set-Cookie'] = escape(s.session_key) + '=' +
- escape(exports.serialize(s.secret, req.session));
+ escape(exports.serialize(s.secret, req.session)) +
+ (headers['Set-Cookie'] ? '; ' + headers['Set-Cookie']: '');
// call the original writeHead on the request
return _writeHead.apply(res, args);
@@ -236,6 +236,13 @@ exports['readCookies'] = function(test){
test.done();
};
+exports['readCookies alternate format'] = function(test){
+ var req = {headers: {cookie: "name1=data1;test=\"abcXYZ%20123\""}};
+ var r = sessions.readCookies(req);
+ test.same(r, {name1: 'data1', test: '"abcXYZ 123"'}, 'test header read ok');
+ test.done();
+};
+
exports['readCookies no cookie in headers'] = function(test){
var req = {headers: {}};
var r = sessions.readCookies(req);
@@ -379,3 +386,19 @@ exports['onInit no secret set'] = function(test){
}
test.done();
};
+
+exports['set multiple cookies'] = function(test){
+ var req = {headers: {cookie:''}};
+ var res = {writeHead: function(statusCode, headers){
+ test.equals(
+ headers['Set-Cookie'].split(/\s*;\s*/g)[1],
+ 'testcookie=testvalue'
+ );
+ test.equals(headers['Set-Cookie'].split(/\s*;\s*/g).length, 2);
+ test.done();
+ }};
+ sessions.filter({secret: 'secret'})(req, res, function(){
+ req.session = {test: 'test'};
+ res.writeHead(200, {'Set-Cookie':'testcookie=testvalue'});
+ });
+};

0 comments on commit 2a141bb

Please sign in to comment.