From c2322f2b3dba5cd477c9fca0ae8879750abb2c68 Mon Sep 17 00:00:00 2001 From: tmilewski Date: Sat, 7 Jun 2014 19:26:19 -0400 Subject: [PATCH] remove .status=string #298 --- lib/response.js | 8 ++++++-- test/response/status.js | 33 +++++++++++++++++---------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/response.js b/lib/response.js index 0e0524c..9524961 100644 --- a/lib/response.js +++ b/lib/response.js @@ -13,6 +13,7 @@ var destroy = require('dethroy'); var http = require('http'); var path = require('path'); var vary = require('vary'); +var assert = require('assert'); var basename = path.basename; var extname = path.extname; @@ -71,14 +72,17 @@ module.exports = { /** * Set response status code. * - * @param {Number|String} code + * @param {Number} code * @api public */ set status(code) { - if ('number' != typeof code) code = status(code); + assert(typeof code === 'number', 'status code must be a number'); + if(!http.STATUS_CODES[code]) throw new Error('invalid status code: ' + code); + this._explicitStatus = true; this.res.statusCode = code; + if (this.body && status.empty[code]) this.body = null; }, diff --git a/test/response/status.js b/test/response/status.js index f7f4bf8..b76da75 100644 --- a/test/response/status.js +++ b/test/response/status.js @@ -5,37 +5,38 @@ var assert = require('assert'); var koa = require('../..'); describe('res.status=', function(){ - describe('when a status string', function(){ + describe('when a status code', function(){ describe('and valid', function(){ it('should set the status', function(){ var res = response(); - res.status = 'forbidden'; + res.status = 403; res.status.should.equal(403); }) - it('should be case-insensitive', function(){ - var res = response(); - res.status = 'ForBidden'; - res.status.should.equal(403); + it('should not throw', function(){ + assert.doesNotThrow(function() { + response().status = 403; + }); }) }) describe('and invalid', function(){ it('should throw', function(){ - var res = response(); - var err; - - try { - res.status = 'maru'; - } catch (e) { - err = e; - } - - assert(err); + assert.throws(function() { + response().status = 999; + }, 'invalid status code: 999'); }) }) }) + describe('when a status string', function(){ + it('should throw', function(){ + assert.throws(function() { + response().status = 'forbidden'; + }, 'status code must be a number'); + }) + }) + function strip(status) { it('should strip content related header fields', function(done){ var app = koa();