Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove req.session.cookie.hasLongExpires because YAGNI #870

Merged
merged 2 commits into from Dec 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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