Added specs for param()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent eee6926 · Browse files · 3 files·+55 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+55 added−2 removed
- tests:+40 added−0 removed
- other:+15 added−2 removed
Provenance
- Commit
9f23c7b- Tree
65b74a4- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
eee6926
3 files changed+55 added−2 removed
modified:lib/express/core.js+6 added-1 removed
164 unmodified lines165 */166 167 mapParams: function(route) {168 route.keys.each(function(key, i){169- this.request.params.path[key] = this.request.captures[++i]169+ var val = this.request.captures[++i]170+ if (key in Express.params)171+ if ((val = Express.params[key].call(val, val)) === false)172+ this.request.passed = true173+ this.request.params.path[key] = val174 }, this)175 }176 })177 145 unmodified lines323 config: [],324 routes: [],325 plugins: [],326 settings: {},327+ params: {},328 server: new Server329 }330 331 // --- Defaults22 unmodified linesmodified:lib/express/dsl.js+9 added-1 removed
105 unmodified linesExpand all1061071081091101101111121131141151161171181191201211221231241251261271281292 unmodified lines105 unmodified linesExpand all fn = env, env = 'all' if (fn instanceof Function) return Express.config.push([env, fn]) if (typeof env !== 'string') throw new Error('environment required') throw new TypeError('environment required') Express.config.each(function(conf){ if (conf[0] === env || conf[0] === 'all') conf[1].call(Express) })}
exports.param = function(key, fn) { if (typeof key !== 'string') throw new TypeError('param key must be a string') if (typeof fn !== 'function') throw new TypeError('param must pass a function to process "' + key + '"') Express.params[key] = fn}
// --- Routing API
exports.get = exports.view = route('get')exports.post = exports.create = route('post')2 unmodified linesmodified:spec/spec.routing.js+40 added
2 unmodified lines3 before_each4 reset()5 end6 7+ describe 'param()'8+ describe 'given no args'9+ it 'should throw an error'10+ -{ param() }.should.throw_error TypeError11+ end12+ end13+ 14+ describe 'given a non-string as the first argument'15+ it 'should throw an error'16+ -{ param(12) }.should.throw_error TypeError17+ end18+ end19+ 20+ describe 'given no callback function'21+ it 'should throw an error'22+ -{ param('foo') }.should.throw_error TypeError23+ end24+ end25+ 26+ describe 'given a non-function as the second argument'27+ it 'should throw an error'28+ -{ param('foo', 2) }.should.throw_error TypeError29+ end30+ end31+ 32+ describe 'with key / callback function'33+ it 'should pre-process a value before it is passed to a route'34+ param('user_id', function(val){35+ val = parseInt(val)36+ return isNaN(val) ? false : val37+ })38+ get('/user/:user_id', function(id){39+ require('sys').p(id)40+ return String(typeof id === 'number')41+ })42+ get('/user/12').body.should.eql 'true'43+ end44+ end45+ end46+ 47 describe 'route'48 describe 'with callback function'49 it 'should respond with a body string'50 get('/user', function(){307 unmodified lines