unset content-type when the type is unknown

closes #532
closes #536
This commit is contained in:
Santiago Sotomayor 2015-10-19 01:15:50 -03:00 committed by jongleberry
parent 406a2966ca
commit de20a5c96c
3 changed files with 34 additions and 7 deletions

View file

@ -301,7 +301,12 @@ module.exports = {
*/ */
set type(type) { 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');
}
}, },
/** /**

View file

@ -237,6 +237,28 @@ describe('app.respond', function(){
}) })
}) })
describe('when this.type === null', function(){
it('should not send Content-Type header', function(done){
var app = 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(){ describe('when HEAD is used', function(){
it('should not respond with the body', function(done){ it('should not respond with the body', function(done){
var app = koa(); var app = koa();

View file

@ -42,14 +42,14 @@ describe('ctx.type=', function(){
}) })
describe('with an unknown extension', function(){ describe('with an unknown extension', function(){
it('should default to application/octet-stream',function(){ it('should not set a content-type', function(){
var ctx = context(); var ctx = context();
ctx.type = 'asdf'; ctx.type = 'asdf';
ctx.type.should.equal('application/octet-stream'); assert(!ctx.type);
ctx.response.header['content-type'].should.equal('application/octet-stream'); assert(!ctx.response.header['content-type']);
}) });
}) });
}) });
describe('ctx.type', function(){ describe('ctx.type', function(){
describe('with no Content-Type', function(){ describe('with no Content-Type', function(){