[rb] make it easier to work with proxies in default http client
diff --git a/rb/lib/selenium/webdriver/remote/http/default.rb b/rb/lib/selenium/webdriver/remote/http/default.rb index c2f9ac4..5bff8a0 100644 --- a/rb/lib/selenium/webdriver/remote/http/default.rb +++ b/rb/lib/selenium/webdriver/remote/http/default.rb
@@ -35,9 +35,11 @@ # Debuggers that freeze the process will not be able to evaluate any operations if that happens. # @param [Numeric] open_timeout - Open timeout to apply to HTTP client. # @param [Numeric] read_timeout - Read timeout (seconds) to apply to HTTP client. - def initialize(open_timeout: nil, read_timeout: nil) + # @param [Proxy] proxy - Where to route HTTP calls sent by client. + def initialize(open_timeout: nil, read_timeout: nil, proxy: nil) @open_timeout = open_timeout @read_timeout = read_timeout + @proxy = proxy super() end @@ -45,6 +47,18 @@ @http&.finish end + def proxy + @proxy ||= begin + proxy = ENV.fetch('http_proxy', nil) || ENV.fetch('HTTP_PROXY', nil) + no_proxy = ENV.fetch('no_proxy', nil) || ENV.fetch('NO_PROXY', nil) + + if proxy + proxy = "http://#{proxy}" unless proxy.start_with?('http://') + Proxy.new(http: proxy, no_proxy: no_proxy) + end + end + end + private def http @@ -135,18 +149,6 @@ end end - def proxy - @proxy ||= begin - proxy = ENV.fetch('http_proxy', nil) || ENV.fetch('HTTP_PROXY', nil) - no_proxy = ENV.fetch('no_proxy', nil) || ENV.fetch('NO_PROXY', nil) - - if proxy - proxy = "http://#{proxy}" unless proxy.start_with?('http://') - Proxy.new(http: proxy, no_proxy: no_proxy) - end - end - end - def use_proxy? return false if proxy.nil?
diff --git a/rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb b/rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb index 8ed74ae..9eb305e 100644 --- a/rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb +++ b/rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb
@@ -24,6 +24,7 @@ module Remote module Http describe Default do + let(:proxy) { Proxy.new(http: 'http://foo:bar@proxy.org:8080') } let(:client) do client = described_class.new client.server_url = URI.parse('http://example.com') @@ -39,7 +40,7 @@ end describe '#initialize' do - let(:client) { described_class.new(read_timeout: 22, open_timeout: 23) } + let(:client) { described_class.new(read_timeout: 22, open_timeout: 23, proxy: proxy) } it 'accepts read timeout options' do expect(client.open_timeout).to eq 23 @@ -48,10 +49,14 @@ it 'accepts open timeout options' do expect(client.read_timeout).to eq 22 end + + it 'accepts a proxy' do + expect(client.send(:proxy)).to eq proxy + end end it 'uses the specified proxy' do - client.proxy = Proxy.new(http: 'http://foo:bar@proxy.org:8080') + client.proxy = proxy http = client.send :http expect(http).to be_proxy