Skip to content
Myr

Router.js

This file

Path

test/Router.js

505 lines · 13.4 KiB · JavaScript

Last changed by

Match routes iteratively to prevent stack overflowsDouglas Christopher Wilsonbefore adoptionec8daf0

History of this file

Take it

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 1–45why, one step back
1
2var after = require('after');3var express = require('../')4 , Router = express.Router5 , methods = require('methods')6 , assert = require('assert');78describe('Router', function(){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 });2021 it('should support .use of other routers', function(done){22 var router = new Router();23 var another = new Router();2425 another.get('/bar', function(req, res){26 res.end();27 });28 router.use('/foo', another);2930 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 });44 });45

Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee

lines 46–55why, one step back
46 it('should handle blank URL', function(done){
47 var router = new Router();4849 router.use(function (req, res) {50 false.should.be.true;51 });5253 router.handle({ url: '', method: 'GET' }, {}, done);54 });55

Match routes iteratively to prevent stack overflowsDouglas Christopher Wilsonbefore adoptionec8daf0

lines 56–70why, one step back
56 it('should not stack overflow with many registered routes', function(done){
57 var handler = function(req, res){ res.end(new Error('wrong handler')) };58 var router = new Router();5960 for (var i = 0; i < 6000; i++) {61 router.get('/thing' + i, handler)62 }6364 router.get('/', function (req, res) {65 res.end();66 });6768 router.handle({ url: '/', method: 'GET' }, { end: done });69 });70

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 71–236why, one step back
71 describe('.handle', function(){
72 it('should dispatch', function(done){73 var router = new Router();7475 router.route('/foo').get(function(req, res){76 res.send('foo');77 });7879 var res = {80 send: function(val) {81 val.should.equal('foo');82 done();83 }84 }85 router.handle({ url: '/foo', method: 'GET' }, res);86 })87 })8889 describe('.multiple callbacks', function(){90 it('should throw if a callback is null', function(){91 assert.throws(function () {92 var router = new Router();93 router.route('/foo').all(null);94 })95 })9697 it('should throw if a callback is undefined', function(){98 assert.throws(function () {99 var router = new Router();100 router.route('/foo').all(undefined);101 })102 })103104 it('should throw if a callback is not a function', function(){105 assert.throws(function () {106 var router = new Router();107 router.route('/foo').all('not a function');108 })109 })110111 it('should not throw if all callbacks are functions', function(){112 var router = new Router();113 router.route('/foo').all(function(){}).all(function(){});114 })115 })116117 describe('error', function(){118 it('should skip non error middleware', function(done){119 var router = new Router();120121 router.get('/foo', function(req, res, next){122 next(new Error('foo'));123 });124125 router.get('/bar', function(req, res, next){126 next(new Error('bar'));127 });128129 router.use(function(req, res, next){130 assert(false);131 });132133 router.use(function(err, req, res, next){134 assert.equal(err.message, 'foo');135 done();136 });137138 router.handle({ url: '/foo', method: 'GET' }, {}, done);139 });140141 it('should handle throwing inside routes with params', function(done) {142 var router = new Router();143144 router.get('/foo/:id', function(req, res, next){145 throw new Error('foo');146 });147148 router.use(function(req, res, next){149 assert(false);150 });151152 router.use(function(err, req, res, next){153 assert.equal(err.message, 'foo');154 done();155 });156157 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});158 });159160 it('should handle throwing in handler after async param', function(done) {161 var router = new Router();162163 router.param('user', function(req, res, next, val){164 process.nextTick(function(){165 req.user = val;166 next();167 });168 });169170 router.use('/:user', function(req, res, next){171 throw new Error('oh no!');172 });173174 router.use(function(err, req, res, next){175 assert.equal(err.message, 'oh no!');176 done();177 });178179 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});180 });181182 it('should handle throwing inside error handlers', function(done) {183 var router = new Router();184185 router.use(function(req, res, next){186 throw new Error('boom!');187 });188189 router.use(function(err, req, res, next){190 throw new Error('oops');191 });192193 router.use(function(err, req, res, next){194 assert.equal(err.message, 'oops');195 done();196 });197198 router.handle({ url: '/', method: 'GET' }, {}, done);199 });200 })201202 describe('FQDN', function () {203 it('should not obscure FQDNs', function (done) {204 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };205 var router = new Router();206207 router.use(function (req, res, next) {208 assert.equal(req.hit++, 0);209 assert.equal(req.url, 'http://example.com/foo');210 next();211 });212213 router.handle(request, {}, function (err) {214 if (err) return done(err);215 assert.equal(request.hit, 1);216 done();217 });218 });219220 it('should ignore FQDN in search', function (done) {221 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };222 var router = new Router();223224 router.use('/proxy', function (req, res, next) {225 assert.equal(req.hit++, 0);226 assert.equal(req.url, '/?url=http://example.com/blog/post/1');227 next();228 });229230 router.handle(request, {}, function (err) {231 if (err) return done(err);232 assert.equal(request.hit, 1);233 done();234 });235 });236

