fix dirty checking of nested objects

store json when loading, and then compare json at end to determine
if a child object has changed.
This commit is contained in:
Sean McArthur 2013-11-29 09:54:04 -08:00
Родитель c9ad4e7d8a
Коммит 3f156dd5df
2 изменённых файлов: 66 добавлений и 12 удалений

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

@ -186,6 +186,7 @@ function Session(req, res, cookies, opts) {
}
this.content = {};
this.json = JSON.stringify(this.content);
this.loaded = false;
this.dirty = false;
@ -279,7 +280,7 @@ Session.prototype = {
},
updateCookie: function() {
if (this.dirty) {
if (this.isDirty()) {
// support for adding/removing cookie expires
this.opts.cookie.expires = this.expires;
@ -316,10 +317,15 @@ Session.prototype = {
}
}
this.json = JSON.stringify(this.content);
this.loaded = true;
return true;
},
isDirty: function() {
return this.dirty || (this.json !== JSON.stringify(this.content));
},
// called to create a proxy that monitors the session
// for new properties being set
monitor: function() {
@ -374,7 +380,7 @@ Session.prototype = {
var cookieSession = function(opts) {
function clientSessionFactory(opts) {
if (!opts)
throw "no options provided, some are required"; // XXX rename opts?
@ -405,7 +411,7 @@ var cookieSession = function(opts) {
const propertyName = opts.requestKey || opts.cookieName;
return function(req, res, next) {
return function clientSession(req, res, next) {
if (req[propertyName]) {
return next(); //self aware
}
@ -432,7 +438,7 @@ var cookieSession = function(opts) {
};
};
module.exports = cookieSession;
module.exports = clientSessionFactory;
// Expose encode and decode method

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

@ -43,18 +43,26 @@ function create_app() {
var suite = vows.describe('all');
suite.addBatch({
"a single request object" : {
"middleware" : {
topic: function() {
var self = this;
var app = create_app();
app.get("/foo", function(req, res) {
self.callback(null, req);
res.send("hello");
var middleware = cookieSessions({
cookieName: 'session',
secret: 'yo',
activeDuration: 0,
cookie: {
maxAge: 5000
}
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function(res, $) {});
var req = {
headers: {}
};
var res = {};
middleware(req, res, function(err) {
self.callback(err, req, res);
});
},
"includes a session object": function(err, req) {
assert.isObject(req.session);
@ -66,6 +74,9 @@ suite.addBatch({
"session object has reset function": function(err, req) {
assert.isFunction(req.session.reset);
},
"session object has setDuration function": function(err, req) {
assert.isFunction(req.session.setDuration);
},
"set variables and clear them yields no variables": function(err, req) {
req.session.bar = 'baz';
req.session.reset();
@ -231,6 +242,43 @@ suite.addBatch({
assert.equal(req.session.foo, 'foobar');
assert.isUndefined(req.session.bar);
}
},
"across three requests with deep objects" : {
topic: function() {
var self = this;
// simple app
var app = create_app();
app.get("/foo", function(req, res) {
req.session.reset();
req.session.foo = 'foobar';
req.session.bar = { a: 'b' };
res.send("foo");
});
app.get("/bar", function(req, res) {
req.session.bar.c = 'd';
res.send("bar");
});
app.get("/baz", function(req, res) {
self.callback(null, req);
res.send("baz");
});
var browser = tobi.createBrowser(app);
browser.get("/foo", function(res, $) {
browser.get("/bar", function(res, $) {
browser.get("/baz", function(res, $) {
});
});
});
},
"session maintains state": function(err, req) {
assert.equal(req.session.foo, 'foobar');
assert.equal(req.session.bar.c, 'd');
}
}
});