Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added req.acceptsCharset()

  • Loading branch information...
commit 5e071a2e4b5a37dfaf4eb4580c160259176c32b7 1 parent ff9b82c
@tj tj authored
Showing with 67 additions and 1 deletion.
  1. +18 −1 lib/request.js
  2. +49 −0 test/req.acceptsCharset.js
View
19 lib/request.js
@@ -98,7 +98,8 @@ req.get = function(field, param){
};
/**
- * Check if the given `type` is acceptable.
+ * Check if the given `type` is acceptable,
+ * otherwise you should respond with 406 "Not Acceptable".
*
* Examples:
*
@@ -127,6 +128,22 @@ req.accepts = function(type){
};
/**
+ * Check if the given `charset` is acceptable,
+ * otherwise you should respond with 406 "Not Acceptable".
+ *
+ * @param {String} charset
+ * @return {Boolean}
+ * @api public
+ */
+
+req.acceptsCharset = function(charset){
+ var accepted = this.acceptedCharsets;
+ return accepted.length
+ ? ~accepted.indexOf(charset)
+ : true;
+};
+
+/**
* Return an array of Accepted media types
* ordered from highest quality to lowest.
*
View
49 test/req.acceptsCharset.js
@@ -0,0 +1,49 @@
+
+var express = require('../')
+ , request = require('./support/http');
+
+describe('req', function(){
+ describe('.acceptsCharset(type)', function(){
+ describe('when Accept-Charset is not present', function(){
+ it('should return true', function(done){
+ var app = express();
+
+ app.use(function(req, res, next){
+ res.end(req.acceptsCharset('utf-8') ? 'yes' : 'no');
+ });
+
+ request(app)
+ .get('/')
+ .expect('yes', done);
+ })
+ })
+
+ describe('when Accept-Charset is not present', function(){
+ it('should return true when present', function(done){
+ var app = express();
+
+ app.use(function(req, res, next){
+ res.end(req.acceptsCharset('utf-8') ? 'yes' : 'no');
+ });
+
+ request(app)
+ .get('/')
+ .set('Accept-Charset', 'foo, bar, utf-8')
+ .expect('yes', done);
+ })
+
+ it('should return false otherwise', function(done){
+ var app = express();
+
+ app.use(function(req, res, next){
+ res.end(req.acceptsCharset('utf-8') ? 'yes' : 'no');
+ });
+
+ request(app)
+ .get('/')
+ .set('Accept-Charset', 'foo, bar')
+ .expect('no', done);
+ })
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.