cookie() now accepts name/val params
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 48bea03 · Browse files · 2 files·+31 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−44 removed
- tests:+18 added−28 removed
- other:+13 added−16 removed
Provenance
- Commit
f2c484d- Tree
0076df8- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
48bea03
2 files changed+31 added−44 removed
modified:lib/express/plugins/cookie.js+13 added-16 removed
10 unmodified lines11121314151516171819202121222324252526272829303130313233343536378 unmodified lines46474849485051525354555654555657585758596061626364656667686970717071727374727310 unmodified lines
exports.parseCookie = function(cookie) { return $(cookie.replace(/^ *| *$/g, '').split(/ *; */)).reduce({}, function(hash, pair){ var parts = pair.split(/ *= */) hash[parts[0].toLowerCase()] = parts[1] hash[parts[0]] = parts[1] return hash })}
/** * Convert _hash_ to a cookie string. * Compile cookie _name_, _val_ and _options_ to a string. * * All dates are converted to 'Wdy, DD-Mon-YYYY HH:MM:SS GMT' * * @param {hash} hash * @param {string} name * @param {string} val * @param {hash} options * @return {string} * @api public */
exports.compileCookie = function(hash) { return $(hash).map(function(val, key){exports.compileCookie = function(name, val, options) { return name + '=' + val + '; ' + $(options).map(function(val, key){ if (val instanceof Date) val = val.toString() .replace(/^(\w+)/, '$1,') .replace(/(\w+) (\d+) (\d+)/, '$2-$1-$3')8 unmodified lines /** * Get or set cookie values. * * @param {string} key * @param {string} name * @param {string} val * @param {hash} options * @return {string} * @api public */
cookie: function(key, val) { if (!this.response.cookie) this.response.cookie = {} return val === undefined ? this.response.cookie[key] : this.response.cookie[key] = val cookie: function(name, val, options) { if (!val) return this.response.cookie[name] this.header('set-cookie', exports.compileCookie(name, val, options)) } })
// --- Cookie
exports.Cookie = Plugin.extend({ on: { request: function(event) { event.request.response.cookie = {} if (event.request.headers.cookie) event.request.response.cookie = exports.parseCookie(event.request.headers.cookie) }, response: function(event) { if (event.request.response.cookie) event.request.header('set-cookie', exports.compileCookie(event.request.response.cookie)) } }})No newline at end of fileNo newline at end of filemodified:spec/spec.plugins.cookie.js+18 added-28 removed
8 unmodified lines91011121313141516171718192021212223242526262728293030313233343535363738394041424344454647454647484950515252535455564 unmodified lines616263646566676869707172737475767778798081826566676869707172737475767778798081828384858 unmodified lines describe 'Cookie' describe 'compileCookie()' it 'should return a cookie string' var data = { var options = { path: '/', domain: '.vision-media.ca' } compileCookie(data).should.eql 'path=/; domain=.vision-media.ca' compileCookie('foo', 'bar', options).should.eql 'foo=bar; path=/; domain=.vision-media.ca' end it 'should currectly format any Date objects' var data = { var options = { expires: new Date('May 25, 1987 11:13:00'), path: '/foo', domain: '.vision-media.ca' } compileCookie(data).should.eql 'expires=Mon, 25-May-1987 11:13:00 GMT; path=/foo; domain=.vision-media.ca' compileCookie('foo', 'bar', options).should.eql 'foo=bar; expires=Mon, 25-May-1987 11:13:00 GMT; path=/foo; domain=.vision-media.ca' end it 'should convert true to a key without a value' var data = { var options = { path: '/', secure: true, httpOnly: true } compileCookie(data).should.eql 'path=/; secure; httpOnly' compileCookie('foo', 'bar', options).should.eql 'foo=bar; path=/; secure; httpOnly' end end describe 'parseCookie()' it 'should parse cookie key/value pairs' var attrs = 'sid=1232431234234; data=foo' parseCookie(attrs).should.eql { sid: '1232431234234', data: 'foo' } end it 'should normalize keys to lowercase' var attrs = 'SID=1232431234234; data=foo' parseCookie(attrs).should.eql { sid: '1232431234234', data: 'foo' } it 'should preserve case' var attrs = 'SID=1232431234234; Data=foo' parseCookie(attrs).should.eql { SID: '1232431234234', Data: 'foo' } end it 'should disregard ad-hoc invalid whitespace' var attrs = ' SID = 1232431234234 ; data = foo' parseCookie(attrs).should.eql { sid: '1232431234234', data: 'foo' } parseCookie(attrs).should.eql { SID: '1232431234234', data: 'foo' } end end describe 'on'4 unmodified lines }) get('/user', { headers: { cookie: 'foo=bar' }}).body.should.eql 'bar' end end describe 'response' it 'should set the Set-Cookie header' get('/user', function(){ this.cookie('SID', '732423sdfs73243') this.cookie('path', '/') return '' }) get('/user').headers['set-cookie'].should.eql 'SID=732423sdfs73243; path=/' end it 'should not set the Set-Cookie header when nothing is available' get('/user', function(){ return '' }) get('/user').headers.should.not.have_property 'set-cookie' end end end describe 'cookie()' it 'should get request cookie values' get('/user', function(){ return this.cookie('foo') }) get('/user', { headers: { cookie: 'foo=bar' }}).body.should.eql 'bar' end it 'should populate Set-Cookie' get('/user', function(){ this.cookie('SID', '732423sdfs73243', { path: '/', secure: true }) return '' }) get('/user').headers['set-cookie'].should.eql 'SID=732423sdfs73243; path=/; secure' end end endendNo newline at end of fileNo newline at end of file