Fix handling of URLs containing :// in the pathAria Stewartbefore adoptioneabd456

lines 237–253why, one step back
237 it('should ignore FQDN in path', function (done) {
238 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };239 var router = new Router();240241 router.use('/proxy', function (req, res, next) {242 assert.equal(req.hit++, 0);243 assert.equal(req.url, '/http://example.com/blog/post/1');244 next();245 });246247 router.handle(request, {}, function (err) {248 if (err) return done(err);249 assert.equal(request.hit, 1);250 done();251 });252 });253

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 254–344why, one step back
254 it('should adjust FQDN req.url', function (done) {
255 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };256 var router = new Router();257258 router.use('/blog', function (req, res, next) {259 assert.equal(req.hit++, 0);260 assert.equal(req.url, 'http://example.com/post/1');261 next();262 });263264 router.handle(request, {}, function (err) {265 if (err) return done(err);266 assert.equal(request.hit, 1);267 done();268 });269 });270271 it('should adjust FQDN req.url with multiple handlers', function (done) {272 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };273 var router = new Router();274275 router.use(function (req, res, next) {276 assert.equal(req.hit++, 0);277 assert.equal(req.url, 'http://example.com/blog/post/1');278 next();279 });280281 router.use('/blog', function (req, res, next) {282 assert.equal(req.hit++, 1);283 assert.equal(req.url, 'http://example.com/post/1');284 next();285 });286287 router.handle(request, {}, function (err) {288 if (err) return done(err);289 assert.equal(request.hit, 2);290 done();291 });292 });293294 it('should adjust FQDN req.url with multiple routed handlers', function (done) {295 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };296 var router = new Router();297298 router.use('/blog', function (req, res, next) {299 assert.equal(req.hit++, 0);300 assert.equal(req.url, 'http://example.com/post/1');301 next();302 });303304 router.use('/blog', function (req, res, next) {305 assert.equal(req.hit++, 1);306 assert.equal(req.url, 'http://example.com/post/1');307 next();308 });309310 router.use(function (req, res, next) {311 assert.equal(req.hit++, 2);312 assert.equal(req.url, 'http://example.com/blog/post/1');313 next();314 });315316 router.handle(request, {}, function (err) {317 if (err) return done(err);318 assert.equal(request.hit, 3);319 done();320 });321 });322 })323324 describe('.all', function() {325 it('should support using .all to capture all http verbs', function(done){326 var router = new Router();327328 var count = 0;329 router.all('/foo', function(){ count++; });330331 var url = '/foo?bar=baz';332333 methods.forEach(function testMethod(method) {334 router.handle({ url: url, method: method }, {}, function() {});335 });336337 assert.equal(count, methods.length);338 done();339 })340 })341342 describe('.use', function() {343 it('should require arguments', function(){344 var router = new Router();

Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f

line 345why, one step back
345 router.use.bind(router).should.throw(/requires middleware function/)

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 346–349why, one step back
346 })
347348 it('should not accept non-functions', function(){349 var router = new Router();

Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f

lines 350–353why, one step back
350 router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)
351 router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)352 router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)353 router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

line 354why, one step back
354 })

