req.acceptsEncodings: default to 'identity'

This commit is contained in:
Jonathan Ong 2013-11-20 17:46:11 -08:00
parent 9d7dd437d7
commit 5ae3f4eb30
3 changed files with 4 additions and 2 deletions

View file

@ -224,7 +224,7 @@ switch (this.accepts('json', 'html', 'text')) {
### req.acceptsEncodings(encodings) ### req.acceptsEncodings(encodings)
Check if `encodings` are acceptable, returning Check if `encodings` are acceptable, returning
the best match when true, otherwise `undefined`. the best match when true, otherwise `identity`.
```js ```js
// Accept-Encoding: gzip // Accept-Encoding: gzip

View file

@ -428,7 +428,7 @@ module.exports = {
if (!Array.isArray(encodings)) encodings = [].slice.call(arguments); if (!Array.isArray(encodings)) encodings = [].slice.call(arguments);
var n = new Negotiator(this.req); var n = new Negotiator(this.req);
if (!encodings.length) return n.preferredEncodings(); if (!encodings.length) return n.preferredEncodings();
return n.preferredEncodings(encodings)[0]; return n.preferredEncodings(encodings)[0] || 'identity';
}, },
/** /**

View file

@ -8,6 +8,7 @@ describe('ctx.acceptsEncodings()', function(){
var ctx = context(); var ctx = context();
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'; ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2';
ctx.acceptsEncodings().should.eql(['gzip', 'compress', 'identity']); ctx.acceptsEncodings().should.eql(['gzip', 'compress', 'identity']);
ctx.acceptsEncodings('gzip', 'compress').should.equal('gzip');
}) })
}) })
@ -15,6 +16,7 @@ describe('ctx.acceptsEncodings()', function(){
it('should return identity', function(){ it('should return identity', function(){
var ctx = context(); var ctx = context();
ctx.acceptsEncodings().should.eql(['identity']); ctx.acceptsEncodings().should.eql(['identity']);
ctx.acceptsEncodings('gzip', 'deflate').should.equal('identity');
}) })
}) })
}) })