Skip to content

Commit

Permalink
Add tests for #host and #port of SslAcceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed Apr 29, 2016
1 parent 7c7ea0f commit f63e459
Showing 1 changed file with 59 additions and 10 deletions.
69 changes: 59 additions & 10 deletions spec/ione/io/ssl_acceptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ module Ione
module Io
describe SslAcceptor do
let :acceptor do
described_class.new('example.com', 4321, backlog = 3, unblocker, reactor, ssl_context, socket_impl, ssl_socket_impl)
described_class.new(host, port, backlog = 3, unblocker, reactor, ssl_context, socket_impl, ssl_socket_impl)
end

let :host do
'example.com'
end

let :port do
4321
end

let :unblocker do
Expand Down Expand Up @@ -42,22 +50,30 @@ module Io
double(:client_socket)
end

let :local_address do
double(:local_address, ip_unpack: [host, port])
end

before do
socket_impl.stub(:getaddrinfo).and_return([[nil, 'PORT', nil, 'IP1', 'FAMILY1', 'TYPE1']])
socket_impl.stub(:sockaddr_in).with('PORT', 'IP1').and_return('SOCKADDRX')
socket_impl.stub(:unpack_sockaddr_in).with('SOCKADDRX').and_return([3333, 'example.com'])
socket_impl.stub(:new).and_return(socket)
socket.stub(:bind)
socket.stub(:listen)
socket.stub(:accept_nonblock).and_return([client_socket, 'SOCKADDRX'])
socket.stub(:local_address).and_return(local_address)
ssl_socket_impl.stub(:new).with(client_socket, ssl_context).and_return(ssl_socket)
ssl_socket.stub(:accept_nonblock)
end

describe '#read' do
let :accepted_handlers do
[]
end

before do
socket_impl.stub(:getaddrinfo).and_return([[nil, 'PORT', nil, 'IP1', 'FAMILY1', 'TYPE1']])
socket_impl.stub(:sockaddr_in).with('PORT', 'IP1').and_return('SOCKADDRX')
socket_impl.stub(:unpack_sockaddr_in).with('SOCKADDRX').and_return([3333, 'example.com'])
socket_impl.stub(:new).and_return(socket)
socket.stub(:bind)
socket.stub(:listen)
socket.stub(:accept_nonblock).and_return([client_socket, 'SOCKADDRX'])
reactor.stub(:accept) { |h| accepted_handlers << h }
ssl_socket_impl.stub(:new).with(client_socket, ssl_context).and_return(ssl_socket)
ssl_socket.stub(:accept_nonblock)
end

it 'accepts a new connection' do
Expand Down Expand Up @@ -111,6 +127,39 @@ module Io
end
end
end

describe '#host' do
let :local_address do
double(:local_address, ip_unpack: ['1.1.1.1', port])
end

it 'returns the host the server is listening on' do
acceptor.bind
acceptor.host.should eq('1.1.1.1')
end
end

describe '#port' do
it 'returns the port the server is listening on' do
acceptor.bind
acceptor.port.should eq(4321)
end

context 'when the requested port was 0' do
let :port do
0
end

let :local_address do
double(:local_address, ip_unpack: ['example.com', 65432])
end

it 'returns the port that was chosen for the server' do
acceptor.bind
acceptor.port.should eq(65432)
end
end
end
end
end
end

0 comments on commit f63e459

Please sign in to comment.