2015-10-12 04:59:30 +00:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const request = require('supertest');
|
2016-03-14 14:51:14 +00:00
|
|
|
const assert = require('assert');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('app.use(fn)', () => {
|
2019-06-26 03:15:22 +00:00
|
|
|
it('should compose middleware', async() => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
const calls = [];
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(1);
|
2015-10-25 07:54:57 +00:00
|
|
|
return next().then(() => {
|
2015-10-27 14:59:40 +00:00
|
|
|
calls.push(6);
|
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
});
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(2);
|
2015-10-25 07:54:57 +00:00
|
|
|
return next().then(() => {
|
2015-10-27 14:59:40 +00:00
|
|
|
calls.push(5);
|
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
});
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-12 04:59:30 +00:00
|
|
|
calls.push(3);
|
2015-10-25 07:54:57 +00:00
|
|
|
return next().then(() => {
|
2015-10-27 14:59:40 +00:00
|
|
|
calls.push(4);
|
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const server = app.listen();
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
await request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect(404);
|
|
|
|
|
|
|
|
assert.deepEqual(calls, [1, 2, 3, 4, 5, 6]);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2019-06-26 03:15:22 +00:00
|
|
|
it('should compose mixed middleware', async() => {
|
2016-03-14 14:51:14 +00:00
|
|
|
process.once('deprecation', () => {}); // silence deprecation message
|
|
|
|
const app = new Koa();
|
|
|
|
const calls = [];
|
|
|
|
|
|
|
|
app.use((ctx, next) => {
|
|
|
|
calls.push(1);
|
|
|
|
return next().then(() => {
|
|
|
|
calls.push(6);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-28 21:21:05 +00:00
|
|
|
app.use(function * (next){
|
2016-03-14 14:51:14 +00:00
|
|
|
calls.push(2);
|
|
|
|
yield next;
|
|
|
|
calls.push(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use((ctx, next) => {
|
|
|
|
calls.push(3);
|
|
|
|
return next().then(() => {
|
|
|
|
calls.push(4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const server = app.listen();
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
await request(server)
|
2016-03-14 14:51:14 +00:00
|
|
|
.get('/')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect(404);
|
|
|
|
|
|
|
|
assert.deepEqual(calls, [1, 2, 3, 4, 5, 6]);
|
2016-03-14 14:51:14 +00:00
|
|
|
});
|
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
// https://github.com/koajs/koa/pull/530#issuecomment-148138051
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should catch thrown errors in non-async functions', () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use(ctx => ctx.throw('Not Found', 404));
|
2015-10-12 04:59:30 +00:00
|
|
|
|
2017-09-26 04:07:57 +00:00
|
|
|
return request(app.callback())
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect(404);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2015-10-25 07:54:57 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should accept both generator and function middleware', () => {
|
2016-03-14 14:51:14 +00:00
|
|
|
process.once('deprecation', () => {}); // silence deprecation message
|
|
|
|
const app = new Koa();
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
app.use((ctx, next) => next());
|
2016-03-28 21:21:05 +00:00
|
|
|
app.use(function * (next){ this.body = 'generator'; });
|
2016-03-14 14:51:14 +00:00
|
|
|
|
2017-09-26 04:07:57 +00:00
|
|
|
return request(app.callback())
|
2016-03-14 14:51:14 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(200)
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect('generator');
|
2016-03-14 14:51:14 +00:00
|
|
|
});
|
|
|
|
|
2015-11-25 00:06:30 +00:00
|
|
|
it('should throw error for non function', () => {
|
2015-10-25 07:54:57 +00:00
|
|
|
const app = new Koa();
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
[null, undefined, 0, false, 'not a function'].forEach(v => {
|
|
|
|
assert.throws(() => app.use(v), /middleware must be a function!/);
|
|
|
|
});
|
2015-10-25 07:54:57 +00:00
|
|
|
});
|
2015-10-27 14:59:40 +00:00
|
|
|
|
2016-03-14 14:51:14 +00:00
|
|
|
it('should output deprecation message for generator functions', done => {
|
|
|
|
process.once('deprecation', message => {
|
2016-10-17 15:45:06 +00:00
|
|
|
assert(/Support for generators will be removed/.test(message));
|
2016-03-14 14:51:14 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-10-27 14:59:40 +00:00
|
|
|
|
2016-03-14 14:51:14 +00:00
|
|
|
const app = new Koa();
|
2016-03-28 21:21:05 +00:00
|
|
|
app.use(function * (){});
|
2015-10-27 14:59:40 +00:00
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|