Added req.acceptedLanguages

This commit is contained in:
TJ Holowaychuk
2011-11-19 22:12:09 -08:00
parent a8fd8cb645
commit e8c373694c
3 changed files with 62 additions and 1 deletions

View File

@@ -173,6 +173,30 @@ req.__defineGetter__('accepted', function(){
: [];
});
/**
* Return an array of Accepted languages
* ordered from highest quality to lowest.
*
* Examples:
*
* Accept-Language: en;q=.5, en-us
* ['en-us', 'en']
*
* @return {Array}
* @api public
*/
req.__defineGetter__('acceptedLanguages', function(){
var accept = this.header('Accept-Language');
return accept
? utils
.parseQuality(accept)
.map(function(obj){
return obj.value;
})
: [];
});
/**
* Return the value of param `name` when present or `defaultValue`.
*

View File

@@ -4,7 +4,7 @@ var express = require('../')
describe('req', function(){
describe('.accepted', function(){
it('should return an array of accepted content-types', function(done){
it('should return an array of accepted media types', function(done){
var app = express();
app.use(function(req, res){

View File

@@ -0,0 +1,37 @@
var express = require('../')
, request = require('./support/http');
describe('req', function(){
describe('.acceptedLanguages', function(){
it('should return an array of accepted languages', function(done){
var app = express();
app.use(function(req, res){
req.acceptedLanguages[0].should.equal('en-us');
req.acceptedLanguages[1].should.equal('en');
res.end();
});
request(app)
.get('/')
.set('Accept-Language', 'en;q=.5, en-us')
.expect(200, done);
})
describe('when Accept-Language is not present', function(){
it('should default to []', function(done){
var app = express();
app.use(function(req, res){
req.acceptedLanguages.should.have.length(0);
res.end();
});
request(app)
.get('/')
.expect(200, done);
})
})
})
})