Skip to content

Commit

Permalink
feat(gzip): allow gzip by default for all requests (#221)
Browse files Browse the repository at this point in the history
* feat(gzip): allow gzip by default for all requests

fixed #196

* feat(gzip): make gzip optional

Allow option to disable gzip compression with a flag (gzipEnable: false)
gzip compression still remains enabled by default

fixes #196

* docs(sphere-client): update online documentation

update documentation at http://sphereio.github.io/sphere-node-sdk/
  • Loading branch information
wizzy25 authored and hisabimbola committed Mar 31, 2017
1 parent 74aa25d commit 99f4f0c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/coffee/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ ALL_SERVICES = [
# stats:
# includeHeaders: true
# maskSensitiveHeaderData: false
# gzipEnable: true # not necessary as gzip is enabled by default. Set to false to disable
# client.products.fetch()
# .then (result) ->
# # result.statusCode
Expand Down
3 changes: 3 additions & 0 deletions 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,6 +156,7 @@ class Rest
request_options =
uri: "#{@_options.uri}#{params.resource}"
json: true
gzip: @_options.gzipEnable
method: params.method
host: @_options.host
headers: @_options.headers
Expand Down
1 change: 1 addition & 0 deletions src/spec/connect/rest.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe 'Rest', ->
expected_options =
uri: "https://api.sphere.io/#{Config.project_key}#{endpoint}"
json: true
gzip: true
method: method
host: 'api.sphere.io'
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

0 comments on commit 99f4f0c

Please sign in to comment.