1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
require "spec"
require "socket"
describe Socket::Addrinfo, tags: "network" do
describe ".resolve" do
it "returns an array" do
addrinfos = Socket::Addrinfo.resolve("localhost", 80, type: Socket::Type::STREAM)
typeof(addrinfos).should eq(Array(Socket::Addrinfo))
addrinfos.size.should_not eq(0)
end
it "yields each result" do
Socket::Addrinfo.resolve("localhost", 80, type: Socket::Type::DGRAM) do |addrinfo|
typeof(addrinfo).should eq(Socket::Addrinfo)
end
end
it "eventually raises returned error" do
expect_raises(Socket::Error) do
Socket::Addrinfo.resolve("localhost", 80, type: Socket::Type::DGRAM) do |addrinfo|
Socket::Error.new("please fail")
end
end
end
it "raises helpful message on getaddrinfo failure" do
expect_raises(Socket::Addrinfo::Error, "Hostname lookup for badhostname failed: ") do
Socket::Addrinfo.resolve("badhostname", 80, type: Socket::Type::DGRAM)
end
end
{% if flag?(:win32) %}
it "raises timeout error" do
expect_raises(IO::TimeoutError) do
Socket::Addrinfo.resolve("badhostname", 80, type: Socket::Type::STREAM, timeout: 0.milliseconds)
end
end
{% end %}
end
describe ".tcp" do
it "returns an array" do
addrinfos = Socket::Addrinfo.tcp("localhost", 80)
typeof(addrinfos).should eq(Array(Socket::Addrinfo))
addrinfos.size.should_not eq(0)
end
it "yields each result" do
Socket::Addrinfo.tcp("localhost", 80) do |addrinfo|
typeof(addrinfo).should eq(Socket::Addrinfo)
end
end
{% if flag?(:win32) %}
it "raises timeout error" do
expect_raises(IO::TimeoutError) do
Socket::Addrinfo.tcp("badhostname", 80, timeout: 0.milliseconds)
end
end
{% end %}
end
describe ".udp" do
it "returns an array" do
addrinfos = Socket::Addrinfo.udp("localhost", 80)
typeof(addrinfos).should eq(Array(Socket::Addrinfo))
addrinfos.size.should_not eq(0)
end
it "yields each result" do
Socket::Addrinfo.udp("localhost", 80) do |addrinfo|
typeof(addrinfo).should eq(Socket::Addrinfo)
end
end
{% if flag?(:win32) %}
it "raises timeout error" do
expect_raises(IO::TimeoutError) do
Socket::Addrinfo.udp("badhostname", 80, timeout: 0.milliseconds)
end
end
{% end %}
end
describe "#ip_address" do
it do
addrinfos = Socket::Addrinfo.udp("localhost", 80)
typeof(addrinfos.first.ip_address).should eq(Socket::IPAddress)
end
end
it "#inspect" do
addrinfos = Socket::Addrinfo.tcp("127.0.0.1", 12345)
addrinfos.first.inspect.should eq "Socket::Addrinfo(127.0.0.1:12345, INET, STREAM, TCP)"
addrinfos = Socket::Addrinfo.udp("127.0.0.1", 12345)
addrinfos.first.inspect.should eq "Socket::Addrinfo(127.0.0.1:12345, INET, DGRAM, UDP)"
end
describe "Error" do
{% unless flag?(:win32) || flag?(:wasm32) %}
# This method is not available on windows/wasm because windows/wasm support was introduced after deprecation.
it ".new (deprecated)" do
error = Socket::Addrinfo::Error.new(LibC::EAI_NONAME, "No address found", "foobar.com")
error.os_error.should eq Errno.new(LibC::EAI_NONAME)
error.message.not_nil!.should eq "Hostname lookup for foobar.com failed: No address found"
end
{% end %}
end
end
|