add http proxy support to net/http

This commit is contained in:
rick 2010-04-27 09:53:33 -04:00
parent 7739157945
commit a43e0896a6
5 changed files with 92 additions and 4 deletions

View File

@ -13,7 +13,7 @@ module Faraday
is_ssl = env[:url].scheme == 'https'
http = Net::HTTP.new(env[:url].host, env[:url].port || (is_ssl ? 443 : 80))
http = net_http_class(env).new(env[:url].host, env[:url].port || (is_ssl ? 443 : 80))
if http.use_ssl = is_ssl
ssl = env[:ssl]
if ssl[:verify] == false
@ -46,6 +46,14 @@ module Faraday
rescue Errno::ECONNREFUSED
raise Error::ConnectionFailed, "connection refused"
end
def net_http_class(env)
if proxy = env[:request][:proxy]
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
else
Net::HTTP
end
end
end
end
end

View File

@ -34,6 +34,7 @@ module Faraday
@ssl = options[:ssl] || {}
@parallel_manager = options[:parallel]
self.url_prefix = url if url
proxy(options[:proxy])
merge_params @params, options[:params] if options[:params]
merge_headers @headers, options[:headers] if options[:headers]
if block
@ -92,6 +93,22 @@ module Faraday
@parallel_manager = nil
end
def proxy(arg = nil)
return @proxy if arg.nil?
@proxy =
case arg
when String then {:uri => proxy_arg_to_uri(arg)}
when URI then {:uri => arg}
when Hash then arg
if arg[:uri] = proxy_arg_to_uri(arg[:uri])
arg
else
raise ArgumentError, "no :uri option."
end
end
end
# return the assembled Rack application for this instance.
def to_app
@builder.to_app
@ -193,5 +210,12 @@ module Faraday
'%' << $1.unpack('H2'*bytesize($1)).join('%').tap { |c| c.upcase! }
end
end
def proxy_arg_to_uri(arg)
case arg
when String then URI.parse(arg)
when URI then arg
end
end
end
end

View File

@ -57,6 +57,10 @@ module Faraday
# :request - Hash of options for configuring the request.
# :timeout - open/read timeout Integer in seconds
# :open_timeout - read timeout Integer in seconds
# :proxy - Hash of proxy options
# :uri - Proxy Server URI
# :user - Proxy server username
# :password - Proxy server password
# :ssl - Hash of options for configuring SSL requests.
def to_env_hash(connection, request_method)
env_headers = connection.headers.dup
@ -70,7 +74,7 @@ module Faraday
:request_headers => env_headers.update(headers),
:parallel_manager => connection.parallel_manager,
:response => Response.new,
:request => connection.options,
:request => connection.options.merge(:proxy => connection.proxy),
:ssl => connection.ssl}
end

View File

@ -155,6 +155,47 @@ class TestConnection < Faraday::TestCase
assert_equal 'https://sushi.com/sushi/sake.html', uri.to_s
end
def test_proxy_accepts_string
conn = Faraday::Connection.new
conn.proxy 'http://proxy.com'
assert_equal 'proxy.com', conn.proxy.host
end
def test_proxy_accepts_string
conn = Faraday::Connection.new
conn.proxy 'http://proxy.com'
assert_equal 'proxy.com', conn.proxy[:uri].host
assert_equal [:uri], conn.proxy.keys
end
def test_proxy_accepts_uri
conn = Faraday::Connection.new
conn.proxy Addressable::URI.parse('http://proxy.com')
assert_equal 'proxy.com', conn.proxy[:uri].host
assert_equal [:uri], conn.proxy.keys
end
def test_proxy_accepts_hash_with_string_uri
conn = Faraday::Connection.new
conn.proxy :uri => 'http://proxy.com', :user => 'rick'
assert_equal 'proxy.com', conn.proxy[:uri].host
assert_equal 'rick', conn.proxy[:user]
end
def test_proxy_accepts_hash
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_requires_uri
conn = Faraday::Connection.new
assert_raises ArgumentError do
conn.proxy :uri => :bad_uri, :user => 'rick'
end
end
def test_params_to_query_converts_hash_of_params_to_uri_escaped_query_string
conn = Faraday::Connection.new
class << conn

View File

@ -6,15 +6,15 @@ class TestEnv < Faraday::TestCase
@conn.options[:timeout] = 3
@conn.options[:open_timeout] = 5
@conn.ssl[:verify] = false
@conn.proxy 'http://proxy.com'
@input = {
:body => 'abc',
:headers => {'Server' => 'Faraday'}}
@env_setup = Faraday::Request.create do |req|
@env = env_for @conn do |req|
req.url 'foo.json', 'a' => 1
req['Server'] = 'Faraday'
req.body = @input[:body]
end
@env = @env_setup.to_env_hash(@conn, :get)
end
def test_request_create_stores_method
@ -42,4 +42,15 @@ class TestEnv < Faraday::TestCase
def test_request_create_stores_ssl_options
assert_equal false, @env[:ssl][:verify]
end
def test_request_create_stores_proxy_options
assert_equal 'proxy.com', @env[:request][:proxy][:uri].host
end
def env_for(connection)
env_setup = Faraday::Request.create do |req|
yield req
end
env_setup.to_env_hash(connection, :get)
end
end