Skip to content
Myr

Router.js

This file

Path

test/Router.js

515 lines · 13.8 KiB · JavaScript

Last changed by

Remove app.param(fn) signatureDouglas Christopher Wilsonbefore adoption1e2951a

History of this file

Take it

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

Merge tag '3.13.0'Douglas Christopher Wilsonbefore adoptionbcee730

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '3.13.0'Douglas Christopher Wilsonbefore adoptionbcee730

lines 71–201why, 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 })201

tests: add missing FQDN router URL testsDouglas Christopher Wilsonbefore adoption968b00c

lines 202–236why, one step back
202 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

tests: add missing FQDN router URL testsDouglas Christopher Wilsonbefore adoption968b00c

lines 254–323why, 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 })323

Merge tag '3.13.0'Douglas Christopher Wilsonbefore adoptionbcee730

lines 324–341why, one step back
324 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 })341

tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0

lines 342–344why, one step back
342 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/)

tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0

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/)

tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0

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 })

tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0

lines 377–378why, one step back
377 })
378

Merge tag '3.13.0'Douglas Christopher Wilsonbefore adoptionbcee730

line 379why, one step back
379 describe('.param', function() {

Remove app.param(fn) signatureDouglas Christopher Wilsonbefore adoption1e2951a

lines 380–389why, one step back
380 it('should require function', function () {
381 var router = new Router();382 assert.throws(router.param.bind(router, 'id'), /argument fn is required/);383 });384385 it('should reject non-function', function () {386 var router = new Router();387 assert.throws(router.param.bind(router, 'id', 42), /argument fn must be a function/);388 });389

Merge tag '3.13.0'Douglas Christopher Wilsonbefore adoptionbcee730

lines 390–515why, one step back
390 it('should call param function when routing VERBS', function(done) {
391 var router = new Router();392393 router.param('id', function(req, res, next, id) {394 assert.equal(id, '123');395 next();396 });397398 router.get('/foo/:id/bar', function(req, res, next) {399 assert.equal(req.params.id, '123');400 next();401 });402403 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);404 });405406 it('should call param function when routing middleware', function(done) {407 var router = new Router();408409 router.param('id', function(req, res, next, id) {410 assert.equal(id, '123');411 next();412 });413414 router.use('/foo/:id/bar', function(req, res, next) {415 assert.equal(req.params.id, '123');416 assert.equal(req.url, '/baz');417 next();418 });419420 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);421 });422423 it('should only call once per request', function(done) {424 var count = 0;425 var req = { url: '/foo/bob/bar', method: 'get' };426 var router = new Router();427 var sub = new Router();428429 sub.get('/bar', function(req, res, next) {430 next();431 });432433 router.param('user', function(req, res, next, user) {434 count++;435 req.user = user;436 next();437 });438439 router.use('/foo/:user/', new Router());440 router.use('/foo/:user/', sub);441442 router.handle(req, {}, function(err) {443 if (err) return done(err);444 assert.equal(count, 1);445 assert.equal(req.user, 'bob');446 done();447 });448 });449450 it('should call when values differ', function(done) {451 var count = 0;452 var req = { url: '/foo/bob/bar', method: 'get' };453 var router = new Router();454 var sub = new Router();455456 sub.get('/bar', function(req, res, next) {457 next();458 });459460 router.param('user', function(req, res, next, user) {461 count++;462 req.user = user;463 next();464 });465466 router.use('/foo/:user/', new Router());467 router.use('/:user/bob/', sub);468469 router.handle(req, {}, function(err) {470 if (err) return done(err);471 assert.equal(count, 2);472 assert.equal(req.user, 'foo');473 done();474 });475 });476 });477478 describe('parallel requests', function() {479 it('should not mix requests', function(done) {480 var req1 = { url: '/foo/50/bar', method: 'get' };481 var req2 = { url: '/foo/10/bar', method: 'get' };482 var router = new Router();483 var sub = new Router();484485 done = after(2, done);486487 sub.get('/bar', function(req, res, next) {488 next();489 });490491 router.param('ms', function(req, res, next, ms) {492 ms = parseInt(ms, 10);493 req.ms = ms;494 setTimeout(next, ms);495 });496497 router.use('/foo/:ms/', new Router());498 router.use('/foo/:ms/', sub);499500 router.handle(req1, {}, function(err) {501 assert.ifError(err);502 assert.equal(req1.ms, 50);503 assert.equal(req1.originalUrl, '/foo/50/bar');504 done();505 });506507 router.handle(req2, {}, function(err) {508 assert.ifError(err);509 assert.equal(req2.ms, 10);510 assert.equal(req2.originalUrl, '/foo/10/bar');511 done();512 });513 });514 });515})