From 0c438ed435bffa7bed6b75b9aef31146df741b5b Mon Sep 17 00:00:00 2001 From: Santiago Sotomayor Date: Mon, 19 Oct 2015 01:15:50 -0300 Subject: [PATCH] unset content-type when the type is unknown closes #532 closes #536 --- lib/response.js | 7 ++++++- test/application/respond.js | 22 ++++++++++++++++++++++ test/response/type.js | 8 ++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/response.js b/lib/response.js index 5a8370e..3c59a65 100644 --- a/lib/response.js +++ b/lib/response.js @@ -301,7 +301,12 @@ module.exports = { */ set type(type) { - this.set('Content-Type', getType(type) || 'application/octet-stream'); + type = getType(type) || false; + if (type) { + this.set('Content-Type', type); + } else { + this.remove('Content-Type'); + } }, /** diff --git a/test/application/respond.js b/test/application/respond.js index 4ae26c0..4b153f4 100644 --- a/test/application/respond.js +++ b/test/application/respond.js @@ -34,6 +34,28 @@ describe('app.respond', function(){ }); }); + describe('when this.type === null', function(){ + it('should not send Content-Type header', function(done){ + var app = new Koa(); + + app.use(function *(){ + this.body = ''; + this.type = null; + }); + + var server = app.listen(); + + request(server) + .get('/') + .expect(200) + .end(function(err, res){ + if (err) return done(err); + res.should.not.have.header('content-type'); + done(); + }); + }); + }); + describe('when HEAD is used', function(){ it('should not respond with the body', function(done){ const app = new Koa(); diff --git a/test/response/type.js b/test/response/type.js index ea0b183..7416083 100644 --- a/test/response/type.js +++ b/test/response/type.js @@ -42,11 +42,11 @@ describe('ctx.type=', function(){ }); describe('with an unknown extension', function(){ - it('should default to application/octet-stream', function(){ - const ctx = context(); + it('should not set a content-type', function(){ + var ctx = context(); ctx.type = 'asdf'; - ctx.type.should.equal('application/octet-stream'); - ctx.response.header['content-type'].should.equal('application/octet-stream'); + assert(!ctx.type); + assert(!ctx.response.header['content-type']); }); }); });