require 'spec_helper' describe Hara::App do before :all do Class.new do include Hara::App def after_connect @states = [] end def before_action action, *args @states << [:before_action, action, args] end def after_action action, *args @states << [:after_action, action, args] end def on_close close_info @states << :closed end def action_missing action, *args send_msg [:action_missing, action, args] end define_action :hello do |msg| send_msg "hello#{msg}" end define_action :exit do close 3333, "Bye" end def states @states end end end before :each do @handshake = FakeHandshake.new @socket = FakeSocket.new @app = Hara::Application.new @handshake, @socket @socket.app = @app end after :each do if @app && @app.alive? @app.terminate! end end it 'client_infos' do @app.client_ip.should == '127.0.0.1' @app.client_port.should.is_a? Integer @app.headers.should.is_a? Hash end it 'remote call actions' do @socket.client_send(:hello, [' world']) msg = nil wait_until{msg = @socket.client_read} msg.should == 'hello world' end it 'close should close connection' do @socket.client_send(:exit, []) wait_until{!@app.alive?} @socket.alive?.should == false @socket.close_info.should == [3333, 'Bye'] end it 'error remote call' do @socket.client_send(:hello, []) wait_until{!@app.alive?} end it 'action_missing should work' do @socket.client_send(:hello_world, ['hello', 'world']) msg = nil wait_until{msg = @socket.client_read} msg.should == ['action_missing', 'hello_world', ['hello', 'world']] end it 'callbacks should work' do 2.times{ @socket.client_send(:hello, [' world'])} states = @app.states sleep 0.2 @app.terminate wait_until{!@app.alive?} action_callbacks = [[:before_action, "hello", [" world"]], [:after_action, "hello", [" world"]]] states.should == [*(action_callbacks * 2),:closed] end end