diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-07-11 18:37:18 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-07-11 18:37:18 +0000 |
commit | 468985371143fc0f1f86c60897fa64c22321c8fc (patch) | |
tree | 0442c3fd2e17fa510ad6ea948221224dbf1069fe /test/net | |
parent | dea582458af3ac34fcaad7fd8de46560c012fb3b (diff) | |
download | ruby-468985371143fc0f1f86c60897fa64c22321c8fc.tar.gz |
* 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
Diffstat (limited to 'test/net')
-rw-r--r-- | test/net/http/test_http.rb | 42 |
1 files changed, 42 insertions, 0 deletions
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 |