Skip to content

Commit

Permalink
Session() casts object into Session
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 28, 2010
1 parent 10b6c5a commit 29275ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/connect/middleware/session/session.js
Expand Up @@ -12,15 +12,26 @@
var utils = require('./../../utils');

/**
* Initialize Session with the given sid.
* Initialize Session with the given sid, or cast a session-like
* object into a Session. This is useful when your data is serialized
* and stuffed into a database.
*
* Examples:
*
* new Session(123);
* Session({ id: 123, lastAccess: 4324394 });
*
* @param {String} sid
* @api public
*/

var Session = exports.Session = function Session(sid) {
this.id = sid;
this.touch();
if (this === global) {
return utils.merge(new Session, sid);
} else if (sid) {
this.id = sid;
this.touch();
}
};

/**
Expand Down
11 changes: 11 additions & 0 deletions test/session.test.js
Expand Up @@ -8,11 +8,22 @@ var connect = require('connect'),
assert = require('assert'),
http = require('http');

// Session

var Session = require('connect/middleware/session/session').Session;

// Stores

var MemoryStore = require('connect/middleware/session/memory').MemoryStore;

module.exports = {
'test Session() cast': function(){
var sess = Session({ id: '123', lastAccess: 123 });
assert.ok(sess instanceof Session, 'Test Session() cast');
assert.equal('123', sess.id);
assert.equal(123, sess.lastAccess);
},

'test MemoryStore': function(){
var n = 0, sid;
var server = helpers.run(
Expand Down

0 comments on commit 29275ac

Please sign in to comment.