9f27c1c414
change var to const for static require()'d modules make constant var references in app use const keyword refactor context to use es6 constants refactor request to use es6 constants, let block-scope coming next use const in response object for static refs make context tests use es6 constants experimental unit tests -> const use const for static references in unit test over req use const for static refs in res tests update app tests to use const for static refs make the context test use es6 constants for static refs use constants in the README es6 constants seem to work in --harmony on 0.12 too use const's for immutable refs in benchmarks ensure all JS files have blank newline at top add newline to bottom of file where missing add a webchat freenode link to irc channel no need to assign error in catch{}-able test app.silent option to turn off err logging keep test env logging for backwards-compat
116 lines
2.6 KiB
JavaScript
116 lines
2.6 KiB
JavaScript
|
|
'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';
|
|
|
|
this.throw(418, 'boom');
|
|
})
|
|
|
|
const server = app.listen();
|
|
|
|
request(server)
|
|
.get('/')
|
|
.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';
|
|
|
|
this.throw(418, 'boom');
|
|
})
|
|
|
|
const server = app.listen();
|
|
|
|
request(server)
|
|
.get('/')
|
|
.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);
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('when non-error thrown', function(){
|
|
it('should response non-error thrown message', function(done){
|
|
const app = koa();
|
|
|
|
app.use(function *(next){
|
|
throw 'string error';
|
|
})
|
|
|
|
const server = app.listen();
|
|
|
|
request(server)
|
|
.get('/')
|
|
.expect(500)
|
|
.expect('Content-Type', 'text/plain; charset=utf-8')
|
|
.expect('Internal Server Error', done);
|
|
})
|
|
})
|
|
})
|