Permalink
Browse files

Merge branch 'proxy_auth' of https://github.com/guenin/httparty into …

…guenin-proxy_auth
  • Loading branch information...
2 parents 36b95b1 + da5b162 commit 5f7ca3514e71000068f1afaaca55f2754ffec4bb @jnunemaker committed Apr 12, 2012
Showing with 42 additions and 3 deletions.
  1. +6 −2 lib/httparty.rb
  2. +1 −1 lib/httparty/request.rb
  3. +19 −0 spec/httparty/request_spec.rb
  4. +16 −0 spec/httparty_spec.rb
View
@@ -41,6 +41,8 @@ def self.included(base)
# [:+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 ClassMethods
#
# 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.
View
@@ -102,7 +102,7 @@ def attach_ssl_certificates(http)
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))
@@ -227,6 +227,25 @@
@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
View
@@ -39,6 +39,22 @@
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')

0 comments on commit 5f7ca35

Please sign in to comment.