Skip to content

Commit

Permalink
Use and test against session.cookie.expires instead of private proper…
Browse files Browse the repository at this point in the history
…ty session.cookie._expires
  • Loading branch information
lazd committed Jun 28, 2013
1 parent 5f6898e commit 1008f3b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 14 deletions.
8 changes: 4 additions & 4 deletions lib/connect-mongo.js
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
116 changes: 106 additions & 10 deletions test/connect-mongo.test.js
Expand Up @@ -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();
Expand Down Expand Up @@ -296,7 +324,7 @@ exports.test_set_no_stringify_with_raw_db = function(done) {
cleanup(store, db, collection, function() {
done();
});
});
});
});
});
};
Expand All @@ -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'
}
};

Expand All @@ -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();
});
});
});
});
};
Expand Down Expand Up @@ -445,7 +507,7 @@ exports.test_set_no_stringify_with_native_db = function(done) {
cleanup(store, db, collection, function() {
done();
});
});
});
});
});
};
Expand All @@ -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'
}
};

Expand All @@ -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();
Expand Down

0 comments on commit 1008f3b

Please sign in to comment.