Permalink
Browse files

add throwing when a non-function is passed to a route

  • Loading branch information...
1 parent 9bd86cd commit ce7d7bfd8dc985df02c12ee819ff5dbd423a7458 @thlorenz thlorenz committed with tj Dec 14, 2012
Showing with 33 additions and 3 deletions.
  1. +8 −2 lib/router/index.js
  2. +25 −1 test/Router.js
View
@@ -243,11 +243,17 @@ Router.prototype.route = function(method, path, callbacks){
// ensure path was given
if (!path) throw new Error('Router#' + method + '() requires a path');
+ // ensure all callbacks are functions
+ callbacks.forEach(function(fn){
+ if ('function' == typeof fn) return;
+ throw new Error('Router#' + method + '() requires all callbacks to be functions');
+ });
+
// create the route
debug('defined %s %s', method, path);
var route = new Route(method, path, callbacks, {
- sensitive: this.caseSensitive
- , strict: this.strict
+ sensitive: this.caseSensitive,
+ strict: this.strict
});
// add it
View
@@ -76,4 +76,28 @@ describe('Router', function(){
.expect('foo', done);
})
})
-})
+
+ describe('.multiple callbacks', function(){
+ it('should throw if a callback is null', function(){
+ assert.throws(function () {
+ router.route('get', '/foo', null, function(){});
+ })
+ })
+
+ it('should throw if a callback is undefined', function(){
+ assert.throws(function () {
+ router.route('get', '/foo', undefined, function(){});
+ })
+ })
+
+ it('should throw if a callback is not a function', function(){
+ assert.throws(function () {
+ router.route('get', '/foo', 'not a function', function(){});
+ })
+ })
+
+ it('should not throw if all callbacks are functions', function(){
+ router.route('get', '/foo', function(){}, function(){});
+ })
+ })
+})

0 comments on commit ce7d7bf

Please sign in to comment.