Merge pull request #269 from koajs/status-default-404

change .status default to 404. Closes #263
This commit is contained in:
TJ Holowaychuk 2014-05-01 15:51:41 -07:00
commit c757034adb
3 changed files with 10 additions and 8 deletions

View file

@ -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]) {

View file

@ -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'];

View file

@ -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");