diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1120b20..6db7f0a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,6 @@ jobs: ruby-version: 2.7 bundler-cache: true # 'bundle install' and cache env: - BUNDLE_WITHOUT: "development:test" BUNDLE_WITH: lint - name: Rubocop @@ -34,7 +33,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ['2.5', '2.6', '2.7'] + ruby: [ '2.6', '2.7', '3.0', '3.1' ] steps: - uses: actions/checkout@v1 diff --git a/.rubocop.yml b/.rubocop.yml index ce1fec6..69e9d74 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,7 +4,9 @@ require: AllCops: DisplayCopNames: true DisplayStyleGuide: true - TargetRubyVersion: 2.5 + TargetRubyVersion: 2.6 + NewCops: enable + SuggestExtensions: false Style/DoubleNegation: Enabled: false diff --git a/Gemfile b/Gemfile index 1029c25..28ba547 100644 --- a/Gemfile +++ b/Gemfile @@ -5,10 +5,9 @@ source 'https://rubygems.org' gemspec # Required gems to run all Faraday tests -gem 'multipart-parser' group :lint, :development do - gem 'rubocop', '~> 0.91.1' + gem 'rubocop', '~> 1.37' gem 'rubocop-packaging', '~> 0.5' gem 'rubocop-performance', '~> 1.0' end diff --git a/faraday-http.gemspec b/faraday-http.gemspec index 07d9898..8b0d221 100644 --- a/faraday-http.gemspec +++ b/faraday-http.gemspec @@ -15,16 +15,17 @@ Gem::Specification.new do |spec| spec.homepage = 'https://github.com/lostisland/faraday-http' spec.license = 'MIT' - spec.required_ruby_version = '>= 2.5' + spec.required_ruby_version = '>= 2.6' spec.metadata['homepage_uri'] = spec.homepage spec.metadata['source_code_uri'] = 'https://github.com/lostisland/faraday-http' spec.metadata['changelog_uri'] = 'https://github.com/lostisland/faraday-http/releases' + spec.metadata['rubygems_mfa_required'] = 'true' spec.files = Dir.glob('lib/**/*') + %w[README.md LICENSE] spec.require_paths = ['lib'] - spec.add_dependency 'faraday', '~> 1.0' + spec.add_dependency 'faraday', '~> 2.5' spec.add_dependency 'http', '>= 4.0', '< 6' spec.add_development_dependency 'bundler', '~> 2.0' diff --git a/lib/faraday/adapter/http.rb b/lib/faraday/adapter/http.rb index 9fe59e4..21fa410 100644 --- a/lib/faraday/adapter/http.rb +++ b/lib/faraday/adapter/http.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true +require 'http' + module Faraday class Adapter # HTTP.rb adapter. class HTTP < Faraday::Adapter - dependency 'http' - # Takes the environment and performs the request. # # @param env [Faraday::Env] the request environment. @@ -20,13 +20,22 @@ def call(env) private def perform_request(env) - conn = setup_connection(env) + connection = setup_connection(env) - resp = conn.request env[:method], env[:url], - body: env[:body], - ssl_context: ssl_context(env[:ssl]) + response = connection.request env[:method], env[:url], + body: env[:body], + ssl_context: ssl_context(env[:ssl]) + + if env.stream_response? + env.stream_response do |&on_data| + request_with_wrapped_block(response, env, &on_data) + end + body = '' + else + body = response.body.to_s + end - save_response(env, resp.code, resp.body.to_s, resp.headers, resp.status.reason) + save_response(env, response.code, body, response.headers, response.status.reason) rescue ::HTTP::TimeoutError raise Faraday::TimeoutError, $ERROR_INFO rescue ::HTTP::ConnectionError @@ -37,6 +46,14 @@ def perform_request(env) raise end + def request_with_wrapped_block(response, env) + return unless block_given? + + while (chunk = response.body.readpartial) + yield(chunk, env) + end + end + def setup_connection(env) conn = ::HTTP @@ -97,27 +114,19 @@ def ssl_context(ssl) end def ssl_client_cert(cert) - case cert - when NilClass then nil - when String - OpenSSL::X509::Certificate.new(File.read(cert)) - when OpenSSL::X509::Certificate - cert - else - raise Faraday::Error, "invalid ssl.client_cert: #{cert.inspect}" - end + return nil if cert.nil? + return OpenSSL::X509::Certificate.new(File.read(cert)) if cert.is_a(String) + return cert if cert.is_a?(OpenSSL::X509::Certificate) + + raise Faraday::Error, "invalid ssl.client_cert: #{cert.inspect}" end def ssl_client_key(cert) - case cert - when NilClass then nil - when String - OpenSSL::PKey::RSA.new(File.read(cert)) - when OpenSSL::PKey::RSA, OpenSSL::PKey::DSA - cert - else - raise Faraday::Error, "invalid ssl.client_key: #{cert.inspect}" - end + return nil if cert.nil? + return OpenSSL::PKey::RSA.new(File.read(cert)) if cert.is_a(String) + return cert if cert.is_a?(OpenSSL::PKey::RSA, OpenSSL::PKey::DSA) + + raise Faraday::Error, "invalid ssl.client_key: #{cert.inspect}" end end end diff --git a/lib/faraday/http/version.rb b/lib/faraday/http/version.rb index fd6a231..46c0bbd 100644 --- a/lib/faraday/http/version.rb +++ b/lib/faraday/http/version.rb @@ -2,6 +2,6 @@ module Faraday module Http - VERSION = '1.1.0' + VERSION = '2.0.0' end end diff --git a/spec/faraday/http/adapter_spec.rb b/spec/faraday/http/adapter_spec.rb index 133d71b..02df5e9 100644 --- a/spec/faraday/http/adapter_spec.rb +++ b/spec/faraday/http/adapter_spec.rb @@ -2,7 +2,7 @@ RSpec.describe Faraday::Adapter::HTTP do features :request_body_on_query_methods, :reason_phrase_parse, :trace_method, :connect_method, - :skip_response_body_on_head, :local_socket_binding + :skip_response_body_on_head, :local_socket_binding, :streaming it_behaves_like 'an adapter' @@ -20,10 +20,9 @@ let(:conn) do conn_options[:ssl] ||= {} - conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE'] + conn_options[:ssl][:ca_file] ||= ENV.fetch('SSL_FILE', nil) Faraday.new(remote, conn_options) do |conn| - conn.request :multipart conn.request :url_encoded conn.response :raise_error conn.adapter described_class, *adapter_options