Skip to content
Myr

Router.js

This file

Path

test/Router.js

560 lines · 15.0 KiB · JavaScript

Last changed by

lint: remove usages of "=="Alejandro Estradabefore adoption40e04ec

History of this file

Take it

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

docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c

lines 1–10why, 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 = new Router();

lint: remove usages of "=="Alejandro Estradabefore adoption40e04ec

line 11why, one step back
11 assert(typeof router === 'function')

docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c

line 12why, one step back
12

lint: remove usages of "=="Alejandro Estradabefore adoption40e04ec

lines 13–15why, one step back
13 assert(typeof router.get === 'function')
14 assert(typeof router.handle === 'function')15 assert(typeof router.use === 'function')

docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c

lines 16–46why, one step back
16 });
1718 it('should support .use of other routers', function(done){19 var router = new Router();20 var another = new Router();2122 another.get('/bar', function(req, res){23 res.end();24 });25 router.use('/foo', another);2627 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });28 });2930 it('should support dynamic routes', function(done){31 var router = new Router();32 var another = new Router();3334 another.get('/:bar', function(req, res){35 req.params.bar.should.equal('route');36 res.end();37 });38 router.use('/:foo', another);3940 router.handle({ url: '/test/route', method: 'GET' }, { end: done });41 });4243 it('should handle blank URL', function(done){44 var router = new Router();4546 router.use(function (req, res) {

tests: fix incorrect should usageDaniel Walasekbefore adoption713d2ae

line 47why, one step back
47 false.should.be.true()

docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c

lines 48–367why, one step back
48 });
4950 router.handle({ url: '', method: 'GET' }, {}, done);51 });5253 it('should handle missing URL', function (done) {54 var router = new Router()5556 router.use(function (req, res) {57 throw new Error('should not be called')58 })5960 router.handle({ method: 'GET' }, {}, done)61 })6263 it('should not stack overflow with many registered routes', function(done){64 var handler = function(req, res){ res.end(new Error('wrong handler')) };65 var router = new Router();6667 for (var i = 0; i < 6000; i++) {68 router.get('/thing' + i, handler)69 }7071 router.get('/', function (req, res) {72 res.end();73 });7475 router.handle({ url: '/', method: 'GET' }, { end: done });76 });7778 describe('.handle', function(){79 it('should dispatch', function(done){80 var router = new Router();8182 router.route('/foo').get(function(req, res){83 res.send('foo');84 });8586 var res = {87 send: function(val) {88 val.should.equal('foo');89 done();90 }91 }92 router.handle({ url: '/foo', method: 'GET' }, res);93 })94 })9596 describe('.multiple callbacks', function(){97 it('should throw if a callback is null', function(){98 assert.throws(function () {99 var router = new Router();100 router.route('/foo').all(null);101 })102 })103104 it('should throw if a callback is undefined', function(){105 assert.throws(function () {106 var router = new Router();107 router.route('/foo').all(undefined);108 })109 })110111 it('should throw if a callback is not a function', function(){112 assert.throws(function () {113 var router = new Router();114 router.route('/foo').all('not a function');115 })116 })117118 it('should not throw if all callbacks are functions', function(){119 var router = new Router();120 router.route('/foo').all(function(){}).all(function(){});121 })122 })123124 describe('error', function(){125 it('should skip non error middleware', function(done){126 var router = new Router();127128 router.get('/foo', function(req, res, next){129 next(new Error('foo'));130 });131132 router.get('/bar', function(req, res, next){133 next(new Error('bar'));134 });135136 router.use(function(req, res, next){137 assert(false);138 });139140 router.use(function(err, req, res, next){141 assert.equal(err.message, 'foo');142 done();143 });144145 router.handle({ url: '/foo', method: 'GET' }, {}, done);146 });147148 it('should handle throwing inside routes with params', function(done) {149 var router = new Router();150151 router.get('/foo/:id', function(req, res, next){152 throw new Error('foo');153 });154155 router.use(function(req, res, next){156 assert(false);157 });158159 router.use(function(err, req, res, next){160 assert.equal(err.message, 'foo');161 done();162 });163164 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});165 });166167 it('should handle throwing in handler after async param', function(done) {168 var router = new Router();169170 router.param('user', function(req, res, next, val){171 process.nextTick(function(){172 req.user = val;173 next();174 });175 });176177 router.use('/:user', function(req, res, next){178 throw new Error('oh no!');179 });180181 router.use(function(err, req, res, next){182 assert.equal(err.message, 'oh no!');183 done();184 });185186 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});187 });188189 it('should handle throwing inside error handlers', function(done) {190 var router = new Router();191192 router.use(function(req, res, next){193 throw new Error('boom!');194 });195196 router.use(function(err, req, res, next){197 throw new Error('oops');198 });199200 router.use(function(err, req, res, next){201 assert.equal(err.message, 'oops');202 done();203 });204205 router.handle({ url: '/', method: 'GET' }, {}, done);206 });207 })208209 describe('FQDN', function () {210 it('should not obscure FQDNs', function (done) {211 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };212 var router = new Router();213214 router.use(function (req, res, next) {215 assert.equal(req.hit++, 0);216 assert.equal(req.url, 'http://example.com/foo');217 next();218 });219220 router.handle(request, {}, function (err) {221 if (err) return done(err);222 assert.equal(request.hit, 1);223 done();224 });225 });226227 it('should ignore FQDN in search', function (done) {228 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };229 var router = new Router();230231 router.use('/proxy', function (req, res, next) {232 assert.equal(req.hit++, 0);233 assert.equal(req.url, '/?url=http://example.com/blog/post/1');234 next();235 });236237 router.handle(request, {}, function (err) {238 if (err) return done(err);239 assert.equal(request.hit, 1);240 done();241 });242 });243244 it('should ignore FQDN in path', function (done) {245 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };246 var router = new Router();247248 router.use('/proxy', function (req, res, next) {249 assert.equal(req.hit++, 0);250 assert.equal(req.url, '/http://example.com/blog/post/1');251 next();252 });253254 router.handle(request, {}, function (err) {255 if (err) return done(err);256 assert.equal(request.hit, 1);257 done();258 });259 });260261 it('should adjust FQDN req.url', function (done) {262 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };263 var router = new Router();264265 router.use('/blog', function (req, res, next) {266 assert.equal(req.hit++, 0);267 assert.equal(req.url, 'http://example.com/post/1');268 next();269 });270271 router.handle(request, {}, function (err) {272 if (err) return done(err);273 assert.equal(request.hit, 1);274 done();275 });276 });277278 it('should adjust FQDN req.url with multiple handlers', function (done) {279 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };280 var router = new Router();281282 router.use(function (req, res, next) {283 assert.equal(req.hit++, 0);284 assert.equal(req.url, 'http://example.com/blog/post/1');285 next();286 });287288 router.use('/blog', function (req, res, next) {289 assert.equal(req.hit++, 1);290 assert.equal(req.url, 'http://example.com/post/1');291 next();292 });293294 router.handle(request, {}, function (err) {295 if (err) return done(err);296 assert.equal(request.hit, 2);297 done();298 });299 });300301 it('should adjust FQDN req.url with multiple routed handlers', function (done) {302 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };303 var router = new Router();304305 router.use('/blog', function (req, res, next) {306 assert.equal(req.hit++, 0);307 assert.equal(req.url, 'http://example.com/post/1');308 next();309 });310311 router.use('/blog', function (req, res, next) {312 assert.equal(req.hit++, 1);313 assert.equal(req.url, 'http://example.com/post/1');314 next();315 });316317 router.use(function (req, res, next) {318 assert.equal(req.hit++, 2);319 assert.equal(req.url, 'http://example.com/blog/post/1');320 next();321 });322323 router.handle(request, {}, function (err) {324 if (err) return done(err);325 assert.equal(request.hit, 3);326 done();327 });328 });329 })330331 describe('.all', function() {332 it('should support using .all to capture all http verbs', function(done){333 var router = new Router();334335 var count = 0;336 router.all('/foo', function(){ count++; });337338 var url = '/foo?bar=baz';339340 methods.forEach(function testMethod(method) {341 router.handle({ url: url, method: method }, {}, function() {});342 });343344 assert.equal(count, methods.length);345 done();346 })347348 it('should be called for any URL when "*"', function (done) {349 var cb = after(4, done)350 var router = new Router()351352 function no () {353 throw new Error('should not be called')354 }355356 router.all('*', function (req, res) {357 res.end()358 })359360 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)361 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)362 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)363 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)364 })365 })366367 describe('.use', function() {

Improve error messages when non-function provided as middlewareHrvoje Šimićbefore adoption2df1ad2

lines 368–370why, one step back
368 it('should require middleware', function () {
369 var router = new Router()370 assert.throws(function () { router.use('/') }, /requires a middleware function/)

docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c

lines 371–372why, one step back
371 })
372

Improve error messages when non-function provided as middlewareHrvoje Šimićbefore adoption2df1ad2

lines 373–390why, one step back
373 it('should reject string as middleware', function () {
374 var router = new Router()375 assert.throws(function () { router.use('/', 'foo') }, /requires a middleware function but got a string/)376 })377378 it('should reject number as middleware', function () {379 var router = new Router()380 assert.throws(function () { router.use('/', 42) }, /requires a middleware function but got a number/)381 })382383 it('should reject null as middleware', function () {384 var router = new Router()385 assert.throws(function () { router.use('/', null) }, /requires a middleware function but got a Null/)386 })387388 it('should reject Date as middleware', function () {389 var router = new Router()390 assert.throws(function () { router.use('/', new Date()) }, /requires a middleware function but got a Date/)

docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c

lines 391–560why, one step back
391 })
392393 it('should be called for any URL', function (done) {394 var cb = after(4, done)395 var router = new Router()396397 function no () {398 throw new Error('should not be called')399 }400401 router.use(function (req, res) {402 res.end()403 })404405 router.handle({ url: '/', method: 'GET' }, { end: cb }, no)406 router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)407 router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)408 router.handle({ url: '*', method: 'GET' }, { end: cb }, no)409 })410411 it('should accept array of middleware', function(done){412 var count = 0;413 var router = new Router();414415 function fn1(req, res, next){416 assert.equal(++count, 1);417 next();418 }419420 function fn2(req, res, next){421 assert.equal(++count, 2);422 next();423 }424425 router.use([fn1, fn2], function(req, res){426 assert.equal(++count, 3);427 done();428 });429430 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});431 })432 })433434 describe('.param', function() {435 it('should call param function when routing VERBS', function(done) {436 var router = new Router();437438 router.param('id', function(req, res, next, id) {439 assert.equal(id, '123');440 next();441 });442443 router.get('/foo/:id/bar', function(req, res, next) {444 assert.equal(req.params.id, '123');445 next();446 });447448 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);449 });450451 it('should call param function when routing middleware', function(done) {452 var router = new Router();453454 router.param('id', function(req, res, next, id) {455 assert.equal(id, '123');456 next();457 });458459 router.use('/foo/:id/bar', function(req, res, next) {460 assert.equal(req.params.id, '123');461 assert.equal(req.url, '/baz');462 next();463 });464465 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);466 });467468 it('should only call once per request', function(done) {469 var count = 0;470 var req = { url: '/foo/bob/bar', method: 'get' };471 var router = new Router();472 var sub = new Router();473474 sub.get('/bar', function(req, res, next) {475 next();476 });477478 router.param('user', function(req, res, next, user) {479 count++;480 req.user = user;481 next();482 });483484 router.use('/foo/:user/', new Router());485 router.use('/foo/:user/', sub);486487 router.handle(req, {}, function(err) {488 if (err) return done(err);489 assert.equal(count, 1);490 assert.equal(req.user, 'bob');491 done();492 });493 });494495 it('should call when values differ', function(done) {496 var count = 0;497 var req = { url: '/foo/bob/bar', method: 'get' };498 var router = new Router();499 var sub = new Router();500501 sub.get('/bar', function(req, res, next) {502 next();503 });504505 router.param('user', function(req, res, next, user) {506 count++;507 req.user = user;508 next();509 });510511 router.use('/foo/:user/', new Router());512 router.use('/:user/bob/', sub);513514 router.handle(req, {}, function(err) {515 if (err) return done(err);516 assert.equal(count, 2);517 assert.equal(req.user, 'foo');518 done();519 });520 });521 });522523 describe('parallel requests', function() {524 it('should not mix requests', function(done) {525 var req1 = { url: '/foo/50/bar', method: 'get' };526 var req2 = { url: '/foo/10/bar', method: 'get' };527 var router = new Router();528 var sub = new Router();529530 done = after(2, done);531532 sub.get('/bar', function(req, res, next) {533 next();534 });535536 router.param('ms', function(req, res, next, ms) {537 ms = parseInt(ms, 10);538 req.ms = ms;539 setTimeout(next, ms);540 });541542 router.use('/foo/:ms/', new Router());543 router.use('/foo/:ms/', sub);544545 router.handle(req1, {}, function(err) {546 assert.ifError(err);547 assert.equal(req1.ms, 50);548 assert.equal(req1.originalUrl, '/foo/50/bar');549 done();550 });551552 router.handle(req2, {}, function(err) {553 assert.ifError(err);554 assert.equal(req2.ms, 10);555 assert.equal(req2.originalUrl, '/foo/10/bar');556 done();557 });558 });559 });560})