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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
require "spec_helper"
describe Faye::Client do
let :dispatcher do
uri = URI("http://localhost/bayeux")
dispatcher = double(:dispatcher,
:endpoint => uri,
:connection_type => "fake-transport",
:connection_types => ["fake-transport", "another-transport"],
:retry => 5,
:select_transport => nil,
:send_message => nil)
class << dispatcher
attr_accessor :client_id, :timeout
include Faye::Publisher
end
Faye::Publisher.instance_method(:initialize).bind(dispatcher).call
dispatcher.client_id = "fakeid"
dispatcher
end
before do
Faye::Dispatcher.stub(:new).and_return(dispatcher)
EM.stub(:add_timer)
end
def stub_response(response)
dispatcher.stub(:send_message) do |message|
response["id"] = message["id"]
@client.__send__(:receive_message, response)
end
end
def create_client
@client = Faye::Client.new("http://localhost/")
end
def create_connected_client
create_client
stub_response "channel" => "/meta/handshake",
"successful" => true,
"version" => "1.0",
"supportedConnectionTypes" => ["websocket"],
"clientId" => "fakeid"
@client.handshake
end
def subscribe(client, channel, callback = nil)
stub_response "channel" => "/meta/subscribe",
"successful" => true,
"clientId" => "fakeid",
"subscription" => channel
@subs_called = 0
callback ||= lambda { |m| @subs_called = 1 }
@client.subscribe(channel, &callback)
end
describe :initialize do
it "puts the client in the UNCONNECTED state" do
client = Faye::Client.new("http://localhost/")
client.instance_eval { @state }.should == Faye::Client::UNCONNECTED
end
end
describe :handshake do
before { create_client }
it "creates a transport the server must support" do
dispatcher.should_receive(:select_transport).with(["long-polling", "callback-polling", "in-process"])
@client.handshake
end
it "sends a handshake message to the server" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/handshake",
"version" => "1.0",
"supportedConnectionTypes" => ["fake-transport", "another-transport"],
"id" => instance_of(String)
}, 72, {})
@client.handshake
end
it "puts the client in the CONNECTING state" do
@client.handshake
@client.instance_eval { @state }.should == Faye::Client::CONNECTING
end
describe "with an outgoing extension installed" do
before do
extension = Class.new do
def outgoing(message, callback)
message["ext"] = { "auth" => "password" }
callback.call(message)
end
end
@client.add_extension(extension.new)
end
it "passes the handshake message through the extension" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/handshake",
"version" => "1.0",
"supportedConnectionTypes" => ["fake-transport", "another-transport"],
"id" => instance_of(String),
"ext" => { "auth" => "password" }
}, 72, {})
@client.handshake
end
end
describe "on successful response" do
before do
stub_response "channel" => "/meta/handshake",
"successful" => true,
"version" => "1.0",
"supportedConnectionTypes" => ["long-polling", "websocket"],
"clientId" => "handshakeid"
end
it "stores the clientId" do
@client.handshake
dispatcher.client_id.should == "handshakeid"
end
it "puts the client in the CONNECTED state" do
@client.handshake
@client.instance_eval { @state }.should == Faye::Client::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
dispatcher.should_receive(:select_transport).with(["long-polling", "websocket"])
@client.handshake
end
end
describe "on unsuccessful response" do
before do
stub_response "channel" => "/meta/handshake",
"successful" => false,
"version" => "1.0",
"supportedConnectionTypes" => ["websocket"]
end
it "schedules a retry" do
EM.should_receive(:add_timer)
@client.handshake
end
it "puts the client in the UNCONNECTED state" do
@client.handshake
@client.instance_eval { @state }.should == Faye::Client::UNCONNECTED
end
end
describe "with existing subscriptions after a server restart" do
before do
create_connected_client
@message = nil
subscribe @client, "/messages/foo", lambda { |m| @message = m }
@client.__send__(:receive_message, "advice" => { "reconnect" => "handshake" })
stub_response "channel" => "/meta/handshake",
"successful" => true,
"version" => "1.0",
"supportedConnectionTypes" => ["websocket"],
"clientId" => "reconnectid"
end
it "resends the subscriptions to the server" do
dispatcher.should_receive(:send_message).with(hash_including("channel" => "/meta/handshake"), 72, {})
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/subscribe",
"clientId" => "reconnectid",
"subscription" => "/messages/foo",
"id" => instance_of(String)
}, 72, {})
@client.handshake
end
it "retains the listeners for the subscriptions" do
@client.handshake
@client.__send__(:receive_message, "channel" => "/messages/foo", "data" => "ok")
@message.should == "ok"
end
end
describe "with a connected client" do
before { create_connected_client }
it "does not send a handshake message to the server" do
dispatcher.should_not_receive(:send_message).with({
"channel" => "/meta/handshake",
"version" => "1.0",
"supportedConnectionTypes" => ["fake-transport", "another-transport"],
"id" => instance_of(String)
}, 72, {})
@client.handshake
end
end
end
describe :connect do
describe "with an unconnected client" do
before do
stub_response "channel" => "/meta/handshake",
"successful" => true,
"version" => "1.0",
"supportedConnectionTypes" => ["websocket"],
"clientId" => "handshakeid"
create_client
end
it "handshakes before connecting" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/connect",
"clientId" => "handshakeid",
"connectionType" => "fake-transport",
"id" => instance_of(String)
}, 72, {})
@client.connect
end
end
describe "with a connected client" do
before { create_connected_client }
it "sends a connect message to the server" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/connect",
"clientId" => "fakeid",
"connectionType" => "fake-transport",
"id" => instance_of(String)
}, 72, {})
@client.connect
end
it "only opens one connect request at a time" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/connect",
"clientId" => "fakeid",
"connectionType" => "fake-transport",
"id" => instance_of(String)
}, 72, {}).
exactly(1).
and_return(nil) # override stub implementation
@client.connect
@client.connect
end
end
end
describe :disconnect do
before { create_connected_client }
it "sends a disconnect message to the server" do
dispatcher.stub(:close)
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/disconnect",
"clientId" => "fakeid",
"id" => instance_of(String)
}, 72, {})
@client.disconnect
end
it "puts the client in the DISCONNECTED state" do
dispatcher.stub(:close)
@client.disconnect
@client.instance_eval { @state }.should == Faye::Client::DISCONNECTED
end
describe "on successful response" do
before do
stub_response "channel" => "/meta/disconnect",
"successful" => true,
"clientId" => "fakeid"
end
it "closes the dispatcher" do
dispatcher.should_receive(:close)
@client.disconnect
end
end
end
describe :subscribe do
before do
create_connected_client
@subscribe_message = {
"channel" => "/meta/subscribe",
"clientId" => "fakeid",
"subscription" => "/foo/*",
"id" => instance_of(String)
}
end
describe "with no prior subscriptions" do
it "sends a subscribe message to the server" do
dispatcher.should_receive(:send_message).with(@subscribe_message, 72, {})
@client.subscribe("/foo/*")
end
# The Bayeux spec says the server should accept a list of subscriptions
# in one message but the cometD server doesn't actually support this
describe "with an array of subscriptions" do
it "sends multiple subscribe messages" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/subscribe",
"clientId" => "fakeid",
"subscription" => "/foo",
"id" => instance_of(String)
}, 72, {})
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/subscribe",
"clientId" => "fakeid",
"subscription" => "/bar",
"id" => instance_of(String)
}, 72, {})
@client.subscribe(["/foo", "/bar"])
end
it "returns an array of subscriptions" do
subs = @client.subscribe(["/foo", "/bar"])
subs.size.should == 2
subs.should be_all { |s| Faye::Subscription === s }
end
end
describe "on successful response" do
before do
stub_response "channel" => "/meta/subscribe",
"successful" => true,
"clientId" => "fakeid",
"subscription" => "/foo/*"
end
it "sets up a listener for the subscribed channel" do
@message = nil
@client.subscribe("/foo/*") { |m| @message = m }
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => "hi")
@message.should == "hi"
end
it "yields the channel when requested" do
@message = nil
@client.subscribe("/foo/*").with_channel { |c, m| @message = [c, m] }
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => "hi")
@message.should == ["/foo/bar", "hi"]
end
it "does not call the listener for non-matching channels" do
@message = nil
@client.subscribe("/foo/*") { |m| @message = m }
@client.__send__(:receive_message, "channel" => "/bar", "data" => "hi")
@message.should be_nil
end
it "activates the subscription" do
active = false
@client.subscribe("/foo/*").callback { active = true }
active.should be true
end
describe "with an incoming extension installed" do
before do
extension = Class.new do
def incoming(message, callback)
message["data"]["changed"] = true if message["data"]
callback.call(message)
end
end
@client.add_extension(extension.new)
@message = nil
@client.subscribe("/foo/*") { |m| @message = m }
end
it "passes delivered messages through the extension" do
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => { "hello" => "there" })
@message.should == { "hello" => "there", "changed" => true }
end
end
describe "with an outgoing extension installed" do
before do
extension = Class.new do
def outgoing(message, callback)
message["data"]["changed"] = true if message["data"]
callback.call(message)
end
end
@client.add_extension(extension.new)
@message = nil
@client.subscribe("/foo/*") { |m| @message = m }
end
it "leaves messages unchanged" do
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => { "hello" => "there" })
@message.should == { "hello" => "there" }
end
end
describe "with an incoming extension that invalidates the response" do
before do
extension = Class.new do
def incoming(message, callback)
message["successful"] = false if message["channel"] == "/meta/subscribe"
callback.call(message)
end
end
@client.add_extension(extension.new)
end
it "does not set up a listener for the subscribed channel" do
@message = nil
@client.subscribe("/foo/*") { |m| @message = m }
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => "hi")
@message.should be_nil
end
it "does not activate the subscription" do
active = false
@client.subscribe("/foo/*").callback { active = true }
active.should be false
end
end
end
describe "on unsuccessful response" do
before do
stub_response "channel" => "/meta/subscribe",
"error" => "403:/meta/foo:Forbidden channel",
"successful" => false,
"clientId" => "fakeid",
"subscription" => "/meta/foo"
end
it "does not set up a listener for the subscribed channel" do
@message = nil
@client.subscribe("/meta/foo") { |m| @message = m }
@client.__send__(:receive_message, "channel" => "/meta/foo", "data" => "hi")
@message.should be_nil
end
it "does not activate the subscription" do
active = false
@client.subscribe("/meta/foo").callback { active = true }
active.should be false
end
it "reports the error through an errback" do
error = nil
@client.subscribe("/meta/foo").errback { |e| error = e }
error.code.should == 403
error.params.should == ["/meta/foo"]
error.message.should == "Forbidden channel"
end
end
end
describe "with an existing subscription" do
before do
subscribe @client, "/foo/*"
end
it "does not send another subscribe message to the server" do
dispatcher.should_not_receive(:send_message).with(@subscribe_message, 72, {})
@client.subscribe("/foo/*")
end
it "sets up another listener on the channel" do
@client.subscribe("/foo/*") { @subs_called += 1 }
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => "hi")
@subs_called.should == 2
end
it "activates the subscription" do
active = false
@client.subscribe("/foo/*").callback { active = true }
active.should be true
end
end
end
describe :unsubscribe do
before do
create_connected_client
@unsubscribe_message = {
"channel" => "/meta/unsubscribe",
"clientId" => "fakeid",
"subscription" => "/foo/*",
"id" => instance_of(String)
}
end
describe "with no subscriptions" do
it "does not send an unsubscribe message to the server" do
dispatcher.should_not_receive(:send_message).with(@unsubscribe_message, 72, {})
@client.unsubscribe("/foo/*")
end
end
describe "with a single subscription" do
before do
@message = nil
@subscription = subscribe @client, "/foo/*", lambda { |m| @message = m }
end
it "sends an unsubscribe message to the server" do
dispatcher.should_receive(:send_message).with(@unsubscribe_message, 72, {})
@client.unsubscribe("/foo/*")
end
it "removes the listener from the channel" do
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => "first")
@client.unsubscribe("/foo/*", @subscription)
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => "second")
@message.should == "first"
end
end
describe "with multiple subscriptions to the same channel" do
before do
@messages = []
@hey = subscribe @client, "/foo/*", lambda { |m| @messages << ("hey " + m["text"]) }
@bye = subscribe @client, "/foo/*", lambda { |m| @messages << ("bye " + m["text"]) }
end
it "removes one of the listeners from the channel" do
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => { "text" => "you" })
@client.unsubscribe("/foo/*", @hey)
@client.__send__(:receive_message, "channel" => "/foo/bar", "data" => { "text" => "you" })
@messages.should == ["hey you", "bye you", "bye you"]
end
it "does not send an unsubscribe message if one listener is removed" do
dispatcher.should_not_receive(:send_message).with(@unsubscribe_message, 72, {})
@client.unsubscribe("/foo/*", @bye)
end
it "sends an unsubscribe message if each listener is removed" do
dispatcher.should_receive(:send_message).with(@unsubscribe_message, 72, {})
@client.unsubscribe("/foo/*", @bye)
@client.unsubscribe("/foo/*", @hey)
end
it "sends an unsubscribe message if all listeners are removed" do
dispatcher.should_receive(:send_message).with(@unsubscribe_message, 72, {})
@client.unsubscribe("/foo/*")
end
end
describe "with multiple subscriptions to different channels" do
before do
subscribe @client, "/foo"
subscribe @client, "/bar"
end
it "sends multiple unsubscribe messages if given an array" do
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/unsubscribe",
"clientId" => "fakeid",
"subscription" => "/foo",
"id" => instance_of(String)
}, 72, {})
dispatcher.should_receive(:send_message).with({
"channel" => "/meta/unsubscribe",
"clientId" => "fakeid",
"subscription" => "/bar",
"id" => instance_of(String)
}, 72, {})
@client.unsubscribe(["/foo", "/bar"])
end
end
end
describe :publish do
before { create_connected_client }
it "sends the message to the server with an ID" do
dispatcher.should_receive(:send_message).with({
"channel" => "/messages/foo",
"clientId" => "fakeid",
"data" => { "hello" => "world" },
"id" => instance_of(String)
}, 72, {})
@client.publish("/messages/foo", "hello" => "world")
end
it "throws an error when publishing to an invalid channel" do
dispatcher.should_not_receive(:send_message).with(hash_including("channel" => "/messages/*"), 72, {})
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 "on receipt of the published message" do
before do
stub_response "channel" => "/messages/foo",
"data" => { "text" => "hi" },
"clientId" => "fakeid"
end
it "does not trigger the callbacks" do
published = false
publication = @client.publish("/messages/foo", "text" => "hi")
publication.callback { published = true }
publication.errback { published = true }
published.should be false
end
end
describe "with an outgoing extension installed" do
before do
extension = Class.new do
def outgoing(message, callback)
message["ext"] = { "auth" => "password" }
callback.call(message)
end
end
@client.add_extension(extension.new)
end
it "passes messages through the extension" do
dispatcher.should_receive(:send_message).with({
"channel" => "/messages/foo",
"clientId" => "fakeid",
"data" => { "hello" => "world" },
"id" => instance_of(String),
"ext" => { "auth" => "password" }
}, 72, {})
@client.publish("/messages/foo", "hello" => "world")
end
end
describe "with an incoming extension installed" do
before do
extension = Class.new do
def incoming(message, callback)
message["ext"] = { "auth" => "password" }
callback.call(message)
end
end
@client.add_extension(extension.new)
end
it "leaves the message unchanged" do
dispatcher.should_receive(:send_message).with({
"channel" => "/messages/foo",
"clientId" => "fakeid",
"data" => { "hello" => "world" },
"id" => instance_of(String)
}, 72, {})
@client.publish("/messages/foo", "hello" => "world")
end
end
end
end
|