Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Setting Proxy without Using HTTP_PROXY, etc. #180

Merged
merged 10 commits into from
Jan 30, 2020
19 changes: 12 additions & 7 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ def initialize(api_key, application_key, silent=true, timeout=nil, endpoint=nil)
def connect
connection = Net::HTTP

# After ruby 2.0 Net::HTTP looks for the env variable but not ruby 1.9
if RUBY_VERSION < '2.0.0'
proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
if proxy
proxy_uri = URI.parse(proxy)
connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
end
# Expose using a proxy without setting the HTTPS_PROXY or HTTP_PROXY variables
proxy = Dogapi.find_proxy()

if proxy
proxy_uri = URI.parse(proxy)
connection = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
end

uri = URI.parse(@api_host)
Expand Down Expand Up @@ -170,4 +169,10 @@ def Dogapi.find_localhost
raise 'Cannot determine local hostname via hostname -f'
end
end

def Dogapi.find_proxy
ENV['DD_PROXY_HTTPS'] || ENV['dd_proxy_https'] ||
ENV['DD_PROXY_HTTP'] || ENV['dd_proxy_http'] ||
ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
end
end
20 changes: 20 additions & 0 deletions spec/unit/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@
ENV['http_proxy'] = nil
end

it 'uses-the dogapi specific proxy if set' do
service = Dogapi::APIService.new('api_key', 'app_key')

service.connect do |conn|
expect(conn.proxy_address).to be(nil)
expect(conn.proxy_port).to be(nil)
end

ENV['https_proxy'] = 'https://www.proxy.com:443'
ENV['dd_proxy_https'] = 'https://www.otherproxy.com:443'

service.connect do |conn|
expect(conn.proxy_address).to eq 'www.otherproxy.com'
expect(conn.proxy_port).to eq 443
end

ENV['https_proxy'] = nil
zippolyte marked this conversation as resolved.
Show resolved Hide resolved
ENV['dd_proxy_https'] = nil
end

it 'respects the endpoint configuration' do
service = Dogapi::APIService.new('api_key', 'app_key', true, nil, 'https://app.example.com')

Expand Down