Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance req.acceptsEncodings #6086

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,37 @@ req.accepts = function(){
};

/**
* Check if the given `encoding`s are accepted.
* Checks if the specified encodings are accepted based on the request's `Accept-Encoding` header.
* Returns the best matching encoding or an array of acceptable encodings.
*
* @param {String} ...encoding
* @return {String|Array}
* The `encoding` argument(s) can be:
* - A single encoding string (e.g., "gzip")
* - Multiple encoding strings as arguments (e.g., `"gzip", "deflate"`)
* - A comma-delimited list of encodings (e.g., `"gzip, deflate"`)
*
* Examples:
*
* // Accept-Encoding: gzip, deflate
* req.acceptsEncodings('gzip');
* // => "gzip"
*
* req.acceptsEncodings('gzip', 'deflate');
* // => "gzip"
*
* req.acceptsEncodings('br');
* // => false
*
* req.acceptsEncodings('gzip, br');
* // => "gzip"
*
* @param {...String} encodings - The encoding(s) to check against the `Accept-Encoding` header.
* @return {String|Array} - The best matching encoding, or an array of acceptable encodings.
* @public
*/

req.acceptsEncodings = function(){
var accept = accepts(this);
return accept.encodings.apply(accept, arguments);
req.acceptsEncodings = function(...encodings) {
const accept = accepts(this);
return accept.encodings(...encodings);
};

/**
Expand Down
34 changes: 32 additions & 2 deletions test/req.acceptsEncodings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('req', function(){

request(app)
.get('/')
.set('Accept-Encoding', ' gzip, deflate')
.set('Accept-Encoding', 'gzip, deflate')
.expect(200, { gzip: 'gzip', deflate: 'deflate' }, done)
})

Expand All @@ -32,8 +32,38 @@ describe('req', function(){

request(app)
.get('/')
.set('Accept-Encoding', ' gzip, deflate')
.set('Accept-Encoding', 'gzip, deflate')
.expect(200, { bogus: false }, done)
})

it('should return the best matching encoding from multiple options', function(done) {
var app = express();

app.get('/', function (req, res) {
res.send({
best: req.acceptsEncodings('br', 'gzip', 'deflate')
})
})

request(app)
.get('/')
.set('Accept-Encoding', 'gzip, deflate')
.expect(200, { best: 'gzip' }, done)
});

it('should handle multiple encodings correctly', function(done) {
var app = express();

app.get('/', function (req, res) {
res.send({
result: req.acceptsEncodings('gzip', 'deflate', 'br')
})
})

request(app)
.get('/')
.set('Accept-Encoding', 'gzip, deflate, br')
.expect(200, { result: 'gzip' }, done)
});
})
})
Loading