Skip to content

Commit bc8fc09

Browse files
committed
Support gzip content encoding
1 parent 504616e commit bc8fc09

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ matrix:
4848
gemfile: gemfiles/4.1.gemfile
4949
- rvm: 2.6.0
5050
gemfile: gemfiles/4.2.gemfile
51+
- rvm: ruby-head
52+
gemfile: gemfiles/4.0.gemfile
53+
- rvm: ruby-head
54+
gemfile: gemfiles/4.1.gemfile
55+
- rvm: ruby-head
56+
gemfile: gemfiles/4.2.gemfile
5157
# We need to install latest version of bundler, because one in travis
5258
# image is too old to recognize platform => :mri_22 in Gemfile.
5359
before_install:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- [#359](https://github.com/JsonApiClient/json_api_client/pull/359) - Support gzip content encoding
6+
57
## 1.15.0
68

79
- [#346](https://github.com/JsonApiClient/json_api_client/pull/346) - add the option to have immutable resources

lib/json_api_client/connection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def initialize(options = {})
1414
builder.use Middleware::JsonRequest
1515
builder.use Middleware::Status, status_middleware_options
1616
builder.use Middleware::ParseJson
17+
builder.use ::FaradayMiddleware::Gzip
1718
builder.adapter(*adapter_options)
1819
end
1920
yield(self) if block_given?

test/unit/connection_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def self.parse(*args)
1515
end
1616
end
1717

18+
class RegularResource < TestResource
19+
end
20+
1821
class CustomConnectionResource < TestResource
1922
self.connection_class = NullConnection
2023
self.parser = NullParser
@@ -69,4 +72,40 @@ def test_can_specify_http_proxy
6972
assert_equal proxy.uri.to_s, 'http://proxy.example.com'
7073
end
7174

75+
def test_gzipping_without_server_support
76+
stub_request(:get, "http://example.com/regular_resources")
77+
.with(headers: {'Accept-Encoding'=>'gzip,deflate'})
78+
.to_return(
79+
status: 200,
80+
body: {data: [{id: "1", type: "regular_resources", attributes: {foo: "bar"}}]}.to_json,
81+
headers: {content_type: "application/vnd.api+json"}
82+
)
83+
84+
resources = RegularResource.all
85+
assert_equal 1, resources.length
86+
resource = resources.first
87+
assert_equal "bar", resource.foo
88+
end
89+
90+
def test_gzipping_with_server_support
91+
io = StringIO.new
92+
gz = Zlib::GzipWriter.new(io)
93+
gz.write({data: [{id: "1", type: "regular_resources", attributes: {foo: "bar"}}]}.to_json)
94+
gz.close
95+
body = io.string
96+
body.force_encoding('BINARY') if body.respond_to?(:force_encoding)
97+
98+
stub_request(:get, "http://example.com/regular_resources")
99+
.with(headers: {'Accept-Encoding'=>'gzip,deflate'})
100+
.to_return(
101+
status: 200,
102+
body: body,
103+
headers: {content_type: "application/vnd.api+json", content_encoding: 'gzip'}
104+
)
105+
106+
resources = RegularResource.all
107+
assert_equal 1, resources.length
108+
resource = resources.first
109+
assert_equal "bar", resource.foo
110+
end
72111
end

0 commit comments

Comments
 (0)