2014-01-24 22:29:57 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-02-21 06:52:57 +00:00
|
|
|
var assert = require('assert');
|
2014-01-24 22:29:57 +00:00
|
|
|
var request = require('supertest');
|
2014-11-16 13:52:14 +00:00
|
|
|
var koa = require('../..');
|
2017-02-21 06:52:57 +00:00
|
|
|
var context = require('../context');
|
2014-01-24 22:29:57 +00:00
|
|
|
|
|
|
|
describe('ctx.onerror(err)', function(){
|
|
|
|
it('should respond', function(done){
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
this.body = 'something else';
|
|
|
|
|
2014-03-13 01:29:14 +00:00
|
|
|
this.throw(418, 'boom');
|
2014-01-24 22:29:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var server = app.listen();
|
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-03-13 01:29:14 +00:00
|
|
|
.expect(418)
|
2014-01-24 22:29:57 +00:00
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
|
|
|
.expect('Content-Length', '4')
|
|
|
|
.end(done);
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should unset all headers', function(done){
|
|
|
|
var 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');
|
2014-01-24 22:29:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var server = app.listen();
|
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
2014-03-13 01:29:14 +00:00
|
|
|
.expect(418)
|
2014-01-24 22:29:57 +00:00
|
|
|
.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();
|
|
|
|
})
|
|
|
|
})
|
2014-08-06 13:31:55 +00:00
|
|
|
|
2016-02-29 20:22:59 +00:00
|
|
|
it('should set headers specified in the error', function(done){
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
this.set('Vary', 'Accept-Encoding');
|
|
|
|
this.set('X-CSRF-Token', 'asdf');
|
|
|
|
this.body = 'response';
|
|
|
|
|
|
|
|
throw Object.assign(new Error('boom'), {
|
|
|
|
status: 418,
|
|
|
|
expose: true,
|
|
|
|
headers: {
|
|
|
|
'X-New-Header': 'Value'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var server = app.listen();
|
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(418)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
|
|
|
.expect('X-New-Header', 'Value')
|
|
|
|
.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();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-03-02 09:30:33 +00:00
|
|
|
it('should ignore error after headerSent', function(done){
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
app.on('error', function(err){
|
|
|
|
err.message.should.equal('mock error');
|
|
|
|
err.headerSent.should.equal(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(function*() {
|
|
|
|
this.status = 200;
|
|
|
|
this.set('X-Foo', 'Bar');
|
|
|
|
this.res.flushHeaders();
|
|
|
|
yield Promise.reject(new Error('mock error'));
|
|
|
|
this.body = 'response';
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app.listen())
|
|
|
|
.get('/')
|
|
|
|
.expect('X-Foo', 'Bar')
|
|
|
|
.expect(200, function() {});
|
|
|
|
})
|
|
|
|
|
2014-08-06 13:10:52 +00:00
|
|
|
describe('when invalid err.status', function(){
|
|
|
|
describe('not number', function(){
|
|
|
|
it('should respond 500', function(done){
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
this.body = 'something else';
|
|
|
|
var err = new Error('some error');
|
|
|
|
err.status = 'notnumber';
|
2014-08-06 13:31:55 +00:00
|
|
|
throw err;
|
2014-08-06 13:10:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var 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){
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
this.body = 'something else';
|
|
|
|
var err = new Error('some error');
|
|
|
|
err.status = 9999;
|
2014-08-06 13:31:55 +00:00
|
|
|
throw err;
|
2014-08-06 13:10:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var 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){
|
|
|
|
var app = koa();
|
|
|
|
|
|
|
|
app.use(function *(next){
|
|
|
|
throw 'string error';
|
|
|
|
})
|
|
|
|
|
|
|
|
var server = app.listen();
|
|
|
|
|
|
|
|
request(server)
|
|
|
|
.get('/')
|
|
|
|
.expect(500)
|
|
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
|
|
|
.expect('Internal Server Error', done);
|
|
|
|
})
|
|
|
|
})
|
2017-02-21 06:52:57 +00:00
|
|
|
|
|
|
|
it('should use res.getHeaderNames when available', function(){
|
|
|
|
var removed = 0;
|
|
|
|
var ctx = context();
|
|
|
|
|
|
|
|
ctx.app.emit = function() {};
|
|
|
|
ctx.res = {
|
|
|
|
getHeaderNames: function() { return ['content-type', 'content-length'] },
|
|
|
|
removeHeader: function() { removed++ },
|
|
|
|
end: function() {},
|
|
|
|
emit: function() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.onerror(new Error('error'));
|
|
|
|
|
|
|
|
assert.equal(removed, 2);
|
|
|
|
})
|
2014-04-24 17:08:08 +00:00
|
|
|
})
|