Feature #6617 ยป net-http_bind_to_local_ip-tests.patch
test/net/http/test_http.rb | ||
---|---|---|
616 | 616 |
} |
617 | 617 |
end |
618 | 618 |
end |
619 |
|
|
620 |
class TestNetHTTPLocalBind < Test::Unit::TestCase |
|
621 |
CONFIG = { |
|
622 |
'host' => '127.0.0.1', |
|
623 |
'port' => 10081, |
|
624 |
'proxy_host' => nil, |
|
625 |
'proxy_port' => nil, |
|
626 |
} |
|
627 |
|
|
628 |
include TestNetHTTPUtils |
|
629 |
|
|
630 |
def test_bind_to_local_host |
|
631 |
@server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip } |
|
632 |
|
|
633 |
http = Net::HTTP.new(config('host'), config('port')) |
|
634 |
http.local_host = _select_local_ip_address |
|
635 |
assert_not_nil(http.local_host) |
|
636 |
assert_nil(http.local_port) |
|
637 |
|
|
638 |
res = http.get('/show_ip') |
|
639 |
assert_equal(http.local_host, res.body) |
|
640 |
end |
|
641 |
|
|
642 |
def test_bind_to_local_port |
|
643 |
@server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s } |
|
644 |
|
|
645 |
http = Net::HTTP.new(config('host'), config('port')) |
|
646 |
http.local_host = _select_local_ip_address |
|
647 |
http.local_port = [*10000..20000].shuffle.first.to_s |
|
648 |
assert_not_nil(http.local_host) |
|
649 |
assert_not_nil(http.local_port) |
|
650 |
|
|
651 |
res = http.get('/show_port') |
|
652 |
assert_equal(http.local_port, res.body) |
|
653 |
end |
|
654 |
|
|
655 |
def _select_local_ip_address |
|
656 |
Socket.ip_address_list.find { |addr| |
|
657 |
not addr.ipv4_loopback? and not addr.ipv4_multicast? |
|
658 |
}.ip_address |
|
659 |
end |
|
660 |
end |