Skip to content

Commit

Permalink
Bulk of memory store based session support complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 2, 2010
1 parent 6b9a079 commit f4b4c03
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
7 changes: 4 additions & 3 deletions lib/express/plugins/cookie.js
Expand Up @@ -88,13 +88,14 @@ exports.Cookie = Plugin.extend({
on: {

/**
* Parser request cookie data.
* Parse request cookie data.
*/

request: function(event) {
event.request.response.cookies = []
if (event.request.headers.cookie)
event.request.cookies = exports.parseCookie(event.request.headers.cookie)
event.request.cookies = event.request.headers.cookie ?
exports.parseCookie(event.request.headers.cookie) :
{}
},

/**
Expand Down
35 changes: 31 additions & 4 deletions lib/express/plugins/session.js
@@ -1,15 +1,33 @@

// Express - Session - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)

exports.MemoryStore = Class({
init: function() {
this.store = {}
},

fetch: function(sid) {
return this.store[sid] || {}
},

commit: function(sid, hash) {
return this.store[sid] = hash
},

clear: function() {
this.store = {}
}
})

exports.Session = Plugin.extend({
extend: {

/**
* Initialize extensions.
* Initialize memory store.
*/

init: function() {

this.store = new (set('session store') || exports.MemoryStore)
}
},

Expand All @@ -18,17 +36,26 @@ exports.Session = Plugin.extend({
on: {

/**
* Parser request cookie data.
* Create session id when not found; delegate to store.
*/

request: function(event) {
var request = event.request
if (!(request.sid = request.cookie('sid'))) {
var inOneDay = new Date(Number(new Date) + 86400),
options = set('session') || { expires: inOneDay }
request.cookie('sid', request.sid = uid(), options)
}
request.session = exports.Session.store.fetch(request.sid)
},

/**
* Set the Set-Cookie header when response cookies are available.
* Delegate to store, allowing it to save sessions changes.
*/

response: function(event) {
if (event.request.sid)
exports.Session.store.commit(event.request.sid, event.request.session)
}
}
})
2 changes: 2 additions & 0 deletions lib/express/spec/mocks.js
Expand Up @@ -99,6 +99,8 @@ reset = function() {
Express.routes = []
Express.plugins = []
Express.settings = {}
if (typeof Session !== 'undefined')
Session.store.clear()
configure('test')
}

Expand Down
37 changes: 27 additions & 10 deletions spec/spec.plugins.session.js
Expand Up @@ -7,16 +7,33 @@ describe 'Express'
end

describe 'Session'
it 'should persist within specs'
post('/login', function(){
this.session.name = 'tj'
return ''
})
get('/login', function(){
return this.session.name
})
get('/login').status.should.eql 200
get('/login').body.should.eql 'tj'
describe 'when sid cookie is not present'
it 'should set sid cookie'
get('/login', function(){ return '' })
get('/login').headers['set-cookie'].should.match(/^sid=(\w+);/)
end
end

describe 'when sid cookie is present'
it 'should not set sid'
get('/login', function(){ return '' })
get('/login', { headers: { cookie: 'sid=123' }}).headers.should.not.have_property 'set-cookie'
end
end

describe 'with MemoryStore'
it 'should persist'
post('/login', function(){
return this.session.name = 'tj'
})
get('/login', function(){
return this.session.name
})
var headers = { headers: { cookie: 'sid=123' }}
post('/login', headers)
get('/login', headers).status.should.eql 200
get('/login', headers).body.should.eql 'tj'
end
end
end
end

0 comments on commit f4b4c03

Please sign in to comment.