req.acceptsLanguage - default to first type fi header not set

This commit is contained in:
Jonathan Ong 2013-11-26 14:52:24 -08:00
parent c24ab00b23
commit e447e731b6
2 changed files with 25 additions and 5 deletions

View file

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

View file

@ -20,6 +20,8 @@ describe('ctx.acceptsLanguages(langs)', function(){
})
describe('with multiple arguments', function(){
describe('when Accept-Language is populated', function(){
describe('if any types types match', function(){
it('should return the best fit', function(){
var ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
@ -27,6 +29,23 @@ describe('ctx.acceptsLanguages(langs)', function(){
})
})
describe('if no types match', function(){
it('should return false', function(){
var ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages('fr', 'au').should.be.false;
})
})
})
describe('when Accept-Language is not populated', function(){
it('should return the first type', function(){
var ctx = context();
ctx.acceptsLanguages('es', 'en').should.equal('es');
})
})
})
describe('with an array', function(){
it('should return the best fit', function(){
var ctx = context();