Add support for http_proxy environment variable

If no proxy is provided, but the http_proxy environment var
is set, this is used transparently.
This commit is contained in:
Duncan Robertson 2011-09-09 12:43:34 +01:00
parent a267870cfd
commit e262523d08
3 changed files with 28 additions and 0 deletions

View File

@ -33,6 +33,8 @@ module Faraday
@parallel_manager = options[:parallel]
self.url_prefix = url if url
options[:proxy] = ENV['http_proxy'] if options[:proxy].nil?
proxy(options[:proxy])
@params.update options[:params] if options[:params]

View File

@ -1,6 +1,15 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class TestConnection < Faraday::TestCase
def setup
@tmp_http_proxy = ENV['http_proxy']
ENV['http_proxy'] = nil
end
def teardown
ENV['http_proxy'] = @tmp_http_proxy
end
def test_initialize_parses_host_out_of_given_url
conn = Faraday::Connection.new "http://sushi.com"
assert_equal 'sushi.com', conn.host
@ -197,6 +206,7 @@ class TestConnection < Faraday::TestCase
end
def test_proxy_accepts_string
ENV['http_proxy'] = "http://duncan.proxy.com:80"
conn = Faraday::Connection.new
conn.proxy 'http://proxy.com'
assert_equal 'proxy.com', conn.proxy[:uri].host
@ -204,6 +214,7 @@ class TestConnection < Faraday::TestCase
end
def test_proxy_accepts_uri
ENV['http_proxy'] = "http://duncan.proxy.com:80"
conn = Faraday::Connection.new
conn.proxy Addressable::URI.parse('http://proxy.com')
assert_equal 'proxy.com', conn.proxy[:uri].host
@ -211,6 +222,7 @@ class TestConnection < Faraday::TestCase
end
def test_proxy_accepts_hash_with_string_uri
ENV['http_proxy'] = "http://duncan.proxy.com:80"
conn = Faraday::Connection.new
conn.proxy :uri => 'http://proxy.com', :user => 'rick'
assert_equal 'proxy.com', conn.proxy[:uri].host
@ -218,12 +230,19 @@ class TestConnection < Faraday::TestCase
end
def test_proxy_accepts_hash
ENV['http_proxy'] = "http://duncan.proxy.com:80"
conn = Faraday::Connection.new
conn.proxy :uri => Addressable::URI.parse('http://proxy.com'), :user => 'rick'
assert_equal 'proxy.com', conn.proxy[:uri].host
assert_equal 'rick', conn.proxy[:user]
end
def test_proxy_accepts_http_env
ENV['http_proxy'] = "http://duncan.proxy.com:80"
conn = Faraday::Connection.new
assert_equal 'duncan.proxy.com', conn.proxy[:uri].host
end
def test_proxy_requires_uri
conn = Faraday::Connection.new
assert_raises ArgumentError do

View File

@ -2,6 +2,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class EnvTest < Faraday::TestCase
def setup
@tmp_http_proxy = ENV['http_proxy']
ENV['http_proxy'] = "http://duncan.proxy.com:80"
@conn = Faraday.new :url => 'http://sushi.com/api',
:headers => {'Mime-Version' => '1.0'},
:request => {:oauth => {:consumer_key => 'anonymous'}}
@ -12,6 +15,10 @@ class EnvTest < Faraday::TestCase
@conn.proxy 'http://proxy.com'
end
def teardown
ENV['http_proxy'] = @tmp_http_proxy
end
def test_request_create_stores_method
env = make_env(:get)
assert_equal :get, env[:method]