Router.js
This file
Path
test/Router.js
156 lines · 3.9 KiB · JavaScript
Last changed by
make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b
History of this fileTake it
The walk stopped after 200 commits: lines older than 15496da point at it.
remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 1–3why, one step back12var express = require('../')3 , Router = express.Routeradd Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
line 4why, one step back4 , methods = require('methods')remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 5–8why, one step back5 , assert = require('assert');67describe('Router', function(){8make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b
lines 9–33why, one step back9 it('should return a function with router methods', function() {10 var router = Router();11 assert(typeof router == 'function');1213 var router = new Router();14 assert(typeof router == 'function');1516 assert(typeof router.get == 'function');17 assert(typeof router.handle == 'function');18 assert(typeof router.use == 'function');19 });2021 it('should support .use of other routers', function(done) {22 var router = Router();23 var another = Router();2425 another.get('/bar', function(req, res) {26 res.done();27 });28 router.use('/foo', another);2930 router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });31 });3233 describe('.handle', function(){remove ./client.jsTJ Holowaychukbefore adoption15496da
line 34why, one step back34 it('should dispatch', function(done){remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 35–37why, one step back35 var router = new Router();3637 router.route('/foo').get(function(req, res){remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 38–40why, one step back38 res.send('foo');39 });40remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 41–47why, one step back41 var res = {42 send: function(val) {43 val.should.equal('foo');44 done();45 }46 }47 router.handle({ url: '/foo', method: 'GET' }, res);remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 48–53why, one step back48 })49 })5051 describe('.multiple callbacks', function(){52 it('should throw if a callback is null', function(){53 assert.throws(function () {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 54–55why, one step back54 var router = new Router();55 router.route('/foo').all(null);remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 56–60why, one step back56 })57 })5859 it('should throw if a callback is undefined', function(){60 assert.throws(function () {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 61–62why, one step back61 var router = new Router();62 router.route('/foo').all(undefined);remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 63–67why, one step back63 })64 })6566 it('should throw if a callback is not a function', function(){67 assert.throws(function () {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 68–69why, one step back68 var router = new Router();69 router.route('/foo').all('not a function');remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 70–73why, one step back70 })71 })7273 it('should not throw if all callbacks are functions', function(){remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 74–75why, one step back74 var router = new Router();75 router.route('/foo').all(function(){}).all(function(){});add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
lines 76–78why, one step back76 })77 })78remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 79–103why, one step back79 describe('error', function(){80 it('should skip non error middleware', function(done){81 var router = new Router();8283 router.get('/foo', function(req, res, next){84 next(new Error('foo'));85 });8687 router.get('/bar', function(req, res, next){88 next(new Error('bar'));89 });9091 router.use(function(req, res, next){92 assert(false);93 });9495 router.use(function(err, req, res, next){96 assert.equal(err.message, 'foo');97 done();98 });99100 router.handle({ url: '/foo', method: 'GET' }, {}, done);101 });102 })103add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
line 104why, one step back104 describe('.all', function() {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 105why, one step back105 it('should support using .all to capture all http verbs', function(done){add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
lines 106–107why, one step back106 var router = new Router();107remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 108–109why, one step back108 var count = 0;109 router.all('/foo', function(){ count++; });add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
lines 110–113why, one step back110111 var url = '/foo?bar=baz';112113 methods.forEach(function testMethod(method) {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 114why, one step back114 router.handle({ url: url, method: method }, {}, function() {});add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
line 115why, one step back115 });remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 116–118why, one step back116117 assert.equal(count, methods.length);118 done();remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 119–120why, one step back119 })120 })Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 121–155why, one step back121122 describe('.param', function() {123 it('should call param function when routing VERBS', function(done) {124 var router = new Router();125126 router.param('id', function(req, res, next, id) {127 assert.equal(id, '123');128 next();129 });130131 router.get('/foo/:id/bar', function(req, res, next) {132 assert.equal(req.params.id, '123');133 next();134 });135136 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);137 });138139 it('should call param function when routing middleware', function(done) {140 var router = new Router();141142 router.param('id', function(req, res, next, id) {143 assert.equal(id, '123');144 next();145 });146147 router.use('/foo/:id/bar', function(req, res, next) {148 assert.equal(req.params.id, '123');149 assert.equal(req.url, '/baz');150 next();151 });152153 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);154 });155 });remove ./client.jsTJ Holowaychukbefore adoption15496da
line 156why, one step back156})