Created
August 22, 2010 14:10
-
-
Save igrep/543808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
module SocketSpecs | |
# helper to get the hostname associated to 127.0.0.1 | |
def self.hostname | |
# Calculate each time, without caching, since the result might | |
# depend on things like do_not_reverse_lookup mode, which is | |
# changing from test to test | |
Socket.getaddrinfo("127.0.0.1", nil)[0][2] | |
end | |
def self.hostnamev6 | |
Socket.getaddrinfo("::1", nil)[0][2] | |
end | |
def self.port | |
40001 | |
end | |
def self.str_port | |
"40001" | |
end | |
def self.sockaddr_in(port, host) | |
Socket::SockAddr_In.new(Socket.sockaddr_in(port, host)) | |
end | |
def self.socket_path | |
tmp("unix_server_spec.socket", false) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby -wKu | |
# vim: set fileencoding=utf-8 : | |
require 'socket-specs' #same as spec/ruby/library/socket/fixtures/classes.rb | |
describe "UDPSocket.bind" do | |
before :each do | |
@socket = UDPSocket.new | |
end | |
after :each do | |
@socket.close unless @socket.closed? | |
end | |
it "binds the socket to a port" do | |
@socket.bind(SocketSpecs.hostname, SocketSpecs.port) | |
lambda { @socket.bind(SocketSpecs.hostname, SocketSpecs.port) }.should raise_error | |
end | |
it "receives a hostname and a port" do | |
@socket.bind(SocketSpecs.hostname, SocketSpecs.port) | |
port, host = Socket.unpack_sockaddr_in(@socket.getsockname) | |
host.should == "127.0.0.1" | |
port.should == SocketSpecs.port | |
end | |
it "binds to INADDR_ANY if the hostname is empty" do | |
@socket.bind("", SocketSpecs.port) | |
port, host = Socket.unpack_sockaddr_in(@socket.getsockname) | |
host.should == "0.0.0.0" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment