Router.js
This file
Path
test/Router.js
438 lines · 11.5 KiB · JavaScript
Last changed by
tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
History of this fileTake it
The walk stopped after 200 commits: lines older than c054581 point at it.
fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
line 1why, one step back1tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b
line 2why, one step back2var after = require('after');fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
lines 3–131why, one step back3var 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 });4546 describe('.handle', function(){47 it('should dispatch', function(done){48 var router = new Router();4950 router.route('/foo').get(function(req, res){51 res.send('foo');52 });5354 var res = {55 send: function(val) {56 val.should.equal('foo');57 done();58 }59 }60 router.handle({ url: '/foo', method: 'GET' }, res);61 })62 })6364 describe('.multiple callbacks', function(){65 it('should throw if a callback is null', function(){66 assert.throws(function () {67 var router = new Router();68 router.route('/foo').all(null);69 })70 })7172 it('should throw if a callback is undefined', function(){73 assert.throws(function () {74 var router = new Router();75 router.route('/foo').all(undefined);76 })77 })7879 it('should throw if a callback is not a function', function(){80 assert.throws(function () {81 var router = new Router();82 router.route('/foo').all('not a function');83 })84 })8586 it('should not throw if all callbacks are functions', function(){87 var router = new Router();88 router.route('/foo').all(function(){}).all(function(){});89 })90 })9192 describe('error', function(){93 it('should skip non error middleware', function(done){94 var router = new Router();9596 router.get('/foo', function(req, res, next){97 next(new Error('foo'));98 });99100 router.get('/bar', function(req, res, next){101 next(new Error('bar'));102 });103104 router.use(function(req, res, next){105 assert(false);106 });107108 router.use(function(err, req, res, next){109 assert.equal(err.message, 'foo');110 done();111 });112113 router.handle({ url: '/foo', method: 'GET' }, {}, done);114 });115116 it('should handle throwing inside routes with params', function(done) {117 var router = new Router();118119 router.get('/foo/:id', function(req, res, next){120 throw new Error('foo');121 });122123 router.use(function(req, res, next){124 assert(false);125 });126127 router.use(function(err, req, res, next){128 assert.equal(err.message, 'foo');129 done();130 });131Fix catching errors from top-level handlersDouglas Christopher Wilsonbefore adoption4d3e0d8
lines 132–154why, one step back132 router.handle({ url: '/foo/2', method: 'GET' }, {}, function() {});133 });134135 it('should handle throwing in handler after async param', function(done) {136 var router = new Router();137138 router.param('user', function(req, res, next, val){139 process.nextTick(function(){140 req.user = val;141 next();142 });143 });144145 router.use('/:user', function(req, res, next){146 throw new Error('oh no!');147 });148149 router.use(function(err, req, res, next){150 assert.equal(err.message, 'oh no!');151 done();152 });153154 router.handle({ url: '/bob', method: 'GET' }, {}, function() {});fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
line 155why, one step back155 });tests: add more route testsDouglas Christopher Wilsonbefore adoption21393c2
lines 156–174why, one step back156157 it('should handle throwing inside error handlers', function(done) {158 var router = new Router();159160 router.use(function(req, res, next){161 throw new Error('boom!');162 });163164 router.use(function(err, req, res, next){165 throw new Error('oops');166 });167168 router.use(function(err, req, res, next){169 assert.equal(err.message, 'oops');170 done();171 });172173 router.handle({ url: '/', method: 'GET' }, {}, done);174 });fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
lines 175–176why, one step back175 })176tests: add missing FQDN router URL testsDouglas Christopher Wilsonbefore adoption968b00c
lines 177–281why, one step back177 describe('FQDN', function () {178 it('should not obscure FQDNs', function (done) {179 var request = { hit: 0, url: 'http://example.com/foo', method: 'GET' };180 var router = new Router();181182 router.use(function (req, res, next) {183 assert.equal(req.hit++, 0);184 assert.equal(req.url, 'http://example.com/foo');185 next();186 });187188 router.handle(request, {}, function (err) {189 if (err) return done(err);190 assert.equal(request.hit, 1);191 done();192 });193 });194195 it('should ignore FQDN in search', function (done) {196 var request = { hit: 0, url: '/proxy?url=http://example.com/blog/post/1', method: 'GET' };197 var router = new Router();198199 router.use('/proxy', function (req, res, next) {200 assert.equal(req.hit++, 0);201 assert.equal(req.url, '/?url=http://example.com/blog/post/1');202 next();203 });204205 router.handle(request, {}, function (err) {206 if (err) return done(err);207 assert.equal(request.hit, 1);208 done();209 });210 });211212 it('should adjust FQDN req.url', function (done) {213 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };214 var router = new Router();215216 router.use('/blog', function (req, res, next) {217 assert.equal(req.hit++, 0);218 assert.equal(req.url, 'http://example.com/post/1');219 next();220 });221222 router.handle(request, {}, function (err) {223 if (err) return done(err);224 assert.equal(request.hit, 1);225 done();226 });227 });228229 it('should adjust FQDN req.url with multiple handlers', function (done) {230 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };231 var router = new Router();232233 router.use(function (req, res, next) {234 assert.equal(req.hit++, 0);235 assert.equal(req.url, 'http://example.com/blog/post/1');236 next();237 });238239 router.use('/blog', function (req, res, next) {240 assert.equal(req.hit++, 1);241 assert.equal(req.url, 'http://example.com/post/1');242 next();243 });244245 router.handle(request, {}, function (err) {246 if (err) return done(err);247 assert.equal(request.hit, 2);248 done();249 });250 });251252 it('should adjust FQDN req.url with multiple routed handlers', function (done) {253 var request = { hit: 0, url: 'http://example.com/blog/post/1', method: 'GET' };254 var router = new Router();255256 router.use('/blog', function (req, res, next) {257 assert.equal(req.hit++, 0);258 assert.equal(req.url, 'http://example.com/post/1');259 next();260 });261262 router.use('/blog', function (req, res, next) {263 assert.equal(req.hit++, 1);264 assert.equal(req.url, 'http://example.com/post/1');265 next();266 });267268 router.use(function (req, res, next) {269 assert.equal(req.hit++, 2);270 assert.equal(req.url, 'http://example.com/blog/post/1');271 next();272 });273274 router.handle(request, {}, function (err) {275 if (err) return done(err);276 assert.equal(request.hit, 3);277 done();278 });279 });280 })281fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
lines 282–299why, one step back282 describe('.all', function() {283 it('should support using .all to capture all http verbs', function(done){284 var router = new Router();285286 var count = 0;287 router.all('/foo', function(){ count++; });288289 var url = '/foo?bar=baz';290291 methods.forEach(function testMethod(method) {292 router.handle({ url: url, method: method }, {}, function() {});293 });294295 assert.equal(count, methods.length);296 done();297 })298 })299tests: add router.use validation testsDouglas Christopher Wilsonbefore adoption1ca01c0
lines 300–311why, one step back300 describe('.use', function() {301 it('should require arguments', function(){302 var router = new Router();303 router.use.bind(router).should.throw(/requires callback function/)304 })305306 it('should not accept non-functions', function(){307 var router = new Router();308 router.use.bind(router, '/', 'hello').should.throw(/requires callback function/)309 })310 })311fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
lines 312–344why, one step back312 describe('.param', function() {313 it('should call param function when routing VERBS', function(done) {314 var router = new Router();315316 router.param('id', function(req, res, next, id) {317 assert.equal(id, '123');318 next();319 });320321 router.get('/foo/:id/bar', function(req, res, next) {322 assert.equal(req.params.id, '123');323 next();324 });325326 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);327 });328329 it('should call param function when routing middleware', function(done) {330 var router = new Router();331332 router.param('id', function(req, res, next, id) {333 assert.equal(id, '123');334 next();335 });336337 router.use('/foo/:id/bar', function(req, res, next) {338 assert.equal(req.params.id, '123');339 assert.equal(req.url, '/baz');340 next();341 });342343 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);344 });invoke router.param() only when necessaryDouglas Christopher Wilsonbefore adoption98d17e2
lines 345–398why, one step back345346 it('should only call once per request', function(done) {347 var count = 0;348 var req = { url: '/foo/bob/bar', method: 'get' };349 var router = new Router();350 var sub = new Router();351352 sub.get('/bar', function(req, res, next) {353 next();354 });355356 router.param('user', function(req, res, next, user) {357 count++;358 req.user = user;359 next();360 });361362 router.use('/foo/:user/', new Router());363 router.use('/foo/:user/', sub);364365 router.handle(req, {}, function(err) {366 if (err) return done(err);367 assert.equal(count, 1);368 assert.equal(req.user, 'bob');369 done();370 });371 });372373 it('should call when values differ', function(done) {374 var count = 0;375 var req = { url: '/foo/bob/bar', method: 'get' };376 var router = new Router();377 var sub = new Router();378379 sub.get('/bar', function(req, res, next) {380 next();381 });382383 router.param('user', function(req, res, next, user) {384 count++;385 req.user = user;386 next();387 });388389 router.use('/foo/:user/', new Router());390 router.use('/:user/bob/', sub);391392 router.handle(req, {}, function(err) {393 if (err) return done(err);394 assert.equal(count, 2);395 assert.equal(req.user, 'foo');396 done();397 });398 });fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
line 399why, one step back399 });fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a
lines 400–407why, one step back400401 describe('parallel requests', function() {402 it('should not mix requests', function(done) {403 var req1 = { url: '/foo/50/bar', method: 'get' };404 var req2 = { url: '/foo/10/bar', method: 'get' };405 var router = new Router();406 var sub = new Router();407tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b
lines 408–409why, one step back408 done = after(2, done);409fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a
lines 410–426why, one step back410 sub.get('/bar', function(req, res, next) {411 next();412 });413414 router.param('ms', function(req, res, next, ms) {415 ms = parseInt(ms, 10);416 req.ms = ms;417 setTimeout(next, ms);418 });419420 router.use('/foo/:ms/', new Router());421 router.use('/foo/:ms/', sub);422423 router.handle(req1, {}, function(err) {424 assert.ifError(err);425 assert.equal(req1.ms, 50);426 assert.equal(req1.originalUrl, '/foo/50/bar');tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b
line 427why, one step back427 done();fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a
lines 428–433why, one step back428 });429430 router.handle(req2, {}, function(err) {431 assert.ifError(err);432 assert.equal(req2.ms, 10);433 assert.equal(req2.originalUrl, '/foo/10/bar');tests: flow control with afterDouglas Christopher Wilsonbefore adoption23ff74b
line 434why, one step back434 done();fix issue routing requests among sub routersDouglas Christopher Wilsonbefore adoptionababa6a
lines 435–437why, one step back435 });436 });437 });fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581
line 438why, one step back438})