fix: enhance req.acceptsCharsets method (#6088)

* fix: enhance req.acceptsCharsets method

* Update req.acceptsCharsets.js

---------

Co-authored-by: Monaam Aouini <abdelmonaem.aouini@mispay.co>
Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>
This commit is contained in:
AbdelMonaam Aouini
2026-01-07 15:41:34 +01:00
committed by GitHub
parent 3e81873b52
commit 6cd404eb28
2 changed files with 37 additions and 7 deletions

View File

@@ -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);
};
/**

View File

@@ -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);
})
})
})
})