From 2d1147ed212102088cffc388413a8ddacbac8328 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Fri, 24 Jan 2014 14:29:57 -0800 Subject: [PATCH] context.onerror: fix response handling closes #199 --- lib/context.js | 4 ++++ test/context/onerror.js | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 test/context/onerror.js diff --git a/lib/context.js b/lib/context.js index 386f99d..088979d 100644 --- a/lib/context.js +++ b/lib/context.js @@ -115,6 +115,9 @@ var proto = module.exports = { // delegate this.app.emit('error', err, this); + // unset all headers + this.res._headers = {}; + // force text/plain this.type = 'text'; @@ -128,6 +131,7 @@ var proto = module.exports = { var code = http.STATUS_CODES[err.status]; var msg = err.expose ? err.message : code; this.status = err.status; + this.length = Buffer.byteLength(msg); this.res.end(msg); } }; diff --git a/test/context/onerror.js b/test/context/onerror.js new file mode 100644 index 0000000..6ec053f --- /dev/null +++ b/test/context/onerror.js @@ -0,0 +1,53 @@ + +var koa = require('../..'); +var request = require('supertest'); + +describe('ctx.onerror(err)', function(){ + it('should respond', function(done){ + var app = koa(); + + app.use(function *(next){ + this.body = 'something else'; + + this.throw(499, 'boom'); + }) + + var server = app.listen(); + + request(server) + .get('/') + .expect(499) + .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'; + + this.throw(499, 'boom'); + }) + + var server = app.listen(); + + request(server) + .get('/') + .expect(499) + .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'); + res.headers.should.not.have.property('x-powered-by'); + + done(); + }) + }) +}) \ No newline at end of file