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

master
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);
var n = new Negotiator(this.req);
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,10 +20,29 @@ describe('ctx.acceptsCharsets()', function(){
})
describe('with multiple arguments', function(){
it('should return the best fit', 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-7', 'utf-8').should.equal('utf-8');
describe('when Accept-Charset is populated', function(){
describe('if any types match', function(){
it('should return the best fit', 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-7', 'utf-8').should.equal('utf-8');
})
})
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');
})
})
})