From dc48202e4a2df5100c10073b22e153d69eec59b3 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Wed, 31 Jan 2024 20:07:51 -0500 Subject: [PATCH] Add support for Faraday 2.x. --- .github/workflows/test.yml | 8 ++------ .rubocop_todo.yml | 8 +++++--- CHANGELOG.md | 3 ++- Gemfile | 4 +++- README.md | 4 ++-- features/steps/api_navigation.rb | 6 +++--- features/steps/default_config.rb | 4 ++-- features/support/env.rb | 15 ++++++++++++++- hyperclient.gemspec | 5 ++--- lib/hyperclient/entry_point.rb | 6 +++--- test/hyperclient/entry_point_test.rb | 26 +++++++++++++------------- test/hyperclient_test.rb | 10 +++++----- test/test_helper.rb | 6 +++++- 13 files changed, 61 insertions(+), 44 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d531776..555c3dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,15 +9,11 @@ jobs: matrix: entry: - { ruby: '3.2' } - - { ruby: '2.7', faraday: '0.9.0' } - - { ruby: '2.7', faraday: '0.17.0' } - - { ruby: '2.7', faraday: '~> 1.0' } + - { ruby: '2.7' } - { ruby: 'jruby-9.3' } - { ruby: 'ruby-head', allowed-failure: true } - { ruby: 'jruby-head', allowed-failure: true } - name: test (${{ matrix.entry.ruby }}, ${{ matrix.entry.faraday || 'default' }}) - env: - FARADAY_VERSION: ${{ matrix.entry.faraday }} + name: test (${{ matrix.entry.ruby }}) steps: - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a77901a..11596b3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-02-16 15:23:43 UTC using RuboCop version 1.45.1. +# on 2024-02-01 15:09:00 UTC using RuboCop version 1.48.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -35,7 +35,7 @@ Lint/RedundantSafeNavigation: Exclude: - 'lib/hyperclient/link.rb' -# Offense count: 2 +# Offense count: 3 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: nested, compact @@ -43,8 +43,9 @@ Style/ClassAndModuleChildren: Exclude: - 'features/steps/api_navigation.rb' - 'features/steps/default_config.rb' + - 'features/support/env.rb' -# Offense count: 4 +# Offense count: 5 # Configuration parameters: AllowedConstants. Style/Documentation: Exclude: @@ -53,4 +54,5 @@ Style/Documentation: - 'features/steps/api_navigation.rb' - 'features/steps/default_config.rb' - 'features/support/api.rb' + - 'features/support/env.rb' - 'features/support/fixtures.rb' diff --git a/CHANGELOG.md b/CHANGELOG.md index d6d7f27..ff81465 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ ## Changelog -### 1.1.0 (Next) +### 2.0.0 (Next) * [#268](https://github.com/codegram/hyperclient/pull/268): Replace Travis-CI with GHA - [@dblock](https://github.com/dblock). * [#269](https://github.com/codegram/hyperclient/pull/269): Re-add code coverage - [@dblock](https://github.com/dblock). * [#270](https://github.com/codegram/hyperclient/pull/270): Upgrade Rubocop to 1.45 - [@dblock](https://github.com/dblock). * [#271](https://github.com/codegram/hyperclient/pull/271): Test against Ruby 3.2 - [@dblock](https://github.com/dblock). +* [#285](https://github.com/codegram/hyperclient/pull/285): Support Faraday 2.x - [@dblock](https://github.com/dblock). * Your contribution here. ### 1.0.1 (2021/01/02) diff --git a/Gemfile b/Gemfile index 8c84199..aeb85b0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source 'https://rubygems.org' -gem 'faraday', ENV['FARADAY_VERSION'] if ENV['FARADAY_VERSION'] && !ENV['FARADAY_VERSION'].empty? +gem 'faraday', '> 2' +gem 'faraday_hal_middleware', github: 'fetch/faraday_hal_middleware' gemspec @@ -25,6 +26,7 @@ end group :test do gem 'danger-changelog', '~> 0.6.0' gem 'danger-toc', '~> 0.2.0' + gem 'faraday-follow_redirects' gem 'minitest' gem 'mocha' gem 'rack-test' diff --git a/README.md b/README.md index 8ab765f..1a7c78b 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Hyperclient constructs a connection using typical [Faraday](http://github.com/lo ```ruby api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client| client.connection do |conn| - conn.use Faraday::Request::OAuth + conn.use Faraday::Request::Instrumentation end end ``` @@ -60,7 +60,7 @@ You can pass options to the Faraday connection block in the `connection` block: ```ruby api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client| client.connection(ssl: { verify: false }) do |conn| - conn.use Faraday::Request::OAuth + conn.use Faraday::Request::Instrumentation end end ``` diff --git a/features/steps/api_navigation.rb b/features/steps/api_navigation.rb index 59c6c28..b75b075 100644 --- a/features/steps/api_navigation.rb +++ b/features/steps/api_navigation.rb @@ -43,13 +43,13 @@ class Spinach::Features::ApiNavigation < Spinach::FeatureSteps end step 'I should be able to access it\'s title and body' do - @post._attributes.title.wont_equal nil - @post._attributes.body.wont_equal nil + assert_kind_of String, @post._attributes.title + assert_kind_of String, @post._attributes.body end step 'I should also be able to access it\'s embedded comments' do comment = @post._embedded.comments.first - comment._attributes.title.wont_equal nil + assert_kind_of String, comment._attributes.title end step 'I should be able to navigate to next page' do diff --git a/features/steps/default_config.rb b/features/steps/default_config.rb index 8c86470..3d21c9e 100644 --- a/features/steps/default_config.rb +++ b/features/steps/default_config.rb @@ -25,7 +25,7 @@ class Spinach::Features::DefaultConfig < Spinach::FeatureSteps end step 'it should have been parsed as JSON' do - @posts._attributes.total_posts.to_i.must_equal 4 - @posts._attributes['total_posts'].to_i.must_equal 4 + assert_equal 4, @posts._attributes.total_posts.to_i + assert_equal 4, @posts._attributes['total_posts'].to_i end end diff --git a/features/support/env.rb b/features/support/env.rb index 5d73b35..c3dd8f5 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,4 +1,17 @@ -require 'minitest/spec' +require 'minitest' require 'webmock' WebMock.enable! require 'hyperclient' + +Minitest::Spec.new nil if defined?(Minitest::Spec) +Spinach.config[:failure_exceptions] << Minitest::Assertion + +class Spinach::FeatureSteps + include Minitest::Assertions + attr_accessor :assertions + + def initialize(*args) + super(*args) + self.assertions = 0 + end +end diff --git a/hyperclient.gemspec b/hyperclient.gemspec index fb8678c..7b43226 100644 --- a/hyperclient.gemspec +++ b/hyperclient.gemspec @@ -13,8 +13,7 @@ Gem::Specification.new do |gem| gem.version = Hyperclient::VERSION gem.add_dependency 'addressable' - gem.add_dependency 'faraday', '>= 0.9.0' - gem.add_dependency 'faraday_hal_middleware' - gem.add_dependency 'faraday_middleware' + gem.add_dependency 'faraday', '>= 2.0' + gem.add_dependency 'faraday_hal_middleware', '>= 0.2.0' gem.metadata['rubygems_mfa_required'] = 'true' end diff --git a/lib/hyperclient/entry_point.rb b/lib/hyperclient/entry_point.rb index 96cebc8..f9ce119 100644 --- a/lib/hyperclient/entry_point.rb +++ b/lib/hyperclient/entry_point.rb @@ -1,4 +1,4 @@ -require 'faraday_middleware' +require 'faraday/follow_redirects' require 'faraday_hal_middleware' module Hyperclient @@ -20,7 +20,7 @@ def message # # client = Hyperclient::EntryPoint.new('http://my.api.org') do |entry_point| # entry_point.connection do |conn| - # conn.use Faraday::Request::OAuth + # conn.use Faraday::Request::Instrumentation # end # entry_point.headers['Access-Token'] = 'token' # end @@ -138,7 +138,7 @@ def faraday_block=(value) def default_faraday_block lambda do |connection, &block| connection.use Faraday::Response::RaiseError - connection.use FaradayMiddleware::FollowRedirects + connection.use Faraday::FollowRedirects::Middleware connection.request :hal_json connection.response :hal_json, content_type: /\bjson$/ diff --git a/test/hyperclient/entry_point_test.rb b/test/hyperclient/entry_point_test.rb index 7dcd3ce..bdc22dd 100644 --- a/test/hyperclient/entry_point_test.rb +++ b/test/hyperclient/entry_point_test.rb @@ -26,18 +26,18 @@ module Hyperclient it 'can insert additional middleware after a connection has been constructed' do _(entry_point.connection).must_be_kind_of Faraday::Connection - entry_point.connection.use :instrumentation + entry_point.connection.use Faraday::Request::Instrumentation handlers = entry_point.connection.builder.handlers - _(handlers).must_include FaradayMiddleware::Instrumentation + _(handlers).must_include Faraday::Request::Instrumentation end it 'creates a Faraday connection with the default block' do handlers = entry_point.connection.builder.handlers _(handlers).must_include Faraday::Response::RaiseError - _(handlers).must_include FaradayMiddleware::FollowRedirects - _(handlers).must_include FaradayMiddleware::EncodeHalJson - _(handlers).must_include FaradayMiddleware::ParseHalJson + _(handlers).must_include Faraday::FollowRedirects::Middleware + _(handlers).must_include Faraday::HalJson::Request + _(handlers).must_include Faraday::HalJson::Response _(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder end @@ -137,9 +137,9 @@ module Hyperclient it 'creates a Faraday connection with the default block' do handlers = entry_point.connection.builder.handlers _(handlers).wont_include Faraday::Response::RaiseError - _(handlers).wont_include FaradayMiddleware::FollowRedirects - _(handlers).must_include FaradayMiddleware::EncodeJson - _(handlers).must_include FaradayMiddleware::ParseJson + _(handlers).wont_include Faraday::FollowRedirects + _(handlers).must_include Faraday::Request::Json + _(handlers).must_include Faraday::Response::Json end end end @@ -148,7 +148,7 @@ module Hyperclient let(:entry_point) do EntryPoint.new 'http://my.api.org' do |entry_point| entry_point.connection do |conn| - conn.use Faraday::Request::OAuth + conn.use Faraday::Request::Instrumentation end entry_point.headers['Access-Token'] = 'token' end @@ -168,11 +168,11 @@ module Hyperclient it 'creates a Faraday connection with the default block plus any additional handlers' do handlers = entry_point.connection.builder.handlers - _(handlers).must_include Faraday::Request::OAuth + _(handlers).must_include Faraday::Request::Instrumentation _(handlers).must_include Faraday::Response::RaiseError - _(handlers).must_include FaradayMiddleware::FollowRedirects - _(handlers).must_include FaradayMiddleware::EncodeHalJson - _(handlers).must_include FaradayMiddleware::ParseHalJson + _(handlers).must_include Faraday::FollowRedirects::Middleware + _(handlers).must_include Faraday::HalJson::Request + _(handlers).must_include Faraday::HalJson::Response _(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder end diff --git a/test/hyperclient_test.rb b/test/hyperclient_test.rb index 9efd2a3..1daef19 100644 --- a/test/hyperclient_test.rb +++ b/test/hyperclient_test.rb @@ -13,7 +13,7 @@ let(:client) do Hyperclient.new('http://api.example.org') do |client| client.connection(default: true) do |conn| - conn.use Faraday::Request::OAuth + conn.use Faraday::Request::Instrumentation end client.headers['Access-Token'] = 'token' end @@ -31,11 +31,11 @@ it 'creates a Faraday connection with the default block plus any additional handlers' do handlers = client.connection.builder.handlers - _(handlers).must_include Faraday::Request::OAuth + _(handlers).must_include Faraday::Request::Instrumentation _(handlers).must_include Faraday::Response::RaiseError - _(handlers).must_include FaradayMiddleware::FollowRedirects - _(handlers).must_include FaradayMiddleware::EncodeHalJson - _(handlers).must_include FaradayMiddleware::ParseHalJson + _(handlers).must_include Faraday::FollowRedirects::Middleware + _(handlers).must_include Faraday::HalJson::Request + _(handlers).must_include Faraday::HalJson::Response end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index c9f1875..f0a1d9c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,10 +17,14 @@ require 'minitest/autorun' require 'minitest/pride' +require 'minitest/unit' require 'mocha/minitest' require 'json' -MiniTest::Test.class_eval do +require 'faraday' +require 'faraday/request/instrumentation' + +MiniTest::Assertions.class_eval do def stub_request(conn, adapter_class = Faraday::Adapter::Test, &stubs_block) adapter_handler = conn.builder.handlers.find { |h| h.klass < Faraday::Adapter } if adapter_handler