mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 12:19:51 +01:00
parent
1716e3b067
commit
cf41a8f254
|
|
@ -1,3 +1,8 @@
|
|||
unreleased
|
||||
==========
|
||||
|
||||
* Fix `app.use` to accept array of middleware without path
|
||||
|
||||
4.9.0 / 2014-09-08
|
||||
==================
|
||||
|
||||
|
|
|
|||
|
|
@ -157,9 +157,18 @@ app.use = function use(fn) {
|
|||
var self = this;
|
||||
|
||||
// default path to '/'
|
||||
// disambiguate app.use([fn])
|
||||
if (typeof fn !== 'function') {
|
||||
offset = 1;
|
||||
path = fn;
|
||||
var arg = fn;
|
||||
|
||||
while (Array.isArray(arg) && arg.length !== 0) {
|
||||
arg = arg[0];
|
||||
}
|
||||
|
||||
if (typeof arg !== 'function' && (arg == null || arg.length !== 0)) {
|
||||
offset = 1;
|
||||
path = fn;
|
||||
}
|
||||
}
|
||||
|
||||
var fns = flatten(slice.call(arguments, offset));
|
||||
|
|
|
|||
204
test/app.use.js
204
test/app.use.js
|
|
@ -171,6 +171,90 @@ describe('app', function(){
|
|||
.post('/foo')
|
||||
.expect(200, 'saw POST /foo', cb);
|
||||
})
|
||||
|
||||
it('should accept array of middleware', function (done) {
|
||||
var app = express();
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.end();
|
||||
}
|
||||
|
||||
app.use([fn1, fn2, fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
it('should accept multiple arrays of middleware', function (done) {
|
||||
var app = express();
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.end();
|
||||
}
|
||||
|
||||
app.use([fn1, fn2], [fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
it('should accept nested arrays of middleware', function (done) {
|
||||
var app = express();
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.end();
|
||||
}
|
||||
|
||||
app.use([[fn1], fn2], [fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, done);
|
||||
})
|
||||
})
|
||||
|
||||
describe('.use(path, middleware)', function(){
|
||||
|
|
@ -254,6 +338,90 @@ describe('app', function(){
|
|||
.expect(200, 'saw POST /bar', cb);
|
||||
})
|
||||
|
||||
it('should accept array of middleware', function (done) {
|
||||
var app = express();
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.end();
|
||||
}
|
||||
|
||||
app.use('/foo', [fn1, fn2, fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/foo')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
it('should accept multiple arrays of middleware', function (done) {
|
||||
var app = express();
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.end();
|
||||
}
|
||||
|
||||
app.use('/foo', [fn1, fn2], [fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/foo')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
it('should accept nested arrays of middleware', function (done) {
|
||||
var app = express();
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.end();
|
||||
}
|
||||
|
||||
app.use('/foo', [fn1, [fn2]], [fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/foo')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, done);
|
||||
})
|
||||
|
||||
it('should support array of paths', function (done) {
|
||||
var app = express();
|
||||
var cb = after(3, done);
|
||||
|
|
@ -275,6 +443,42 @@ describe('app', function(){
|
|||
.expect(200, 'saw GET / through /bar', cb);
|
||||
})
|
||||
|
||||
it('should support array of paths with middleware array', function (done) {
|
||||
var app = express();
|
||||
var cb = after(2, done);
|
||||
|
||||
function fn1(req, res, next) {
|
||||
res.setHeader('x-fn-1', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn2(req, res, next) {
|
||||
res.setHeader('x-fn-2', 'hit');
|
||||
next();
|
||||
}
|
||||
|
||||
function fn3(req, res, next) {
|
||||
res.setHeader('x-fn-3', 'hit');
|
||||
res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl);
|
||||
}
|
||||
|
||||
app.use(['/foo/', '/bar'], [[fn1], fn2], [fn3]);
|
||||
|
||||
request(app)
|
||||
.get('/foo')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, 'saw GET / through /foo', cb);
|
||||
|
||||
request(app)
|
||||
.get('/bar')
|
||||
.expect('x-fn-1', 'hit')
|
||||
.expect('x-fn-2', 'hit')
|
||||
.expect('x-fn-3', 'hit')
|
||||
.expect(200, 'saw GET / through /bar', cb);
|
||||
})
|
||||
|
||||
it('should support regexp path', function (done) {
|
||||
var app = express();
|
||||
var cb = after(4, done);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user