-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement no_proxy feature * Handle prefixed and subdomains by improving proxy_allowed?
- Loading branch information
1 parent
6b3add2
commit dea7726
Showing
5 changed files
with
146 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,23 @@ def teardown | |
Faraday.default_connection_options = nil | ||
end | ||
|
||
def with_env(key, proxy) | ||
old_value = ENV.fetch(key, false) | ||
ENV[key] = proxy | ||
def with_env(new_env) | ||
old_env = {} | ||
|
||
new_env.each do |key, value| | ||
old_env[key] = ENV.fetch(key, false) | ||
ENV[key] = value | ||
end | ||
|
||
begin | ||
yield | ||
ensure | ||
if old_value == false | ||
ENV.delete key | ||
else | ||
ENV[key] = old_value | ||
old_env.each do |key, value| | ||
if value == false | ||
ENV.delete key | ||
else | ||
ENV[key] = value | ||
end | ||
end | ||
end | ||
end | ||
|
@@ -285,23 +292,23 @@ def test_build_exclusive_url_handles_uri_instances | |
end | ||
|
||
def test_proxy_accepts_string | ||
with_env 'http_proxy', "http://duncan.proxy.com:80" do | ||
with_env 'http_proxy' => "http://duncan.proxy.com:80" do | ||
conn = Faraday::Connection.new | ||
conn.proxy 'http://proxy.com' | ||
assert_equal 'proxy.com', conn.proxy.host | ||
end | ||
end | ||
|
||
def test_proxy_accepts_uri | ||
with_env 'http_proxy', "http://duncan.proxy.com:80" do | ||
with_env 'http_proxy' => "http://duncan.proxy.com:80" do | ||
conn = Faraday::Connection.new | ||
conn.proxy URI.parse('http://proxy.com') | ||
assert_equal 'proxy.com', conn.proxy.host | ||
end | ||
end | ||
|
||
def test_proxy_accepts_hash_with_string_uri | ||
with_env 'http_proxy', "http://duncan.proxy.com:80" do | ||
with_env 'http_proxy' => "http://duncan.proxy.com:80" do | ||
conn = Faraday::Connection.new | ||
conn.proxy :uri => 'http://proxy.com', :user => 'rick' | ||
assert_equal 'proxy.com', conn.proxy.host | ||
|
@@ -310,7 +317,7 @@ def test_proxy_accepts_hash_with_string_uri | |
end | ||
|
||
def test_proxy_accepts_hash | ||
with_env 'http_proxy', "http://duncan.proxy.com:80" do | ||
with_env 'http_proxy' => "http://duncan.proxy.com:80" do | ||
conn = Faraday::Connection.new | ||
conn.proxy :uri => URI.parse('http://proxy.com'), :user => 'rick' | ||
assert_equal 'proxy.com', conn.proxy.host | ||
|
@@ -319,44 +326,44 @@ def test_proxy_accepts_hash | |
end | ||
|
||
def test_proxy_accepts_http_env | ||
with_env 'http_proxy', "http://duncan.proxy.com:80" do | ||
with_env 'http_proxy' => "http://duncan.proxy.com:80" do | ||
conn = Faraday::Connection.new | ||
assert_equal 'duncan.proxy.com', conn.proxy.host | ||
end | ||
end | ||
|
||
def test_proxy_accepts_http_env_with_auth | ||
with_env 'http_proxy', "http://a%40b:my%[email protected]:80" do | ||
with_env 'http_proxy' => "http://a%40b:my%[email protected]:80" do | ||
conn = Faraday::Connection.new | ||
assert_equal 'a@b', conn.proxy.user | ||
assert_equal 'my pass', conn.proxy.password | ||
end | ||
end | ||
|
||
def test_proxy_accepts_env_without_scheme | ||
with_env 'http_proxy', "localhost:8888" do | ||
with_env 'http_proxy' => "localhost:8888" do | ||
uri = Faraday::Connection.new.proxy[:uri] | ||
assert_equal 'localhost', uri.host | ||
assert_equal 8888, uri.port | ||
end | ||
end | ||
|
||
def test_no_proxy_from_env | ||
with_env 'http_proxy', nil do | ||
with_env 'http_proxy' => nil do | ||
conn = Faraday::Connection.new | ||
assert_nil conn.proxy | ||
end | ||
end | ||
|
||
def test_no_proxy_from_blank_env | ||
with_env 'http_proxy', '' do | ||
with_env 'http_proxy' => '' do | ||
conn = Faraday::Connection.new | ||
assert_nil conn.proxy | ||
end | ||
end | ||
|
||
def test_proxy_doesnt_accept_uppercase_env | ||
with_env 'HTTP_PROXY', "http://localhost:8888/" do | ||
with_env 'HTTP_PROXY' => "http://localhost:8888/" do | ||
conn = Faraday::Connection.new | ||
assert_nil conn.proxy | ||
end | ||
|
@@ -369,6 +376,79 @@ def test_proxy_requires_uri | |
end | ||
end | ||
|
||
def test_proxy_allowed_when_url_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://example.com'), false | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_prefixed_url_is_not_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://prefixedexample.com'), true | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_subdomain_url_is_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://subdomain.example.com'), false | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_url_not_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example2.com' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://example.com'), true | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_ip_address_is_not_in_no_proxy_list_but_url_is | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'localhost' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://127.0.0.1'), false | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_url_is_not_in_no_proxy_list_but_ip_address_is | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => '127.0.0.1' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://localhost'), false | ||
end | ||
end | ||
|
||
def test_proxy_allowed_in_multi_element_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example0.com,example.com,example1.com' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://example0.com'), false | ||
assert_equal conn.proxy_allowed?('http://example.com'), false | ||
assert_equal conn.proxy_allowed?('http://example1.com'), false | ||
assert_equal conn.proxy_allowed?('http://example2.com'), true | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_ports_match_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com:7171' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://example.com:7171'), false | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_port_is_not_set_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://example.com:7171'), false | ||
end | ||
end | ||
|
||
def test_proxy_allowed_when_ports_mismatch_in_no_proxy_list | ||
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com:3000' do | ||
conn = Faraday::Connection.new | ||
assert_equal conn.proxy_allowed?('http://example.com:7171'), true | ||
end | ||
end | ||
|
||
def test_dups_connection_object | ||
conn = Faraday::Connection.new 'http://sushi.com/foo', | ||
:ssl => { :verify => :none }, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters