spec/app_spec.rb in hara-0.1.0 vs spec/app_spec.rb in hara-0.2.0

- old
+ new

@@ -1,109 +1,76 @@ require 'spec_helper' describe Hara::App do before :all do Class.new Hara::App do - def init - @states = [] + def after_connect + @states = [] end def before_action action, *args - @states << [:before_action, action, args] + @states << [:before_action, action, args] end def after_action action, *args - @states << [:after_action, action, args] + @states << [:after_action, action, args] end - def after_process - @states << :after_process - end - def on_close - @states << :closed + @states << :closed end def action_missing action, *args - socket << [:action_missing, action, args] + socket.send [:action_missing, action, args] end define_action :hello do |msg| - socket << "hello#{msg}" + socket.send "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 + @states end end end before :each do + @handshake = FayeHandshake.new @socket = FayeSocket.new - @socket.remote_ip = 'localhost' + @app = Hara::Application.new @handshake, @socket 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 + @app.process_msg(Hara.encode_msg(:hello, ' world')) 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 + @app.process_msg('a error call') 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 + @app.process_msg(Hara.encode_msg(:hello_world, 'hello', 'world')) 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) + 2.times{ @app.process_msg(Hara.encode_msg(:hello, ' world'))} states = @app.states sleep 0.2 - @app.halt + @app.terminate sleep 0.1 while @app.alive? - states.should == [:success, [:before_action, "hello", [" world"]], [:after_action, "hello", [" world"]], :after_process,:closed] + action_callbacks = [[:before_action, "hello", [" world"]], [:after_action, "hello", [" world"]]] + states.should == [*(action_callbacks * 2),:closed] end end