Fix router.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoption7289171

lines 355–376why, one step back
355
356 it('should accept array of middleware', function(done){357 var count = 0;358 var router = new Router();359360 function fn1(req, res, next){361 assert.equal(++count, 1);362 next();363 }364365 function fn2(req, res, next){366 assert.equal(++count, 2);367 next();368 }369370 router.use([fn1, fn2], function(req, res){371 assert.equal(++count, 3);372 done();373 });374375 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});376 })

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 377–505why, one step back
377 })
378379 describe('.param', function() {380 it('should call param function when routing VERBS', function(done) {381 var router = new Router();382383 router.param('id', function(req, res, next, id) {384 assert.equal(id, '123');385 next();386 });387388 router.get('/foo/:id/bar', function(req, res, next) {389 assert.equal(req.params.id, '123');390 next();391 });392393 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);394 });395396 it('should call param function when routing middleware', function(done) {397 var router = new Router();398399 router.param('id', function(req, res, next, id) {400 assert.equal(id, '123');401 next();402 });403404 router.use('/foo/:id/bar', function(req, res, next) {405 assert.equal(req.params.id, '123');406 assert.equal(req.url, '/baz');407 next();408 });409410 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);411 });412413 it('should only call once per request', function(done) {414 var count = 0;415 var req = { url: '/foo/bob/bar', method: 'get' };416 var router = new Router();417 var sub = new Router();418419 sub.get('/bar', function(req, res, next) {420 next();421 });422423 router.param('user', function(req, res, next, user) {424 count++;425 req.user = user;426 next();427 });428429 router.use('/foo/:user/', new Router());430 router.use('/foo/:user/', sub);431432 router.handle(req, {}, function(err) {433 if (err) return done(err);434 assert.equal(count, 1);435 assert.equal(req.user, 'bob');436 done();437 });438 });439440 it('should call when values differ', function(done) {441 var count = 0;442 var req = { url: '/foo/bob/bar', method: 'get' };443 var router = new Router();444 var sub = new Router();445446 sub.get('/bar', function(req, res, next) {447 next();448 });449450 router.param('user', function(req, res, next, user) {451 count++;452 req.user = user;453 next();454 });455456 router.use('/foo/:user/', new Router());457 router.use('/:user/bob/', sub);458459 router.handle(req, {}, function(err) {460 if (err) return done(err);461 assert.equal(count, 2);462 assert.equal(req.user, 'foo');463 done();464 });465 });466 });467468 describe('parallel requests', function() {469 it('should not mix requests', function(done) {470 var req1 = { url: '/foo/50/bar', method: 'get' };471 var req2 = { url: '/foo/10/bar', method: 'get' };472 var router = new Router();473 var sub = new Router();474475 done = after(2, done);476477 sub.get('/bar', function(req, res, next) {478 next();479 });480481 router.param('ms', function(req, res, next, ms) {482 ms = parseInt(ms, 10);483 req.ms = ms;484 setTimeout(next, ms);485 });486487 router.use('/foo/:ms/', new Router());488 router.use('/foo/:ms/', sub);489490 router.handle(req1, {}, function(err) {491 assert.ifError(err);492 assert.equal(req1.ms, 50);493 assert.equal(req1.originalUrl, '/foo/50/bar');494 done();495 });496497 router.handle(req2, {}, function(err) {498 assert.ifError(err);499 assert.equal(req2.ms, 10);500 assert.equal(req2.originalUrl, '/foo/10/bar');501 done();502 });503 });504 });505})