Merge pull request #261 from dead-horse/simplify-respond

simplify respond
This commit is contained in:
TJ Holowaychuk 2014-04-15 08:33:25 -07:00
commit 204e4f5717
2 changed files with 44 additions and 18 deletions

View file

@ -5,6 +5,7 @@
var debug = require('debug')('koa:application'); var debug = require('debug')('koa:application');
var onFinished = require('finished'); var onFinished = require('finished');
var Emitter = require('events').EventEmitter; var Emitter = require('events').EventEmitter;
var Stream = require('stream');
var compose = require('koa-compose'); var compose = require('koa-compose');
var context = require('./context'); var context = require('./context');
var request = require('./request'); var request = require('./request');
@ -174,7 +175,6 @@ function *respond(next) {
var body = this.body; var body = this.body;
var code = this.status = this.status || 404; var code = this.status = this.status || 404;
var head = 'HEAD' == this.method;
// ignore body // ignore body
if (status.empty[code]) { if (status.empty[code]) {
@ -183,10 +183,12 @@ function *respond(next) {
return res.end(); return res.end();
} }
if (null == body) { if ('HEAD' == this.method) {
// empty body if (isJSON(body)) this.length = Buffer.byteLength(JSON.stringify(body));
if (head) return res.end(); return res.end();
}
if (null == body) {
// status body // status body
this.type = 'text'; this.type = 'text';
body = status[code]; body = status[code];
@ -195,27 +197,29 @@ function *respond(next) {
} }
// Buffer body // Buffer body
if (Buffer.isBuffer(body)) { if (Buffer.isBuffer(body)) return res.end(body);
if (head) return res.end();
return res.end(body);
}
// string body // string body
if ('string' == typeof body) { if ('string' == typeof body) return res.end(body);
if (head) return res.end();
return res.end(body);
}
// Stream body // Stream body
if ('function' == typeof body.pipe) { if (body instanceof Stream) return body.pipe(res);
if (head) return res.end();
return body.pipe(res);
}
// body: json // body: json
body = JSON.stringify(body); body = JSON.stringify(body);
this.length = Buffer.byteLength(body); this.length = Buffer.byteLength(body);
if (head) return res.end();
res.end(body); res.end(body);
} }
/**
* Check if `body` should be interpreted as json.
*
*/
function isJSON(body) {
if (!body) return false;
if ('string' == typeof body) return false;
if (body instanceof Stream) return false;
if (Buffer.isBuffer(body)) return false;
return true;
}

View file

@ -156,12 +156,34 @@ describe('app.respond', function(){
.expect(200) .expect(200)
.end(function(err, res){ .end(function(err, res){
if (err) return done(err); if (err) return done(err);
res.should.have.header('Content-Type', 'text/plain; charset=utf-8');
res.should.have.header('Content-Length', '5'); res.should.have.header('Content-Length', '5');
assert(0 == res.text.length); assert(0 == res.text.length);
done(); done();
}); });
}) })
it('should keep the headers', function(done){
var app = koa();
app.use(function *(){
this.body = { hello: 'world' };
});
var server = app.listen();
request(server)
.head('/')
.expect(200)
.end(function(err, res){
if (err) return done(err);
res.should.have.header('Content-Type', 'application/json');
res.should.have.header('Content-Length', '17');
assert(0 == res.text.length);
done();
});
})
it('should respond with a 404 if no body was set', function(done){ it('should respond with a 404 if no body was set', function(done){
var app = koa(); var app = koa();