mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Support disabling the proxy by setting to falsey.
Newer ruby versions of Net::HTTP will use ENV['http_proxy'] directly. You should be able to disable the proxy in RestClient without overriding ENV['http_proxy'] directly. Now, detect when a proxy has been explicitly set to falsey, and explicitly create a Net::HTTP object with proxy disabled in that case.
This commit is contained in:
parent
85180bb1e8
commit
d2ceac62cf
3 changed files with 83 additions and 22 deletions
|
@ -90,8 +90,23 @@ module RestClient
|
|||
Request.execute(:method => :options, :url => url, :headers => headers, &block)
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_accessor :proxy
|
||||
# A global proxy URL to use for all requests. This can be overridden on a
|
||||
# per-request basis by passing `:proxy` to RestClient::Request.
|
||||
def self.proxy
|
||||
@proxy
|
||||
end
|
||||
def self.proxy=(value)
|
||||
@proxy = value
|
||||
@proxy_set = true
|
||||
end
|
||||
|
||||
# Return whether RestClient.proxy was set explicitly. We use this to
|
||||
# differentiate between no value being set and a value explicitly set to nil.
|
||||
#
|
||||
# @return [Boolean]
|
||||
#
|
||||
def self.proxy_set?
|
||||
!!@proxy_set
|
||||
end
|
||||
|
||||
# Setup the log for RestClient calls.
|
||||
|
|
|
@ -273,29 +273,41 @@ module RestClient
|
|||
# The proxy URI for this request. If `:proxy` was provided on this request,
|
||||
# use it over `RestClient.proxy`.
|
||||
#
|
||||
# @return [URI, nil]
|
||||
# Return false if a proxy was explicitly set and is falsy.
|
||||
#
|
||||
# @return [URI, false, nil]
|
||||
#
|
||||
def proxy_uri
|
||||
if defined?(@proxy)
|
||||
if @proxy
|
||||
URI.parse(@proxy)
|
||||
else
|
||||
nil
|
||||
false
|
||||
end
|
||||
elsif RestClient.proxy_set?
|
||||
if RestClient.proxy
|
||||
URI.parse(RestClient.proxy)
|
||||
else
|
||||
false
|
||||
end
|
||||
elsif RestClient.proxy
|
||||
URI.parse(RestClient.proxy)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def net_http_class
|
||||
def net_http_object(hostname, port)
|
||||
p_uri = proxy_uri
|
||||
|
||||
if p_uri
|
||||
Net::HTTP::Proxy(p_uri.hostname, p_uri.port, p_uri.user, p_uri.password)
|
||||
if p_uri.nil?
|
||||
# no proxy set
|
||||
Net::HTTP.new(hostname, port)
|
||||
elsif !p_uri
|
||||
# proxy explicitly set to none
|
||||
Net::HTTP.new(hostname, port, nil, nil, nil, nil)
|
||||
else
|
||||
Net::HTTP
|
||||
Net::HTTP.new(hostname, port,
|
||||
p_uri.hostname, p_uri.port, p_uri.user, p_uri.password)
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -409,7 +421,7 @@ module RestClient
|
|||
|
||||
setup_credentials req
|
||||
|
||||
net = net_http_class.new(uri.hostname, uri.port)
|
||||
net = net_http_object(uri.hostname, uri.port)
|
||||
net.use_ssl = uri.is_a?(URI::HTTPS)
|
||||
net.ssl_version = ssl_version if ssl_version
|
||||
net.ciphers = ssl_ciphers if ssl_ciphers
|
||||
|
|
|
@ -413,43 +413,77 @@ describe RestClient::Request, :include_helpers do
|
|||
end
|
||||
|
||||
describe "proxy" do
|
||||
before do
|
||||
allow(Net::HTTP).to receive(:new).and_call_original
|
||||
@proxy_req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
||||
end
|
||||
|
||||
it "creates a proxy class if a proxy url is given" do
|
||||
RestClient.stub(:proxy).and_return("http://example.com/")
|
||||
@request.net_http_class.proxy_class?.should be true
|
||||
RestClient.stub(:proxy_set?).and_return(true)
|
||||
@proxy_req.net_http_object('host', 80).proxy?.should be true
|
||||
end
|
||||
|
||||
it "creates a proxy class with the correct address if a IPv6 proxy url is given" do
|
||||
RestClient.stub(:proxy).and_return("http://[::1]/")
|
||||
@request.net_http_class.proxy_address.should == "::1"
|
||||
RestClient.stub(:proxy_set?).and_return(true)
|
||||
@proxy_req.net_http_object('host', 80).proxy?.should be true
|
||||
@proxy_req.net_http_object('host', 80).proxy_address.should == '::1'
|
||||
end
|
||||
|
||||
it "creates a non-proxy class if a proxy url is not given" do
|
||||
@request.net_http_class.proxy_class?.should be_falsey
|
||||
@proxy_req.net_http_object('host', 80).proxy?.should be_falsey
|
||||
end
|
||||
|
||||
it "disables proxy on a per-request basis" do
|
||||
allow(Net::HTTP).to receive(:new).and_call_original
|
||||
RestClient.stub(:proxy).and_return('http://example.com')
|
||||
@request.net_http_class.proxy_class?.should be true
|
||||
RestClient.stub(:proxy_set?).and_return(true)
|
||||
@proxy_req.net_http_object('host', 80).proxy?.should be true
|
||||
|
||||
disabled_req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil)
|
||||
disabled_req.net_http_class.proxy_class?.should be_falsey
|
||||
disabled_req.net_http_object('host', 80).proxy?.should be_falsey
|
||||
end
|
||||
|
||||
it "sets proxy on a per-request basis" do
|
||||
@request.net_http_class.proxy_class?.should be_falsey
|
||||
allow(Net::HTTP).to receive(:new).and_call_original
|
||||
@proxy_req.net_http_object('some', 80).proxy?.should be_falsey
|
||||
|
||||
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => 'http://example.com')
|
||||
req.net_http_class.proxy_class?.should be true
|
||||
req.net_http_object('host', 80).proxy?.should be true
|
||||
end
|
||||
|
||||
it "overrides proxy from environment" do
|
||||
allow(Net::HTTP).to receive(:new).and_call_original
|
||||
allow(ENV).to receive(:[]).with("http_proxy").and_return("http://127.0.0.1")
|
||||
allow(ENV).to receive(:[]).with("no_proxy").and_return(nil)
|
||||
allow(ENV).to receive(:[]).with("NO_PROXY").and_return(nil)
|
||||
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
||||
obj = req.net_http_object('host', 80)
|
||||
obj.proxy?.should be true
|
||||
obj.proxy_address.should eq '127.0.0.1'
|
||||
|
||||
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil)
|
||||
obj = req.net_http_object('host', 80)
|
||||
obj.proxy?.should be_falsey
|
||||
|
||||
RestClient.stub(:proxy_set?).and_return(true)
|
||||
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
||||
obj = req.net_http_object('host', 80)
|
||||
obj.proxy?.should be_falsey
|
||||
end
|
||||
|
||||
it "overrides global proxy with per-request proxy" do
|
||||
allow(Net::HTTP).to receive(:new).and_call_original
|
||||
RestClient.stub(:proxy).and_return('http://example.com')
|
||||
@request.net_http_class.proxy_class?.should be true
|
||||
@request.net_http_class.proxy_address.should == 'example.com'
|
||||
RestClient.stub(:proxy_set?).and_return(true)
|
||||
obj = @proxy_req.net_http_object('host', 80)
|
||||
obj.proxy?.should be true
|
||||
obj.proxy_address.should eq 'example.com'
|
||||
|
||||
req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => 'http://127.0.0.1/')
|
||||
req.net_http_class.proxy_class?.should be true
|
||||
req.net_http_class.proxy_address.should == '127.0.0.1'
|
||||
req.net_http_object('host', 80).proxy?.should be true
|
||||
req.net_http_object('host', 80).proxy_address.should == '127.0.0.1'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue