Router.js
This file
Path
test/Router.js
121 lines · 3.3 KiB · JavaScript
Last changed by
remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
History of this fileTake it
The walk stopped after 200 commits: lines older than d581592 point at it.
3.5.2Douglas Christopher Wilsonbefore adoptiond581592
lines 1–3why, one step back12var express = require('../')3 , Router = express.Routerremove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
line 4why, one step back4 , request = require('supertest')3.5.2Douglas Christopher Wilsonbefore adoptiond581592
lines 5–121why, one step back5 , methods = require('methods')6 , assert = require('assert');78describe('Router', function(){9 var router, app;1011 beforeEach(function(){12 router = new Router;13 app = express();14 })1516 describe('.match(method, url, i)', function(){17 it('should match based on index', function(){18 router.route('get', '/foo', function(){});19 router.route('get', '/foob?', function(){});20 router.route('get', '/bar', function(){});2122 var method = 'GET';23 var url = '/foo?bar=baz';2425 var route = router.match(method, url, 0);26 route.constructor.name.should.equal('Route');27 route.method.should.equal('get');28 route.path.should.equal('/foo');2930 var route = router.match(method, url, 1);31 route.path.should.equal('/foob?');3233 var route = router.match(method, url, 2);34 assert(!route);3536 url = '/bar';37 var route = router.match(method, url);38 route.path.should.equal('/bar');39 })40 })41 42 describe('.matchRequest(req, i)', function(){43 it('should match based on index', function(){44 router.route('get', '/foo', function(){});45 router.route('get', '/foob?', function(){});46 router.route('get', '/bar', function(){});47 var req = { method: 'GET', url: '/foo?bar=baz' };4849 var route = router.matchRequest(req, 0);50 route.constructor.name.should.equal('Route');51 route.method.should.equal('get');52 route.path.should.equal('/foo');5354 var route = router.matchRequest(req, 1);55 req._route_index.should.equal(1);56 route.path.should.equal('/foob?');5758 var route = router.matchRequest(req, 2);59 assert(!route);6061 req.url = '/bar';62 var route = router.matchRequest(req);63 route.path.should.equal('/bar');64 })65 })6667 describe('.middleware', function(){68 it('should dispatch', function(done){69 router.route('get', '/foo', function(req, res){70 res.send('foo');71 });7273 app.use(router.middleware);7475 request(app)76 .get('/foo')77 .expect('foo', done);78 })79 })8081 describe('.multiple callbacks', function(){82 it('should throw if a callback is null', function(){83 assert.throws(function () {84 router.route('get', '/foo', null, function(){});85 })86 })8788 it('should throw if a callback is undefined', function(){89 assert.throws(function () {90 router.route('get', '/foo', undefined, function(){});91 })92 })9394 it('should throw if a callback is not a function', function(){95 assert.throws(function () {96 router.route('get', '/foo', 'not a function', function(){});97 })98 })99100 it('should not throw if all callbacks are functions', function(){101 router.route('get', '/foo', function(){}, function(){});102 })103 })104105 describe('.all', function() {106 it('should support using .all to capture all http verbs', function() {107 var router = new Router();108109 router.all('/foo', function(){});110111 var url = '/foo?bar=baz';112113 methods.forEach(function testMethod(method) {114 var route = router.match(method, url);115 route.constructor.name.should.equal('Route');116 route.method.should.equal(method);117 route.path.should.equal('/foo');118 });119 })120 })121})