Skip to content

Commit

Permalink
less strict for status, close #350
Browse files Browse the repository at this point in the history
add res.message[=]
  • Loading branch information
dead-horse committed Oct 9, 2014
1 parent 8774979 commit efdd7d3
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docs/api/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ koa uses [http-assert](https://github.com/jshttp/http-assert) for assertions.
- `ctx.body=`
- `ctx.status`
- `ctx.status=`
- `ctx.message`
- `ctx.message=`
- `ctx.length=`
- `ctx.length`
- `ctx.type=`
Expand Down
11 changes: 10 additions & 1 deletion docs/api/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ __NOTE__: don't worry too much about memorizing these strings,
if you have a typo an error will be thrown, displaying this list
so you can make a correction.

### response.message

Get response status message. By default, `response.message` is
associated with `response.status`.

### response.message=

Set response status message to the given value.

### response.length=

Set response Content-Length to the given value.
Expand All @@ -106,7 +115,7 @@ so you can make a correction.
- `Object` json-stringified
- `null` no content response

If `response.status` has not been set, Koa will automatically set the status to `200` or `204`.
If `response.status` has not been set, Koa will automatically set the status to `200` or `204`.

#### String

Expand Down
10 changes: 5 additions & 5 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

var debug = require('debug')('koa:application');
var Emitter = require('events').EventEmitter;
var onFinished = require('on-finished');
var response = require('./response');
var compose = require('koa-compose');
var isJSON = require('koa-is-json');
var response = require('./response');
var context = require('./context');
var request = require('./request');
var onFinished = require('on-finished');
var statuses = require('statuses');
var Cookies = require('cookies');
var accepts = require('accepts');
var status = require('statuses');
var assert = require('assert');
var Stream = require('stream');
var http = require('http');
Expand Down Expand Up @@ -187,7 +187,7 @@ function *respond(next) {
var code = this.status;

// ignore body
if (status.empty[code]) {
if (statuses.empty[code]) {
// strip headers
this.body = null;
return res.end();
Expand All @@ -201,7 +201,7 @@ function *respond(next) {
// status body
if (null == body) {
this.type = 'text';
body = status[code];
body = this.message || String(code);
if (body) this.length = Buffer.byteLength(body);
return res.end(body);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
var createError = require('http-errors');
var httpAssert = require('http-assert');
var delegate = require('delegates');
var statuses = require('statuses');
var assert = require('assert');
var http = require('http');

/**
* Context prototype.
Expand Down Expand Up @@ -126,10 +126,10 @@ var proto = module.exports = {
if ('ENOENT' == err.code) err.status = 404;

// default to 500
if ('number' != typeof err.status || !http.STATUS_CODES[err.status]) err.status = 500;
if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;

// respond
var code = http.STATUS_CODES[err.status];
var code = statuses[err.status];
var msg = err.expose ? err.message : code;
this.status = err.status;
this.length = Buffer.byteLength(msg);
Expand All @@ -148,6 +148,7 @@ delegate(proto, 'response')
.method('vary')
.method('set')
.access('status')
.access('message')
.access('body')
.access('length')
.access('type')
Expand Down
37 changes: 30 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

var ensureErrorHandler = require('error-inject');
var getType = require('mime-types').contentType;
var onFinish = require('on-finished');
var isJSON = require('koa-is-json');
var escape = require('escape-html');
var onFinish = require('on-finished');
var typeis = require('type-is').is;
var status = require('statuses');
var statuses = require('statuses');
var destroy = require('destroy');
var assert = require('assert');
var http = require('http');
Expand Down Expand Up @@ -69,10 +69,33 @@ module.exports = {

set status(code) {
assert('number' == typeof code, 'status code must be a number');
assert(http.STATUS_CODES[code], 'invalid status code: ' + code);
assert(statuses[code], 'invalid status code: ' + code);
this._explicitStatus = true;
this.res.statusCode = code;
if (this.body && status.empty[code]) this.body = null;
this.res.statusMessage = statuses[code];
if (this.body && statuses.empty[code]) this.body = null;
},

/**
* Get response status message
*
* @return {String}
* @api public
*/

get message() {
return this.res.statusMessage || statuses[this.status];
},

/**
* Set response status message
*
* @param {String} msg
* @api public
*/

set message(msg) {
this.res.statusMessage = msg;
},

/**
Expand All @@ -99,7 +122,7 @@ module.exports = {

// no content
if (null == val) {
if (!status.empty[this.status]) this.status = 204;
if (!statuses.empty[this.status]) this.status = 204;
this.res.removeHeader('Content-Type');
this.res.removeHeader('Content-Length');
this.res.removeHeader('Transfer-Encoding');
Expand Down Expand Up @@ -223,7 +246,7 @@ module.exports = {
this.set('Location', url);

// status
if (!status.redirect[this.status]) this.status = 302;
if (!statuses.redirect[this.status]) this.status = 302;

// html
if (this.ctx.accepts('html')) {
Expand Down Expand Up @@ -451,7 +474,7 @@ module.exports = {
toJSON: function(){
return {
status: this.status,
string: http.STATUS_CODES[this.status],
message: this.message,
header: this.header
}
}
Expand Down
76 changes: 70 additions & 6 deletions test/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

var request = require('supertest');
var statuses = require('statuses');
var assert = require('assert');
var http = require('http');
var koa = require('..');
Expand Down Expand Up @@ -487,6 +488,69 @@ describe('app.respond', function(){
})
})
})

describe('with custom status=700', function(){
it('should respond with the associated status message', function (done){
var app = koa();
statuses['700'] = 'custom status';

app.use(function *(){
this.status = 700;
})

var server = app.listen();

request(server)
.get('/')
.expect(700)
.expect('custom status')
.end(function(err, res){
if (err) return done(err);
res.res.statusMessage.should.equal('custom status');
done();
})
})
})

describe('with custom statusMessage=ok', function(){
it('should respond with the custom status message', function (done){
var app = koa();

app.use(function *(){
this.status = 200;
this.message = 'ok';
})

var server = app.listen();

request(server)
.get('/')
.expect(200)
.expect('ok')
.end(function(err, res){
if (err) return done(err);
res.res.statusMessage.should.equal('ok');
done();
})
})
})

describe('with custom status without message', function (){
it('should respond with the status code number', function (done){
var app = koa();

app.use(function *(){
this.res.statusCode = 701;
})

var server = app.listen();

request(server)
.get('/')
.expect(701)
.expect('701', done);
})
})
})

describe('when .body is a null', function(){
Expand Down Expand Up @@ -909,12 +973,12 @@ describe('app.respond', function(){

describe('app.context', function(){
var app1 = koa();
app1.context.message = 'hello';
app1.context.msg = 'hello';
var app2 = koa();

it('should merge properties', function(done){
app1.use(function *(next){
assert.equal(this.message, 'hello')
assert.equal(this.msg, 'hello')
this.status = 204
});

Expand All @@ -925,7 +989,7 @@ describe('app.context', function(){

it('should not affect the original prototype', function(done){
app2.use(function *(next){
assert.equal(this.message, undefined)
assert.equal(this.msg, undefined)
this.status = 204;
});

Expand Down Expand Up @@ -965,12 +1029,12 @@ describe('app.request', function(){

describe('app.response', function(){
var app1 = koa();
app1.response.message = 'hello';
app1.response.msg = 'hello';
var app2 = koa();

it('should merge properties', function(done){
app1.use(function *(next){
assert.equal(this.response.message, 'hello')
assert.equal(this.response.msg, 'hello')
this.status = 204
});

Expand All @@ -981,7 +1045,7 @@ describe('app.response', function(){

it('should not affect the original prototype', function(done){
app2.use(function *(next){
assert.equal(this.response.message, undefined)
assert.equal(this.response.msg, undefined)
this.status = 204;
});

Expand Down
4 changes: 2 additions & 2 deletions test/context/toJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ describe('ctx.toJSON()', function(){

res.should.eql({
status: 200,
string: 'OK',
message: 'OK',
header: {
'content-type': 'text/html; charset=utf-8',
'content-length': '10'
}
});
})
})
})
2 changes: 1 addition & 1 deletion test/response/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('res.inspect()', function(){
res.inspect().should.eql({
body: 'hello',
status: 200,
string: 'OK',
message: 'OK',
header: {
'content-length': '5',
'content-type': 'text/plain; charset=utf-8'
Expand Down
29 changes: 29 additions & 0 deletions test/response/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

var Stream = require('stream');
var response = require('../context').response;

describe('res.message', function(){
it('should return the response status message', function(){
var res = response();
res.status = 200;
res.message.should.equal('OK');
})

describe('when res.message not present', function(){
it('should look up in statuses', function(){
var res = response();
res.res.statusCode = 200;
res.message.should.equal('OK');
})
})
})

describe('res.message=', function(){
it('should set response status message', function(){
var res = response();
res.status = 200;
res.message = 'ok';
res.res.statusMessage.should.equal('ok');
res.inspect().message.should.equal('ok');
})
})
Loading

0 comments on commit efdd7d3

Please sign in to comment.