Permalink
Browse files

Use and test against session.cookie.expires instead of private proper…

…ty session.cookie._expires
  • Loading branch information...
1 parent 5f6898e commit 1008f3bfc129ad4a53811637bd0927475f82f24a @lazd lazd committed Jun 28, 2013
Showing with 110 additions and 14 deletions.
  1. +4 −4 lib/connect-mongo.js
  2. +106 −10 test/connect-mongo.test.js
View
@@ -220,8 +220,8 @@ module.exports = function(connect) {
var s = {_id: sid, session: this._serialize_session(session)};
if (session && session.cookie) {
- if (session.cookie._expires) {
- s.expires = new Date(session.cookie._expires);
+ if (session.cookie.expires) {
+ s.expires = new Date(session.cookie.expires);
} else {
// If there's no expiration date specified, it is
// browser-session cookie, as per the connect docs.
@@ -232,9 +232,9 @@ module.exports = function(connect) {
s.expires = new Date(today.getTime() + twoWeeks);
}
- // Convert the cookie object to its JSON representation
+ // Convert the cookie object to its JSON representation if possible
// This gets rid of the duplicate object under session.cookie.data property
- s.session.cookie = session.cookie.toJSON();
+ s.session.cookie = session.cookie.toJSON ? session.cookie.toJSON() : session.cookie;
}
this._get_collection(function(collection) {
View
@@ -123,18 +123,46 @@ exports.test_set_expires = function(done) {
foo:'bar',
cookie:
{
- _expires: '2011-04-26T03:10:12.890Z'
+ expires: '2011-04-26T03:10:12.890Z'
}
};
-
+
store.set(sid, data, function(err, session) {
assert.strictEqual(err, null);
// Verify it was saved
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session.session, JSON.stringify(data));
assert.strictEqual(session._id, sid);
- assert.equal(session.expires.toJSON(), new Date(data.cookie._expires).toJSON());
+ assert.equal(session.expires.toJSON(), new Date(data.cookie.expires).toJSON());
+
+ cleanup(store, db, collection, function() {
+ done();
+ });
+ });
+ });
+ });
+};
+
+exports.test_set_expires_no_stringify = function(done) {
+ open_db({db: options.db, stringify: false}, function(store, db, collection) {
+ var sid = 'test_set_expires-sid';
+ var data = {
+ foo:'bar',
+ cookie:
+ {
+ expires: '2011-04-26T03:10:12.890Z'
+ }
+ };
+
+ store.set(sid, data, function(err, session) {
+ assert.strictEqual(err, null);
+
+ // Verify it was saved
+ collection.findOne({_id: sid}, function(err, session) {
+ assert.deepEqual(session.session, data);
+ assert.strictEqual(session._id, sid);
+ assert.equal(session.expires.toJSON(), new Date(data.cookie.expires).toJSON());
cleanup(store, db, collection, function() {
done();
@@ -296,7 +324,7 @@ exports.test_set_no_stringify_with_raw_db = function(done) {
cleanup(store, db, collection, function() {
done();
});
- });
+ });
});
});
};
@@ -308,7 +336,7 @@ exports.test_set_expires_with_raw_db = function(done) {
foo:'bar',
cookie:
{
- _expires: '2011-04-26T03:10:12.890Z'
+ expires: '2011-04-26T03:10:12.890Z'
}
};
@@ -319,12 +347,46 @@ exports.test_set_expires_with_raw_db = function(done) {
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session.session, JSON.stringify(data));
assert.strictEqual(session._id, sid);
- assert.equal(session.expires.toJSON(), new Date(data.cookie._expires).toJSON());
+ assert.equal(session.expires.toJSON(), new Date(data.cookie.expires).toJSON());
cleanup(store, db, collection, function() {
done();
});
- });
+ });
+ });
+ });
+};
+
+
+exports.test_set_expires_no_stringify_with_raw_db = function(done) {
+ var options = {
+ mongoose_connection: options_with_mongoose_connection.mongoose_connection,
+ stringify: false
+ };
+
+ open_db(options, function(store, db, collection) {
+ var sid = 'test_set_expires-sid';
+ var data = {
+ foo:'bar',
+ cookie:
+ {
+ expires: '2011-04-26T03:10:12.890Z'
+ }
+ };
+
+ store.set(sid, data, function(err, session) {
+ assert.strictEqual(err, null);
+
+ // Verify it was saved
+ collection.findOne({_id: sid}, function(err, session) {
+ assert.deepEqual(session.session, data);
+ assert.strictEqual(session._id, sid);
+ assert.equal(session.expires.toJSON(), new Date(data.cookie.expires).toJSON());
+
+ cleanup(store, db, collection, function() {
+ done();
+ });
+ });
});
});
};
@@ -445,7 +507,7 @@ exports.test_set_no_stringify_with_native_db = function(done) {
cleanup(store, db, collection, function() {
done();
});
- });
+ });
});
});
};
@@ -457,7 +519,7 @@ exports.test_set_expires_with_native_db = function(done) {
foo:'bar',
cookie:
{
- _expires: '2011-04-26T03:10:12.890Z'
+ expires: '2011-04-26T03:10:12.890Z'
}
};
@@ -468,7 +530,41 @@ exports.test_set_expires_with_native_db = function(done) {
collection.findOne({_id: sid}, function(err, session) {
assert.deepEqual(session.session, JSON.stringify(data));
assert.strictEqual(session._id, sid);
- assert.equal(session.expires.toJSON(), new Date(data.cookie._expires).toJSON());
+ assert.equal(session.expires.toJSON(), new Date(data.cookie.expires).toJSON());
+
+ cleanup(store, db, collection, function() {
+ done();
+ });
+ });
+ });
+ });
+};
+
+
+exports.test_set_expires_no_stringify_with_native_db = function(done) {
+ var options = {
+ db: options_with_mongo_native_db.db,
+ stringify: false
+ };
+
+ open_db(options, function(store, db, collection) {
+ var sid = 'test_set_expires-sid';
+ var data = {
+ foo:'bar',
+ cookie:
+ {
+ expires: '2011-04-26T03:10:12.890Z'
+ }
+ };
+
+ store.set(sid, data, function(err, session) {
+ assert.strictEqual(err, null);
+
+ // Verify it was saved
+ collection.findOne({_id: sid}, function(err, session) {
+ assert.deepEqual(session.session, data);
+ assert.strictEqual(session._id, sid);
+ assert.equal(session.expires.toJSON(), new Date(data.cookie.expires).toJSON());
cleanup(store, db, collection, function() {
done();

0 comments on commit 1008f3b

Please sign in to comment.