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

master
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); if (!Array.isArray(langs)) langs = [].slice.call(arguments);
var n = new Negotiator(this.req); var n = new Negotiator(this.req);
if (!langs.length) return n.preferredLanguages(); 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,10 +20,29 @@ describe('ctx.acceptsLanguages(langs)', function(){
}) })
describe('with multiple arguments', function(){ describe('with multiple arguments', function(){
it('should return the best fit', function(){ describe('when Accept-Language is populated', function(){
var ctx = context(); describe('if any types types match', function(){
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; it('should return the best fit', function(){
ctx.acceptsLanguages('es', 'en').should.equal('es'); var ctx = context();
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt';
ctx.acceptsLanguages('es', 'en').should.equal('es');
})
})
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');
})
}) })
}) })