diff --git a/lib/request.js b/lib/request.js index 69990da39b6..9f517721c3e 100644 --- a/lib/request.js +++ b/lib/request.js @@ -147,17 +147,34 @@ req.acceptsEncodings = function(){ }; /** - * Check if the given `charset`s are acceptable, - * otherwise you should respond with 406 "Not Acceptable". + * Checks if the specified `charset`s are acceptable based on the request's `Accept-Charset` header. + * Returns the best matching charset or an array of acceptable charsets. * - * @param {String} ...charset - * @return {String|Array} + * The `charset` argument(s) can be: + * - A single charset string (e.g., "utf-8") + * - Multiple charset strings as arguments (e.g., `"utf-8", "iso-8859-1"`) + * - A comma-delimited list of charsets (e.g., `"utf-8, iso-8859-1"`) + * + * Examples: + * + * // Accept-Charset: utf-8, iso-8859-1 + * req.acceptsCharsets('utf-8'); + * // => "utf-8" + * + * req.acceptsCharsets('utf-8', 'iso-8859-1'); + * // => "utf-8" + * + * req.acceptsCharsets('utf-8, utf-16'); + * // => "utf-8" + * + * @param {...String} charsets - The charset(s) to check against the `Accept-Charset` header. + * @return {String|Array} - The best matching charset, or an array of acceptable charsets. * @public */ -req.acceptsCharsets = function(){ - var accept = accepts(this); - return accept.charsets.apply(accept, arguments); +req.acceptsCharsets = function(...charsets) { + const accept = accepts(this); + return accept.charsets(...charsets); }; /** diff --git a/test/req.acceptsCharsets.js b/test/req.acceptsCharsets.js index 360a9878a78..2df68ae1097 100644 --- a/test/req.acceptsCharsets.js +++ b/test/req.acceptsCharsets.js @@ -45,6 +45,19 @@ describe('req', function(){ .set('Accept-Charset', 'foo, bar') .expect('no', done); }) + + it('should return the best matching charset from multiple inputs', function (done) { + var app = express(); + + app.use(function(req, res, next){ + res.end(req.acceptsCharsets('utf-8', 'iso-8859-1')); + }); + + request(app) + .get('/') + .set('Accept-Charset', 'iso-8859-1, utf-8') + .expect('iso-8859-1', done); + }) }) }) })