Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gzip content encoding #359

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ matrix:
gemfile: gemfiles/4.1.gemfile
- rvm: 2.6.0
gemfile: gemfiles/4.2.gemfile
- rvm: ruby-head
gemfile: gemfiles/4.0.gemfile
- rvm: ruby-head
gemfile: gemfiles/4.1.gemfile
- rvm: ruby-head
gemfile: gemfiles/4.2.gemfile
# We need to install latest version of bundler, because one in travis
# image is too old to recognize platform => :mri_22 in Gemfile.
before_install:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- [#359](https://github.com/JsonApiClient/json_api_client/pull/359) - Support gzip content encoding

## 1.15.0

- [#346](https://github.com/JsonApiClient/json_api_client/pull/346) - add the option to have immutable resources
Expand Down
1 change: 1 addition & 0 deletions lib/json_api_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(options = {})
builder.use Middleware::JsonRequest
builder.use Middleware::Status, status_middleware_options
builder.use Middleware::ParseJson
builder.use ::FaradayMiddleware::Gzip
builder.adapter(*adapter_options)
end
yield(self) if block_given?
Expand Down
39 changes: 39 additions & 0 deletions test/unit/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def self.parse(*args)
end
end

class RegularResource < TestResource
end

class CustomConnectionResource < TestResource
self.connection_class = NullConnection
self.parser = NullParser
Expand Down Expand Up @@ -69,4 +72,40 @@ def test_can_specify_http_proxy
assert_equal proxy.uri.to_s, 'http://proxy.example.com'
end

def test_gzipping_without_server_support
stub_request(:get, "http://example.com/regular_resources")
.with(headers: {'Accept-Encoding'=>'gzip,deflate'})
.to_return(
status: 200,
body: {data: [{id: "1", type: "regular_resources", attributes: {foo: "bar"}}]}.to_json,
headers: {content_type: "application/vnd.api+json"}
)

resources = RegularResource.all
assert_equal 1, resources.length
resource = resources.first
assert_equal "bar", resource.foo
end

def test_gzipping_with_server_support
io = StringIO.new
gz = Zlib::GzipWriter.new(io)
gz.write({data: [{id: "1", type: "regular_resources", attributes: {foo: "bar"}}]}.to_json)
gz.close
body = io.string
body.force_encoding('BINARY') if body.respond_to?(:force_encoding)

stub_request(:get, "http://example.com/regular_resources")
.with(headers: {'Accept-Encoding'=>'gzip,deflate'})
.to_return(
status: 200,
body: body,
headers: {content_type: "application/vnd.api+json", content_encoding: 'gzip'}
)

resources = RegularResource.all
assert_equal 1, resources.length
resource = resources.first
assert_equal "bar", resource.foo
end
end