Route.js
This file
Path
test/Route.js
274 lines · 6.3 KiB · JavaScript
Last changed by
Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ee4dd8 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 2–3why, one step back23var after = require('after');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 4why, one step back4var assert = require('assert')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 5–9why, one step back5var express = require('../')6 , Route = express.Route7 , methods = require('methods')89describe('Route', function(){Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 10–14why, one step back10 it('should work without handlers', function(done) {11 var req = { method: 'GET', url: '/' }12 var route = new Route('/foo')13 route.dispatch(req, {}, done)14 })deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
line 15why, one step back15Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 16–46why, one step back16 it('should not stack overflow with a large sync stack', function (done) {17 this.timeout(5000) // long-running test1819 var req = { method: 'GET', url: '/' }20 var route = new Route('/foo')2122 route.get(function (req, res, next) {23 req.counter = 024 next()25 })2627 for (var i = 0; i < 6000; i++) {28 route.all(function (req, res, next) {29 req.counter++30 next()31 })32 }3334 route.get(function (req, res, next) {35 req.called = true36 next()37 })3839 route.dispatch(req, {}, function (err) {40 if (err) return done(err)41 assert.ok(req.called)42 assert.strictEqual(req.counter, 6000)43 done()44 })45 })46deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 47–58why, one step back47 describe('.all', function(){48 it('should add handler', function(done){49 var req = { method: 'GET', url: '/' };50 var route = new Route('/foo');5152 route.all(function(req, res, next) {53 req.called = true;54 next();55 });5657 route.dispatch(req, {}, function (err) {58 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 59why, one step back59 assert.ok(req.called)deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 60–68why, one step back60 done();61 });62 })6364 it('should handle VERBS', function(done) {65 var count = 0;66 var route = new Route('/foo');67 var cb = after(methods.length, function (err) {68 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 69why, one step back69 assert.strictEqual(count, methods.length)deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 70–99why, one step back70 done();71 });7273 route.all(function(req, res, next) {74 count++;75 next();76 });7778 methods.forEach(function testMethod(method) {79 var req = { method: method, url: '/' };80 route.dispatch(req, {}, cb);81 });82 })8384 it('should stack', function(done) {85 var req = { count: 0, method: 'GET', url: '/' };86 var route = new Route('/foo');8788 route.all(function(req, res, next) {89 req.count++;90 next();91 });9293 route.all(function(req, res, next) {94 req.count++;95 next();96 });9798 route.dispatch(req, {}, function (err) {99 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 100why, one step back100 assert.strictEqual(req.count, 2)deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 101–117why, one step back101 done();102 });103 })104 })105106 describe('.VERB', function(){107 it('should support .get', function(done){108 var req = { method: 'GET', url: '/' };109 var route = new Route('');110111 route.get(function(req, res, next) {112 req.called = true;113 next();114 })115116 route.dispatch(req, {}, function (err) {117 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 118why, one step back118 assert.ok(req.called)deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 119–126why, one step back119 done();120 });121 })122123 it('should limit to just .VERB', function(done){124 var req = { method: 'POST', url: '/' };125 var route = new Route('');126Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 127why, one step back127 route.get(function () {deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 128–137why, one step back128 throw new Error('not me!');129 })130131 route.post(function(req, res, next) {132 req.called = true;133 next();134 })135136 route.dispatch(req, {}, function (err) {137 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 138why, one step back138 assert.ok(req.called)deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 139–163why, one step back139 done();140 });141 })142143 it('should allow fallthrough', function(done){144 var req = { order: '', method: 'GET', url: '/' };145 var route = new Route('');146147 route.get(function(req, res, next) {148 req.order += 'a';149 next();150 })151152 route.all(function(req, res, next) {153 req.order += 'b';154 next();155 });156157 route.get(function(req, res, next) {158 req.order += 'c';159 next();160 })161162 route.dispatch(req, {}, function (err) {163 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 164why, one step back164 assert.strictEqual(req.order, 'abc')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 165–189why, one step back165 done();166 });167 })168 })169170 describe('errors', function(){171 it('should handle errors via arity 4 functions', function(done){172 var req = { order: '', method: 'GET', url: '/' };173 var route = new Route('');174175 route.all(function(req, res, next){176 next(new Error('foobar'));177 });178179 route.all(function(req, res, next){180 req.order += '0';181 next();182 });183184 route.all(function(err, req, res, next){185 req.order += 'a';186 next(err);187 });188189 route.dispatch(req, {}, function (err) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 190–192why, one step back190 assert.ok(err)191 assert.strictEqual(err.message, 'foobar')192 assert.strictEqual(req.order, 'a')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 193–200why, one step back193 done();194 });195 })196197 it('should handle throw', function(done) {198 var req = { order: '', method: 'GET', url: '/' };199 var route = new Route('');200Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 201why, one step back201 route.all(function () {deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 202–215why, one step back202 throw new Error('foobar');203 });204205 route.all(function(req, res, next){206 req.order += '0';207 next();208 });209210 route.all(function(err, req, res, next){211 req.order += 'a';212 next(err);213 });214215 route.dispatch(req, {}, function (err) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 216–218why, one step back216 assert.ok(err)217 assert.strictEqual(err.message, 'foobar')218 assert.strictEqual(req.order, 'a')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 219–226why, one step back219 done();220 });221 });222223 it('should handle throwing inside error handlers', function(done) {224 var req = { method: 'GET', url: '/' };225 var route = new Route('');226Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 227why, one step back227 route.get(function () {deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 228–241why, one step back228 throw new Error('boom!');229 });230231 route.get(function(err, req, res, next){232 throw new Error('oops');233 });234235 route.get(function(err, req, res, next){236 req.message = err.message;237 next();238 });239240 route.dispatch(req, {}, function (err) {241 if (err) return done(err);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 242why, one step back242 assert.strictEqual(req.message, 'oops')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 243–255why, one step back243 done();244 });245 });246247 it('should handle throw in .all', function(done) {248 var req = { method: 'GET', url: '/' };249 var route = new Route('');250251 route.all(function(req, res, next){252 throw new Error('boom!');253 });254255 route.dispatch(req, {}, function(err){Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 256–257why, one step back256 assert.ok(err)257 assert.strictEqual(err.message, 'boom!')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 258–267why, one step back258 done();259 });260 });261262 it('should handle single error handler', function(done) {263 var req = { method: 'GET', url: '/' };264 var route = new Route('');265266 route.all(function(err, req, res, next){267 // this should not executeMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 268why, one step back268 throw new Error('should not be called')deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8
lines 269–274why, one step back269 });270271 route.dispatch(req, {}, done);272 });273 })274})