From 3b4377f24f6bb6556ab915810384d678546c9b8d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 18 Jul 2013 12:20:30 +0800 Subject: [PATCH] fixed #3 handle must be function --- Makefile | 2 +- lib/urlrouter.js | 8 +++++++- test/urlrouter.test.js | 27 ++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f3a35f7..560a90e 100644 --- a/Makefile +++ b/Makefile @@ -40,4 +40,4 @@ test-version: test-all: test test-cov -.PHONY: install test test-cov test-version test-all \ No newline at end of file +.PHONY: install test test-cov test-version test-all diff --git a/lib/urlrouter.js b/lib/urlrouter.js index e702fd5..eaadd99 100644 --- a/lib/urlrouter.js +++ b/lib/urlrouter.js @@ -58,7 +58,7 @@ function router(fn, options) { function createMethod(name) { var localRoutes = routes[name.toUpperCase()] = []; // fn(url[, middleware[s]], handle) - return function (urlpattern, handle) { + return function routeMethod(urlpattern, handle) { var middleware = null; // slice middleware @@ -68,6 +68,11 @@ function router(fn, options) { middleware = utils.flatten(middleware); } + var t = typeof handle; + if (t !== 'function') { + throw new TypeError('handle must be function, not ' + t); + } + localRoutes.push([utils.createRouter(urlpattern), handle, middleware]); }; } @@ -117,6 +122,7 @@ function router(fn, options) { return handle(req, res, next); } }; + return routeMiddleware(); } } diff --git a/test/urlrouter.test.js b/test/urlrouter.test.js index 119b810..2293a84 100644 --- a/test/urlrouter.test.js +++ b/test/urlrouter.test.js @@ -381,6 +381,31 @@ var routerWithCustomHandler = urlrouter(function (app) { } }); +describe('router.get("/home", undefined)', function () { + it('should throw type error', function () { + (function () { + urlrouter(function (app) { + app.get('/error'); + }); + }).should.throw('handle must be function, not undefined'); + (function () { + urlrouter(function (app) { + app.get('/error', null); + }); + }).should.throw('handle must be function, not object'); + (function () { + urlrouter(function (app) { + app.get('/error', 123); + }); + }).should.throw('handle must be function, not number'); + (function () { + urlrouter(function (app) { + app.get('/error', {}); + }); + }).should.throw('handle must be function, not object'); + }); +}); + describe('options.pageNotFound() and options.errorHandler()', function () { var app; before(function (done) { @@ -422,4 +447,4 @@ describe('use connect with options.errorHandler()', function () { done(); }); }); -}); \ No newline at end of file +});