mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Added support for proxy authentication
This commit is contained in:
parent
777493ecdf
commit
da5b16273a
4 changed files with 42 additions and 3 deletions
|
@ -41,6 +41,8 @@ module HTTParty
|
|||
# [:+body+:] Body of the request. If passed a Hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
|
||||
# [:+http_proxyaddr+:] Address of proxy server to use.
|
||||
# [:+http_proxyport+:] Port of proxy server to use.
|
||||
# [:+http_proxyuser+:] User for proxy server authentication.
|
||||
# [:+http_proxypass+:] Password for proxy server authentication.
|
||||
# [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+.
|
||||
# [:+query+:] Query string, or a Hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use a Hash. See also HTTParty::ClassMethods.default_params.
|
||||
# [:+timeout+:] Timeout for opening connection and reading data.
|
||||
|
@ -67,11 +69,13 @@ module HTTParty
|
|||
#
|
||||
# class Foo
|
||||
# include HTTParty
|
||||
# http_proxy 'http://foo.com', 80
|
||||
# http_proxy 'http://foo.com', 80, 'user', 'pass'
|
||||
# end
|
||||
def http_proxy(addr=nil, port = nil)
|
||||
def http_proxy(addr=nil, port=nil, user=nil, pass=nil)
|
||||
default_options[:http_proxyaddr] = addr
|
||||
default_options[:http_proxyport] = port
|
||||
default_options[:http_proxyuser] = user
|
||||
default_options[:http_proxypass] = pass
|
||||
end
|
||||
|
||||
# Allows setting a base uri to be used for each request.
|
||||
|
|
|
@ -102,7 +102,7 @@ module HTTParty
|
|||
end
|
||||
|
||||
def http
|
||||
http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport])
|
||||
http = Net::HTTP.new(uri.host, uri.port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass])
|
||||
http.use_ssl = ssl_implied?
|
||||
|
||||
if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
|
||||
|
|
|
@ -216,6 +216,25 @@ describe HTTParty::Request do
|
|||
@request.send(:http)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with a proxy' do
|
||||
it 'should use a proxy address and port' do
|
||||
request = HTTParty::Request.new(Net::HTTP::Get, 'https://foobar.com',
|
||||
:http_proxyaddr => '1.2.3.4', :http_proxyport => 8080)
|
||||
http = request.send(:http)
|
||||
http.proxy_address.should == '1.2.3.4'
|
||||
http.proxy_port.should == 8080
|
||||
end
|
||||
|
||||
it 'should use a proxy user and password when provided' do
|
||||
request = HTTParty::Request.new(Net::HTTP::Get, 'https://foobar.com',
|
||||
:http_proxyaddr => '1.2.3.4', :http_proxyport => 8080,
|
||||
:http_proxyuser => 'user', :http_proxypass => 'pass')
|
||||
http = request.send(:http)
|
||||
http.proxy_user.should == 'user'
|
||||
http.proxy_pass.should == 'pass'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when setting timeout" do
|
||||
|
|
|
@ -39,6 +39,22 @@ describe HTTParty do
|
|||
|
||||
end
|
||||
|
||||
describe 'http_proxy' do
|
||||
it 'should set the address' do
|
||||
@klass.http_proxy 'proxy.foo.com', 80
|
||||
options = @klass.default_options
|
||||
options[:http_proxyaddr].should == 'proxy.foo.com'
|
||||
options[:http_proxyport].should == 80
|
||||
end
|
||||
|
||||
it 'should set the proxy user and pass when they are provided' do
|
||||
@klass.http_proxy 'proxy.foo.com', 80, 'user', 'pass'
|
||||
options = @klass.default_options
|
||||
options[:http_proxyuser].should == 'user'
|
||||
options[:http_proxypass].should == 'pass'
|
||||
end
|
||||
end
|
||||
|
||||
describe "base uri" do
|
||||
before(:each) do
|
||||
@klass.base_uri('api.foo.com/v1')
|
||||
|
|
Loading…
Reference in a new issue