change .status default to 404. Closes #263

master
TJ Holowaychuk 2014-04-28 21:17:30 -07:00
parent 48ac0669c5
commit 15ab936001
3 changed files with 10 additions and 8 deletions

View File

@ -118,6 +118,7 @@ app.callback = function(){
var self = this; var self = this;
return function(req, res){ return function(req, res){
res.statusCode = 404;
var ctx = self.createContext(req, res); var ctx = self.createContext(req, res);
onFinished(ctx, ctx.onerror); onFinished(ctx, ctx.onerror);
fn.call(ctx, ctx.onerror); fn.call(ctx, ctx.onerror);
@ -176,7 +177,7 @@ function *respond(next) {
if (res.headersSent || !this.writable) return; if (res.headersSent || !this.writable) return;
var body = this.body; var body = this.body;
var code = this.status = this.status || 404; var code = this.status;
// ignore body // ignore body
if (status.empty[code]) { if (status.empty[code]) {

View File

@ -53,7 +53,7 @@ module.exports = {
*/ */
get statusString() { get statusString() {
return http.STATUS_CODES[this.status || 404]; return http.STATUS_CODES[this.status];
}, },
/** /**
@ -64,7 +64,7 @@ module.exports = {
*/ */
get status() { get status() {
return this._status; return this.res.statusCode;
}, },
/** /**
@ -76,7 +76,8 @@ module.exports = {
set status(code) { set status(code) {
if ('number' != typeof code) code = 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; if (this.body && status.empty[code]) this.body = null;
}, },
@ -112,7 +113,7 @@ module.exports = {
} }
// set the status // set the status
if (!this.status) this.status = 200; if (!this._explicitStatus) this.status = 200;
// set the content-type only if not yet set // set the content-type only if not yet set
var setType = !this.header['content-type']; var setType = !this.header['content-type'];

View File

@ -125,6 +125,7 @@ describe('app.respond', function(){
this.respond = false; this.respond = false;
var res = this.res; var res = this.res;
res.statusCode = 200;
setImmediate(function(){ setImmediate(function(){
res.setHeader('Content-Type', 'text/plain'); res.setHeader('Content-Type', 'text/plain');
res.end('lol'); res.end('lol');
@ -284,14 +285,13 @@ describe('app.respond', function(){
}) })
describe('when res has already been written to', function(){ describe('when res has already been written to', function(){
it('should not cause an app error', function(done){ it('should not cause an app error', function(done){
var app = koa(); var app = koa();
app.use(function *(next){ app.use(function *(next){
var res = this.res; var res = this.res;
this.status = 200;
res.setHeader("Content-Type", "text/html") res.setHeader("Content-Type", "text/html")
res.status = 200;
res.write('Hello'); res.write('Hello');
setTimeout(function(){ setTimeout(function(){
res.end("Goodbye") res.end("Goodbye")
@ -321,8 +321,8 @@ describe('app.respond', function(){
app.use(function *(next){ app.use(function *(next){
var res = this.res; var res = this.res;
this.status = 200;
res.setHeader("Content-Type", "text/html") res.setHeader("Content-Type", "text/html")
res.status = 200;
res.write('Hello'); res.write('Hello');
setTimeout(function(){ setTimeout(function(){
res.end("Goodbye"); res.end("Goodbye");