require 'spec_helper' describe Hara::App do before :all do Class.new Hara::App do def init @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 after_process @states << :after_process end def on_close @states << :closed end def action_missing action, *args socket << [:action_missing, action, args] end define_action :hello do |msg| socket << "hello#{msg}" end def authentication socket.remote_ip == 'localhost' end def authentication_failed @states << :fail sleep 0.1 end def after_authentication @states << :success sleep 0.1 end def states @states end end end before :each do @socket = FayeSocket.new @socket.remote_ip = 'localhost' end after :each do if @app && @app.alive? @app.terminate! end end it 'authentication_failed' do @socket.remote_ip = '0.0.0.0' @app = Hara::Application.new @socket sleep 0.1 while @app.states.size == 0 @app.states.should == [:fail] end it 'authentication_succeeded' do @app = Hara::Application.new @socket sleep 0.1 while @app.states.size == 0 @app.states.should == [:success] end it 'remote call actions' do @socket.client_send({action: :hello, args: [' world']}.to_json) @app = Hara::Application.new @socket sleep 0.1 until msg = @socket.client_read msg.should == 'hello world' end it 'error remote call' do @socket.client_send("a error call") @app = Hara::Application.new @socket sleep 0.1 while @app.alive? msg = @socket.client_read msg.should == nil end it 'action_missing should work' do @socket.client_send({action: :hello_world, args: ['hello', 'world']}.to_json) @app = Hara::Application.new @socket sleep 0.1 until msg = @socket.client_read msg.should == [:action_missing, 'hello_world', ['hello', 'world']] end it 'callbacks should work' do @app = Hara::Application.new @socket @socket.client_send({action: :hello, args: [' world']}.to_json) states = @app.states sleep 0.2 @app.halt sleep 0.1 while @app.alive? states.should == [:success, [:before_action, "hello", [" world"]], [:after_action, "hello", [" world"]], :after_process,:closed] end end