Skip to content
Myr

Router.js

This file

Path

test/Router.js

187 lines · 4.7 KiB · JavaScript

Last changed by

don't call done twice for thrown errors inside parameterized routesRoman Shtylmanbefore adoptioncb1fbce

History of this file

Take it

The walk stopped after 200 commits: lines older than c1d16e0 point at it.

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 1–3why, one step back
1
2var express = require('../')3 , Router = express.Router

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 4why, one step back
4 , methods = require('methods')

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 5–7why, one step back
5 , assert = require('assert');
67describe('Router', function(){

make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b

lines 8–19why, one step back
8 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 });19

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 20–22why, one step back
20 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 back
23

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 24–25why, one step back
24 another.get('/bar', function(req, res){
25 res.end();

make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b

lines 26–28why, one step back
26 });
27 router.use('/foo', another);28

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 29–42why, one step back
29 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 back
43 });
4445 describe('.handle', function(){

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

line 46why, one step back
46 it('should dispatch', function(done){

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 47–49why, one step back
47 var router = new Router();
4849 router.route('/foo').get(function(req, res){

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 50–52why, one step back
50 res.send('foo');
51 });52

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 53–59why, one step back
53 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.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 60–65why, one step back
60 })
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 back
66 var router = new Router();
67 router.route('/foo').all(null);

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 68–72why, one step back
68 })
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 back
73 var router = new Router();
74 router.route('/foo').all(undefined);

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 75–79why, one step back
75 })
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 back
80 var router = new Router();
81 router.route('/foo').all('not a function');

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 82–85why, one step back
82 })
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 back
86 var router = new Router();
87 router.route('/foo').all(function(){}).all(function(){});

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 88–90why, one step back
88 })
89 })90

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 91–113why, one step back
91 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 });

don't call done twice for thrown errors inside parameterized routesRoman Shtylmanbefore adoptioncb1fbce

lines 114–132why, one step back
114
115 it('should handle throwing inside routes with params', function(done) {116 var router = new Router();117118 router.get('/foo/:id', function(req, res, next){119 throw new Error('foo');120 });121122 router.use(function(req, res, next){123 assert(false);124 });125126 router.use(function(err, req, res, next){127 assert.equal(err.message, 'foo');128 done();129 });130131 router.handle({ url: '/foo/2', method: 'GET' }, {}, done);132 });

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 133–134why, one step back
133 })
134

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 135why, one step back
135 describe('.all', function() {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

line 136why, one step back
136 it('should support using .all to capture all http verbs', function(done){

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 137–138why, one step back
137 var router = new Router();
138

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 139–140why, one step back
139 var count = 0;
140 router.all('/foo', function(){ count++; });

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 141–144why, one step back
141
142 var url = '/foo?bar=baz';143144 methods.forEach(function testMethod(method) {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

line 145why, one step back
145 router.handle({ url: url, method: method }, {}, function() {});

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 146why, one step back
146 });

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 147–149why, one step back
147
148 assert.equal(count, methods.length);149 done();

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

lines 150–151why, one step back
150 })
151 })

Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5

lines 152–186why, one step back
152
153 describe('.param', function() {154 it('should call param function when routing VERBS', function(done) {155 var router = new Router();156157 router.param('id', function(req, res, next, id) {158 assert.equal(id, '123');159 next();160 });161162 router.get('/foo/:id/bar', function(req, res, next) {163 assert.equal(req.params.id, '123');164 next();165 });166167 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);168 });169170 it('should call param function when routing middleware', function(done) {171 var router = new Router();172173 router.param('id', function(req, res, next, id) {174 assert.equal(id, '123');175 next();176 });177178 router.use('/foo/:id/bar', function(req, res, next) {179 assert.equal(req.params.id, '123');180 assert.equal(req.url, '/baz');181 next();182 });183184 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);185 });186 });

Release 3.3.7TJ Holowaychukbefore adoptionc1d16e0

line 187why, one step back
187})