koa-lite/test/context/onerror.js

117 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
const request = require('supertest');
2015-10-13 06:19:42 +00:00
const Koa = require('../..');
describe('ctx.onerror(err)', () => {
it('should respond', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.body = 'something else';
ctx.throw(418, 'boom');
2015-10-12 20:36:41 +00:00
});
const server = app.listen();
request(server)
.get('/')
.expect(418)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Length', '4')
.end(done);
2015-10-12 20:36:41 +00:00
});
it('should unset all headers', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.set('Vary', 'Accept-Encoding');
ctx.set('X-CSRF-Token', 'asdf');
ctx.body = 'response';
ctx.throw(418, 'boom');
2015-10-12 20:36:41 +00:00
});
const server = app.listen();
request(server)
.get('/')
.expect(418)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Length', '4')
.end((err, res) => {
if (err) return done(err);
res.headers.should.not.have.property('vary');
res.headers.should.not.have.property('x-csrf-token');
done();
});
2015-10-12 20:36:41 +00:00
});
describe('when invalid err.status', () => {
describe('not number', () => {
it('should respond 500', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.body = 'something else';
const err = new Error('some error');
err.status = 'notnumber';
throw err;
2015-10-12 20:36:41 +00:00
});
const server = app.listen();
request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error', done);
2015-10-12 20:36:41 +00:00
});
});
describe('not http status code', () => {
it('should respond 500', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
app.use((ctx, next) => {
ctx.body = 'something else';
const err = new Error('some error');
err.status = 9999;
throw err;
2015-10-12 20:36:41 +00:00
});
const server = app.listen();
request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error', done);
2015-10-12 20:36:41 +00:00
});
});
});
2015-04-14 02:30:19 +00:00
describe('when non-error thrown', () => {
it('should response non-error thrown message', done => {
2015-10-13 06:19:42 +00:00
const app = new Koa();
2015-04-14 02:30:19 +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
const server = app.listen();
2015-04-14 02:30:19 +00:00
request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error', done);
2015-10-12 20:36:41 +00:00
});
});
});