Permalink
Browse files

Added req.acceptedCharsets

  • Loading branch information...
1 parent 2adf020 commit 499d3d6d78ac5924402665f559beb7bc50803720 @tj tj committed Nov 20, 2011
Showing with 61 additions and 0 deletions.
  1. +24 −0 lib/request.js
  2. +37 −0 test/req.acceptedCharsets.js
View
@@ -198,6 +198,30 @@ req.__defineGetter__('acceptedLanguages', function(){
});
/**
+ * Return an array of Accepted charsets
+ * ordered from highest quality to lowest.
+ *
+ * Examples:
+ *
+ * Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8
+ * ['unicode-1-1', 'iso-8859-5']
+ *
+ * @return {Array}
+ * @api public
+ */
+
+req.__defineGetter__('acceptedCharsets', function(){
+ var accept = this.header('Accept-Charset');
+ return accept
+ ? utils
+ .parseQuality(accept)
+ .map(function(obj){
+ return obj.value;
+ })
+ : [];
+});
+
+/**
* Return the value of param `name` when present or `defaultValue`.
*
* - Checks route placeholders, ex: _/user/:id_
@@ -0,0 +1,37 @@
+
+var express = require('../')
+ , request = require('./support/http');
+
+describe('req', function(){
+ describe('.acceptedCharsets', function(){
+ it('should return an array of accepted charsets', function(done){
+ var app = express();
+
+ app.use(function(req, res){
+ req.acceptedCharsets[0].should.equal('unicode-1-1');
+ req.acceptedCharsets[1].should.equal('iso-8859-5');
+ res.end();
+ });
+
+ request(app)
+ .get('/')
+ .set('Accept-Charset', 'iso-8859-5;q=.2, unicode-1-1;q=0.8')
+ .expect(200, done);
+ })
+
+ describe('when Accept-Charset is not present', function(){
+ it('should default to []', function(done){
+ var app = express();
+
+ app.use(function(req, res){
+ req.acceptedCharsets.should.have.length(0);
+ res.end();
+ });
+
+ request(app)
+ .get('/')
+ .expect(200, done);
+ })
+ })
+ })
+})

0 comments on commit 499d3d6

Please sign in to comment.