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

Add support for local host and port binding #238

Merged
merged 3 commits into from
Sep 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def self.included(base)
# [:+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.
# [:+local_host:] Local address to bind to before connecting.
# [:+local_port:] Local port to bind to before connecting.
#
# There are also another set of options with names corresponding to various class methods. The methods in question are those that let you set a class-wide default, and the options override the defaults on a request-by-request basis. Those options are:
# * :+base_uri+: see HTTParty::ClassMethods.base_uri.
Expand Down
19 changes: 19 additions & 0 deletions lib/httparty/connection_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ def connection
http.ciphers = options[:ciphers]
end

# Bind to a specific local address or port
#
# @see https://bugs.ruby-lang.org/issues/6617
if options[:local_host]
if RUBY_VERSION >= "2.0.0"
http.local_host = options[:local_host]
else
Kernel.warn("Warning: option :local_host requires Ruby version 2.0 or later")
end
end

if options[:local_port]
if RUBY_VERSION >= "2.0.0"
http.local_port = options[:local_port]
else
Kernel.warn("Warning: option :local_port requires Ruby version 2.0 or later")
end
end

return http
end

Expand Down
7 changes: 7 additions & 0 deletions spec/httparty/connection_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@
end
end

context 'when providing a local bind address and port' do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be wrapped with the ruby version if statement as well.

let(:options) { {:local_host => "127.0.0.1", :local_port => 12345 } }

its(:local_host) { should == '127.0.0.1' }
its(:local_port) { should == 12345 }
end if RUBY_VERSION >= '2.0'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. 😏

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah. Missed it. Thanks.


context "when providing PEM certificates" do
let(:pem) { :pem_contents }
let(:options) { {:pem => pem, :pem_password => "password"} }
Expand Down