Skip to content

Commit

Permalink
Merge pull request #1076 from chapel/fix-double-encoding
Browse files Browse the repository at this point in the history
Test for both formats of Content-Encoding header
  • Loading branch information
Eran Hammer committed Sep 30, 2013
2 parents d16eff8 + 82aac4e commit 257c61e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/response/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internals.Generic.prototype._transmit = function (request, callback) {
return send();
}

if (!self._headers['content-encoding']) {
if (!self._headers['Content-Encoding'] && !self._headers['content-encoding']) {
var negotiator = new Negotiator(request.raw.req);
var encoding = negotiator.preferredEncoding(['gzip', 'deflate', 'identity']);
if (['gzip', 'deflate'].indexOf(encoding) !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/response/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ internals.Stream.prototype._transmit = function (request, callback) {
var self = this;

var encoder = null;
if (!this._headers['content-encoding']) {
if (!this._headers['Content-Encoding'] && !this._headers['content-encoding']) {
var negotiator = new Negotiator(request.raw.req);
var encoding = negotiator.preferredEncoding(['gzip', 'deflate', 'identity']);
if (encoding === 'deflate' || encoding === 'gzip') {
Expand Down
122 changes: 115 additions & 7 deletions test/integration/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ describe('Payload', function () {

server.route(postHandler);

var getHandler = {
method: 'GET',
path: '/',
config: {
handler: function (req) {

req.reply('{"test":"true"}');
}
}
};

server.route(getHandler);

before(function (done) {

server.start(function () {
Expand Down Expand Up @@ -121,7 +134,7 @@ describe('Payload', function () {
});
});

it('returns a gzip response when accept-encoding: gzip is requested', function (done) {
it('returns a gzip response on a post request when accept-encoding: gzip is requested', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -135,7 +148,21 @@ describe('Payload', function () {
});
});

it('returns a gzip response when accept-encoding: * is requested', function (done) {
it('returns a gzip response on a get request when accept-encoding: gzip is requested', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: { 'accept-encoding': 'gzip' } }, function (err, res, body) {

Zlib.gzip(rawBody, function (err, zippedBody) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});

it('returns a gzip response on a post request when accept-encoding: * is requested', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -149,7 +176,21 @@ describe('Payload', function () {
});
});

it('returns a deflate response when accept-encoding: deflate is requested', function (done) {
it('returns a gzip response on a get request when accept-encoding: * is requested', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: { 'accept-encoding': '*' } }, function (err, res, body) {

Zlib.gzip(rawBody, function (err, zippedBody) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});

it('returns a deflate response on a post request when accept-encoding: deflate is requested', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -163,7 +204,21 @@ describe('Payload', function () {
});
});

it('returns a gzip response when accept-encoding: gzip,q=1; deflate,q=.5 is requested', function (done) {
it('returns a deflate response on a get request when accept-encoding: deflate is requested', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: { 'accept-encoding': 'deflate' } }, function (err, res, body) {

Zlib.deflate(rawBody, function (err, zippedBody) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});

it('returns a gzip response on a post request when accept-encoding: gzip,q=1; deflate,q=.5 is requested', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -177,7 +232,21 @@ describe('Payload', function () {
});
});

it('returns a deflate response when accept-encoding: deflate,q=1; gzip,q=.5 is requested', function (done) {
it('returns a gzip response on a get request when accept-encoding: gzip,q=1; deflate,q=.5 is requested', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: { 'accept-encoding': 'gzip,q=1; deflate,q=.5' } }, function (err, res, body) {

Zlib.gzip(rawBody, function (err, zippedBody) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});

it('returns a deflate response on a post request when accept-encoding: deflate,q=1; gzip,q=.5 is requested', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -191,7 +260,21 @@ describe('Payload', function () {
});
});

it('returns a gzip response when accept-encoding: deflate, gzip is requested', function (done) {
it('returns a deflate response on a get request when accept-encoding: deflate,q=1; gzip,q=.5 is requested', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: { 'accept-encoding': 'deflate,q=1; gzip,q=.5' } }, function (err, res, body) {

Zlib.deflate(rawBody, function (err, zippedBody) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});

it('returns a gzip response on a post request when accept-encoding: deflate, gzip is requested', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -205,7 +288,21 @@ describe('Payload', function () {
});
});

it('returns an identity response when accept-encoding is missing', function (done) {
it('returns a gzip response on a get request when accept-encoding: deflate, gzip is requested', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: { 'accept-encoding': 'deflate, gzip' } }, function (err, res, body) {

Zlib.gzip(rawBody, function (err, zippedBody) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});

it('returns an identity response on a post request when accept-encoding is missing', function (done) {

var rawBody = '{"test":"true"}';

Expand All @@ -215,4 +312,15 @@ describe('Payload', function () {
done();
});
});

it('returns an identity response on a get request when accept-encoding is missing', function (done) {

var rawBody = '{"test":"true"}';

Request.get({ url: uri, headers: {}}, function (err, res, body) {

expect(body).to.equal(rawBody);
done();
});
});
});

0 comments on commit 257c61e

Please sign in to comment.