2018-08-03 12:19:40 -04:00
|
|
|
require_relative '../spec_helper'
|
|
|
|
require_relative '../fixtures/classes'
|
|
|
|
|
|
|
|
describe 'TCPServer#initialize' do
|
2018-09-25 06:41:16 -04:00
|
|
|
describe 'with a single Integer argument' do
|
2018-08-03 12:19:40 -04:00
|
|
|
before do
|
|
|
|
@server = TCPServer.new(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@server.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the port to the given argument' do
|
2018-09-25 06:41:16 -04:00
|
|
|
@server.local_address.ip_port.should be_kind_of(Integer)
|
2018-08-03 12:19:40 -04:00
|
|
|
@server.local_address.ip_port.should > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
platform_is_not :windows do
|
2018-08-08 23:32:51 -04:00
|
|
|
it 'sets the hostname to 0.0.0.0 or ::' do
|
|
|
|
a = @server.local_address
|
|
|
|
a.ip_address.should == (a.ipv6? ? '::' : '0.0.0.0')
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sets the socket to binmode" do
|
|
|
|
@server.binmode?.should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a single String argument containing a numeric value' do
|
|
|
|
before do
|
|
|
|
@server = TCPServer.new('0')
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@server.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the port to the given argument' do
|
2018-09-25 06:41:16 -04:00
|
|
|
@server.local_address.ip_port.should be_kind_of(Integer)
|
2018-08-03 12:19:40 -04:00
|
|
|
@server.local_address.ip_port.should > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
platform_is_not :windows do
|
2018-08-08 23:32:51 -04:00
|
|
|
it 'sets the hostname to 0.0.0.0 or ::' do
|
|
|
|
a = @server.local_address
|
|
|
|
a.ip_address.should == (a.ipv6? ? '::' : '0.0.0.0')
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a single String argument containing a non numeric value' do
|
|
|
|
it 'raises SocketError' do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { TCPServer.new('cats') }.should raise_error(SocketError)
|
2018-08-03 12:19:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-25 06:41:16 -04:00
|
|
|
describe 'with a String and an Integer' do
|
2018-08-03 12:19:40 -04:00
|
|
|
SocketSpecs.each_ip_protocol do |family, ip_address|
|
|
|
|
before do
|
|
|
|
@server = TCPServer.new(ip_address, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@server.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the port to the given port argument' do
|
2018-09-25 06:41:16 -04:00
|
|
|
@server.local_address.ip_port.should be_kind_of(Integer)
|
2018-08-03 12:19:40 -04:00
|
|
|
@server.local_address.ip_port.should > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the hostname to the given host argument' do
|
|
|
|
@server.local_address.ip_address.should == ip_address
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with a String and a custom object' do
|
|
|
|
before do
|
|
|
|
dummy = mock(:dummy)
|
|
|
|
dummy.stub!(:to_str).and_return('0')
|
|
|
|
|
|
|
|
@server = TCPServer.new('127.0.0.1', dummy)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@server.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the port to the given port argument' do
|
2018-09-25 06:41:16 -04:00
|
|
|
@server.local_address.ip_port.should be_kind_of(Integer)
|
2018-08-03 12:19:40 -04:00
|
|
|
@server.local_address.ip_port.should > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the hostname to the given host argument' do
|
|
|
|
@server.local_address.ip_address.should == '127.0.0.1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|