Skip to content
Snippets Groups Projects
Commit 864fff29 authored by Alex Reisner's avatar Alex Reisner
Browse files

Get rid of use_proxy configuration setting.

Instead use a proxy if http_proxy or https_proxy is set. Also revise
code style to be more consistent with rest of library.
parent c129780d
No related branches found
No related tags found
No related merge requests found
......@@ -15,8 +15,11 @@ module Geocoder
# use HTTPS for lookup requests? (if supported)
[:use_https, false],
# use Proxy when http_proxy / https_proxy is set in ENV
[:use_proxy, true],
# URL of HTTP proxy
[:http_proxy, nil],
# URL of HTTPS proxy
[:https_proxy, nil],
# API key for geocoding service
[:api_key, nil],
......
......@@ -55,20 +55,22 @@ module Geocoder
private # -------------------------------------------------------------
##
# Object used to fetch requests
# Object used to make HTTP requests.
#
def http_client
proxy_url = ENV[Geocoder::Configuration.use_https ? 'https_proxy' : 'http_proxy']
return Net::HTTP unless Geocoder::Configuration.use_proxy && proxy_url
begin
uri = URI.parse(proxy_url)
rescue URI::InvalidURIError
raise ConfigurationError, "The proxy URL in environment (" +
"#{Geocoder::Configuration.use_https ? 'https_proxy' : 'http_proxy'} => #{proxy_url}" +
") was not parsed correctly by URI::Parse"
secure = Geocoder::Configuration.use_https
proxy_name = "http#{'s' if secure}_proxy"
if proxy_url = Geocoder::Configuration.send(proxy_name)
begin
uri = URI.parse(proxy_url)
rescue URI::InvalidURIError
raise ConfigurationError,
"Error parsing HTTP#{'S' if secure} proxy URL: '#{proxy_url}'"
end
Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
else
Net::HTTP
end
Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
end
##
......
......@@ -20,34 +20,24 @@ class GeocoderTest < Test::Unit::TestCase
# --- sanity checks ---
def test_uses_proxy_by_default
lookup = Geocoder::Lookup::Base.new
ENV.delete 'http_proxy'
ENV.delete 'https_proxy'
assert ! lookup.send(:http_client).proxy_class?
ENV['http_proxy'] = 'http://localhost'
assert_equal 'localhost', lookup.send(:http_client).proxy_address
def test_uses_proxy_when_specified
Geocoder::Configuration.http_proxy = 'http://localhost'
lookup = Geocoder::Lookup::Google.new
assert lookup.send(:http_client).proxy_class?
end
Geocoder::Configuration.use_https = true
ENV['https_proxy'] = 'https://localhost-ssl'
assert_equal 'localhost-ssl', lookup.send(:http_client).proxy_address
def test_doesnt_use_proxy_when_not_specified
lookup = Geocoder::Lookup::Google.new
assert !lookup.send(:http_client).proxy_class?
end
def test_exception_raised_on_bad_proxy_url
ENV['http_proxy'] = ' \\_O< Quack Quack'
Geocoder::Configuration.http_proxy = ' \\_O< Quack Quack'
assert_raise Geocoder::ConfigurationError do
Geocoder::Lookup::Base.new.send(:http_client)
Geocoder::Lookup::Google.new.send(:http_client)
end
end
def test_uses_direct_connection_when_forced
Geocoder::Configuration.use_proxy = false
ENV['http_proxy'] = 'http://localhost:8080'
assert ! Geocoder::Lookup::Base.new.send(:http_client).proxy_class?
end
def test_uses_https_for_secure_query
Geocoder::Configuration.use_https = true
g = Geocoder::Lookup::Google.new
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment