Skip to content
Myr

Router.js

This file

Path

test/Router.js

321 lines · 8.1 KiB · JavaScript

Last changed by

Fix catching errors from top-level handlersDouglas Christopher Wilsonbefore adoption4d3e0d8

History of this file

Take it

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

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

line 1why, one step back
1

tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b

line 2why, one step back
2var after = require('after');

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 3–8why, one step back
3var express = require('../')
4 , Router = express.Router5 , methods = require('methods')6 , assert = require('assert');78describe('Router', function(){

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

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

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

lines 21–23why, one step back
21 it('should support .use of other routers', function(done){
22 var router = new Router();23 var another = new Router();

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

line 24why, one step back
24

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

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

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

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

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

lines 30–43why, one step back
30 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });
31 });3233 it('should support dynamic routes', function(done){34 var router = new Router();35 var another = new Router();3637 another.get('/:bar', function(req, res){38 req.params.bar.should.equal('route');39 res.end();40 });41 router.use('/:foo', another);4243 router.handle({ url: '/test/route', method: 'GET' }, { end: done });

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

lines 44–46why, one step back
44 });
4546 describe('.handle', function(){

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 54–60why, one step back
54 var res = {
55 send: function(val) {56 val.should.equal('foo');57 done();58 }59 }60 router.handle({ url: '/foo', method: 'GET' }, res);

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 61–66why, one step back
61 })
62 })6364 describe('.multiple callbacks', function(){65 it('should throw if a callback is null', function(){66 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 67–68why, one step back
67 var router = new Router();
68 router.route('/foo').all(null);

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 69–73why, one step back
69 })
70 })7172 it('should throw if a callback is undefined', function(){73 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 74–75why, one step back
74 var router = new Router();
75 router.route('/foo').all(undefined);

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 76–80why, one step back
76 })
77 })7879 it('should throw if a callback is not a function', function(){80 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 81–82why, one step back
81 var router = new Router();
82 router.route('/foo').all('not a function');

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 83–86why, one step back
83 })
84 })8586 it('should not throw if all callbacks are functions', function(){

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 87–88why, one step back
87 var router = new Router();
88 router.route('/foo').all(function(){}).all(function(){});

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 92–114why, one step back
92 describe('error', function(){
93 it('should skip non error middleware', function(done){94 var router = new Router();9596 router.get('/foo', function(req, res, next){97 next(new Error('foo'));98 });99100 router.get('/bar', function(req, res, next){101 next(new Error('bar'));102 });103104 router.use(function(req, res, next){105 assert(false);106 });107108 router.use(function(err, req, res, next){109 assert.equal(err.message, 'foo');110 done();111 });112113 router.handle({ url: '/foo', method: 'GET' }, {}, done);114 });

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

lines 115–131why, one step back
115
116 it('should handle throwing inside routes with params', function(done) {117 var router = new Router();118119 router.get('/foo/:id', function(req, res, next){120 throw new Error('foo');121 });122123 router.use(function(req, res, next){124 assert(false);125 });126127 router.use(function(err, req, res, next){128 assert.equal(err.message, 'foo');129 done();130 });131

Fix catching errors from top-level handlersDouglas Christopher Wilsonbefore adoption4d3e0d8

lines 132–154why, one step back
132 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});
133 });134135 it('should handle throwing in handler after async param', function(done) {136 var router = new Router();137138 router.param('user', function(req, res, next, val){139 process.nextTick(function(){140 req.user = val;141 next();142 });143 });144145 router.use('/:user', function(req, res, next){146 throw new Error('oh no!');147 });148149 router.use(function(err, req, res, next){150 assert.equal(err.message, 'oh no!');151 done();152 });153154 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});

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

line 155why, one step back
155 });

tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2

