diff options
Diffstat (limited to 'spec/ruby/library/socket/socket/getnameinfo_spec.rb')
-rw-r--r-- | spec/ruby/library/socket/socket/getnameinfo_spec.rb | 94 |
1 files changed, 91 insertions, 3 deletions
diff --git a/spec/ruby/library/socket/socket/getnameinfo_spec.rb b/spec/ruby/library/socket/socket/getnameinfo_spec.rb index da6d5a46e5..394a90cb47 100644 --- a/spec/ruby/library/socket/socket/getnameinfo_spec.rb +++ b/spec/ruby/library/socket/socket/getnameinfo_spec.rb @@ -1,8 +1,6 @@ -require_relative '../../../spec_helper' +require_relative '../spec_helper' require_relative '../fixtures/classes' -require 'socket' - describe "Socket.getnameinfo" do before :each do @reverse_lookup = BasicSocket.do_not_reverse_lookup @@ -62,5 +60,95 @@ describe "Socket.getnameinfo" do name_info = Socket.getnameinfo ["AF_INET", 9, 'foo', '127.0.0.1'] name_info[1].should == 'discard' end +end + +describe 'Socket.getnameinfo' do + describe 'using a String as the first argument' do + before do + @addr = Socket.sockaddr_in(80, '127.0.0.1') + end + + it 'raises SocketError when using an invalid String' do + lambda { Socket.getnameinfo('cats') }.should raise_error(SocketError) + end + + describe 'without custom flags' do + it 'returns an Array containing the hostname and service name' do + Socket.getnameinfo(@addr).should == [SocketSpecs.hostname_reverse_lookup, 'http'] + end + end + + describe 'using NI_NUMERICHOST as the flag' do + it 'returns an Array containing the numeric hostname and service name' do + array = Socket.getnameinfo(@addr, Socket::NI_NUMERICHOST) + + %w{127.0.0.1 ::1}.include?(array[0]).should == true + + array[1].should == 'http' + end + end + end + + SocketSpecs.each_ip_protocol do |family, ip_address, family_name| + before do + @hostname = SocketSpecs.hostname_reverse_lookup(ip_address) + end + describe 'using a 3 element Array as the first argument' do + before do + @addr = [family_name, 80, @hostname] + end + + it 'raises ArgumentError when using an invalid Array' do + lambda { Socket.getnameinfo([family_name]) }.should raise_error(ArgumentError) + end + + describe 'without custom flags' do + it 'returns an Array containing the hostname and service name' do + array = Socket.getnameinfo(@addr) + array.should be_an_instance_of(Array) + array[0].should include(@hostname) + array[1].should == 'http' + end + end + + platform_is_not :windows do + describe 'using NI_NUMERICHOST as the flag' do + it 'returns an Array containing the numeric hostname and service name' do + Socket.getnameinfo(@addr, Socket::NI_NUMERICHOST).should == [ip_address, 'http'] + end + end + end + end + + describe 'using a 4 element Array as the first argument' do + before do + @addr = [family_name, 80, ip_address, ip_address] + end + + describe 'without custom flags' do + it 'returns an Array containing the hostname and service name' do + array = Socket.getnameinfo(@addr) + array.should be_an_instance_of(Array) + array[0].should == @hostname + array[1].should == 'http' + end + + it 'uses the 3rd value as the hostname if the 4th is not present' do + addr = [family_name, 80, ip_address, nil] + + array = Socket.getnameinfo(addr) + array.should be_an_instance_of(Array) + array[0].should == @hostname + array[1].should == 'http' + end + end + + describe 'using NI_NUMERICHOST as the flag' do + it 'returns an Array containing the numeric hostname and service name' do + Socket.getnameinfo(@addr, Socket::NI_NUMERICHOST).should == [ip_address, 'http'] + end + end + end + end end |