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
Provenance
- Commit
cf41a8f- Tree
56405e5- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
1716e3b
3 files changed+220 added−2 removed
modified:lib/application.js+11 added-2 removed
155 unmodified lines156 var path = '/';157 var self = this;158 159 // default path to '/'160+ // disambiguate app.use([fn])161 if (typeof fn !== 'function') {162+ var arg = fn;163+164+ while (Array.isArray(arg) && arg.length !== 0) {165+ arg = arg[0];166+ }167+168+ if (typeof arg !== 'function' && (arg == null || arg.length !== 0)) {161- offset = 1;162- path = fn;169+ offset = 1;170+ path = fn;171+ }172 }173 174 var fns = flatten(slice.call(arguments, offset));175 392 unmodified linesmodified:test/app.use.js+204 added
169 unmodified lines170 request(app)171 .post('/foo')172 .expect(200, 'saw POST /foo', cb);173 })174+175+ it('should accept array of middleware', function (done) {176+ var app = express();177+178+ function fn1(req, res, next) {179+ res.setHeader('x-fn-1', 'hit');180+ next();181+ }182+183+ function fn2(req, res, next) {184+ res.setHeader('x-fn-2', 'hit');185+ next();186+ }187+188+ function fn3(req, res, next) {189+ res.setHeader('x-fn-3', 'hit');190+ res.end();191+ }192+193+ app.use([fn1, fn2, fn3]);194+195+ 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+ })202+203+ it('should accept multiple arrays of middleware', function (done) {204+ var app = express();205+206+ function fn1(req, res, next) {207+ res.setHeader('x-fn-1', 'hit');208+ next();209+ }210+211+ function fn2(req, res, next) {212+ res.setHeader('x-fn-2', 'hit');213+ next();214+ }215+216+ function fn3(req, res, next) {217+ res.setHeader('x-fn-3', 'hit');218+ res.end();219+ }220+221+ app.use([fn1, fn2], [fn3]);222+223+ 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+ })230+231+ it('should accept nested arrays of middleware', function (done) {232+ var app = express();233+234+ function fn1(req, res, next) {235+ res.setHeader('x-fn-1', 'hit');236+ next();237+ }238+239+ function fn2(req, res, next) {240+ res.setHeader('x-fn-2', 'hit');241+ next();242+ }243+244+ function fn3(req, res, next) {245+ res.setHeader('x-fn-3', 'hit');246+ res.end();247+ }248+249+ app.use([[fn1], fn2], [fn3]);250+251+ 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 })259 260 describe('.use(path, middleware)', function(){261 it('should strip path from req.url', function (done) {75 unmodified lines337 .post('/foo/bar')338 .expect(200, 'saw POST /bar', cb);339 })340 341+ it('should accept array of middleware', function (done) {342+ var app = express();343+344+ function fn1(req, res, next) {345+ res.setHeader('x-fn-1', 'hit');346+ next();347+ }348+349+ function fn2(req, res, next) {350+ res.setHeader('x-fn-2', 'hit');351+ next();352+ }353+354+ function fn3(req, res, next) {355+ res.setHeader('x-fn-3', 'hit');356+ res.end();357+ }358+359+ app.use('/foo', [fn1, fn2, fn3]);360+361+ 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+ })368+369+ it('should accept multiple arrays of middleware', function (done) {370+ var app = express();371+372+ function fn1(req, res, next) {373+ res.setHeader('x-fn-1', 'hit');374+ next();375+ }376+377+ function fn2(req, res, next) {378+ res.setHeader('x-fn-2', 'hit');379+ next();380+ }381+382+ function fn3(req, res, next) {383+ res.setHeader('x-fn-3', 'hit');384+ res.end();385+ }386+387+ app.use('/foo', [fn1, fn2], [fn3]);388+389+ 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+ })396+397+ it('should accept nested arrays of middleware', function (done) {398+ var app = express();399+400+ function fn1(req, res, next) {401+ res.setHeader('x-fn-1', 'hit');402+ next();403+ }404+405+ function fn2(req, res, next) {406+ res.setHeader('x-fn-2', 'hit');407+ next();408+ }409+410+ function fn3(req, res, next) {411+ res.setHeader('x-fn-3', 'hit');412+ res.end();413+ }414+415+ app.use('/foo', [fn1, [fn2]], [fn3]);416+417+ 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+ })424+425 it('should support array of paths', function (done) {426 var app = express();427 var cb = after(3, done);428 13 unmodified lines442 .get('/bar')443 .expect(200, 'saw GET / through /bar', cb);444 })445 446+ it('should support array of paths with middleware array', function (done) {447+ var app = express();448+ var cb = after(2, done);449+450+ function fn1(req, res, next) {451+ res.setHeader('x-fn-1', 'hit');452+ next();453+ }454+455+ function fn2(req, res, next) {456+ res.setHeader('x-fn-2', 'hit');457+ next();458+ }459+460+ function fn3(req, res, next) {461+ res.setHeader('x-fn-3', 'hit');462+ res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);463+ }464+465+ app.use(['/foo/', '/bar'], [[fn1], fn2], [fn3]);466+467+ 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);473+474+ 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+ })481+482 it('should support regexp path', function (done) {483 var app = express();484 var cb = after(4, done);485 22 unmodified linesmodified:History.md+5 added
1+unreleased2+==========3+4+ * Fix `app.use` to accept array of middleware without path5+6 4.9.0 / 2014-09-087 ==================8 9 * Add `res.sendStatus`2219 unmodified lines