From 15ab936001659623388e56410f344c7c8c1d63e6 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Mon, 28 Apr 2014 21:17:30 -0700 Subject: [PATCH] change .status default to 404. Closes #263 --- lib/application.js | 3 ++- lib/response.js | 9 +++++---- test/application.js | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/application.js b/lib/application.js index 19cc7bf..bab5f95 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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]) { diff --git a/lib/response.js b/lib/response.js index ef15f95..39cdaef 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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']; diff --git a/test/application.js b/test/application.js index d3f8e1d..b13a90d 100644 --- a/test/application.js +++ b/test/application.js @@ -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");