Router.js
This file
Path
test/Router.js
490 lines · 13.0 KiB · JavaScript
Last changed by
Fix handling of URLs containing :// in the pathAria Stewartbefore adoptioneabd456
History of this fileTake it
The walk stopped after 200 commits: lines older than 269dc53 point at it.
Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 1–45why, one step back12var 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 });45Fix handling of invalid empty URLsDouglas Christopher Wilsonbefore adoption68290ee
lines 46–55why, one step back46 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 });55Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 56–186why, one step back56 describe('.handle', function(){57 it('should dispatch', function(done){58 var router = new Router();5960 router.route('/foo').get(function(req, res){61 res.send('foo');62 });6364 var res = {65 send: function(val) {66 val.should.equal('foo');67 done();68 }69 }70 router.handle({ url: '/foo', method: 'GET' }, res);71 })72 })7374 describe('.multiple callbacks', function(){75 it('should throw if a callback is null', function(){76 assert.throws(function () {77 var router = new Router();78 router.route('/foo').all(null);79 })80 })8182 it('should throw if a callback is undefined', function(){83 assert.throws(function () {84 var router = new Router();85 router.route('/foo').all(undefined);86 })87 })8889 it('should throw if a callback is not a function', function(){90 assert.throws(function () {91 var router = new Router();92 router.route('/foo').all('not a function');93 })94 })9596 it('should not throw if all callbacks are functions', function(){97 var router = new Router();98 router.route('/foo').all(function(){}).all(function(){});99 })100 })101102 describe('error', function(){103 it('should skip non error middleware', function(done){104 var router = new Router();105106 router.get('/foo', function(req, res, next){107 next(new Error('foo'));108 });109110 router.get('/bar', function(req, res, next){111 next(new Error('bar'));112 });113114 router.use(function(req, res, next){115 assert(false);116 });117118 router.use(function(err, req, res, next){119 assert.equal(err.message, 'foo');120 done();121 });122123 router.handle({ url: '/foo', method: 'GET' }, {}, done);124 });125126 it('should handle throwing inside routes with params', function(done) {127 var router = new Router();128129 router.get('/foo/:id', function(req, res, next){130 throw new Error('foo');131 });132133 router.use(function(req, res, next){134 assert(false);135 });136137 router.use(function(err, req, res, next){138 assert.equal(err.message, 'foo');139 done();140 });141142 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});143 });144145 it('should handle throwing in handler after async param', function(done) {146 var router = new Router();147148 router.param('user', function(req, res, next, val){149 process.nextTick(function(){150 req.user = val;151 next();152 });153 });154155 router.use('/:user', function(req, res, next){156 throw new Error('oh no!');157 });158159 router.use(function(err, req, res, next){160 assert.equal(err.message, 'oh no!');161 done();162 });163164 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});165 });166167 it('should handle throwing inside error handlers', function(done) {168 var router = new Router();169170 router.use(function(req, res, next){171 throw new Error('boom!');172 });173174 router.use(function(err, req, res, next){175 throw new Error('oops');176 });177178 router.use(function(err, req, res, next){179 assert.equal(err.message, 'oops');180 done();181 });182183 router.handle({ url: '/', method: 'GET' }, {}, done);184 });185 })186tests: add missing FQDN router URL testsDouglas Christopher Wilsonbefore adoption968b00c
lines 187–221why, one step back187 describe('FQDN', function () {188 it('should not obscure FQDNs', function (done) {189 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };190 var router = new Router();191192 router.use(function (req, res, next) {193 assert.equal(req.hit++, 0);194 assert.equal(req.url, 'http://example.com/foo');195 next();196 });197198 router.handle(request, {}, function (err) {199 if (err) return done(err);200 assert.equal(request.hit, 1);201 done();202 });203 });204205 it('should ignore FQDN in search', function (done) {206 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };207 var router = new Router();208209 router.use('/proxy', function (req, res, next) {210 assert.equal(req.hit++, 0);211 assert.equal(req.url, '/?url=http://example.com/blog/post/1');212 next();213 });214215 router.handle(request, {}, function (err) {216 if (err) return done(err);217 assert.equal(request.hit, 1);218 done();219 });220 });221Fix handling of URLs containing :// in the pathAria Stewartbefore adoptioneabd456
lines 222–238why, one step back222 it('should ignore FQDN in path', function (done) {223 var request = { hit: 0, url: '/proxy/http://example.com/blog/post/1', method: 'GET' };224 var router = new Router();225226 router.use('/proxy', function (req, res, next) {227 assert.equal(req.hit++, 0);228 assert.equal(req.url, '/http://example.com/blog/post/1');229 next();230 });231232 router.handle(request, {}, function (err) {233 if (err) return done(err);234 assert.equal(request.hit, 1);235 done();236 });237 });238tests: add missing FQDN router URL testsDouglas Christopher Wilsonbefore adoption968b00c
lines 239–308why, one step back239 it('should adjust FQDN req.url', function (done) {240 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };241 var router = new Router();242243 router.use('/blog', function (req, res, next) {244 assert.equal(req.hit++, 0);245 assert.equal(req.url, 'http://example.com/post/1');246 next();247 });248249 router.handle(request, {}, function (err) {250 if (err) return done(err);251 assert.equal(request.hit, 1);252 done();253 });254 });255256 it('should adjust FQDN req.url with multiple handlers', function (done) {257 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };258 var router = new Router();259260 router.use(function (req, res, next) {261 assert.equal(req.hit++, 0);262 assert.equal(req.url, 'http://example.com/blog/post/1');263 next();264 });265266 router.use('/blog', function (req, res, next) {267 assert.equal(req.hit++, 1);268 assert.equal(req.url, 'http://example.com/post/1');269 next();270 });271272 router.handle(request, {}, function (err) {273 if (err) return done(err);274 assert.equal(request.hit, 2);275 done();276 });277 });278279 it('should adjust FQDN req.url with multiple routed handlers', function (done) {280 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };281 var router = new Router();282283 router.use('/blog', function (req, res, next) {284 assert.equal(req.hit++, 0);285 assert.equal(req.url, 'http://example.com/post/1');286 next();287 });288289 router.use('/blog', function (req, res, next) {290 assert.equal(req.hit++, 1);291 assert.equal(req.url, 'http://example.com/post/1');292 next();293 });294295 router.use(function (req, res, next) {296 assert.equal(req.hit++, 2);297 assert.equal(req.url, 'http://example.com/blog/post/1');298 next();299 });300301 router.handle(request, {}, function (err) {302 if (err) return done(err);303 assert.equal(request.hit, 3);304 done();305 });306 });307 })308Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 309–326why, one step back309 describe('.all', function() {310 it('should support using .all to capture all http verbs', function(done){311 var router = new Router();312313 var count = 0;314 router.all('/foo', function(){ count++; });315316 var url = '/foo?bar=baz';317318 methods.forEach(function testMethod(method) {319 router.handle({ url: url, method: method }, {}, function() {});320 });321322 assert.equal(count, methods.length);323 done();324 })325 })326tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 327–329why, one step back327 describe('.use', function() {328 it('should require arguments', function(){329 var router = new Router();Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
line 330why, one step back330 router.use.bind(router).should.throw(/requires middleware function/)tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 331–334why, one step back331 })332333 it('should not accept non-functions', function(){334 var router = new Router();Improve error message for bad app.use argumentsDouglas Christopher Wilsonbefore adoptionbf1980f
lines 335–338why, one step back335 router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)336 router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)337 router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)338 router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
line 339why, one step back339 })Fix router.use to accept array of middleware without pathDouglas Christopher Wilsonbefore adoption7289171
lines 340–361why, one step back340341 it('should accept array of middleware', function(done){342 var count = 0;343 var router = new Router();344345 function fn1(req, res, next){346 assert.equal(++count, 1);347 next();348 }349350 function fn2(req, res, next){351 assert.equal(++count, 2);352 next();353 }354355 router.use([fn1, fn2], function(req, res){356 assert.equal(++count, 3);357 done();358 });359360 router.handle({ url: '/foo', method: 'GET' }, {}, function(){});361 })tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 362–363why, one step back362 })363Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 364–490why, one step back364 describe('.param', function() {365 it('should call param function when routing VERBS', function(done) {366 var router = new Router();367368 router.param('id', function(req, res, next, id) {369 assert.equal(id, '123');370 next();371 });372373 router.get('/foo/:id/bar', function(req, res, next) {374 assert.equal(req.params.id, '123');375 next();376 });377378 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);379 });380381 it('should call param function when routing middleware', function(done) {382 var router = new Router();383384 router.param('id', function(req, res, next, id) {385 assert.equal(id, '123');386 next();387 });388389 router.use('/foo/:id/bar', function(req, res, next) {390 assert.equal(req.params.id, '123');391 assert.equal(req.url, '/baz');392 next();393 });394395 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);396 });397398 it('should only call once per request', function(done) {399 var count = 0;400 var req = { url: '/foo/bob/bar', method: 'get' };401 var router = new Router();402 var sub = new Router();403404 sub.get('/bar', function(req, res, next) {405 next();406 });407408 router.param('user', function(req, res, next, user) {409 count++;410 req.user = user;411 next();412 });413414 router.use('/foo/:user/', new Router());415 router.use('/foo/:user/', sub);416417 router.handle(req, {}, function(err) {418 if (err) return done(err);419 assert.equal(count, 1);420 assert.equal(req.user, 'bob');421 done();422 });423 });424425 it('should call when values differ', function(done) {426 var count = 0;427 var req = { url: '/foo/bob/bar', method: 'get' };428 var router = new Router();429 var sub = new Router();430431 sub.get('/bar', function(req, res, next) {432 next();433 });434435 router.param('user', function(req, res, next, user) {436 count++;437 req.user = user;438 next();439 });440441 router.use('/foo/:user/', new Router());442 router.use('/:user/bob/', sub);443444 router.handle(req, {}, function(err) {445 if (err) return done(err);446 assert.equal(count, 2);447 assert.equal(req.user, 'foo');448 done();449 });450 });451 });452453 describe('parallel requests', function() {454 it('should not mix requests', function(done) {455 var req1 = { url: '/foo/50/bar', method: 'get' };456 var req2 = { url: '/foo/10/bar', method: 'get' };457 var router = new Router();458 var sub = new Router();459460 done = after(2, done);461462 sub.get('/bar', function(req, res, next) {463 next();464 });465466 router.param('ms', function(req, res, next, ms) {467 ms = parseInt(ms, 10);468 req.ms = ms;469 setTimeout(next, ms);470 });471472 router.use('/foo/:ms/', new Router());473 router.use('/foo/:ms/', sub);474475 router.handle(req1, {}, function(err) {476 assert.ifError(err);477 assert.equal(req1.ms, 50);478 assert.equal(req1.originalUrl, '/foo/50/bar');479 done();480 });481482 router.handle(req2, {}, function(err) {483 assert.ifError(err);484 assert.equal(req2.ms, 10);485 assert.equal(req2.originalUrl, '/foo/10/bar');486 done();487 });488 });489 });490})