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
Родитель de3f725978
Коммит 46b93175ed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
1 изменённых файлов: 25 добавлений и 24 удалений

Просмотреть файл

@ -865,34 +865,30 @@ class URI::TestGeneric < Test::Unit::TestCase
end end
def test_find_proxy_no_proxy 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| 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_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)) assert_nil(URI("http://192.0.2.2/").find_proxy(env))
getaddress = IPSocket.method(:getaddress) example_address = "192.0.2.1"
begin assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env))
class << IPSocket example_address = "192.0.2.2"
undef getaddress assert_nil(URI.parse("http://example.org").find_proxy(env))
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
} }
with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'example.org') {|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)) 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_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)) 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 end
def test_find_proxy_no_proxy_cidr def test_find_proxy_no_proxy_cidr