Merge pull request #299 from tmilewski/remove-status-as-string

remove .status=string
This commit is contained in:
TJ Holowaychuk 2014-06-10 14:32:13 -07:00
commit ed8beb7d79
2 changed files with 23 additions and 18 deletions

View file

@ -13,6 +13,7 @@ var destroy = require('dethroy');
var http = require('http'); var http = require('http');
var path = require('path'); var path = require('path');
var vary = require('vary'); var vary = require('vary');
var assert = require('assert');
var basename = path.basename; var basename = path.basename;
var extname = path.extname; var extname = path.extname;
@ -60,14 +61,17 @@ module.exports = {
/** /**
* Set response status code. * Set response status code.
* *
* @param {Number|String} code * @param {Number} code
* @api public * @api public
*/ */
set status(code) { 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._explicitStatus = true;
this.res.statusCode = code; this.res.statusCode = code;
if (this.body && status.empty[code]) this.body = null; if (this.body && status.empty[code]) this.body = null;
}, },

View file

@ -5,37 +5,38 @@ var assert = require('assert');
var koa = require('../..'); var koa = require('../..');
describe('res.status=', function(){ describe('res.status=', function(){
describe('when a status string', function(){ describe('when a status code', function(){
describe('and valid', function(){ describe('and valid', function(){
it('should set the status', function(){ it('should set the status', function(){
var res = response(); var res = response();
res.status = 'forbidden'; res.status = 403;
res.status.should.equal(403); res.status.should.equal(403);
}) })
it('should be case-insensitive', function(){ it('should not throw', function(){
var res = response(); assert.doesNotThrow(function() {
res.status = 'ForBidden'; response().status = 403;
res.status.should.equal(403); });
}) })
}) })
describe('and invalid', function(){ describe('and invalid', function(){
it('should throw', function(){ it('should throw', function(){
var res = response(); assert.throws(function() {
var err; response().status = 999;
}, 'invalid status code: 999');
try {
res.status = 'maru';
} catch (e) {
err = e;
}
assert(err);
}) })
}) })
}) })
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) { function strip(status) {
it('should strip content related header fields', function(done){ it('should strip content related header fields', function(done){
var app = koa(); var app = koa();