1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
require "spec_helper"
describe Faye::Server do
let(:engine) { double "engine" }
let(:server) { Faye::Server.new }
before do
Faye::Engine.stub(:get).and_return engine
end
describe :process do
let(:handshake) { { "channel" => "/meta/handshake", "data" => "handshake" } }
let(:connect) { { "channel" => "/meta/connect", "data" => "connect" } }
let(:disconnect) { { "channel" => "/meta/disconnect", "data" => "disconnect" } }
let(:subscribe) { { "channel" => "/meta/subscribe", "data" => "subscribe" } }
let(:unsubscribe) { { "channel" => "/meta/unsubscribe", "data" => "unsubscribe" } }
let(:publish) { { "channel" => "/some/channel", "data" => "publish" } }
before do
engine.stub(:interval).and_return(0)
engine.stub(:timeout).and_return(60)
end
it "returns an empty response for no messages" do
response = nil
server.process([], false) { |r| response = r }
response.should == []
end
it "ignores invalid messages" do
response = nil
server.process([{}, { "channel" => "invalid" }], false) { |r| response = r }
response.should == [
{ "successful" => false,
"error" => "402:data:Missing required parameter"
},
{ "channel" => "invalid",
"successful" => false,
"error" => "402:data:Missing required parameter"
}
]
end
it "rejects unknown meta channels" do
response = nil
server.process([{ "channel" => "/meta/p" }], false) { |r| response = r }
response.should == [
{ "channel" => "/meta/p",
"successful" => false,
"error" => "403:/meta/p:Forbidden channel"
}
]
end
it "routes single messages to appropriate handlers" do
server.should_receive(:handshake).with(handshake, false)
server.process(handshake, false) {}
end
it "routes a list of messages to appropriate handlers" do
server.should_receive(:handshake).with(handshake, false)
server.should_receive(:connect).with(connect, false)
server.should_receive(:disconnect).with(disconnect, false)
server.should_receive(:subscribe).with(subscribe, false)
server.should_receive(:unsubscribe).with(unsubscribe, false)
engine.should_not_receive(:publish).with(handshake)
engine.should_not_receive(:publish).with(connect)
engine.should_not_receive(:publish).with(disconnect)
engine.should_not_receive(:publish).with(subscribe)
engine.should_not_receive(:publish).with(unsubscribe)
engine.should_receive(:publish).with(publish)
server.process([handshake, connect, disconnect, subscribe, unsubscribe, publish], false)
end
describe "handshaking" do
before do
response = { "channel" => "/meta/handshake", "successful" => true }
server.should_receive(:handshake).with(handshake, false).and_yield(response)
end
it "returns the handshake response with advice" do
server.process(handshake, false) do |response|
response.should == [
{ "channel" => "/meta/handshake",
"successful" => true,
"advice" => { "reconnect" => "retry", "interval" => 0, "timeout" => 60000 }
}
]
end
end
end
describe "connecting for messages" do
let(:messages) { [{ "channel" => "/a" }, { "channel" => "/b" }] }
before do
server.should_receive(:connect).with(connect, false).and_yield(messages)
end
it "returns the new messages" do
server.process(connect, false) { |r| r.should == messages }
end
end
end
end
|