Skip to content

Commit

Permalink
fixed #3 handle must be function
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 18, 2013
1 parent 00be598 commit 3b4377f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -40,4 +40,4 @@ test-version:

test-all: test test-cov

.PHONY: install test test-cov test-version test-all
.PHONY: install test test-cov test-version test-all
8 changes: 7 additions & 1 deletion lib/urlrouter.js
Expand Up @@ -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
Expand All @@ -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]);
};
}
Expand Down Expand Up @@ -117,6 +122,7 @@ function router(fn, options) {
return handle(req, res, next);
}
};

return routeMiddleware();
}
}
Expand Down
27 changes: 26 additions & 1 deletion test/urlrouter.test.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -422,4 +447,4 @@ describe('use connect with options.errorHandler()', function () {
done();
});
});
});
});

0 comments on commit 3b4377f

Please sign in to comment.