Bulk of memory store based session support complete.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 6b9a079 · Browse files · 4 files·+64 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+64 added−17 removed
- tests:+29 added−10 removed
- other:+35 added−7 removed
Provenance
- Commit
f4b4c03- Tree
234ccc4- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
6b9a079
4 files changed+64 added−17 removed
modified:lib/express/plugins/cookie.js+4 added-3 removed
86 unmodified lines878889909191929394959697969798991001011028 unmodified lines86 unmodified lines 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) : {} }, /** * Set the Set-Cookie header when response cookies are available.8 unmodified linesmodified:lib/express/plugins/session.js+31 added-4 removed
123456789101112131415161718192021222324258262728291230313233343536373821394041424344454647484950515228535455565758596061
// 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) } }, // --- Events 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) } }})No newline at end of fileNo newline at end of filemodified:lib/express/spec/mocks.js+2 added
97 unmodified lines98 reset = function() {99 Express.routes = []100 Express.plugins = []101 Express.settings = {}102+ if (typeof Session !== 'undefined')103+ Session.store.clear()104 configure('test')105 }106 107 /**24 unmodified linesmodified:spec/spec.plugins.session.js+27 added-10 removed
5 unmodified lines6789101112131415161718191011121314151617181920212223242526272829303132333435363738395 unmodified lines use(require('express/plugins/session').Session) 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 endendNo newline at end of fileNo newline at end of file