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 path = require('path');
var vary = require('vary');
var assert = require('assert');
var basename = path.basename;
var extname = path.extname;
@ -60,14 +61,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;
},

View file

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