From 1dd1d02db08d2a41169237e0388a5c63621508b0 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Tue, 19 Nov 2013 22:20:17 -0800 Subject: [PATCH] app.respond: support 205 status codes as no-content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pretty new to me, but it’s basically the same as 204 --- lib/application.js | 8 ++-- test/application.js | 108 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 102 insertions(+), 14 deletions(-) diff --git a/lib/application.js b/lib/application.js index c238ae7..6e69cc9 100644 --- a/lib/application.js +++ b/lib/application.js @@ -102,7 +102,7 @@ app.callback = function(){ /** * Set signed cookie keys. - * + * * These are passed to [KeyGrip](https://github.com/jed/keygrip), * however you may also pass your own `KeyGrip` instance. For * example the following are acceptable: @@ -180,7 +180,7 @@ function *respond(next){ var res = this.res; var body = this.body; var head = 'HEAD' == this.method; - var noContent = 204 == this.status || 304 == this.status; + var noContent = ~[204, 205, 304].indexOf(this.status); // 404 if (null == body && 200 == this.status) { @@ -211,12 +211,12 @@ function *respond(next){ // Stream body if (body instanceof Stream) { if (!~body.listeners('error').indexOf(this.onerror)) body.on('error', this.onerror); - + if (head) { if (body.close) body.close(); return res.end(); } - + return body.pipe(res); } diff --git a/test/application.js b/test/application.js index bf93c25..1fbb76c 100644 --- a/test/application.js +++ b/test/application.js @@ -104,19 +104,107 @@ describe('app.respond', function(){ }) describe('when .body is missing', function(){ - it('should respond with the associated status message', function(done){ - var app = koa(); + describe('with status=400', function(){ + it('should respond with the associated status message', function(done){ + var app = koa(); - app.use(function *(){ - this.status = 400; - }); + app.use(function *(){ + this.status = 400; + }); - var server = app.listen(); + var server = app.listen(); - request(server) - .get('/') - .expect(400) - .expect('Bad Request', done); + request(server) + .get('/') + .expect(400) + .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(); + }) + }) }) })