change .status default to 404. Closes #263
This commit is contained in:
parent
48ac0669c5
commit
15ab936001
3 changed files with 10 additions and 8 deletions
|
@ -118,6 +118,7 @@ app.callback = function(){
|
|||
var self = this;
|
||||
|
||||
return function(req, res){
|
||||
res.statusCode = 404;
|
||||
var ctx = self.createContext(req, res);
|
||||
onFinished(ctx, ctx.onerror);
|
||||
fn.call(ctx, ctx.onerror);
|
||||
|
@ -176,7 +177,7 @@ function *respond(next) {
|
|||
if (res.headersSent || !this.writable) return;
|
||||
|
||||
var body = this.body;
|
||||
var code = this.status = this.status || 404;
|
||||
var code = this.status;
|
||||
|
||||
// ignore body
|
||||
if (status.empty[code]) {
|
||||
|
|
|
@ -53,7 +53,7 @@ module.exports = {
|
|||
*/
|
||||
|
||||
get statusString() {
|
||||
return http.STATUS_CODES[this.status || 404];
|
||||
return http.STATUS_CODES[this.status];
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -64,7 +64,7 @@ module.exports = {
|
|||
*/
|
||||
|
||||
get status() {
|
||||
return this._status;
|
||||
return this.res.statusCode;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -76,7 +76,8 @@ module.exports = {
|
|||
|
||||
set status(code) {
|
||||
if ('number' != typeof code) code = status(code);
|
||||
this._status = this.res.statusCode = code;
|
||||
this._explicitStatus = true;
|
||||
this.res.statusCode = code;
|
||||
if (this.body && status.empty[code]) this.body = null;
|
||||
},
|
||||
|
||||
|
@ -112,7 +113,7 @@ module.exports = {
|
|||
}
|
||||
|
||||
// set the status
|
||||
if (!this.status) this.status = 200;
|
||||
if (!this._explicitStatus) this.status = 200;
|
||||
|
||||
// set the content-type only if not yet set
|
||||
var setType = !this.header['content-type'];
|
||||
|
|
|
@ -125,6 +125,7 @@ describe('app.respond', function(){
|
|||
this.respond = false;
|
||||
|
||||
var res = this.res;
|
||||
res.statusCode = 200;
|
||||
setImmediate(function(){
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
res.end('lol');
|
||||
|
@ -284,14 +285,13 @@ describe('app.respond', function(){
|
|||
})
|
||||
|
||||
describe('when res has already been written to', function(){
|
||||
|
||||
it('should not cause an app error', function(done){
|
||||
var app = koa();
|
||||
|
||||
app.use(function *(next){
|
||||
var res = this.res;
|
||||
this.status = 200;
|
||||
res.setHeader("Content-Type", "text/html")
|
||||
res.status = 200;
|
||||
res.write('Hello');
|
||||
setTimeout(function(){
|
||||
res.end("Goodbye")
|
||||
|
@ -321,8 +321,8 @@ describe('app.respond', function(){
|
|||
|
||||
app.use(function *(next){
|
||||
var res = this.res;
|
||||
this.status = 200;
|
||||
res.setHeader("Content-Type", "text/html")
|
||||
res.status = 200;
|
||||
res.write('Hello');
|
||||
setTimeout(function(){
|
||||
res.end("Goodbye");
|
||||
|
|
Loading…
Reference in a new issue