koa-lite/test/context/onerror.js

117 lines
2.6 KiB
JavaScript
Raw Normal View History

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