diff --git a/rb/lib/selenium/webdriver/common/proxy.rb b/rb/lib/selenium/webdriver/common/proxy.rb index 644511575915e..4c804338f15e4 100644 --- a/rb/lib/selenium/webdriver/common/proxy.rb +++ b/rb/lib/selenium/webdriver/common/proxy.rb @@ -145,7 +145,7 @@ def as_json(*) 'proxyType' => TYPES[type].downcase, 'ftpProxy' => ftp, 'httpProxy' => http, - 'noProxy' => no_proxy.is_a?(String) ? no_proxy.split(', ') : no_proxy, + 'noProxy' => no_proxy.is_a?(String) ? no_proxy.split(',').map(&:strip).reject(&:empty?) : no_proxy, 'proxyAutoconfigUrl' => pac, 'sslProxy' => ssl, 'autodetect' => auto_detect, diff --git a/rb/lib/selenium/webdriver/remote/http/default.rb b/rb/lib/selenium/webdriver/remote/http/default.rb index 2677eefc2de15..62bd975deca39 100644 --- a/rb/lib/selenium/webdriver/remote/http/default.rb +++ b/rb/lib/selenium/webdriver/remote/http/default.rb @@ -148,24 +148,22 @@ def proxy def use_proxy? return false if proxy.nil? + return true unless proxy.no_proxy - if proxy.no_proxy - ignored = proxy.no_proxy.split(',').any? do |host| - host == '*' || - host == server_url.host || ( - begin - IPAddr.new(host).include?(server_url.host) - rescue ArgumentError - false - end - ) - end + !proxy_ignored? + end - !ignored - else - true + def proxy_ignored? + proxy.no_proxy.split(',').map(&:strip).reject(&:empty?).any? do |host| + host == '*' || host == server_url.host || ip_match?(host) end end + + def ip_match?(host) + IPAddr.new(host).include?(server_url.host) + rescue ArgumentError + false + end end # Default end # Http end # Remote diff --git a/rb/spec/unit/selenium/webdriver/remote/capabilities_spec.rb b/rb/spec/unit/selenium/webdriver/remote/capabilities_spec.rb index b53bb44c060f4..a0329e243b2a8 100644 --- a/rb/spec/unit/selenium/webdriver/remote/capabilities_spec.rb +++ b/rb/spec/unit/selenium/webdriver/remote/capabilities_spec.rb @@ -29,6 +29,18 @@ module Remote expect(caps.as_json['proxy']['noProxy']).to eq(%w[proxy_url localhost]) end + it 'converts noProxy from comma-only separated string to array' do + proxy = Proxy.new(no_proxy: 'proxy_url,localhost') + caps = described_class.new(proxy: proxy) + expect(caps.as_json['proxy']['noProxy']).to eq(%w[proxy_url localhost]) + end + + it 'trims whitespace around noProxy entries' do + proxy = Proxy.new(no_proxy: ' proxy_url , localhost ') + caps = described_class.new(proxy: proxy) + expect(caps.as_json['proxy']['noProxy']).to eq(%w[proxy_url localhost]) + end + it 'does not convert noProxy if it is already array' do proxy = Proxy.new(no_proxy: ['proxy_url']) caps = described_class.new(proxy: proxy) 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 21eff54011c37..07bf9c2404d99 100644 --- a/rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb +++ b/rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb @@ -123,6 +123,20 @@ module Http expect(http).not_to be_proxy end end + + it "trims whitespace around entries in #{no_proxy_var}" do + with_env('http_proxy' => 'proxy.org:8080', no_proxy_var => 'localhost, example.com') do + http = client.send :http + expect(http).not_to be_proxy + end + end + + it "trims leading whitespace on a single entry in #{no_proxy_var}" do + with_env('http_proxy' => 'proxy.org:8080', no_proxy_var => ' example.com') do + http = client.send :http + expect(http).not_to be_proxy + end + end end it 'raises a sane error if a proxy is refusing connections' do