Skip to content

Commit

Permalink
Fix conditions for calling configure_ssl
Browse files Browse the repository at this point in the history
`env[:ssl]` is an instance of `Faraday::SSLOptions`, and as a result,
`configure_ssl` is always executed, even when the connection is to HTTP.
Fixes #37
  • Loading branch information
ma2gedev authored and olleolleolle committed Jul 24, 2024
1 parent 1195316 commit 417da45
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def initialize(app = nil, opts = {}, &block)

def build_connection(env)
net_http_connection(env).tap do |http|
http.use_ssl = env[:url].scheme == 'https' if http.respond_to?(:use_ssl=)
configure_ssl(http, env[:ssl])
if env[:url].scheme == 'https' && env[:ssl]
configure_ssl(http, env[:ssl])
end
configure_request(http, env[:request])
end
end
Expand Down Expand Up @@ -129,7 +130,7 @@ def save_http_response(env, http_response)
end

def configure_ssl(http, ssl)
return unless ssl
http.use_ssl = true if http.respond_to?(:use_ssl=)

http.verify_mode = ssl_verify_mode(ssl)
http.cert_store = ssl_cert_store(ssl)
Expand Down
11 changes: 10 additions & 1 deletion spec/faraday/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
context 'checking http' do
let(:url) { URI('http://example.com') }
let(:adapter) { described_class.new }
let(:http) { adapter.send(:connection, url: url, request: {}) }
let(:ssl) { Faraday::SSLOptions.new }
let(:http) { adapter.send(:connection, url: url, request: {}, ssl: ssl) }

it { expect(http.port).to eq(80) }

it { expect(http).not_to be_use_ssl }

it { expect(http.cert_store).to be_nil }

it 'sets max_retries to 0' do
adapter.send(:configure_request, http, {})

Expand Down Expand Up @@ -50,6 +55,10 @@

it { expect(http.port).to eq(443) }

it { expect(http).to be_use_ssl }

it { expect(http.cert_store).not_to be_nil }

if Gem::Version.new(Faraday::VERSION) > Gem::Version.new('2.3.0') &&
Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
it 'supports verify_hostname option' do
Expand Down

0 comments on commit 417da45

Please sign in to comment.