Skip to content

Commit

Permalink
add req.acceptedEncodings
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 12, 2013
1 parent 776ee26 commit 3b1597d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ req.range = function(size){
return parseRange(size, range);
};

/**
* Return an array of encodings.
*
* Examples:
*
* ['gzip', 'deflate']
*
* @return {Array}
* @api public
*/

req.__defineGetter__('acceptedEncodings', function(){
var accept = this.get('Accept-Encoding');
return accept
? accept.trim().split(/ *, */)
: [];
});

/**
* Return an array of Accepted media types
* ordered from highest quality to lowest.
Expand Down
36 changes: 36 additions & 0 deletions test/req.acceptedEncodings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

var express = require('../')
, request = require('./support/http');

describe('req', function(){
describe('.acceptedEncodings', function(){
it('should return an array of accepted encodings', function(done){
var app = express();

app.use(function(req, res){
req.acceptedEncodings.should.eql(['gzip', 'deflate']);
res.end();
});

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

describe('when Accept-Encoding is not present', function(){
it('should default to []', function(done){
var app = express();

app.use(function(req, res){
req.acceptedEncodings.should.have.length(0);
res.end();
});

request(app)
.get('/')
.expect(200, done);
})
})
})
})

0 comments on commit 3b1597d

Please sign in to comment.