Skip to content
Myr

Router.js

This file

Path

test/Router.js

613 lines · 16.2 KiB · JavaScript

Last changed by

lint: remove unused function arguments in testsRakesh Bishtbefore adoptionc6ee8d6

History of this file

Take it

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

tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137

line 1why, one step back
1'use strict'

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 2–35why, one step back
2
3var after = require('after');4var express = require('../')5 , Router = express.Router6 , methods = require('methods')7 , assert = require('assert');89describe('Router', function(){10 it('should return a function with router methods', function() {11 var router = new Router();12 assert(typeof router === 'function')1314 assert(typeof router.get === 'function')15 assert(typeof router.handle === 'function')16 assert(typeof router.use === 'function')17 });1819 it('should support .use of other routers', function(done){20 var router = new Router();21 var another = new Router();2223 another.get('/bar', function(req, res){24 res.end();25 });26 router.use('/foo', another);2728 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });29 });3031 it('should support dynamic routes', function(done){32 var router = new Router();33 var another = new Router();3435 another.get('/:bar', function(req, res){

tests: remove should usage from Router testsDouglas Christopher Wilsonbefore adoption8e4add7

line 36why, one step back
36 assert.strictEqual(req.params.bar, 'route')

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 37–47why, one step back
37 res.end();
38 });39 router.use('/:foo', another);4041 router.handle({ url: '/test/route', method: 'GET' }, { end: done });42 });4344 it('should handle blank URL', function(done){45 var router = new Router();4647 router.use(function (req, res) {

tests: remove should usage from Router testsDouglas Christopher Wilsonbefore adoption8e4add7

line 48why, one step back
48 throw new Error('should not be called')

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 49–64why, one step back
49 });
5051 router.handle({ url: '', method: 'GET' }, {}, done);52 });5354 it('should handle missing URL', function (done) {55 var router = new Router()5657 router.use(function (req, res) {58 throw new Error('should not be called')59 })6061 router.handle({ method: 'GET' }, {}, done)62 })6364 it('should not stack overflow with many registered routes', function(done){

tests: mark stack overflow as long runningphoenixbefore adoptionfd8e45c

lines 65–66why, one step back
65 this.timeout(5000) // long-running test
66

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 67–80why, one step back
67 var handler = function(req, res){ res.end(new Error('wrong handler')) };
68 var router = new Router();6970 for (var i = 0; i < 6000; i++) {71 router.get('/thing' + i, handler)72 }7374 router.get('/', function (req, res) {75 res.end();76 });7778 router.handle({ url: '/', method: 'GET' }, { end: done });79 });80

Fix hanging on large stack of sync routesDouglas Christopher Wilsonbefore adoption631ada0

