Skip to content

Commit

Permalink
Refactor: Extract repeated calls to a shared map (#973)
Browse files Browse the repository at this point in the history
* Extract repeated calls to a shared map

* Spec for net_http_persistent min_version

* Use realistic value in a spec
  • Loading branch information
olleolleolle committed Apr 14, 2019
1 parent 8a65a58 commit be09719
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/faraday/adapter/net_http_persistent.rb
Expand Up @@ -63,16 +63,22 @@ def perform_request(http, env)
raise
end

SSL_CONFIGURATIONS = {
certificate: :client_cert,
private_key: :client_key,
ca_file: :ca_file,
ssl_version: :version,
min_version: :min_version,
max_version: :max_version
}.freeze

def configure_ssl(http, ssl)
http_set(http, :verify_mode, ssl_verify_mode(ssl))
http_set(http, :cert_store, ssl_cert_store(ssl))

http_set(http, :certificate, ssl[:client_cert]) if ssl[:client_cert]
http_set(http, :private_key, ssl[:client_key]) if ssl[:client_key]
http_set(http, :ca_file, ssl[:ca_file]) if ssl[:ca_file]
http_set(http, :ssl_version, ssl[:version]) if ssl[:version]
http_set(http, :min_version, ssl[:min_version]) if ssl[:min_version]
http_set(http, :max_version, ssl[:max_version]) if ssl[:max_version]
SSL_CONFIGURATIONS
.select { |_, key| ssl[key] }
.each { |target, key| http_set(http, target, ssl[key]) }
end

def http_set(http, attr, value)
Expand Down
22 changes: 22 additions & 0 deletions spec/faraday/adapter/net_http_persistent_spec.rb
Expand Up @@ -40,4 +40,26 @@
# `pool` is only present in net_http_persistent >= 3.0
expect(http.pool.size).to eq(5) if http.respond_to?(:pool)
end

context 'min_version' do
let(:conn_options) do
{
headers: { 'X-Faraday-Adapter' => adapter },
ssl: {
min_version: :TLS1_2
}
}
end

it 'allows to set min_version in SSL settings' do
url = URI('https://example.com')

adapter = described_class.new(nil)

http = adapter.send(:net_http_connection, url: url, request: {})

# `min_version` is only present in net_http_persistent >= 3.1 (UNRELEASED)
expect(http.min_version).to eq(:TLS1_2) if http.respond_to?(:min_version)
end
end
end

0 comments on commit be09719

Please sign in to comment.