Skip to content

Commit

Permalink
feat(gzip): make gzip optional
Browse files Browse the repository at this point in the history
Allow option to disable gzip compression with a flag (gzipEnable: false)
gzip compression still remains enabled by default

fixes #196
  • Loading branch information
wizzy25 committed Mar 23, 2017
1 parent e4a81e6 commit 805f476
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/coffee/connect/rest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Rest
if @_options.access_token
@_options.headers['Authorization'] = "Bearer #{@_options.access_token}"

@_options.gzipEnable = if !_.isUndefined(opts.gzipEnable) then opts.gzipEnable else true

debug 'rest options: %j', @_options
return

Expand Down Expand Up @@ -154,7 +156,7 @@ class Rest
request_options =
uri: "#{@_options.uri}#{params.resource}"
json: true
gzip: true
gzip: @_options.gzipEnable
method: params.method
host: @_options.host
headers: @_options.headers
Expand Down
65 changes: 65 additions & 0 deletions src/spec/integration/gzip.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
debug = require('debug')('spec-integration:products')
_ = require 'underscore'
Promise = require 'bluebird'
{SphereClient} = require '../../lib/main'
Config = require('../../config').config

newProductType = ->
name: 'Vehicle'
description: 'A sample type of vehicle'

describe 'gzip compression', ->

afterEach (done) ->
debug 'About to delete all product types'
@client.productTypes.all().fetch()
.then (payload) =>
debug "Deleting #{payload.body.total} product types"
Promise.all _.map payload.body.results, (productType) =>
@client.productTypes.byId(productType.id).delete(productType.version)
.then (results) ->
debug "Deleted #{results.length} product types"
done()
.catch (error) -> done(_.prettify(error))
, 20000 # 20 sec

it 'should use gzip compression by default for all requests', (done) ->
@client = new SphereClient
config: Config
stats:
includeHeaders: true
debug 'Creating a ProductType'
@client.productTypes.save(newProductType())
.then (result) =>
expect(result.statusCode).toBe 201
@client.productTypes.fetch()
.then (results) ->
gzipReqHeader = results.http.request.headers['accept-encoding']
gzipResHeader = results.http.response.headers['content-encoding']
expect(gzipReqHeader).toBeTruthy()
expect(gzipReqHeader).toBe 'gzip'
expect(gzipResHeader).toBeTruthy()
expect(gzipResHeader).toBe 'gzip'
done()
.catch (error) -> done(_.prettify(error))
, 20000 # 20 sec

it 'should not use gzip compression when disbled', (done) ->
@client = new SphereClient
config: Config
stats:
includeHeaders: true
gzipEnable: false
debug 'Creating a ProductType'
@client.productTypes.save(newProductType())
.then (result) =>
expect(result.statusCode).toBe 201
@client.productTypes.fetch()
.then (results) ->
gzipReqHeader = results.http.request.headers['accept-encoding']
gzipResHeader = results.http.response.headers['content-encoding']
expect(gzipReqHeader).not.toBeDefined()
expect(gzipResHeader).not.toBeDefined()
done()
.catch (error) -> done(_.prettify(error))
, 20000 # 20 sec
27 changes: 0 additions & 27 deletions src/spec/integration/products.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,6 @@ updatePublish = (version) ->
{action: "publish"}
]

describe 'gzip compression', ->
beforeEach (done) ->
@client = new SphereClient
config: Config
stats:
includeHeaders: true
@client.productTypes.save(newProductType())
.then (result) =>
expect(result.statusCode).toBe 201
@productType = result.body
done()
.catch (error) -> done(_.prettify(error))
, 20000 # 20 sec

it 'should use gzip compression for requests', (done) ->
@client.productTypes.fetch()
.then (results) ->
gzipReqHeader = results.http.request.headers['accept-encoding']
gzipResHeader = results.http.response.headers['content-encoding']
expect(gzipReqHeader).toBeTruthy()
expect(gzipReqHeader).toBe 'gzip'
expect(gzipResHeader).toBeTruthy()
expect(gzipResHeader).toBe 'gzip'
done()
.catch (error) -> done(_.prettify(error))
, 20000 # 20 sec

describe 'Integration Products', ->

beforeEach (done) ->
Expand Down

0 comments on commit 805f476

Please sign in to comment.