Router.js
This file
Path
test/Router.js
168 lines · 4.2 KiB · JavaScript
Last changed by
Merge pull request #1984 from blakeembrey/params-updateRoman Shtylmanbefore adoptioncc38ccc
History of this fileTake it
The walk stopped after 200 commits: lines older than f862ad2 point at it.
Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
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')Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 5–7why, one step back5 , assert = require('assert');67describe('Router', function(){make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b
lines 8–19why, one step back8 it('should return a function with router methods', function() {9 var router = Router();10 assert(typeof router == 'function');1112 var router = new Router();13 assert(typeof router == 'function');1415 assert(typeof router.get == 'function');16 assert(typeof router.handle == 'function');17 assert(typeof router.use == 'function');18 });19Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1
lines 20–22why, one step back20 it('should support .use of other routers', function(done){21 var router = new Router();22 var another = new Router();make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b
line 23why, one step back23Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1
lines 24–25why, one step back24 another.get('/bar', function(req, res){25 res.end();make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b
lines 26–28why, one step back26 });27 router.use('/foo', another);28Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1
lines 29–42why, one step back29 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });30 });3132 it('should support dynamic routes', function(done){33 var router = new Router();34 var another = new Router();3536 another.get('/:bar', function(req, res){37 req.params.bar.should.equal('route');38 res.end();39 });40 router.use('/:foo', another);4142 router.handle({ url: '/test/route', method: 'GET' }, { end: done });make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b
lines 43–45why, one step back43 });4445 describe('.handle', function(){Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
line 46why, one step back46 it('should dispatch', function(done){remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 47–49why, one step back47 var router = new Router();4849 router.route('/foo').get(function(req, res){Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 50–52why, one step back50 res.send('foo');51 });52remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 53–59why, one step back53 var res = {54 send: function(val) {55 val.should.equal('foo');56 done();57 }58 }59 router.handle({ url: '/foo', method: 'GET' }, res);Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 60–65why, one step back60 })61 })6263 describe('.multiple callbacks', function(){64 it('should throw if a callback is null', function(){65 assert.throws(function () {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 66–67why, one step back66 var router = new Router();67 router.route('/foo').all(null);Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 68–72why, one step back68 })69 })7071 it('should throw if a callback is undefined', function(){72 assert.throws(function () {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 73–74why, one step back73 var router = new Router();74 router.route('/foo').all(undefined);Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 75–79why, one step back75 })76 })7778 it('should throw if a callback is not a function', function(){79 assert.throws(function () {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 80–81why, one step back80 var router = new Router();81 router.route('/foo').all('not a function');Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 82–85why, one step back82 })83 })8485 it('should not throw if all callbacks are functions', function(){remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 86–87why, one step back86 var router = new Router();87 router.route('/foo').all(function(){}).all(function(){});add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
lines 88–90why, one step back88 })89 })90remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 91–115why, one step back91 describe('error', function(){92 it('should skip non error middleware', function(done){93 var router = new Router();9495 router.get('/foo', function(req, res, next){96 next(new Error('foo'));97 });9899 router.get('/bar', function(req, res, next){100 next(new Error('bar'));101 });102103 router.use(function(req, res, next){104 assert(false);105 });106107 router.use(function(err, req, res, next){108 assert.equal(err.message, 'foo');109 done();110 });111112 router.handle({ url: '/foo', method: 'GET' }, {}, done);113 });114 })115add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
line 116why, one step back116 describe('.all', function() {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 117why, one step back117 it('should support using .all to capture all http verbs', function(done){add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
lines 118–119why, one step back118 var router = new Router();119remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 120–121why, one step back120 var count = 0;121 router.all('/foo', function(){ count++; });add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
lines 122–125why, one step back122123 var url = '/foo?bar=baz';124125 methods.forEach(function testMethod(method) {remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 126why, one step back126 router.handle({ url: url, method: method }, {}, function() {});add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9
line 127why, one step back127 });remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 128–130why, one step back128129 assert.equal(count, methods.length);130 done();Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
lines 131–132why, one step back131 })132 })Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
lines 133–167why, one step back133134 describe('.param', function() {135 it('should call param function when routing VERBS', function(done) {136 var router = new Router();137138 router.param('id', function(req, res, next, id) {139 assert.equal(id, '123');140 next();141 });142143 router.get('/foo/:id/bar', function(req, res, next) {144 assert.equal(req.params.id, '123');145 next();146 });147148 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);149 });150151 it('should call param function when routing middleware', function(done) {152 var router = new Router();153154 router.param('id', function(req, res, next, id) {155 assert.equal(id, '123');156 next();157 });158159 router.use('/foo/:id/bar', function(req, res, next) {160 assert.equal(req.params.id, '123');161 assert.equal(req.url, '/baz');162 next();163 });164165 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);166 });167 });Release 3.2.2TJ Holowaychukbefore adoptionf862ad2
line 168why, one step back168})