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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
require "spec_helper"
describe "server connect" do
let(:engine) { double "engine" }
let(:server) { Faye::Server.new }
before do
Faye::Engine.stub(:get).and_return engine
end
describe :connect do
let(:client_id) { "fakeclientid" }
let(:message) { { "channel" => "/meta/connect",
"clientId" => "fakeclientid",
"connectionType" => "long-polling"
} }
describe "with valid paramters" do
before do
message["advice"] = { "timeout" => 60 }
engine.should_receive(:client_exists).with(client_id).and_yield true
end
it "connects to the engine to wait for new messages" do
engine.should_receive(:connect).with(client_id, { "timeout" => 60 })
server.connect(message) {}
end
it "returns a successful response and any queued messages" do
engine.stub(:connect).and_yield([{ "channel" => "/x", "data" => "hello" }])
server.connect(message) do |response|
response.should == [
{ "channel" => "/meta/connect",
"successful" => true,
"clientId" => client_id
}, {
"channel" => "/x",
"data" => "hello"
}
]
end
end
describe "with a message id" do
before { message["id"] = "foo" }
it "returns the same id" do
engine.stub(:connect)
server.connect(message) do |response|
response.should == {
"channel" => "/meta/connect",
"successful" => true,
"clientId" => client_id,
"id" => "foo"
}
end
end
end
end
describe "with an unknown client" do
before do
engine.should_receive(:client_exists).with(client_id).and_yield false
end
it "does not connect to the engine" do
engine.should_not_receive(:connect)
server.connect(message) {}
end
it "returns an unsuccessful response" do
server.connect(message) do |response|
response.should == {
"channel" => "/meta/connect",
"successful" => false,
"error" => "401:fakeclientid:Unknown client"
}
end
end
end
describe "missing clientId" do
before do
message.delete("clientId")
engine.should_receive(:client_exists).with(nil).and_yield false
end
it "does not connect to the engine" do
engine.should_not_receive(:connect)
server.connect(message) {}
end
it "returns an unsuccessful response" do
server.connect(message) do |response|
response.should == {
"channel" => "/meta/connect",
"successful" => false,
"error" => "402:clientId:Missing required parameter"
}
end
end
end
describe "missing connectionType" do
before do
message.delete("connectionType")
engine.should_receive(:client_exists).with(client_id).and_yield true
end
it "does not connect to the engine" do
engine.should_not_receive(:connect)
server.connect(message) {}
end
it "returns an unsuccessful response" do
server.connect(message) do |response|
response.should == {
"channel" => "/meta/connect",
"successful" => false,
"error" => "402:connectionType:Missing required parameter"
}
end
end
end
describe "with an unknown connectionType" do
before do
message["connectionType"] = "flash"
engine.should_receive(:client_exists).with(client_id).and_yield true
end
it "does not connect to the engine" do
engine.should_not_receive(:connect)
server.connect(message) {}
end
it "returns an unsuccessful response" do
server.connect(message) do |response|
response.should == {
"channel" => "/meta/connect",
"successful" => false,
"error" => "301:flash:Connection types not supported"
}
end
end
end
describe "with an error" do
before do
message["error"] = "invalid"
engine.should_receive(:client_exists).with(client_id).and_yield true
end
it "does not connect to the engine" do
engine.should_not_receive(:connect)
server.connect(message) {}
end
it "returns an unsuccessful response" do
server.connect(message) do |response|
response.should == {
"channel" => "/meta/connect",
"successful" => false,
"error" => "invalid"
}
end
end
end
end
end
|