Skip to content

Commit 060e8e4

Browse files
dblockCopilot
andcommitted
Test JSON 2 and 3 in CI while retaining Ruby 2.7 support
Add JSON response parsing regressions and upgrade RuboCop to permit JSON 3. Keep Faraday unconstrained beyond its existing minimum; current releases fix JSON 3 keyword parser options. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 56d5a87 commit 060e8e4

6 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ jobs:
1010
entry:
1111
- { ruby: '4.0' }
1212
- { ruby: '3.4' }
13+
- { ruby: '3.2', json: '2' }
1314
- { ruby: '3.2' }
14-
- { ruby: '2.7' }
15+
- { ruby: '2.7', json: '2' }
1516
- { ruby: 'ruby-head', allowed-failure: true }
1617
- { ruby: 'jruby-head', allowed-failure: true }
17-
name: test (${{ matrix.entry.ruby }})
18+
name: test (Ruby ${{ matrix.entry.ruby }}, JSON ${{ matrix.entry.json || '3' }})
19+
env:
20+
JSON_VERSION: ${{ matrix.entry.json || '3' }}
1821
steps:
1922
- uses: actions/checkout@v3
2023
- uses: ruby/setup-ruby@v1

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* [#320](https://github.com/codegram/hyperclient/pull/320): Documented how to handle non-`hal+json` responses via the Faraday response middleware's `content_type` matcher - [@dblock](https://github.com/dblock).
99
* [#321](https://github.com/codegram/hyperclient/pull/321): Added Ruby 4.0 to the CI test matrix - [@dblock](https://github.com/dblock).
1010
* [#322](https://github.com/codegram/hyperclient/pull/322): Fixed `Link` caching a mutating request's (`_post`/`_put`/`_patch`/`_delete`) response as its resource, causing subsequent reads (e.g. `#each`, `method_missing`) to return stale data instead of a fresh `_get` - [@dblock](https://github.com/dblock).
11+
* [#325](https://github.com/codegram/hyperclient/pull/325): Added JSON 2.x/3.x to the CI matrix and JSON response parsing regression tests, retaining Ruby 2.7 coverage with JSON 2.x; upgraded RuboCop to 1.91.0 to allow JSON 3.x - [@dblock](https://github.com/dblock).
1112
* Your contribution here.
1213

1314
### 2.0.0 (2024/02/01)

‎CONTRIBUTING.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ bundle install
3232
bundle exec rake
3333
```
3434

35+
CI runs the test suites with JSON 2.x on Ruby 2.7, both JSON 2.x and 3.x on Ruby 3.2, and JSON 3.x on Ruby 3.4, Ruby 4.0, and the head builds.
36+
To select a JSON major version locally, resolve the bundle and run the tests with the same `JSON_VERSION`:
37+
38+
```
39+
JSON_VERSION=2 bundle update json
40+
JSON_VERSION=2 bundle exec rake test spinach
41+
JSON_VERSION=3 bundle update json
42+
JSON_VERSION=3 bundle exec rake test spinach
43+
```
44+
3545
#### Write Tests
3646

3747
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add to [test/hyperclient](test/hyperclient).

‎Gemfile‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ source 'https://rubygems.org'
22

33
gemspec
44

5+
gem 'json', "~> #{ENV['JSON_VERSION']}.0" if ENV['JSON_VERSION']
6+
57
group :development do
68
gem 'growl'
79
gem 'guard'
@@ -13,7 +15,7 @@ end
1315

1416
group :development, :test do
1517
gem 'rake'
16-
gem 'rubocop', '~> 1.89.0', require: false
18+
gem 'rubocop', '~> 1.91.0', require: false
1719
gem 'rubocop-minitest', require: false
1820
gem 'rubocop-rake', require: false
1921
gem 'simplecov', require: false

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Hyperclient is a Hypermedia API client written in Ruby. It fully supports [JSON
2626

2727
The examples in this README use the [Splines Demo API](https://github.com/ruby-grape/grape-with-roar) running [here](https://grape-with-roar.herokuapp.com/api). Use version 1.x with Faraday 1.x, and version 2.x with Faraday 2.x. If you're upgrading from a previous version, please make sure to read [UPGRADING](UPGRADING.md).
2828

29+
Hyperclient is tested with JSON 2.x on Ruby 2.7, both JSON 2.x and 3.x on Ruby 3.2,
30+
and JSON 3.x on newer Rubies.
31+
JSON 3.x requires a Faraday release with keyword parser options support (2.14.4+).
32+
2933
## API Client
3034

3135
Create an API client.

‎test/hyperclient/link_test.rb‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,37 @@ module Hyperclient
167167
end
168168

169169
describe 'get' do
170+
%w[application/hal+json application/json].each do |content_type|
171+
it "parses a #{content_type} response into a resource" do
172+
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
173+
174+
stub_request(entry_point.connection) do |stub|
175+
stub.get('http://api.example.org/productions/1') do
176+
[200, { 'Content-Type' => content_type },
177+
'{"title":"Production","_links":{"self":{"href":"/productions/1"}}}']
178+
end
179+
end
180+
181+
resource = link._get
182+
183+
_(resource.title).must_equal 'Production'
184+
_(resource._links.self._url).must_equal '/productions/1'
185+
_(resource._response.body).must_be_kind_of Hash
186+
end
187+
end
188+
189+
it 'raises a parsing error for an invalid JSON response' do
190+
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
191+
192+
stub_request(entry_point.connection) do |stub|
193+
stub.get('http://api.example.org/productions/1') do
194+
[200, { 'Content-Type' => 'application/hal+json' }, '{"title":']
195+
end
196+
end
197+
198+
_(-> { link._get }).must_raise Faraday::ParsingError
199+
end
200+
170201
it 'sends a GET request with the link url' do
171202
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
172203

0 commit comments

Comments
 (0)