Ephemeral state can now be changed in setDuration

Changing the ephemeral state will cause a new cookie to be set in
the browser.
This commit is contained in:
Francois Marier 2013-09-02 19:22:19 +12:00
Родитель 8588ecf0fa
Коммит 7070a11d68
2 изменённых файлов: 52 добавлений и 12 удалений

Просмотреть файл

@ -208,7 +208,11 @@ function Session(req, res, cookies, opts) {
Session.prototype = {
updateDefaultExpires: function() {
if (!this.opts.cookie.maxAge && !this.opts.cookie.ephemeral) {
if (this.opts.cookie.maxAge) return;
if (this.opts.cookie.ephemeral) {
this.expires = null;
} else {
var time = this.createdAt || new Date().getTime();
// the cookie should expire when it becomes invalid
// we add an extra second because the conversion to a date truncates the milliseconds
@ -236,12 +240,16 @@ Session.prototype = {
this.loaded = true;
},
setDuration: function(newDuration) {
setDuration: function(newDuration, ephemeral) {
if (ephemeral && this.opts.cookie.maxAge) {
throw new Error("you cannot have an ephemeral cookie with a maxAge.");
}
if (!this.loaded)
this.loadFromCookie(true);
this.dirty = true;
this.duration = newDuration;
this.createdAt = new Date().getTime();
this.opts.cookie.ephemeral = ephemeral;
this.updateDefaultExpires();
},
@ -270,10 +278,8 @@ Session.prototype = {
updateCookie: function() {
if (this.dirty) {
// support for expires
if (this.expires) {
this.opts.cookie.expires = this.expires;
}
// support for adding/removing cookie expires
this.opts.cookie.expires = this.expires;
try {
this.cookies.set(this.opts.cookieName, this.box(), this.opts.cookie);

Просмотреть файл

@ -975,6 +975,9 @@ suite.addBatch({
}
});
var shared_browser1;
var shared_browser2;
suite.addBatch({
"non-ephemeral cookie": {
topic: function() {
@ -986,8 +989,7 @@ suite.addBatch({
duration: 5000,
secret: 'yo',
cookie: {
ephemeral: false,
maxAge: 400
ephemeral: false
}
}));
@ -996,13 +998,29 @@ suite.addBatch({
res.send("hello");
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function(res, $) {
app.get("/bar", function(req, res) {
req.session.setDuration(6000, true);
res.send("hello");
});
shared_browser1 = tobi.createBrowser(app);
shared_browser1.get("/foo", function(res, $) {
self.callback(null, res);
});
},
"has an expires attribute": function(err, res) {
assert.match(res.headers['set-cookie'][0], /expires/, "cookie is a session cookie");
},
"changing to an ephemeral one": {
topic: function() {
var self = this;
shared_browser1.get("/bar", function(res, $) {
self.callback(null, res);
});
},
"removes its expires attribute": function(err, res) {
assert.strictEqual(res.headers['set-cookie'][0].indexOf('expires='), -1, "cookie is not ephemeral");
}
}
},
"ephemeral cookie": {
@ -1024,13 +1042,29 @@ suite.addBatch({
res.send("hello");
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function(res, $) {
app.get("/bar", function(req, res) {
req.session.setDuration(6000, false);
res.send("hello");
});
shared_browser2 = tobi.createBrowser(app);
shared_browser2.get("/foo", function(res, $) {
self.callback(null, res);
});
},
"doesn't have an expires attribute": function(err, res) {
assert.strictEqual(res.headers['set-cookie'][0].indexOf('expires='), -1, "cookie is not ephemeral");
},
"changing to an non-ephemeral one": {
topic: function() {
var self = this;
shared_browser2.get("/bar", function(res, $) {
self.callback(null, res);
});
},
"gains an expires attribute": function(err, res) {
assert.match(res.headers['set-cookie'][0], /expires/, "cookie is a session cookie");
}
}
}
});