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 653d7ed26d
commit 0c438ed435
3 changed files with 32 additions and 5 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

@ -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(){ describe('when HEAD is used', function(){
it('should not respond with the body', function(done){ it('should not respond with the body', function(done){
const app = new Koa(); const app = new Koa();

View file

@ -42,11 +42,11 @@ 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(){
const 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']);
}); });
}); });
}); });