From e447e731b684d74c1c56e7e4c80c1dad5b263d4b Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Tue, 26 Nov 2013 14:52:24 -0800 Subject: [PATCH] req.acceptsLanguage - default to first type fi header not set --- lib/request.js | 3 ++- test/request/acceptsLanguages.js | 27 +++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/request.js b/lib/request.js index 8797fbe..2e7e8e1 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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; }, /** diff --git a/test/request/acceptsLanguages.js b/test/request/acceptsLanguages.js index 92f88ed..d7ebb47 100644 --- a/test/request/acceptsLanguages.js +++ b/test/request/acceptsLanguages.js @@ -20,10 +20,29 @@ describe('ctx.acceptsLanguages(langs)', function(){ }) describe('with multiple arguments', function(){ - it('should return the best fit', function(){ - var ctx = context(); - ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'; - ctx.acceptsLanguages('es', 'en').should.equal('es'); + 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'; + 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'); + }) }) })