refactor: use statuses

This commit is contained in:
Jonathan Ong 2014-03-12 18:29:14 -07:00
parent 9b1651a8e6
commit 182f9d6fa4
5 changed files with 16 additions and 56 deletions

View file

@ -11,6 +11,7 @@ var request = require('./request');
var response = require('./response');
var Cookies = require('cookies');
var accepts = require('accepts');
var status = require('statuses');
var assert = require('assert');
var http = require('http');
var only = require('only');
@ -172,17 +173,16 @@ function *respond(next){
if (res.headersSent || !this.writable) return;
var body = this.body;
var status = this.status = this.status || 404;
var code = this.status = this.status || 404;
var head = 'HEAD' == this.method;
var noContent = ~[204, 205, 304].indexOf(status);
// ignore body
if (noContent) return res.end();
if (status.empty[code]) return res.end();
// status body
if (null == body) {
this.type = 'text';
body = http.STATUS_CODES[status];
body = status[code];
}
// Buffer body

View file

@ -4,7 +4,7 @@
*/
var debug = require('debug')('koa:response');
var statuses = require('./status');
var status = require('statuses');
var http = require('http');
var path = require('path');
var mime = require('mime');
@ -66,19 +66,14 @@ module.exports = {
/**
* Set response status code.
*
* @param {Number|String} val
* @param {Number|String} code
* @api public
*/
set status(val) {
if ('string' == typeof val) {
var n = statuses[val.toLowerCase()];
if (!n) throw new Error(statusError(val));
val = n;
}
this._status = this.res.statusCode = val;
if (this.body && ~[204, 205, 304].indexOf(val)) this.body = null;
set status(code) {
if ('number' != typeof code) code = status(code);
this._status = this.res.statusCode = code;
if (this.body && status.empty[code]) this.body = null;
},
/**
@ -217,7 +212,7 @@ module.exports = {
redirect: function(url, alt){
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
this.set('Location', url);
if (!~[300, 301, 302, 303, 305, 307].indexOf(this.status)) this.status = 302;
if (!status.redirect[this.status]) this.status = 302;
// html
if (this.ctx.accepts('html')) {
@ -454,25 +449,6 @@ module.exports = {
}
};
/**
* Return status error message.
*
* @param {String} val
* @return {String}
* @api private
*/
function statusError(val) {
var s = 'invalid status string "' + val + '", try:\n\n';
Object.keys(statuses).forEach(function(name){
var n = statuses[name];
s += ' - ' + n + ' "' + name + '"\n';
});
return s;
}
/**
* Escape special characters in the given string of html.
*

View file

@ -1,17 +0,0 @@
/**
* Module dependencies.
*/
var http = require('http');
var codes = http.STATUS_CODES;
/**
* Produce exports[STATUS] = CODE map.
*/
Object.keys(codes).forEach(function(code){
var n = ~~code;
var s = codes[n].toLowerCase();
exports[s] = n;
});

View file

@ -21,6 +21,7 @@
],
"license": "MIT",
"dependencies": {
"statuses": "~1.0.1",
"accepts": "~1.0.0",
"type-is": "~1.0.0",
"finished": "~1.1.1",

View file

@ -9,14 +9,14 @@ describe('ctx.onerror(err)', function(){
app.use(function *(next){
this.body = 'something else';
this.throw(499, 'boom');
this.throw(418, 'boom');
})
var server = app.listen();
request(server)
.get('/')
.expect(499)
.expect(418)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Length', '4')
.end(done);
@ -30,14 +30,14 @@ describe('ctx.onerror(err)', function(){
this.set('X-CSRF-Token', 'asdf');
this.body = 'response';
this.throw(499, 'boom');
this.throw(418, 'boom');
})
var server = app.listen();
request(server)
.get('/')
.expect(499)
.expect(418)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Content-Length', '4')
.end(function(err, res){