app.respond: support 205 status codes as no-content

pretty new to me, but it’s basically the same as 204
This commit is contained in:
Jonathan Ong 2013-11-19 22:20:17 -08:00
parent 16b016f61f
commit 1dd1d02db0
2 changed files with 102 additions and 14 deletions

View file

@ -102,7 +102,7 @@ app.callback = function(){
/** /**
* Set signed cookie keys. * Set signed cookie keys.
* *
* These are passed to [KeyGrip](https://github.com/jed/keygrip), * These are passed to [KeyGrip](https://github.com/jed/keygrip),
* however you may also pass your own `KeyGrip` instance. For * however you may also pass your own `KeyGrip` instance. For
* example the following are acceptable: * example the following are acceptable:
@ -180,7 +180,7 @@ function *respond(next){
var res = this.res; var res = this.res;
var body = this.body; var body = this.body;
var head = 'HEAD' == this.method; var head = 'HEAD' == this.method;
var noContent = 204 == this.status || 304 == this.status; var noContent = ~[204, 205, 304].indexOf(this.status);
// 404 // 404
if (null == body && 200 == this.status) { if (null == body && 200 == this.status) {
@ -211,12 +211,12 @@ function *respond(next){
// Stream body // Stream body
if (body instanceof Stream) { if (body instanceof Stream) {
if (!~body.listeners('error').indexOf(this.onerror)) body.on('error', this.onerror); if (!~body.listeners('error').indexOf(this.onerror)) body.on('error', this.onerror);
if (head) { if (head) {
if (body.close) body.close(); if (body.close) body.close();
return res.end(); return res.end();
} }
return body.pipe(res); return body.pipe(res);
} }

View file

@ -104,19 +104,107 @@ describe('app.respond', function(){
}) })
describe('when .body is missing', function(){ describe('when .body is missing', function(){
it('should respond with the associated status message', function(done){ describe('with status=400', function(){
var app = koa(); it('should respond with the associated status message', function(done){
var app = koa();
app.use(function *(){ app.use(function *(){
this.status = 400; this.status = 400;
}); });
var server = app.listen(); var server = app.listen();
request(server) request(server)
.get('/') .get('/')
.expect(400) .expect(400)
.expect('Bad Request', done); .expect('Bad Request', done);
})
})
describe('with status=200', function(){
it('should respond with a 404', function(done){
var app = koa();
app.use(function *(){
this.status = 200;
})
var server = app.listen();
request(server)
.get('/')
.expect(404)
.expect('Not Found', done);
})
})
describe('with status=204', function(){
it('should respond without a body', function(done){
var app = koa();
app.use(function *(){
this.status = 204;
})
var server = app.listen();
request(server)
.get('/')
.expect(204)
.expect('')
.end(function (err, res) {
if (err) return done(err);
res.header.should.not.have.property('content-type');
done();
})
})
})
describe('with status=205', function(){
it('should respond without a body', function(done){
var app = koa();
app.use(function *(){
this.status = 205;
})
var server = app.listen();
request(server)
.get('/')
.expect(205)
.expect('')
.end(function (err, res) {
if (err) return done(err);
res.header.should.not.have.property('content-type');
done();
})
})
})
describe('with status=304', function(){
it('should respond without a body', function(done){
var app = koa();
app.use(function *(){
this.status = 304;
})
var server = app.listen();
request(server)
.get('/')
.expect(304)
.expect('')
.end(function (err, res) {
if (err) return done(err);
res.header.should.not.have.property('content-type');
done();
})
})
}) })
}) })