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 = [];
|
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
app.use(function *(ctx, next){
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(1);
|
2015-10-14 00:45:18 +00:00
|
|
|
yield next();
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(6);
|
|
|
|
});
|
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
app.use(function *(ctx, next){
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(2);
|
2015-10-14 00:45:18 +00:00
|
|
|
yield next();
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(5);
|
|
|
|
});
|
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
app.use(function *(ctx, next){
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(3);
|
2015-10-14 00:45:18 +00:00
|
|
|
yield next();
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(4);
|
|
|
|
});
|
|
|
|
|
|
|
|
const server = app.listen();
|
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(404)
|
|
|
|
.end(function(err){
|
|
|
|
if (err) return done(err);
|
2015-10-12 20:36:41 +00:00
|
|
|
calls.should.eql([1, 2, 3, 4, 5, 6]);
|
2015-10-12 04:59:30 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
// https://github.com/koajs/koa/pull/530#issuecomment-148138051
|
|
|
|
it('should catch thrown errors in non-async functions', function(done){
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
app.use(ctx => {
|
|
|
|
ctx.throw('Not Found', 404);
|
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect(404)
|
|
|
|
.end(done);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-25 07:54:57 +00:00
|
|
|
|
|
|
|
it('should throw error for non function', function(done){
|
|
|
|
const app = new Koa();
|
|
|
|
|
|
|
|
(() => app.use('not a function')).should.throw('middleware must be a function!');
|
|
|
|
done();
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|