lines 156–174why, one step back
156
157 it('should handle throwing inside error handlers', function(done) {158 var router = new Router();159160 router.use(function(req, res, next){161 throw new Error('boom!');162 });163164 router.use(function(err, req, res, next){165 throw new Error('oops');166 });167168 router.use(function(err, req, res, next){169 assert.equal(err.message, 'oops');170 done();171 });172173 router.handle({ url: '/', method: 'GET' }, {}, done);174 });

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 175–176why, one step back
175 })
176

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 179–180why, one step back
179 var router = new Router();
180

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 183–186why, one step back
183
184 var url = '/foo?bar=baz';185186 methods.forEach(function testMethod(method) {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

line 188why, one step back
188 });

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 189–191why, one step back
189
190 assert.equal(count, methods.length);191 done();

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

lines 192–193why, one step back
192 })
193 })

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

lines 194–227why, one step back
194
195 describe('.param', function() {196 it('should call param function when routing VERBS', function(done) {197 var router = new Router();198199 router.param('id', function(req, res, next, id) {200 assert.equal(id, '123');201 next();202 });203204 router.get('/foo/:id/bar', function(req, res, next) {205 assert.equal(req.params.id, '123');206 next();207 });208209 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);210 });211212 it('should call param function when routing middleware', function(done) {213 var router = new Router();214215 router.param('id', function(req, res, next, id) {216 assert.equal(id, '123');217 next();218 });219220 router.use('/foo/:id/bar', function(req, res, next) {221 assert.equal(req.params.id, '123');222 assert.equal(req.url, '/baz');223 next();224 });225226 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);227 });

invoke router.param() only when necessaryDouglas Christopher Wilsonbefore adoption98d17e2

lines 228–281why, one step back
228
229 it('should only call once per request', function(done) {230 var count = 0;231 var req = { url: '/foo/bob/bar', method: 'get' };232 var router = new Router();233 var sub = new Router();234235 sub.get('/bar', function(req, res, next) {236 next();237 });238239 router.param('user', function(req, res, next, user) {240 count++;241 req.user = user;242 next();243 });244245 router.use('/foo/:user/', new Router());246 router.use('/foo/:user/', sub);247248 router.handle(req, {}, function(err) {249 if (err) return done(err);250 assert.equal(count, 1);251 assert.equal(req.user, 'bob');252 done();253 });254 });255256 it('should call when values differ', function(done) {257 var count = 0;258 var req = { url: '/foo/bob/bar', method: 'get' };259 var router = new Router();260 var sub = new Router();261262 sub.get('/bar', function(req, res, next) {263 next();264 });265266 router.param('user', function(req, res, next, user) {267 count++;268 req.user = user;269 next();270 });271272 router.use('/foo/:user/', new Router());273 router.use('/:user/bob/', sub);274275 router.handle(req, {}, function(err) {276 if (err) return done(err);277 assert.equal(count, 2);278 assert.equal(req.user, 'foo');279 done();280 });281 });

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

line 282why, one step back
282 });

fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a

lines 283–290why, one step back
283
284 describe('parallel requests', function() {285 it('should not mix requests', function(done) {286 var req1 = { url: '/foo/50/bar', method: 'get' };287 var req2 = { url: '/foo/10/bar', method: 'get' };288 var router = new Router();289 var sub = new Router();290

tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b

lines 291–292why, one step back
291 done = after(2, done);
292

fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a

lines 293–309why, one step back
293 sub.get('/bar', function(req, res, next) {
294 next();295 });296297 router.param('ms', function(req, res, next, ms) {298 ms = parseInt(ms, 10);299 req.ms = ms;300 setTimeout(next, ms);301 });302303 router.use('/foo/:ms/', new Router());304 router.use('/foo/:ms/', sub);305306 router.handle(req1, {}, function(err) {307 assert.ifError(err);308 assert.equal(req1.ms, 50);309 assert.equal(req1.originalUrl, '/foo/50/bar');

tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b

line 310why, one step back
310 done();

fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a

lines 311–316why, one step back
311 });
312313 router.handle(req2, {}, function(err) {314 assert.ifError(err);315 assert.equal(req2.ms, 10);316 assert.equal(req2.originalUrl, '/foo/10/bar');

tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b

line 317why, one step back
317 done();

fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a

lines 318–320why, one step back
318 });
319 });320 });

Merge pull request #1869 from yamatt/masterTJ Holowaychukbefore adoption72daae1

line 321why, one step back
321})