Skip to content
Browse files

Merge pull request #74 from lazd/master

Convert the cookie object to its JSON representation before storing
  • Loading branch information...
2 parents f199378 + 1008f3b commit cd61be364691206dfdf052699d0a7a53dc0fc2ce @kcbanner committed
Showing with 113 additions and 13 deletions.
  1. +7 −3 lib/connect-mongo.js
  2. +106 −10 test/connect-mongo.test.js
View
10 lib/connect-mongo.js
@@ -217,11 +217,11 @@ module.exports = function(connect) {
MongoStore.prototype.set = function(sid, session, callback) {
try {
- var s = {_id: sid, session: this._serialize_session(session)};
+ 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.
@@ -231,6 +231,10 @@ module.exports = function(connect) {
twoWeeks = 1000 * 60 * 60 * 24 * 14;
s.expires = new Date(today.getTime() + twoWeeks);
}
+
+ // 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 ? session.cookie.toJSON() : session.cookie;
}
this._get_collection(function(collection) {
View
116 test/connect-mongo.test.js
@@ -123,10 +123,10 @@ 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);
@@ -134,7 +134,35 @@ exports.test_set_expires = 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 = 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 cd61be3

Please sign in to comment.
Something went wrong with that request. Please try again.