spec/ruby/client_spec.rb in faye-0.6.8 vs spec/ruby/client_spec.rb in faye-0.7.0
- old
+ new
@@ -3,10 +3,11 @@
describe Faye::Client do
let :transport do
transport = mock("transport")
transport.stub(:connection_type).and_return "fake"
transport.stub(:send)
+ transport.extend(Faye::Publisher)
transport
end
before { EM.stub(:add_timer) }
@@ -16,11 +17,11 @@
@client.receive_message(response)
end
end
def create_client
- Faye::Transport.stub(:get).and_return(transport)
+ Faye::Transport.stub(:get).and_yield(transport)
@client = Faye::Client.new("http://localhost/")
end
def create_connected_client
create_client
@@ -104,11 +105,11 @@
describe "on successful response" do
before do
stub_response "channel" => "/meta/handshake",
"successful" => true,
"version" => "1.0",
- "supportedConnectionTypes" => ["websocket"],
+ "supportedConnectionTypes" => ["long-polling", "websocket"],
"clientId" => "fakeid"
end
it "stores the clientId" do
@client.handshake
@@ -117,20 +118,30 @@
it "puts the client in the CONNECTED state" do
@client.handshake
@client.state.should == :CONNECTED
end
-
+
+ it "registers any pre-existing subscriptions" do
+ @client.should_receive(:subscribe).with([], true)
+ @client.handshake
+ end
+
it "selects a new transport based on what the server supports" do
- Faye::Transport.should_receive(:get).with(instance_of(Faye::Client), ["websocket"]).
+ Faye::Transport.should_receive(:get).with(instance_of(Faye::Client), ["long-polling", "websocket"]).
and_return(transport)
@client.handshake
end
-
- it "registers any pre-existing subscriptions" do
- @client.should_receive(:subscribe).with([], true)
- @client.handshake
+
+ describe "with websocket disabled" do
+ before { @client.disable("websocket") }
+
+ it "selects a new transport, excluding websocket" do
+ Faye::Transport.should_receive(:get).with(instance_of(Faye::Client), ["long-polling"]).
+ and_return(transport)
+ @client.handshake
+ end
end
end
describe "on unsuccessful response" do
before do
@@ -255,22 +266,37 @@
describe :disconnect do
before { create_connected_client }
it "sends a disconnect message to the server" do
+ transport.stub(:close)
transport.should_receive(:send).with({
"channel" => "/meta/disconnect",
"clientId" => "fakeid",
"id" => instance_of(String)
}, 60)
@client.disconnect
end
it "puts the client in the DISCONNECTED state" do
+ transport.stub(:close)
@client.disconnect
@client.state.should == :DISCONNECTED
end
+
+ describe "on successful response" do
+ before do
+ stub_response "channel" => "/meta/disconnect",
+ "successful" => true,
+ "clientId" => "fakeid"
+ end
+
+ it "closes the transport" do
+ transport.should_receive(:close)
+ @client.disconnect
+ end
+ end
end
describe :subscribe do
before do
create_connected_client
@@ -565,10 +591,33 @@
it "throws an error when publishing to an invalid channel" do
transport.should_not_receive(:send).with(hash_including("channel" => "/messages/*"), 60)
lambda { @client.publish("/messages/*", "hello" => "world") }.should raise_error
end
+ describe "on publish failure" do
+ before do
+ stub_response "channel" => "/messages/foo",
+ "error" => "407:/messages/foo:Failed to publish",
+ "successful" => false,
+ "clientId" => "fakeid"
+ end
+
+ it "should not be published" do
+ published = false
+ @client.publish("/messages/foo", "text" => "hi").callback { published = true }
+ published.should be_false
+ end
+
+ it "reports the error through an errback" do
+ error = nil
+ @client.publish("/messages/foo", "text" => "hi").errback { |e| error = e }
+ error.code.should == 407
+ error.params.should == ["/messages/foo"]
+ error.message.should == "Failed to publish"
+ end
+ end
+
describe "with an outgoing extension installed" do
before do
extension = Class.new do
def outgoing(message, callback)
message["ext"] = {"auth" => "password"}
@@ -607,9 +656,53 @@
"clientId" => "fakeid",
"data" => {"hello" => "world"},
"id" => instance_of(String)
}, 60)
@client.publish("/messages/foo", "hello" => "world")
+ end
+ end
+ end
+
+ describe "network notifications" do
+ before { create_client }
+
+ describe "in the default state" do
+ it "broadcasts a down notification" do
+ @client.should_receive(:trigger).with("transport:down")
+ transport.trigger(:down)
+ end
+
+ it "broadcasts an up notification" do
+ @client.should_receive(:trigger).with("transport:up")
+ transport.trigger(:up)
+ end
+ end
+
+ describe "when the transport is up" do
+ before { transport.trigger(:up) }
+
+ it "broadcasts a down notification" do
+ @client.should_receive(:trigger).with("transport:down")
+ transport.trigger(:down)
+ end
+
+ it "does not broadcast an up notification" do
+ @client.should_not_receive(:trigger)
+ transport.trigger(:up)
+ end
+ end
+
+ describe "when the transport is down" do
+ before { transport.trigger(:down) }
+
+ it "does not broadcast a down notification" do
+ @client.should_not_receive(:trigger)
+ transport.trigger(:down)
+ end
+
+ it "broadcasts an up notification" do
+ @client.should_receive(:trigger).with("transport:up")
+ transport.trigger(:up)
end
end
end
end