Skip to content
Myr

Fix app.use to accept array of middleware without path

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes #2356

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 1716e3b · Browse files · 3 files+220 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+220 added−2 removed

  • tests:+204 added−0 removed
  • other:+16 added−2 removed

3 files+220 added−2 removed

modified:lib/application.js+11 added-2 removed
155 unmodified lines
156 var path = '/';157 var self = this;158159 // default path to '/'160 // disambiguate app.use([fn])161 if (typeof fn !== 'function') {162 var arg = fn;163164 while (Array.isArray(arg) && arg.length !== 0) {165 arg = arg[0];166 }167168 if (typeof arg !== 'function' && (arg == null || arg.length !== 0)) {161 offset = 1;162 path = fn;169 offset = 1;170 path = fn;171 }172 }173174 var fns = flatten(slice.call(arguments, offset));175
392 unmodified lines
modified:test/app.use.js+204 added
169 unmodified lines
170 request(app)171 .post('/foo')172 .expect(200, 'saw POST /foo', cb);173 })174175 it('should accept array of middleware', function (done) {176 var app = express();177178 function fn1(req, res, next) {179 res.setHeader('x-fn-1', 'hit');180 next();181 }182183 function fn2(req, res, next) {184 res.setHeader('x-fn-2', 'hit');185 next();186 }187188 function fn3(req, res, next) {189 res.setHeader('x-fn-3', 'hit');190 res.end();191 }192193 app.use([fn1, fn2, fn3]);194195 request(app)196 .get('/')197 .expect('x-fn-1', 'hit')198 .expect('x-fn-2', 'hit')199 .expect('x-fn-3', 'hit')200 .expect(200, done);201 })202203 it('should accept multiple arrays of middleware', function (done) {204 var app = express();205206 function fn1(req, res, next) {207 res.setHeader('x-fn-1', 'hit');208 next();209 }210211 function fn2(req, res, next) {212 res.setHeader('x-fn-2', 'hit');213 next();214 }215216 function fn3(req, res, next) {217 res.setHeader('x-fn-3', 'hit');218 res.end();219 }220221 app.use([fn1, fn2], [fn3]);222223 request(app)224 .get('/')225 .expect('x-fn-1', 'hit')226 .expect('x-fn-2', 'hit')227 .expect('x-fn-3', 'hit')228 .expect(200, done);229 })230231 it('should accept nested arrays of middleware', function (done) {232 var app = express();233234 function fn1(req, res, next) {235 res.setHeader('x-fn-1', 'hit');236 next();237 }238239 function fn2(req, res, next) {240 res.setHeader('x-fn-2', 'hit');241 next();242 }243244 function fn3(req, res, next) {245 res.setHeader('x-fn-3', 'hit');246 res.end();247 }248249 app.use([[fn1], fn2], [fn3]);250251 request(app)252 .get('/')253 .expect('x-fn-1', 'hit')254 .expect('x-fn-2', 'hit')255 .expect('x-fn-3', 'hit')256 .expect(200, done);257 })258 })259260 describe('.use(path, middleware)', function(){261 it('should strip path from req.url', function (done) {
75 unmodified lines
337 .post('/foo/bar')338 .expect(200, 'saw POST /bar', cb);339 })340341 it('should accept array of middleware', function (done) {342 var app = express();343344 function fn1(req, res, next) {345 res.setHeader('x-fn-1', 'hit');346 next();347 }348349 function fn2(req, res, next) {350 res.setHeader('x-fn-2', 'hit');351 next();352 }353354 function fn3(req, res, next) {355 res.setHeader('x-fn-3', 'hit');356 res.end();357 }358359 app.use('/foo', [fn1, fn2, fn3]);360361 request(app)362 .get('/foo')363 .expect('x-fn-1', 'hit')364 .expect('x-fn-2', 'hit')365 .expect('x-fn-3', 'hit')366 .expect(200, done);367 })368369 it('should accept multiple arrays of middleware', function (done) {370 var app = express();371372 function fn1(req, res, next) {373 res.setHeader('x-fn-1', 'hit');374 next();375 }376377 function fn2(req, res, next) {378 res.setHeader('x-fn-2', 'hit');379 next();380 }381382 function fn3(req, res, next) {383 res.setHeader('x-fn-3', 'hit');384 res.end();385 }386387 app.use('/foo', [fn1, fn2], [fn3]);388389 request(app)390 .get('/foo')391 .expect('x-fn-1', 'hit')392 .expect('x-fn-2', 'hit')393 .expect('x-fn-3', 'hit')394 .expect(200, done);395 })396397 it('should accept nested arrays of middleware', function (done) {398 var app = express();399400 function fn1(req, res, next) {401 res.setHeader('x-fn-1', 'hit');402 next();403 }404405 function fn2(req, res, next) {406 res.setHeader('x-fn-2', 'hit');407 next();408 }409410 function fn3(req, res, next) {411 res.setHeader('x-fn-3', 'hit');412 res.end();413 }414415 app.use('/foo', [fn1, [fn2]], [fn3]);416417 request(app)418 .get('/foo')419 .expect('x-fn-1', 'hit')420 .expect('x-fn-2', 'hit')421 .expect('x-fn-3', 'hit')422 .expect(200, done);423 })424425 it('should support array of paths', function (done) {426 var app = express();427 var cb = after(3, done);428
13 unmodified lines
442 .get('/bar')443 .expect(200, 'saw GET / through /bar', cb);444 })445446 it('should support array of paths with middleware array', function (done) {447 var app = express();448 var cb = after(2, done);449450 function fn1(req, res, next) {451 res.setHeader('x-fn-1', 'hit');452 next();453 }454455 function fn2(req, res, next) {456 res.setHeader('x-fn-2', 'hit');457 next();458 }459460 function fn3(req, res, next) {461 res.setHeader('x-fn-3', 'hit');462 res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);463 }464465 app.use(['/foo/', '/bar'], [[fn1], fn2], [fn3]);466467 request(app)468 .get('/foo')469 .expect('x-fn-1', 'hit')470 .expect('x-fn-2', 'hit')471 .expect('x-fn-3', 'hit')472 .expect(200, 'saw GET / through /foo', cb);473474 request(app)475 .get('/bar')476 .expect('x-fn-1', 'hit')477 .expect('x-fn-2', 'hit')478 .expect('x-fn-3', 'hit')479 .expect(200, 'saw GET / through /bar', cb);480 })481482 it('should support regexp path', function (done) {483 var app = express();484 var cb = after(4, done);485
22 unmodified lines
modified:History.md+5 added
1unreleased2==========34 * Fix `app.use` to accept array of middleware without path564.9.0 / 2014-09-087==================89 * Add `res.sendStatus`
2219 unmodified lines