1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fakes IPSocket.getaddress in the whole method

To get rid of calling `getaddrinfo`, which may keep FDs
internally.
This commit is contained in:
Nobuyoshi Nakada 2020-05-06 16:06:25 +09:00
parent de3f725978
commit 46b93175ed
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -865,34 +865,30 @@ class URI::TestGeneric < Test::Unit::TestCase
end
def test_find_proxy_no_proxy
getaddress = IPSocket.method(:getaddress)
example_address = nil
IPSocket.singleton_class.class_eval do
undef getaddress
define_method(:getaddress) do |host|
case host
when "example.org", "www.example.org"
example_address
when /\A\d+(?:\.\d+){3}\z/
host
else
raise host
end
end
end
with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {|env|
assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
assert_nil(URI("http://192.0.2.2/").find_proxy(env))
getaddress = IPSocket.method(:getaddress)
begin
class << IPSocket
undef getaddress
def getaddress(host)
host == "example.org" or raise
"192.0.2.1"
end
end
assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env))
class << IPSocket
undef getaddress
def getaddress(host)
host == "example.org" or raise
"192.0.2.2"
end
end
assert_nil(URI.parse("http://example.org").find_proxy(env))
ensure
IPSocket.singleton_class.class_eval do
undef getaddress
define_method(:getaddress, getaddress)
end
end
example_address = "192.0.2.1"
assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env))
example_address = "192.0.2.2"
assert_nil(URI.parse("http://example.org").find_proxy(env))
}
with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'example.org') {|env|
assert_nil(URI("http://example.org/").find_proxy(env))
@ -902,6 +898,11 @@ class URI::TestGeneric < Test::Unit::TestCase
assert_equal(URI('http://127.0.0.1:8080'), URI("http://example.org/").find_proxy(env))
assert_nil(URI("http://www.example.org/").find_proxy(env))
}
ensure
IPSocket.singleton_class.class_eval do
undef getaddress
define_method(:getaddress, getaddress)
end
end
def test_find_proxy_no_proxy_cidr