Skip to content

Commit

Permalink
add initializer on Faraday::Adapter that accept and store a block wit…
Browse files Browse the repository at this point in the history
…h custom configuration

use the `@custom_config` block on `net_http` and `net_http_persistent` adapters
added tests
  • Loading branch information
iMacTia committed Dec 30, 2016
1 parent 389c503 commit bb8990f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def inherited(subclass)
extend Parallelism
self.supports_parallel = false

def initialize(app = nil, &block)
super
@custom_config = block if block_given?
end

def call(env)
env.clear_body if env.needs_body?
end
Expand Down
12 changes: 8 additions & 4 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def call(env)
super
with_net_http_connection(env) do |http|
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]

req = env[:request]
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
http.open_timeout = req[:open_timeout] if req[:open_timeout]
configure_request(http, env[:request])

begin
http_response = perform_request(http, env)
Expand Down Expand Up @@ -109,6 +106,13 @@ def configure_ssl(http, ssl)
http.ssl_version = ssl[:version] if ssl[:version]
end

def configure_request(http, req)
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
http.open_timeout = req[:open_timeout] if req[:open_timeout]

@custom_config.call(http) if @custom_config
end

def ssl_cert_store(ssl)
return ssl[:cert_store] if ssl[:cert_store]
# Use the default cert store by default, i.e. system ca certs
Expand Down
7 changes: 4 additions & 3 deletions lib/faraday/adapter/net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ class Adapter
class NetHttpPersistent < NetHttp
dependency 'net/http/persistent'

def with_net_http_connection(env)
if proxy = env[:request][:proxy]
def net_http_connection(env)
if (proxy = env[:request][:proxy])
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
proxy_uri.user = proxy_uri.password = nil
# awful patch for net-http-persistent 2.8 not unescaping user/password
(class << proxy_uri; self; end).class_eval do
define_method(:user) { proxy[:user] }
define_method(:password) { proxy[:password] }
end if proxy[:user]
return Net::HTTP::Persistent.new 'Faraday', proxy_uri
end

yield Net::HTTP::Persistent.new 'Faraday', proxy_uri
Net::HTTP::Persistent.new 'Faraday'
end

def perform_request(http, env)
Expand Down
12 changes: 12 additions & 0 deletions test/adapters/net_http_persistent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@ def setup
end if ssl_mode?
end

def test_custom_adapter_config
url = URI('https://example.com:1234')

adapter = Faraday::Adapter::NetHttpPersistent.new do |http|
http.idle_timeout = 123
end

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

assert_equal 123, http.idle_timeout
end
end
end
12 changes: 12 additions & 0 deletions test/adapters/net_http_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,17 @@ def test_explicit_port_number
assert_equal 1234, http.port
end

def test_custom_adapter_config
url = URI('https://example.com:1234')

adapter = Faraday::Adapter::NetHttp.new do |http|
http.continue_timeout = 123
end

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

assert_equal 123, http.continue_timeout
end
end
end

0 comments on commit bb8990f

Please sign in to comment.