2 var express = require('../')
3 , Router = express.Router
4 , methods = require('methods')
5 , assert = require('assert');
7 describe('Router', function(){
9 it('should return a function with router methods', function() {
10 var router = Router();
11 assert(typeof router == 'function');
13 var router = new Router();
14 assert(typeof router == 'function');
16 assert(typeof router.get == 'function');
17 assert(typeof router.handle == 'function');
18 assert(typeof router.use == 'function');
21 it('should support .use of other routers', function(done) {
22 var router = Router();
23 var another = Router();
25 another.get('/bar', function(req, res) {
28 router.use('/foo', another);
30 router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });
33 it('should support dynamic routes', function(done){
34 var router = new Router();
35 var another = new Router();
37 another.get('/:bar', function(req, res){
38 req.params.foo.should.equal('test');
39 req.params.bar.should.equal('route');
42 router.use('/:foo', another);
44 router.handle({ url: '/test/route', method: 'GET' }, { end: done });
47 describe('.handle', function(){
48 it('should dispatch', function(done){
49 var router = new Router();
51 router.route('/foo').get(function(req, res){
57 val.should.equal('foo');
61 router.handle({ url: '/foo', method: 'GET' }, res);
65 describe('.multiple callbacks', function(){
66 it('should throw if a callback is null', function(){
67 assert.throws(function () {
68 var router = new Router();
69 router.route('/foo').all(null);
73 it('should throw if a callback is undefined', function(){
74 assert.throws(function () {
75 var router = new Router();
76 router.route('/foo').all(undefined);
80 it('should throw if a callback is not a function', function(){
81 assert.throws(function () {
82 var router = new Router();
83 router.route('/foo').all('not a function');
87 it('should not throw if all callbacks are functions', function(){
88 var router = new Router();
89 router.route('/foo').all(function(){}).all(function(){});
93 describe('error', function(){
94 it('should skip non error middleware', function(done){
95 var router = new Router();
97 router.get('/foo', function(req, res, next){
98 next(new Error('foo'));
101 router.get('/bar', function(req, res, next){
102 next(new Error('bar'));
105 router.use(function(req, res, next){
109 router.use(function(err, req, res, next){
110 assert.equal(err.message, 'foo');
114 router.handle({ url: '/foo', method: 'GET' }, {}, done);
118 describe('.all', function() {
119 it('should support using .all to capture all http verbs', function(done){
120 var router = new Router();
123 router.all('/foo', function(){ count++; });
125 var url = '/foo?bar=baz';
127 methods.forEach(function testMethod(method) {
128 router.handle({ url: url, method: method }, {}, function() {});
131 assert.equal(count, methods.length);
136 describe('.param', function() {
137 it('should call param function when routing VERBS', function(done) {
138 var router = new Router();
140 router.param('id', function(req, res, next, id) {
141 assert.equal(id, '123');
145 router.get('/foo/:id/bar', function(req, res, next) {
146 assert.equal(req.params.id, '123');
150 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);
153 it('should call param function when routing middleware', function(done) {
154 var router = new Router();
156 router.param('id', function(req, res, next, id) {
157 assert.equal(id, '123');
161 router.use('/foo/:id/bar', function(req, res, next) {
162 assert.equal(req.params.id, '123');
163 assert.equal(req.url, '/baz');
167 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);