merge
This commit is contained in:
commit
5cdae4445a
6 changed files with 91 additions and 1 deletions
|
@ -102,6 +102,12 @@ throw err;
|
||||||
error messages since you do not want to leak failure
|
error messages since you do not want to leak failure
|
||||||
details.
|
details.
|
||||||
|
|
||||||
|
### ctx.respond
|
||||||
|
|
||||||
|
To bypass Koa's built-in response handling, you may explicitly set `this.respond = false;`. Use this if you want to write to the raw `res` object instead of letting Koa handle the response for you.
|
||||||
|
|
||||||
|
Note that using this is __not__ supported by Koa. This may break intended functionality of Koa middleware and Koa itself. Using this properly is considered a hack and is only a convenience to those wishing to use traditional `fn(req, res)` functions and middleware within Koa.
|
||||||
|
|
||||||
## Request aliases
|
## Request aliases
|
||||||
|
|
||||||
The following accessors and alias [Request](request.md) equivalents:
|
The following accessors and alias [Request](request.md) equivalents:
|
||||||
|
|
|
@ -184,6 +184,8 @@ function *respond(next){
|
||||||
|
|
||||||
yield *next;
|
yield *next;
|
||||||
|
|
||||||
|
if (false === this.respond) return;
|
||||||
|
|
||||||
var res = this.res;
|
var res = this.res;
|
||||||
if (res.headersSent || !res.socket.writable) return;
|
if (res.headersSent || !res.socket.writable) return;
|
||||||
|
|
||||||
|
|
|
@ -115,6 +115,9 @@ var proto = module.exports = {
|
||||||
// delegate
|
// delegate
|
||||||
this.app.emit('error', err, this);
|
this.app.emit('error', err, this);
|
||||||
|
|
||||||
|
// unset all headers
|
||||||
|
this.res._headers = {};
|
||||||
|
|
||||||
// force text/plain
|
// force text/plain
|
||||||
this.type = 'text';
|
this.type = 'text';
|
||||||
|
|
||||||
|
@ -128,6 +131,7 @@ var proto = module.exports = {
|
||||||
var code = http.STATUS_CODES[err.status];
|
var code = http.STATUS_CODES[err.status];
|
||||||
var msg = err.expose ? err.message : code;
|
var msg = err.expose ? err.message : code;
|
||||||
this.status = err.status;
|
this.status = err.status;
|
||||||
|
this.length = Buffer.byteLength(msg);
|
||||||
this.res.end(msg);
|
this.res.end(msg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
"debug": "*",
|
"debug": "*",
|
||||||
"mime": "~1.2.11",
|
"mime": "~1.2.11",
|
||||||
"fresh": "~0.2.1",
|
"fresh": "~0.2.1",
|
||||||
"koa-compose": "~2.1.0",
|
"koa-compose": "~2.2.0",
|
||||||
"cookies": "~0.3.7",
|
"cookies": "~0.3.7",
|
||||||
"keygrip": "~1.0.0",
|
"keygrip": "~1.0.0",
|
||||||
"delegates": "0.0.3"
|
"delegates": "0.0.3"
|
||||||
|
|
|
@ -94,6 +94,31 @@ describe('app.use(fn)', function(){
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('app.respond', function(){
|
describe('app.respond', function(){
|
||||||
|
describe('when this.respond === false', function(){
|
||||||
|
it('should bypass app.respond', function(done){
|
||||||
|
var app = koa();
|
||||||
|
|
||||||
|
app.use(function *(){
|
||||||
|
this.body = 'Hello';
|
||||||
|
this.respond = false;
|
||||||
|
|
||||||
|
var res = this.res;
|
||||||
|
setImmediate(function () {
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
res.end('lol');
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
var server = app.listen();
|
||||||
|
|
||||||
|
request(server)
|
||||||
|
.get('/')
|
||||||
|
.expect(200)
|
||||||
|
.expect('lol')
|
||||||
|
.end(done);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('when HEAD is used', function(){
|
describe('when HEAD is used', function(){
|
||||||
it('should not respond with the body', function(done){
|
it('should not respond with the body', function(done){
|
||||||
var app = koa();
|
var app = koa();
|
||||||
|
|
53
test/context/onerror.js
Normal file
53
test/context/onerror.js
Normal file
|
@ -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();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in a new issue