summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2020-05-06 16:06:25 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-05-06 16:06:25 +0900
commit46b93175ed9fe061f309fe5538f23dc13aa1de0b (patch)
tree402f6acdd003fd9ad53ccb27ea9a9d47425fa1cc
parentde3f725978730d16ee51401d40fa75ac12c871a0 (diff)
Fakes IPSocket.getaddress in the whole method
To get rid of calling `getaddrinfo`, which may keep FDs internally.
-rw-r--r--test/uri/test_generic.rb49
1 files changed, 25 insertions, 24 deletions
diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb
index 90f779c88c..b449a0a0d1 100644
--- a/test/uri/test_generic.rb
+++ b/test/uri/test_generic.rb
@@ -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