Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add support for multiple Set-Cookie headers and handle headers passed…

… to writeHead as an array
  • Loading branch information...
commit 0959b148640eb5eff9559c0d51d7d9c307f13fef 1 parent 20e24d3
@caolan authored
Showing with 97 additions and 9 deletions.
  1. +27 −3 lib/cookie-sessions.js
  2. +70 −6 test/test-cookie-sessions.js
View
30 lib/cookie-sessions.js
@@ -27,9 +27,25 @@ var exports = module.exports = function(settings){
// and the current timestamp. The cookie needs to be set on every
// 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)) +
- (headers['Set-Cookie'] ? ';' + headers['Set-Cookie']: '');
+
+ var cookiestr = escape(s.session_key) + '=' +
+ escape(exports.serialize(s.secret, req.session));
+
+ if(Array.isArray(headers)) headers.push(['Set-Cookie', cookiestr]);
+ else {
+ // if a Set-Cookie header already exists, convert headers to
+ // array so we can send multiple Set-Cookie headers.
+ if(headers['Set-Cookie'] !== undefined){
+ headers = exports.headersToArray(headers);
+ headers.push(['Set-Cookie', cookiestr]);
+ args[args.length-1] = headers;
+ }
+ // if no Set-Cookie header exists, leave the headers as an
+ // object, and add a Set-Cookie property
+ else {
+ headers['Set-Cookie'] = cookiestr;
+ }
+ }
// call the original writeHead on the request
return _writeHead.apply(res, args);
@@ -39,6 +55,14 @@ var exports = module.exports = function(settings){
};
};
+exports.headersToArray = function(headers){
+ if(Array.isArray(headers)) return headers;
+ return Object.keys(headers).reduce(function(arr, k){
+ arr.push([k, headers[k]]);
+ return arr;
+ }, []);
+};
+
// Extend a given object with all the properties in passed-in object(s).
// From underscore.js (http://documentcloud.github.com/underscore/)
View
76 test/test-cookie-sessions.js
@@ -388,17 +388,81 @@ exports['onInit no secret set'] = function(test){
};
exports['set multiple cookies'] = function(test){
+ var _serialize = sessions.serialize;
+ sessions.serialize = function(){
+ return 'session_data';
+ };
var req = {headers: {cookie:''}};
var res = {writeHead: function(statusCode, headers){
- test.equals(
- headers['Set-Cookie'].split(';')[1],
- 'testcookie=testvalue'
- );
- test.equals(headers['Set-Cookie'].split(';').length, 2);
+ test.equals(statusCode, 200);
+ test.same(headers, [
+ ['other_header', 'val'],
+ ['Set-Cookie', 'testcookie=testvalue'],
+ ['Set-Cookie', '_node=session_data']
+ ]);
+ sessions.serialize = _serialize;
+ test.done();
+ }};
+ sessions({secret: 'secret'})(req, res, function(){
+ req.session = {test: 'test'};
+ res.writeHead(200, {
+ 'other_header': 'val',
+ 'Set-Cookie':'testcookie=testvalue'
+ });
+ });
+};
+
+exports['set single cookie'] = function(test){
+ var _serialize = sessions.serialize;
+ sessions.serialize = function(){
+ return 'session_data';
+ };
+ var req = {headers: {cookie:''}};
+ var res = {writeHead: function(statusCode, headers){
+ test.equals(statusCode, 200);
+ test.same(headers, {
+ 'other_header': 'val',
+ 'Set-Cookie': '_node=session_data'
+ });
+ sessions.serialize = _serialize;
+ test.done();
+ }};
+ sessions({secret: 'secret'})(req, res, function(){
+ req.session = {test: 'test'};
+ res.writeHead(200, {'other_header': 'val'});
+ });
+};
+
+exports['handle headers as array'] = function(test){
+ var _serialize = sessions.serialize;
+ sessions.serialize = function(){
+ return 'session_data';
+ };
+ var req = {headers: {cookie:''}};
+ var res = {writeHead: function(statusCode, headers){
+ test.equals(statusCode, 200);
+ test.same(headers, [
+ ['header1', 'val1'],
+ ['header2', 'val2'],
+ ['Set-Cookie', '_node=session_data']
+ ]);
+ sessions.serialize = _serialize;
test.done();
}};
sessions({secret: 'secret'})(req, res, function(){
req.session = {test: 'test'};
- res.writeHead(200, {'Set-Cookie':'testcookie=testvalue'});
+ res.writeHead(200, [['header1', 'val1'],['header2', 'val2']]);
});
};
+
+exports['convert headers to array'] = function(test){
+ test.same(
+ sessions.headersToArray({'key1':'val1','key2':'val2'}),
+ [['key1','val1'],['key2','val2']]
+ );
+ test.same(
+ sessions.headersToArray([['key1','val1'],['key2','val2']]),
+ [['key1','val1'],['key2','val2']]
+ );
+ test.done();
+};
Please sign in to comment.
Something went wrong with that request. Please try again.