lines 81–85why, one step back
81 it('should not stack overflow with a large sync route stack', function (done) {
82 this.timeout(5000) // long-running test8384 var router = new Router()85

tests: verify all handlers called in stack testsDouglas Christopher Wilsonbefore adoption97f0a51

lines 86–90why, one step back
86 router.get('/foo', function (req, res, next) {
87 req.counter = 088 next()89 })90

Fix hanging on large stack of sync routesDouglas Christopher Wilsonbefore adoption631ada0

line 91why, one step back
91 for (var i = 0; i < 6000; i++) {

tests: verify all handlers called in stack testsDouglas Christopher Wilsonbefore adoption97f0a51

lines 92–95why, one step back
92 router.get('/foo', function (req, res, next) {
93 req.counter++94 next()95 })

Fix hanging on large stack of sync routesDouglas Christopher Wilsonbefore adoption631ada0

lines 96–98why, one step back
96 }
9798 router.get('/foo', function (req, res) {

tests: verify all handlers called in stack testsDouglas Christopher Wilsonbefore adoption97f0a51

line 99why, one step back
99 assert.strictEqual(req.counter, 6000)

Fix hanging on large stack of sync routesDouglas Christopher Wilsonbefore adoption631ada0

lines 100–106why, one step back
100 res.end()
101 })102103 router.handle({ url: '/foo', method: 'GET' }, { end: done })104 })105106 it('should not stack overflow with a large sync middleware stack', function (done) {

Fix handling very large stacks of sync middlewareDouglas Christopher Wilsonbefore adoption708ac4c

lines 107–110why, one step back
107 this.timeout(5000) // long-running test
108109 var router = new Router()110

tests: verify all handlers called in stack testsDouglas Christopher Wilsonbefore adoption97f0a51

lines 111–115why, one step back
111 router.use(function (req, res, next) {
112 req.counter = 0113 next()114 })115

Fix handling very large stacks of sync middlewareDouglas Christopher Wilsonbefore adoption708ac4c

line 116why, one step back
116 for (var i = 0; i < 6000; i++) {

tests: verify all handlers called in stack testsDouglas Christopher Wilsonbefore adoption97f0a51

lines 117–120why, one step back
117 router.use(function (req, res, next) {
118 req.counter++119 next()120 })

Fix handling very large stacks of sync middlewareDouglas Christopher Wilsonbefore adoption708ac4c

lines 121–123why, one step back
121 }
122123 router.use(function (req, res) {

tests: verify all handlers called in stack testsDouglas Christopher Wilsonbefore adoption97f0a51

line 124why, one step back
124 assert.strictEqual(req.counter, 6000)

Fix handling very large stacks of sync middlewareDouglas Christopher Wilsonbefore adoption708ac4c

lines 125–130why, one step back
125 res.end()
126 })127128 router.handle({ url: '/', method: 'GET' }, { end: done })129 })130

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 131–140why, one step back
131 describe('.handle', function(){
132 it('should dispatch', function(done){133 var router = new Router();134135 router.route('/foo').get(function(req, res){136 res.send('foo');137 });138139 var res = {140 send: function(val) {

tests: remove should usage from Router testsDouglas Christopher Wilsonbefore adoption8e4add7

line 141why, one step back
141 assert.strictEqual(val, 'foo')

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 142–203why, one step back
142 done();
143 }144 }145 router.handle({ url: '/foo', method: 'GET' }, res);146 })147 })148149 describe('.multiple callbacks', function(){150 it('should throw if a callback is null', function(){151 assert.throws(function () {152 var router = new Router();153 router.route('/foo').all(null);154 })155 })156157 it('should throw if a callback is undefined', function(){158 assert.throws(function () {159 var router = new Router();160 router.route('/foo').all(undefined);161 })162 })163164 it('should throw if a callback is not a function', function(){165 assert.throws(function () {166 var router = new Router();167 router.route('/foo').all('not a function');168 })169 })170171 it('should not throw if all callbacks are functions', function(){172 var router = new Router();173 router.route('/foo').all(function(){}).all(function(){});174 })175 })176177 describe('error', function(){178 it('should skip non error middleware', function(done){179 var router = new Router();180181 router.get('/foo', function(req, res, next){182 next(new Error('foo'));183 });184185 router.get('/bar', function(req, res, next){186 next(new Error('bar'));187 });188189 router.use(function(req, res, next){190 assert(false);191 });192193 router.use(function(err, req, res, next){194 assert.equal(err.message, 'foo');195 done();196 });197198 router.handle({ url: '/foo', method: 'GET' }, {}, done);199 });200201 it('should handle throwing inside routes with params', function(done) {202 var router = new Router();203

lint: remove unused function arguments in testsRakesh Bishtbefore adoptionc6ee8d6

line 204why, one step back
204 router.get('/foo/:id', function () {

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 205–613why, one step back
205 throw new Error('foo');
206 });207208 router.use(function(req, res, next){209 assert(false);210 });211212 router.use(function(err, req, res, next){213 assert.equal(err.message, 'foo');214 done();215 });216217 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});218 });219220 it('should handle throwing in handler after async param', function(done) {221 var router = new Router();222223 router.param('user', function(req, res, next, val){224 process.nextTick(function(){225 req.user = val;226 next();227 });228 });229230 router.use('/:user', function(req, res, next){231 throw new Error('oh no!');232 });233234 router.use(function(err, req, res, next){235 assert.equal(err.message, 'oh no!');236 done();237 });238239 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});240 });241242 it('should handle throwing inside error handlers', function(done) {243 var router = new Router();244245 router.use(function(req, res, next){246 throw new Error('boom!');247 });248249 router.use(function(err, req, res, next){250 throw new Error('oops');251 });252253 router.use(function(err, req, res, next){254 assert.equal(err.message, 'oops');255 done();256 });257258 router.handle({ url: '/', method: 'GET' }, {}, done);259 });260 })261262 describe('FQDN', function () {263 it('should not obscure FQDNs', function (done) {264 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };265 var router = new Router();266267 router.use(function (req, res, next) {268 assert.equal(req.hit++, 0);269 assert.equal(req.url, 'http://example.com/foo');270 next();271 });272273 router.handle(request, {}, function (err) {274 if (err) return done(err);275 assert.equal(request.hit, 1);276 done();277 });278 });279280 it('should ignore FQDN in search', function (done) {281 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };282 var router = new Router();283284 router.use('/proxy', function (req, res, next) {285 assert.equal(req.hit++, 0);286 assert.equal(req.url, '/?url=http://example.com/blog/post/1');287 next();288 });289290 router.handle(request, {}, function (err) {291 if (err) return done(err);292 assert.equal(request.hit, 1);293 done();294 });295 });296297 it('should ignore FQDN in path', function (done) {298 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };299 var router = new Router();300301 router.use('/proxy', function (req, res, next) {302 assert.equal(req.hit++, 0);303 assert.equal(req.url, '/http://example.com/blog/post/1');304 next();305 });306307 router.handle(request, {}, function (err) {308 if (err) return done(err);309 assert.equal(request.hit, 1);310 done();311 });312 });313314 it('should adjust FQDN req.url', function (done) {315 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };316 var router = new Router();317318 router.use('/blog', function (req, res, next) {319 assert.equal(req.hit++, 0);320 assert.equal(req.url, 'http://example.com/post/1');321 next();322 });323324 router.handle(request, {}, function (err) {325 if (err) return done(err);326 assert.equal(request.hit, 1);327 done();328 });329 });330331 it('should adjust FQDN req.url with multiple handlers', function (done) {332 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };333 var router = new Router();334335 router.use(function (req, res, next) {336 assert.equal(req.hit++, 0);337 assert.equal(req.url, 'http://example.com/blog/post/1');338 next();339 });340341 router.use('/blog', function (req, res, next) {342 assert.equal(req.hit++, 1);343 assert.equal(req.url, 'http://example.com/post/1');344 next();345 });346347 router.handle(request, {}, function (err) {348 if (err) return done(err);349 assert.equal(request.hit, 2);350 done();351 });352 });353354 it('should adjust FQDN req.url with multiple routed handlers', function (done) {355 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };356 var router = new Router();357358 router.use('/blog', function (req, res, next) {359 assert.equal(req.hit++, 0);360 assert.equal(req.url, 'http://example.com/post/1');361 next();362 });363364 router.use('/blog', function (req, res, next) {365 assert.equal(req.hit++, 1);366 assert.equal(req.url, 'http://example.com/post/1');367 next();368 });369370 router.use(function (req, res, next) {371 assert.equal(req.hit++, 2);372 assert.equal(req.url, 'http://example.com/blog/post/1');373 next();374 });375376 router.handle(request, {}, function (err) {377 if (err) return done(err);378 assert.equal(request.hit, 3);379 done();380 });381 });382 })383384 describe('.all', function() {385 it('should support using .all to capture all http verbs', function(done){386 var router = new Router();387388 var count = 0;389 router.all('/foo', function(){ count++; });390391 var url = '/foo?bar=baz';392393 methods.forEach(function testMethod(method) {394 router.handle({ url: url, method: method }, {}, function() {});395 });396397 assert.equal(count, methods.length);398 done();399 })400401 it('should be called for any URL when "*"', function (done) {402 var cb = after(4, done)403 var router = new Router()404405 function no () {406 throw new Error('should not be called')407 }408409 router.all('*', function (req, res) {410 res.end()411 })412413 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)414 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)415 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)416 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)417 })418 })419420 describe('.use', function() {421 it('should require middleware', function () {422 var router = new Router()423 assert.throws(function () { router.use('/') }, /requires a middleware function/)424 })425426 it('should reject string as middleware', function () {427 var router = new Router()428 assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)429 })430431 it('should reject number as middleware', function () {432 var router = new Router()433 assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)434 })435436 it('should reject null as middleware', function () {437 var router = new Router()438 assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)439 })440441 it('should reject Date as middleware', function () {442 var router = new Router()443 assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)444 })445446 it('should be called for any URL', function (done) {447 var cb = after(4, done)448 var router = new Router()449450 function no () {451 throw new Error('should not be called')452 }453454 router.use(function (req, res) {455 res.end()456 })457458 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)459 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)460 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)461 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)462 })463464 it('should accept array of middleware', function(done){465 var count = 0;466 var router = new Router();467468 function fn1(req, res, next){469 assert.equal(++count, 1);470 next();471 }472473 function fn2(req, res, next){474 assert.equal(++count, 2);475 next();476 }477478 router.use([fn1, fn2], function(req, res){479 assert.equal(++count, 3);480 done();481 });482483 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});484 })485 })486487 describe('.param', function() {488 it('should call param function when routing VERBS', function(done) {489 var router = new Router();490491 router.param('id', function(req, res, next, id) {492 assert.equal(id, '123');493 next();494 });495496 router.get('/foo/:id/bar', function(req, res, next) {497 assert.equal(req.params.id, '123');498 next();499 });500501 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);502 });503504 it('should call param function when routing middleware', function(done) {505 var router = new Router();506507 router.param('id', function(req, res, next, id) {508 assert.equal(id, '123');509 next();510 });511512 router.use('/foo/:id/bar', function(req, res, next) {513 assert.equal(req.params.id, '123');514 assert.equal(req.url, '/baz');515 next();516 });517518 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);519 });520521 it('should only call once per request', function(done) {522 var count = 0;523 var req = { url: '/foo/bob/bar', method: 'get' };524 var router = new Router();525 var sub = new Router();526527 sub.get('/bar', function(req, res, next) {528 next();529 });530531 router.param('user', function(req, res, next, user) {532 count++;533 req.user = user;534 next();535 });536537 router.use('/foo/:user/', new Router());538 router.use('/foo/:user/', sub);539540 router.handle(req, {}, function(err) {541 if (err) return done(err);542 assert.equal(count, 1);543 assert.equal(req.user, 'bob');544 done();545 });546 });547548 it('should call when values differ', function(done) {549 var count = 0;550 var req = { url: '/foo/bob/bar', method: 'get' };551 var router = new Router();552 var sub = new Router();553554 sub.get('/bar', function(req, res, next) {555 next();556 });557558 router.param('user', function(req, res, next, user) {559 count++;560 req.user = user;561 next();562 });563564 router.use('/foo/:user/', new Router());565 router.use('/:user/bob/', sub);566567 router.handle(req, {}, function(err) {568 if (err) return done(err);569 assert.equal(count, 2);570 assert.equal(req.user, 'foo');571 done();572 });573 });574 });575576 describe('parallel requests', function() {577 it('should not mix requests', function(done) {578 var req1 = { url: '/foo/50/bar', method: 'get' };579 var req2 = { url: '/foo/10/bar', method: 'get' };580 var router = new Router();581 var sub = new Router();582583 done = after(2, done);584585 sub.get('/bar', function(req, res, next) {586 next();587 });588589 router.param('ms', function(req, res, next, ms) {590 ms = parseInt(ms, 10);591 req.ms = ms;592 setTimeout(next, ms);593 });594595 router.use('/foo/:ms/', new Router());596 router.use('/foo/:ms/', sub);597598 router.handle(req1, {}, function(err) {599 assert.ifError(err);600 assert.equal(req1.ms, 50);601 assert.equal(req1.originalUrl, '/foo/50/bar');602 done();603 });604605 router.handle(req2, {}, function(err) {606 assert.ifError(err);607 assert.equal(req2.ms, 10);608 assert.equal(req2.originalUrl, '/foo/10/bar');609 done();610 });611 });612 });613})