From 0737313f99b26b4c29f0fcbb3e3ceef289ecfcf4 Mon Sep 17 00:00:00 2001 From: pic Date: Wed, 13 Jan 2016 14:50:33 +0100 Subject: [PATCH] Allow nil http_proxyaddr to pass through to Net::HTTP This is useful when a proxy is used by default but you do not want to use it for some requests --- lib/httparty/connection_adapter.rb | 2 +- spec/httparty/connection_adapter_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/httparty/connection_adapter.rb b/lib/httparty/connection_adapter.rb index b4803cdf..aec78b71 100644 --- a/lib/httparty/connection_adapter.rb +++ b/lib/httparty/connection_adapter.rb @@ -76,7 +76,7 @@ def initialize(uri, options = {}) def connection host = clean_host(uri.host) port = uri.port || (uri.scheme == 'https' ? 443 : 80) - if options[:http_proxyaddr] + if options.has_key?(:http_proxyaddr) http = Net::HTTP.new(host, port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass]) else http = Net::HTTP.new(host, port) diff --git a/spec/httparty/connection_adapter_spec.rb b/spec/httparty/connection_adapter_spec.rb index 503c138b..9caf60a5 100644 --- a/spec/httparty/connection_adapter_spec.rb +++ b/spec/httparty/connection_adapter_spec.rb @@ -325,6 +325,19 @@ end end + context 'when providing nil as proxy address' do + let(:uri) { URI 'http://noproxytest.com' } + let(:options) { {http_proxyaddr: nil} } + + it { is_expected.not_to be_a_proxy } + + it "does pass nil proxy parameters to the connection, this forces to not use a proxy" do + http = Net::HTTP.new("noproxytest.com") + expect(Net::HTTP).to receive(:new).once.with("noproxytest.com", 80, nil, nil, nil, nil).and_return(http) + adapter.connection + end + end + context 'when not providing a proxy address' do let(:uri) { URI 'http://proxytest.com' }