spec/unit/connection_manager_spec.rb in websocket-rails-0.1.2 vs spec/unit/connection_manager_spec.rb in websocket-rails-0.1.3
- old
+ new
@@ -11,32 +11,23 @@
def open_connection
subject.call(env)
end
let(:connections) { subject.connections }
- let(:env) { Rack::MockRequest.env_for('/websocket') }
+ let(:dispatcher) { subject.dispatcher }
before(:each) do
- @mock_socket = ConnectionAdapters::Base.new(env)
+ ConnectionAdapters::Base.any_instance.stub(:send)
+ @mock_socket = ConnectionAdapters::Base.new(env,dispatcher)
ConnectionAdapters.stub(:establish_connection).and_return(@mock_socket)
- @dispatcher = double('dispatcher').as_null_object
- Dispatcher.stub(:new).and_return(@dispatcher)
end
context "new connections" do
it "should add one to the total connection count" do
expect { open_connection }.to change { connections.count }.by(1)
end
- it "should execute the :client_connected event" do
- @dispatcher.should_receive(:dispatch) do |event,data,connection|
- event.should == 'client_connected'
- connection.should == @mock_socket
- end
- open_connection
- end
-
it "should store the new connection in the @connections array" do
open_connection
connections.include?(@mock_socket).should be_true
end
@@ -45,78 +36,63 @@
end
end
context "new POST event" do
before(:each) do
- @mock_http = ConnectionAdapters::Http.new(env)
+ @mock_http = ConnectionAdapters::Http.new(env,dispatcher)
app.connections << @mock_http
end
it "should receive the new event for the correct connection" do
- @dispatcher.should_receive(:receive).with('data',@mock_http)
- post '/websocket', {:client_id => @mock_http.id, :data => 'data'}
+ @mock_http.should_receive(:on_message).with(encoded_message)
+ post '/websocket', {:client_id => @mock_http.id, :data => encoded_message}
end
end
context "open connections" do
before(:each) do
- ConnectionAdapters.stub(:establish_connection).and_return(@mock_socket,ConnectionAdapters::Base.new(env))
+ ConnectionAdapters.stub(:establish_connection).and_return(@mock_socket,ConnectionAdapters::Base.new(env,dispatcher))
4.times { open_connection }
end
context "when receiving a new event" do
- before(:all) { MockEvent = Struct.new(:data) }
- before(:each) { open_connection }
+ before(:each) { open_connection }
it "should dispatch the appropriate event through the Dispatcher" do
- mock_event = MockEvent.new(:new_message)
- @dispatcher.should_receive(:receive) do |event,connection|
- event.should == :new_message
- connection.should == @mock_socket
+ mock_event = ["new_message","data"].to_json
+ dispatcher.should_receive(:dispatch) do |event|
+ event.name.should == :new_message
+ event.data.should == "data"
+ event.connection.should == @mock_socket
end
- @mock_socket.onmessage(mock_event)
+ @mock_socket.on_message(mock_event)
end
end
- context "when closing" do
+ context "when closing" do
it "should remove the connection object from the @connections array" do
- @mock_socket.onclose
+ @mock_socket.on_close
connections.include?(@mock_socket).should be_false
end
it "should decrement the connection count by one" do
- expect { @mock_socket.onclose }.to change { connections.count }.by(-1)
+ expect { @mock_socket.on_close }.to change { connections.count }.by(-1)
end
it "should dispatch the :client_disconnected event" do
- @dispatcher.should_receive(:dispatch) do |event,data,connection|
- event.should == 'client_disconnected'
- connection.should == @mock_socket
+ dispatcher.should_receive(:dispatch) do |event|
+ event.name.should == :client_disconnected
+ event.connection.should == @mock_socket
end
- @mock_socket.onclose
+ @mock_socket.on_close
end
end
- context "when experiencing errors" do
- it "should dispatch the :client_error event" do
- @mock_socket.stub(:onclose)
- @dispatcher.should_receive(:dispatch) do |event,data,connection|
- event.should == 'client_error'
- connection.should == @mock_socket
- end
- @mock_socket.onerror
- end
-
- it "should execute the #onclose procedure on connection" do
- @mock_socket.should_receive(:onclose)
- @mock_socket.onerror
- end
- end
end
context "invalid connections" do
before(:each) do
- ConnectionAdapters.stub(:establish_connection).and_return(false)
+ ConnectionAdapters.stub(:establish_connection).and_raise(InvalidConnectionError)
end
it "should return a 400 bad request error code" do
open_connection.first.should == 400
end