2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-08 06:59:24 +00:00
|
|
|
const assert = require('assert');
|
2015-10-05 18:23:47 +00:00
|
|
|
const request = require('supertest');
|
2015-10-13 06:19:42 +00:00
|
|
|
const Koa = require('../..');
|
2017-03-08 06:59:24 +00:00
|
|
|
const context = require('../helpers/context');
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.onerror(err)', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
global.console = jest.genMockFromModule('console');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
global.console = require('console');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should respond', () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.body = 'something else';
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.throw(418, 'boom');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
return request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(418)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect('Content-Length', '4');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should unset all headers', async () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.set('Vary', 'Accept-Encoding');
|
|
|
|
ctx.set('X-CSRF-Token', 'asdf');
|
|
|
|
ctx.body = 'response';
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.throw(418, 'boom');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const res = await request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(418)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect('Content-Length', '4');
|
2014-01-24 22:29:57 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.headers.hasOwnProperty('vary'), false);
|
|
|
|
assert.equal(res.headers.hasOwnProperty('x-csrf-token'), false);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-08-06 13:31:55 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should set headers specified in the error', async () => {
|
2016-02-29 20:22:59 +00:00
|
|
|
const app = new Koa();
|
|
|
|
|
|
|
|
app.use((ctx, next) => {
|
|
|
|
ctx.set('Vary', 'Accept-Encoding');
|
|
|
|
ctx.set('X-CSRF-Token', 'asdf');
|
|
|
|
ctx.body = 'response';
|
|
|
|
|
|
|
|
throw Object.assign(new Error('boom'), {
|
|
|
|
status: 418,
|
|
|
|
expose: true,
|
|
|
|
headers: {
|
|
|
|
'X-New-Header': 'Value'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const server = app.listen();
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const res = await request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(418)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
|
|
|
.expect('X-New-Header', 'Value');
|
2016-02-29 20:22:59 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(res.headers.hasOwnProperty('vary'), false);
|
|
|
|
assert.equal(res.headers.hasOwnProperty('x-csrf-token'), false);
|
2016-02-29 20:22:59 +00:00
|
|
|
});
|
|
|
|
|
2017-02-28 02:52:54 +00:00
|
|
|
it('should ignore error after headerSent', done => {
|
|
|
|
const app = new Koa();
|
|
|
|
|
|
|
|
app.on('error', err => {
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(err.message, 'mock error');
|
|
|
|
assert.equal(err.headerSent, true);
|
2017-02-28 02:52:54 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(async ctx => {
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.set('X-Foo', 'Bar');
|
|
|
|
ctx.flushHeaders();
|
|
|
|
await Promise.reject(new Error('mock error'));
|
|
|
|
ctx.body = 'response';
|
|
|
|
});
|
|
|
|
|
2017-09-26 04:07:57 +00:00
|
|
|
request(app.callback())
|
2017-05-11 03:30:32 +00:00
|
|
|
.get('/')
|
|
|
|
.expect('X-Foo', 'Bar')
|
|
|
|
.expect(200, () => {});
|
2017-02-28 02:52:54 +00:00
|
|
|
});
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when invalid err.status', () => {
|
|
|
|
describe('not number', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should respond 500', () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.body = 'something else';
|
2015-10-05 18:23:47 +00:00
|
|
|
const err = new Error('some error');
|
2014-08-06 13:10:52 +00:00
|
|
|
err.status = 'notnumber';
|
2014-08-06 13:31:55 +00:00
|
|
|
throw err;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
return request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(500)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect('Internal Server Error');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('not http status code', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should respond 500', () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-14 00:45:18 +00:00
|
|
|
ctx.body = 'something else';
|
2015-10-05 18:23:47 +00:00
|
|
|
const err = new Error('some error');
|
2014-08-06 13:10:52 +00:00
|
|
|
err.status = 9999;
|
2014-08-06 13:31:55 +00:00
|
|
|
throw err;
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2014-08-06 13:10:52 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
return request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(500)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect('Internal Server Error');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-14 02:30:19 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when non-error thrown', () => {
|
2017-05-11 03:30:32 +00:00
|
|
|
it('should response non-error thrown message', () => {
|
2015-10-13 06:19:42 +00:00
|
|
|
const app = new Koa();
|
2015-04-14 02:30:19 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
app.use((ctx, next) => {
|
2015-10-12 20:36:41 +00:00
|
|
|
throw 'string error'; // eslint-disable-line no-throw-literal
|
|
|
|
});
|
2015-04-14 02:30:19 +00:00
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const server = app.listen();
|
2015-04-14 02:30:19 +00:00
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
return request(server)
|
2015-10-28 07:53:49 +00:00
|
|
|
.get('/')
|
|
|
|
.expect(500)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
2017-05-11 03:30:32 +00:00
|
|
|
.expect('Internal Server Error');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2017-03-08 06:59:24 +00:00
|
|
|
|
|
|
|
it('should use res.getHeaderNames() accessor when available', () => {
|
|
|
|
let removed = 0;
|
|
|
|
const ctx = context();
|
|
|
|
|
|
|
|
ctx.app.emit = () => {};
|
|
|
|
ctx.res = {
|
|
|
|
getHeaderNames: () => ['content-type', 'content-length'],
|
|
|
|
removeHeader: () => removed++,
|
|
|
|
end: () => {},
|
|
|
|
emit: () => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.onerror(new Error('error'));
|
|
|
|
|
|
|
|
assert.equal(removed, 2);
|
|
|
|
});
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|