From 468985371143fc0f1f86c60897fa64c22321c8fc Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 11 Jul 2012 18:37:18 +0000 Subject: * lib/net/http.rb (Net::HTTP#connect): use local_host and local_port if specified. patched by Ricardo Amorim [Feature #6617] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36367 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/net/http/test_http.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'test/net') diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 8b3928bfd1..0f57e0ce2f 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -616,3 +616,45 @@ class TestNetHTTPKeepAlive < Test::Unit::TestCase } end end + +class TestNetHTTPLocalBind < Test::Unit::TestCase + CONFIG = { + 'host' => '127.0.0.1', + 'port' => 10081, + 'proxy_host' => nil, + 'proxy_port' => nil, + } + + include TestNetHTTPUtils + + def test_bind_to_local_host + @server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip } + + http = Net::HTTP.new(config('host'), config('port')) + http.local_host = _select_local_ip_address + assert_not_nil(http.local_host) + assert_nil(http.local_port) + + res = http.get('/show_ip') + assert_equal(http.local_host, res.body) + end + + def test_bind_to_local_port + @server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s } + + http = Net::HTTP.new(config('host'), config('port')) + http.local_host = _select_local_ip_address + http.local_port = [*10000..20000].shuffle.first.to_s + assert_not_nil(http.local_host) + assert_not_nil(http.local_port) + + res = http.get('/show_port') + assert_equal(http.local_port, res.body) + end + + def _select_local_ip_address + Socket.ip_address_list.find { |addr| + not addr.ipv4_loopback? and not addr.ipv4_multicast? + }.ip_address + end +end -- cgit v1.2.3