From 5294872ad281c9db00951a3d48b5da701672b4f6 Mon Sep 17 00:00:00 2001 From: Ewan Dennis Date: Fri, 19 Feb 2016 16:31:35 +0000 Subject: [PATCH] Added gzipped response support - Request's gzip option now defaults to true with caller control - Bumped nock version to enable gzip support --- lib/sparkpost.js | 5 ++++ package.json | 2 +- test/spec/sparkpost.spec.js | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/sparkpost.js b/lib/sparkpost.js index bbd49fd..2186c50 100644 --- a/lib/sparkpost.js +++ b/lib/sparkpost.js @@ -84,6 +84,11 @@ SparkPost.prototype.request = function( options, callback ) { // set Strict SSL (Always true) options.strictSSL = true; + // default to accepting gzipped responses + if (typeof options.gzip === 'undefined') { + options.gzip = true; + } + request(options, function(err, res, body) { var invalidCodeRegex = /(5|4)[0-9]{2}/; if(err) { diff --git a/package.json b/package.json index d93f023..c17fe06 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "jshint-stylish": "0.4.0", "matchdep": "0.3.0", "mocha": "1.21.4", - "nock": "^1.6.1", + "nock": "^7.2.2", "proxyquire": "1.0.1", "sinon": "^1.14.1", "sinon-chai": "2.5.0", diff --git a/test/spec/sparkpost.spec.js b/test/spec/sparkpost.spec.js index 39a753b..1fcc596 100644 --- a/test/spec/sparkpost.spec.js +++ b/test/spec/sparkpost.spec.js @@ -2,6 +2,7 @@ var chai = require('chai') , expect = chai.expect , sinon = require('sinon') , sinonChai = require('sinon-chai') + , zlib = require('zlib') , nock = require('nock') , SparkPost = require('../../lib/sparkpost'); @@ -165,6 +166,59 @@ describe('SparkPost Library', function() { done(); }); }); + + it('should accept gzipped responses', function(done) { + var TEST_MESSAGE = 'This is a compressible test and it is full of compressible test stuff.' + , compressedMsg = zlib.gzipSync(TEST_MESSAGE + TEST_MESSAGE) + , gzipNock = nock('https://test.sparkpost.com', { + reqheaders: { + 'accept-encoding': 'gzip' + } + }) + .get('/test') + .reply(200, compressedMsg, { + 'X-Transfer-Length': String(compressedMsg.length) + , 'Content-Length': undefined + , 'Content-Encoding': 'gzip' + }) + , options = { + method: 'GET' + , uri: 'https://test.sparkpost.com/test' + }; + + client.request(options, function(err, data) { + expect(err).to.be.null; + expect(data.statusCode).to.equal(200); + expect(data.body).to.equal(TEST_MESSAGE + TEST_MESSAGE); + + // finish async test + done(); + }); + }); + + it('should support explicitly disabled gzip option', function(done) { + var TEST_MESSAGE = 'This is an uncompressed test message'; + + nock('https://test.sparkpost.com') + .get('/test') + .reply(200, TEST_MESSAGE); + + var options = { + method: 'GET' + , uri: 'https://test.sparkpost.com/test' + , gzip: false + }; + + client.request(options, function(err, data) { + expect(err).to.be.null; + expect(data.statusCode).to.equal(200); + expect(data.body).to.equal(TEST_MESSAGE); + expect(data.headers).not.to.have.property('content-encoding'); + + // finish async test + done(); + }); + }); }); describe('get method', function() {