2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const request = require('supertest');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
describe('app.use(fn)', function(){
|
|
|
|
it('should compose middleware', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
const calls = [];
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
calls.push(1);
|
|
|
|
yield next;
|
|
|
|
calls.push(6);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
calls.push(2);
|
|
|
|
yield next;
|
|
|
|
calls.push(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
calls.push(3);
|
|
|
|
yield next;
|
|
|
|
calls.push(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
const server = app.listen();
|
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(404)
|
|
|
|
.end(function(err){
|
|
|
|
if (err) return done(err);
|
|
|
|
calls.should.eql([1,2,3,4,5,6]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should error when a non-generator function is passed', function(){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
app.use(function(){});
|
|
|
|
} catch (err) {
|
|
|
|
err.message.should.equal('app.use() requires a generator function');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not error when a non-generator function is passed when .experimental=true', function(){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
app.experimental = true;
|
|
|
|
app.use(function(){});
|
|
|
|
})
|
|
|
|
})
|