From f63e459407e3524aba3209930902ee0380b40590 Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 29 Apr 2016 08:35:09 +0200 Subject: [PATCH] Add tests for #host and #port of SslAcceptor --- spec/ione/io/ssl_acceptor_spec.rb | 69 ++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/spec/ione/io/ssl_acceptor_spec.rb b/spec/ione/io/ssl_acceptor_spec.rb index 3772c38..e9ab4bb 100644 --- a/spec/ione/io/ssl_acceptor_spec.rb +++ b/spec/ione/io/ssl_acceptor_spec.rb @@ -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 @@ -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 @@ -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