Router.js
This file
Path
test/Router.js
103 lines · 2.8 KiB · JavaScript
Last changed by
add throwing when a non-function is passed to a routeThorsten Lorenzbefore adoptionce7d7bf
History of this fileTake it
The walk stopped after 200 commits: lines older than 8ac3e80 point at it.
Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 1–78why, one step back12var express = require('../')3 , Router = express.Router4 , request = require('./support/http')5 , assert = require('assert');67describe('Router', function(){8 var router, app;910 beforeEach(function(){11 router = new Router;12 app = express();13 })1415 describe('.match(method, url, i)', function(){16 it('should match based on index', function(){17 router.route('get', '/foo', function(){});18 router.route('get', '/foob?', function(){});19 router.route('get', '/bar', function(){});2021 var method = 'GET';22 var url = '/foo?bar=baz';2324 var route = router.match(method, url, 0);25 route.constructor.name.should.equal('Route');26 route.method.should.equal('get');27 route.path.should.equal('/foo');2829 var route = router.match(method, url, 1);30 route.path.should.equal('/foob?');3132 var route = router.match(method, url, 2);33 assert(!route);3435 url = '/bar';36 var route = router.match(method, url);37 route.path.should.equal('/bar');38 })39 })40 41 describe('.matchRequest(req, i)', function(){42 it('should match based on index', function(){43 router.route('get', '/foo', function(){});44 router.route('get', '/foob?', function(){});45 router.route('get', '/bar', function(){});46 var req = { method: 'GET', url: '/foo?bar=baz' };4748 var route = router.matchRequest(req, 0);49 route.constructor.name.should.equal('Route');50 route.method.should.equal('get');51 route.path.should.equal('/foo');5253 var route = router.matchRequest(req, 1);54 req._route_index.should.equal(1);55 route.path.should.equal('/foob?');5657 var route = router.matchRequest(req, 2);58 assert(!route);5960 req.url = '/bar';61 var route = router.matchRequest(req);62 route.path.should.equal('/bar');63 })64 })6566 describe('.middleware', function(){67 it('should dispatch', function(done){68 router.route('get', '/foo', function(req, res){69 res.send('foo');70 });7172 app.use(router.middleware);7374 request(app)75 .get('/foo')76 .expect('foo', done);77 })78 })add throwing when a non-function is passed to a routeThorsten Lorenzbefore adoptionce7d7bf
lines 79–102why, one step back7980 describe('.multiple callbacks', function(){81 it('should throw if a callback is null', function(){82 assert.throws(function () {83 router.route('get', '/foo', null, function(){});84 })85 })8687 it('should throw if a callback is undefined', function(){88 assert.throws(function () {89 router.route('get', '/foo', undefined, function(){});90 })91 })9293 it('should throw if a callback is not a function', function(){94 assert.throws(function () {95 router.route('get', '/foo', 'not a function', function(){});96 })97 })9899 it('should not throw if all callbacks are functions', function(){100 router.route('get', '/foo', function(){}, function(){});101 })102 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
line 103why, one step back103})