Skip to content
Browse files

Fixed req.session.cookie is undefined when req.session is parsed from a

raw cookie.
Fixed test/cookieSession does not properly check assertions within
app.use().
  • Loading branch information...
1 parent 3055802 commit c12907987ad5fa1a802c5b22d9e9044291afe96b @shin1m committed
Showing with 41 additions and 2 deletions.
  1. +2 −0 lib/middleware/cookieSession.js
  2. +39 −2 test/cookieSession.js
View
2 lib/middleware/cookieSession.js
@@ -65,6 +65,7 @@ module.exports = function cookieSession(options){
// cookieParser secret
if (!options.secret && req.secret) {
req.session = req.signedCookies[key] || {};
+ req.session.cookie = cookie;
} else {
// TODO: refactor
var rawCookie = req.cookies[key];
@@ -73,6 +74,7 @@ module.exports = function cookieSession(options){
if (unsigned) {
var originalHash = crc16(unsigned);
req.session = utils.parseJSONCookie(unsigned) || {};
+ req.session.cookie = cookie;
}
}
}
View
41 test/cookieSession.js
@@ -99,7 +99,7 @@ describe('connect.cookieSession()', function(){
break;
case 1:
// the session should be blank now
- req.session.name.should.equal(null);
+ req.session.should.not.have.property('name');
break;
}
@@ -111,20 +111,24 @@ describe('connect.cookieSession()', function(){
.get('/')
.end(function(res){
sess(res).should.not.include('expires');
+ res.body.should.equal('wahoo');
app.request()
.get('/')
.set('Cookie', sess(res).replace(';', 'foobar')) // make invalid session cookie
.end(function(res){
sess(res).should.not.include('expires');
+ res.body.should.equal('wahoo');
done();
});
})
})
describe('req.session.cookie', function(){
+ var Cookie = require('../lib/middleware/session/cookie')
+
it('should be a Cookie', function(done){
app.use(function(req, res){
- req.session.cookie.constructor.name.should.equal('Cookie');
+ req.session.cookie.should.be.an.instanceof(Cookie);
res.end();
});
@@ -134,6 +138,7 @@ describe('connect.cookieSession()', function(){
var cookie = sess(res);
cookie.should.include('Path=/');
cookie.should.include('HttpOnly');
+ res.body.should.equal('');
done();
})
})
@@ -154,6 +159,38 @@ describe('connect.cookieSession()', function(){
done();
})
})
+
+ describe('when req.session is parsed from a raw cookie', function(){
+ var test = function(done){
+ app.use(function(req, res){
+ req.session.cookie.should.be.an.instanceof(Cookie);
+ req.session.name = 'tobi';
+ res.end();
+ });
+
+ app.request()
+ .get('/')
+ .end(function(res){
+ res.body.should.equal('');
+ app.request()
+ .get('/')
+ .set('Cookie', sess(res))
+ .end(function(res){
+ res.body.should.equal('');
+ done();
+ })
+ })
+ };
+
+ it('should be a Cookie', test);
+
+ it('should be a Cookie using cookieParser secret', function(done){
+ app = connect();
+ app.use(connect.cookieParser('some secret'));
+ app.use(connect.cookieSession());
+ test(done);
+ })
+ })
})
describe('cookie option', function(){

0 comments on commit c129079

Please sign in to comment.
Something went wrong with that request. Please try again.