Misc module progress
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent abcac57 · Browse files · 6 files·+142 added−98 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+142 added−98 removed
- tests:+48 added−34 removed
- other:+94 added−64 removed
Provenance
- Commit
15839de- Tree
1d74cd9- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
abcac57
6 files changed+142 added−98 removed
modified:lib/express.cookie.js+52 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
// Express - Cookie - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
(function(){ Express.Cookie = { name : 'cookie', settings : { maxAge : 3600 }, utilities : { /** * Return cookie _key_. When _value_ is present * then a response cookie field will be set. * * @param {string} key * @param {string} value * @return {string, hash} * @api public */
cookie : function(key, value) { return value ? Express.response.cookie[key] = value : Express.response.cookie[key] } }, onRequest : { 'parse cookie' : function() { Express.response.cookie = Express.request.cookie = Express.parseCookie(Express.header('Cookie')) } }, onResponse : { 'set cookie' : function() { var cookie = [] for (var key in Express.response.cookie) if (Express.response.cookie.hasOwnProperty(key)) cookie.push(key + '=' + Express.response.cookie[key]) if (cookie.length) Express.header('Set-Cookie', cookie.join('; ')) } } } })()No newline at end of filemodified:lib/express.core.js+41 added-63 removed
2 unmodified lines3 4 (function(){5 Express = { 6 version : '0.0.1',7+ utilities : {},8+ modules : [],9 routes : [],10 11 // --- Halt12 13 unmodified lines26 // --- Settings27 28 settings : {29 basepath : '/',28- 29- cookie : {30- maxAge : 360031- },32- 30 defaultRoute : {31 callback : function() {32 Express.respond('Page or file cannot be found', 'Not Found')33 }34 }35 },36 40- hookCallbacks : {41- request : [42- 43- // Redirect helpers44- 45- function (request, response) { 37+ // --- Modules38+ 39+ RedirectHelpers : {40+ onInit : {41+ 'set home' : function(){42 Express.home = Express.settings.basepath47- },48- 49- // Cookie parsing50- 51- function (request, response) {52- Express.response.cookie = request.cookie = Express.parseCookie(request.headers['Cookie'])43 }54- ],55- 56- response : [57- 58- // Content length59- 60- function(request, response) {44+ }45+ },46+ 47+ ContentLength : {48+ onRequest : {49+ 'set content length' : function() {50 Express.header('Content-Length', (Express.response.body || '').length)51 }63- ]52+ }53 },54 55 /**56 * Invoke hook _name_ with _args_.3 unmodified lines60 * @api public61 */62 63 hook : function(name, args) {75- if (this.hookCallbacks[name])76- for (i = 0, len = this.hookCallbacks[name].length; i < len; ++i)77- if (typeof this.hookCallbacks[name][i] == 'function')78- this.hookCallbacks[name][i].apply(this, this.argsArray(arguments, 1))64+ for (i = 0, len = this.modules.length; i < len; ++i)65+ if (typeof this.modules[i][name] == 'function')66+ this.modules[i][name].apply(this.modules[i], this.argsArray(arguments, 1))67 },68 69 /**82- * Add middlware _callback_ for hook _name_.70+ * Add _module_.71 *84- * @param {string} name85- * @param {function} callback72+ * @param {hash} module73 * @api public74 */75 89- use : function(name, callback) {90- Express.hookCallbacks[name].push(callback)76+ use : function(module) {77+ Express.settings[module.name] = {}78+79+ if ('init' in module) 80+ module.init.call(module)81+82+ if ('settings' in module) 83+ for (var name in module.settings)84+ if (module.settings.hasOwnProperty(name))85+ Express.settings[module.name][name] = module.settings[name]86+ 87+ if ('utilities' in module) 88+ for (var name in module.utilities)89+ if (module.utilities.hasOwnProperty(name))90+ Express.utilities[name] = module.utilities[name]91+ 92+ Express.modules.push(module)93 },94 95 /**96 * Start Express; binding to _host_ / _port_.36 unmodified lines133 Express.response.body = null134 Express.response.status = 200135 Express.request = request136 request.headers = Express.arrayToHash(request.headers)135- Express.hook('request', request, response)137+ Express.hook('onRequest')138 request.uri.params = Express.parseNestedParams(request.uri.params)139 request.uri.path = Express.normalizePath(request.uri.path)140 try { Express.response.body = Express.callRouteFor(request) }141 catch (e) { Express.fail(e) }140- Express.prepareCookie()141- Express.hook('response', Express.request, Express.response)142+ Express.hook('onResponse')143 response.sendHeader(Express.response.status, Express.hashToArray(Express.response.headers))144 response.sendBody(Express.response.body || '')145 response.finish()146 }12 unmodified lines159 this.response.body = e.message ? e.name + ': ' + e.message : e.toString()160 this.response.status = 500161 },162 162- prepareCookie : function() {163- var cookie = []164- for (var key in this.response.cookie)165- if (this.response.cookie.hasOwnProperty(key))166- cookie.push(key + '=' + this.response.cookie[key])167- if (cookie.length)168- this.header('Set-Cookie', cookie.join('; '))169- },170- 163 /**164 * Return an associative array generated165 * from the key / value pairs in _hash_.166 *123 unmodified lines290 param : function(key, defaultValue) {291 return this.request.uri.params[key] || defaultValue292 },293 302- 294 /**304- * Return cookie _key_. When _value_ is present305- * then a response cookie field will be set.306- *307- * @param {string} key308- * @param {string} value309- * @return {string, hash}310- * @api public311- */312-313- cookie : function(key, value) {314- return value ? this.response.cookie[key] = value :315- this.response.cookie[key]316- },317- 318- /**295 * Parse _params_, splitting { user[info][name] : 'tj' } into296 * { user : { info : { name : 'tj' }}}297 *298 * @param {hash} params116 unmodified lines415 * @api public416 */417 418 eval : function(string) {443- return eval('with (Express){' + string + '}')419+ return eval('with (Express){ with (Express.utilities){ ' + string + '}}')420 },421 422 /**423 * Escape special characters in _html_.143 unmodified lines567 del = Express.routeFunctionFor('delete')568 put = Express.routeFunctionFor('put')569 use = Express.use570 571+ use(Express.RedirectHelpers)572+ use(Express.ContentLength)573 })()modified:lib/express.mocks.js+1 added-1 removed
122345694 unmodified lines
// Express - Spec Helpers - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)// Express - Mocks - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
(function(){ /**94 unmodified linesadded:spec/spec.cookie.js+42 added
123456789101112131415161718192021222324252627282930313233343536373839404142
use(Express.Cookie)
describe 'Express' describe 'Cookie' before_each Express.routes = [] end describe 'settings' it 'should maxAge' Express.settings.cookie.maxAge.should.be_a Number end end describe 'onRequest' it 'should parse cookie fields' get('foo', function(){ cookie('q') }) response = get('foo', { headers : [['Cookie', 'path=/; q=something; domain=.example.net']] }) response.body.should.eql 'something' end end describe 'utilities' describe 'cookie()' before_each Express.response.cookie = Express.parseCookie('path=/; q=foo%3dbar; domain=.example.net') end
it 'should return cookie value when key passed' Express.Cookie.utilities.cookie('path').should.eql '/' Express.Cookie.utilities.cookie('domain').should.eql '.example.net' end
it 'should set response cookies when value passed' Express.Cookie.utilities.cookie('foo', 'bar') Express.Cookie.utilities.cookie('foo').should.eql 'bar' end end end endendNo newline at end of filemodified:spec/spec.core.js+1 added-31 removed
21 unmodified lines22 end23 24 describe '.parseCookie()'25 it 'should parse cookie pairs'26- cookie = 'expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; q=foo%3dbar; domain=example.net'26+ var cookie = 'expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; q=foo%3dbar; domain=example.net'27 parts = Express.parseCookie(cookie)28 parts.expires.should.eql 'Fri, 31-Dec-2010 23:59:59 GMT'29 parts.path.should.eql '/'30 parts.q.should.eql 'foo=bar'31 parts.domain.should.eql 'example.net'32 end33 end34 35- describe '.cookie()'36- before_each37- cookie = 'path=/; q=foo%3dbar; domain=.example.net'38- Express.response.cookie = Express.parseCookie(cookie)39- end40- 41- it 'should return cookie value when key passed'42- Express.cookie('path').should.eql '/'43- Express.cookie('domain').should.eql '.example.net'44- end45- 46- it 'should set response cookies when value passed'47- Express.cookie('foo', 'bar')48- Express.cookie('foo').should.eql 'bar'49- end50- end51- 35 describe '.argsArray()'36 it 'should return an array of arguments'37 Express.argsArray(-{ return arguments }('foo', 'bar')).should.eql ['foo', 'bar']38 end88 unmodified lines127 Express.arrayToHash(headers).should.eql { 'Content-Type' : 'text/plain' }128 end129 end130 148- describe '.hook()'149- it 'should invoke hook callbacks'150- var one, two151- Express.hookCallbacks = { 'test' : [152- function(a, b){ one = [a, b] },153- function(){ two = true }154- ] }155- Express.hook('test', 1, 2)156- one.should.eql [1, 2]157- two.should.be_true158- end159- end160- 131 describe '.redirect()'132 it 'should redirect to the url specified'133 get('foo', function(){ redirect('http://google.com') })134 response = get('foo')12 unmodified linesmodified:spec/spec.node.js+5 added-3 removed
1233456715 unmodified lines23242526272829302931323334353637383937404142434 unmodified lines
__loading__ = []__loadDelay__ = 700__loadDelay__ = 800
readFile = function(path, callback) { __loading__.push(path) var promise = node.fs.cat(path, "utf8")15 unmodified lines}
load('/Users/tjholowaychuk/scripts/gems/JSpec/lib/jspec.js')load('lib/express.core.js')load('lib/express.mocks.js')load('lib/express.mime.js')load('lib/express.cookie.js')load('lib/express.view.js')load('lib/express.mocks.js')
setTimeout(function(){ JSpec .exec('spec/spec.core.js') .exec('spec/spec.routing.js') .exec('spec/spec.mocks.js') .exec('spec/spec.mime.js') .exec('spec/spec.cookie.js') .exec('spec/spec.view.js') .exec('spec/spec.mocks.js') setTimeout(function(){ JSpec.run({ formatter : JSpec.formatters.Terminal, failuresOnly : true }) setTimeout(function() { JSpec.report()4 unmodified lines