Add tests for dynamic mounting.
[express.git] / test / Router.js
blob0a2b5b66df758fb30ced03ba06148e0b3e6c7872
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');
19   });
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) {
26       res.done();
27     });
28     router.use('/foo', another);
30     router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });
31   });
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');
40       res.end();
41     });
42     router.use('/:foo', another);
44     router.handle({ url: '/test/route', method: 'GET' }, { end: done });
45   });
47   describe('.handle', function(){
48     it('should dispatch', function(done){
49       var router = new Router();
51       router.route('/foo').get(function(req, res){
52         res.send('foo');
53       });
55       var res = {
56         send: function(val) {
57           val.should.equal('foo');
58           done();
59         }
60       }
61       router.handle({ url: '/foo', method: 'GET' }, res);
62     })
63   })
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);
70       })
71     })
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);
77       })
78     })
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');
84       })
85     })
87     it('should not throw if all callbacks are functions', function(){
88       var router = new Router();
89       router.route('/foo').all(function(){}).all(function(){});
90     })
91   })
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'));
99       });
101       router.get('/bar', function(req, res, next){
102         next(new Error('bar'));
103       });
105       router.use(function(req, res, next){
106         assert(false);
107       });
109       router.use(function(err, req, res, next){
110         assert.equal(err.message, 'foo');
111         done();
112       });
114       router.handle({ url: '/foo', method: 'GET' }, {}, done);
115     });
116   })
118   describe('.all', function() {
119     it('should support using .all to capture all http verbs', function(done){
120       var router = new Router();
122       var count = 0;
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() {});
129       });
131       assert.equal(count, methods.length);
132       done();
133     })
134   })
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');
142         next();
143       });
145       router.get('/foo/:id/bar', function(req, res, next) {
146         assert.equal(req.params.id, '123');
147         next();
148       });
150       router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);
151     });
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');
158         next();
159       });
161       router.use('/foo/:id/bar', function(req, res, next) {
162         assert.equal(req.params.id, '123');
163         assert.equal(req.url, '/baz');
164         next();
165       });
167       router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);
168     });
169   });