req.acceptsCharsets - default to first type if header not set

This commit is contained in:
Jonathan Ong 2013-11-26 14:49:07 -08:00
parent 8e10f12d38
commit c24ab00b23
2 changed files with 25 additions and 5 deletions

View file

@ -449,7 +449,8 @@ module.exports = {
if (!Array.isArray(charsets)) charsets = [].slice.call(arguments); if (!Array.isArray(charsets)) charsets = [].slice.call(arguments);
var n = new Negotiator(this.req); var n = new Negotiator(this.req);
if (!charsets.length) return n.preferredCharsets(); if (!charsets.length) return n.preferredCharsets();
return n.preferredCharsets(charsets)[0]; if (!this.header['accept-charset']) return charsets[0];
return n.preferredCharsets(charsets)[0] || false;
}, },
/** /**

View file

@ -20,6 +20,8 @@ describe('ctx.acceptsCharsets()', function(){
}) })
describe('with multiple arguments', function(){ describe('with multiple arguments', function(){
describe('when Accept-Charset is populated', function(){
describe('if any types match', function(){
it('should return the best fit', function(){ it('should return the best fit', function(){
var ctx = context(); var ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'; ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
@ -27,6 +29,23 @@ describe('ctx.acceptsCharsets()', function(){
}) })
}) })
describe('if no types match', function(){
it('should return false', function(){
var ctx = context();
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5';
ctx.acceptsCharsets('utf-16').should.be.false;
})
})
})
describe('when Accept-Charset is not populated', function(){
it('should return the first type', function(){
var ctx = context();
ctx.acceptsCharsets('utf-7', 'utf-8').should.equal('utf-7');
})
})
})
describe('with an array', function(){ describe('with an array', function(){
it('should return the best fit', function(){ it('should return the best fit', function(){
var ctx = context(); var ctx = context();