From 285e19dc3b6c53099f196396ffc7e769fb9555a5 Mon Sep 17 00:00:00 2001 From: Monaam Aouini Date: Sun, 27 Oct 2024 10:44:59 +0100 Subject: [PATCH 1/2] fix: enhance req.acceptsCharsets method --- lib/request.js | 31 ++++++++++++++++++++++++------- test/req.acceptsCharsets.js | 13 +++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/request.js b/lib/request.js index 372a9915e9..2fdabf10e6 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 360a9878a7..6e61d9f0a4 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') || 'none'); + }); + + request(app) + .get('/') + .set('Accept-Charset', 'iso-8859-1, utf-8') + .expect('iso-8859-1', done); + }) }) }) }) From 840203e5f9ce104e94473adee5ae5231c59df45a Mon Sep 17 00:00:00 2001 From: AbdelMonaam Aouini <52112750+Abdel-Monaam-Aouini@users.noreply.github.com> Date: Fri, 6 Dec 2024 20:15:21 +0100 Subject: [PATCH 2/2] Update req.acceptsCharsets.js --- test/req.acceptsCharsets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/req.acceptsCharsets.js b/test/req.acceptsCharsets.js index 6e61d9f0a4..2df68ae109 100644 --- a/test/req.acceptsCharsets.js +++ b/test/req.acceptsCharsets.js @@ -50,7 +50,7 @@ describe('req', function(){ var app = express(); app.use(function(req, res, next){ - res.end(req.acceptsCharsets('utf-8', 'iso-8859-1') || 'none'); + res.end(req.acceptsCharsets('utf-8', 'iso-8859-1')); }); request(app)