Skip to content

Commit

Permalink
Merge pull request #870 from undoZen/master
Browse files Browse the repository at this point in the history
remove req.session.cookie.hasLongExpires because YAGNI
  • Loading branch information
jonathanong committed Dec 11, 2013
2 parents 81ad299 + 06ef585 commit 179bcb0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
7 changes: 2 additions & 5 deletions lib/middleware/session.js
Expand Up @@ -258,18 +258,15 @@ function session(options){

// in case of rolling session, always reset the cookie
if (!rollingSessions) {

// long expires, handle expiry server-side
if (!isNew && cookie.hasLongExpires) return debug('already set cookie');


// browser-session length cookie
if (null == cookie.expires) {
if (!isNew) return debug('already set browser-session cookie');
// compare hashes and ids
} else if (originalHash == hash(req.session) && originalId == req.session.id) {
return debug('unmodified session');
}

}

var val = 's:' + signature.sign(req.sessionID, secret);
Expand Down
12 changes: 0 additions & 12 deletions lib/middleware/session/cookie.js
Expand Up @@ -104,18 +104,6 @@ Cookie.prototype = {
}
},

/**
* Check if the cookie has a reasonably large max-age.
*
* @return {Boolean}
* @api private
*/

get hasLongExpires() {
var week = 604800000;
return this.maxAge > (4 * week);
},

/**
* Return a serialized cookie string.
*
Expand Down
65 changes: 55 additions & 10 deletions test/session.js
Expand Up @@ -484,29 +484,74 @@ describe('connect.session()', function(){
})

describe('.maxAge', function(){
it('should set relative in milliseconds', function(done){
var app = connect()
.use(connect.cookieParser())
.use(connect.session({ secret: 'keyboard cat' }))
.use(function(req, res, next){
req.session.cookie.maxAge = 2000;
res.end();
});
var id;
var app = connect()
.use(connect.cookieParser())
.use(connect.session({ secret: 'keyboard cat', cookie: { maxAge: 2000 }}))
.use(function(req, res, next){
req.session.count = req.session.count || 0;
req.session.count++;
if (req.session.count == 2) req.session.cookie.maxAge = 5000;
if (req.session.count == 3) req.session.cookie.maxAge = 3000000000;
res.end(req.session.count.toString());
});

it('should set relative in milliseconds', function(done){
app.request()
.get('/')
.end(function(res){
var a = new Date(expires(res))
, b = new Date;

id = sid(res);

a.getYear().should.equal(b.getYear());
a.getMonth().should.equal(b.getMonth());
a.getDate().should.equal(b.getDate());
// TODO: check 2s + rotate
a.getSeconds().should.not.equal(b.getSeconds());
var delta = a.valueOf() - b.valueOf();
(delta > 1000 && delta < 2000).should.be.ok;
res.body.should.equal('1');
done();
});
})
});

it('should modify cookie when changed', function(done){
app.request()
.get('/')
.set('Cookie', 'connect.sid=' + id)
.end(function(res){
var a = new Date(expires(res))
, b = new Date;

id = sid(res);

a.getYear().should.equal(b.getYear());
a.getMonth().should.equal(b.getMonth());
a.getSeconds().should.not.equal(b.getSeconds());
var delta = a.valueOf() - b.valueOf();
(delta > 4000 && delta < 5000).should.be.ok;
res.body.should.equal('2');
done();
});
});

it('should modify cookie when changed to large value', function(done){
app.request()
.get('/')
.set('Cookie', 'connect.sid=' + id)
.end(function(res){
var a = new Date(expires(res))
, b = new Date;

id = sid(res);

var delta = a.valueOf() - b.valueOf();
(delta > 2999999000 && delta < 3000000000).should.be.ok;
res.body.should.equal('3');
done();
});
});
})

describe('.expires', function(){
Expand Down

0 comments on commit 179bcb0

Please sign in to comment.