Browse files

Fix multiple set cookie calls duplicating cookies

Express/Connect has an override of res.setHeader, which has special handling for 'Set-Cookie'. This causes all already added cookies to get duplicated.

Fix by checking if we're under express/connect, and then behaving appropriately.
  • Loading branch information...
1 parent e0170cf commit f8cbb1fa107e0890bd616524ffed1440fd20f54a @mtkopone mtkopone committed Nov 12, 2012
Showing with 3 additions and 1 deletion.
  1. +1 −1 lib/cookies.js
  2. +1 −0 test/express.js
  3. +1 −0 test/http.js
View
2 lib/cookies.js
@@ -38,7 +38,7 @@ Cookies.prototype = {
set: function(name, value, opts) {
var res = this.response
, req = this.request
- , headers = res.getHeader("Set-Cookie") || []
+ , headers = res.set ? [] : res.getHeader("Set-Cookie") || []
, secure = req.connection.encrypted
, cookie = new Cookie(name, value, opts)
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys
View
1 test/express.js
@@ -56,6 +56,7 @@ http.get( options, function( res ) {
console.log( "\ncookies set:", header )
console.log( "\n============\n" )
+ assert.equal(header.length, 6)
options.path = res.headers[ "Location" ]
options.headers = { "Cookie": header.join(";") }
View
1 test/http.js
@@ -55,6 +55,7 @@ http.get( options, function( res ) {
console.log( "\ncookies set:", cookies )
console.log( "\n============\n" )
+ assert.equal(cookies.length, 6)
options.path = res.headers[ "location" ]
options.headers = { "Cookie": cookies.join(";") }

0 comments on commit f8cbb1f

Please sign in to comment.