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
|
require "spec_helper"
describe Faye::Transport do
before do
Faye.ensure_reactor_running!
end
let :dispatcher do
double(:dispatcher, :endpoint_for => URI.parse("http://example.com/"),
:endpoint => URI.parse("http://example.com/"),
:max_request_size => 2048,
:cookies => CookieJar::Jar.new,
:headers => {},
:proxy => {},
:tls => { :verify_peer => true },
:transports => {},
:ws_extensions => [])
end
describe :get do
before do
Faye::Transport::Local.stub(:usable?).and_yield(false)
Faye::Transport::Http.stub(:usable?).and_yield(false)
end
def get_transport(connection_types)
transport = nil
Faye::Transport.get(dispatcher, connection_types, []) { |t| transport = t }
transport
end
let(:transport) { get_transport ["long-polling", "in-process"] }
let(:local_transport) { get_transport ["in-process"] }
let(:http_transport) { get_transport ["long-polling"] }
describe "when no transport is usable" do
it "raises an exception" do
lambda { transport }.should raise_error
end
end
describe "when a less preferred transport is usable" do
before do
Faye::Transport::Http.stub(:usable?).and_yield(true)
end
it "returns a transport of the usable type" do
transport.should be_kind_of(Faye::Transport::Http)
end
it "rasies an exception of the usable type is not requested" do
lambda { local_transport }.should raise_error
end
it "allows the usable type to be specifically selected" do
http_transport.should be_kind_of(Faye::Transport::Http)
end
end
describe "when all transports are usable" do
before do
Faye::Transport::Local.stub(:usable?).and_yield(true)
Faye::Transport::Http.stub(:usable?).and_yield(true)
end
it "returns the most preferred type" do
transport.should be_kind_of(Faye::Transport::Local)
end
it "does not return disabled types" do
Faye::Transport.get dispatcher, ["long-polling", "in-process"], ["in-process"] do |t|
t.should be_kind_of(Faye::Transport::Http)
end
end
it "allows types to be specifically selected" do
local_transport.should be_kind_of(Faye::Transport::Local)
http_transport.should be_kind_of(Faye::Transport::Http)
end
end
end
describe :send_message do
include RSpec::EM::FakeClock
before { clock.stub }
after { clock.reset }
before do
dispatcher.stub(:client_id).and_return("abc123")
end
def send_message(message)
@transport.send_message(message)
end
describe "for batching transports" do
before do
transport_klass = Class.new(Faye::Transport) do
def batching?
true
end
end
@transport = transport_klass.new(dispatcher, dispatcher.endpoint)
end
it "does not make an immediate request" do
@transport.should_not_receive(:request)
send_message({ "batch" => "me" })
end
it "queues the message to be sent after a timeout" do
@transport.should_receive(:request).with([{ "batch" => "me" }]).once
send_message({ "batch" => "me" })
clock.tick(0.01)
end
it "allows multiple messages to be batched together" do
@transport.should_receive(:request).with([{ "id" => 1 }, { "id" => 2 }]).once
send_message({ "id" => 1 })
send_message({ "id" => 2 })
clock.tick(0.01)
end
it "adds advice to connect messages sent with others" do
@transport.should_receive(:request).with([{ "channel" => "/meta/connect", "advice" => { "timeout" => 0 }}, {}]).once
send_message({ "channel" => "/meta/connect" })
send_message({})
clock.tick(0.01)
end
it "adds no advice to connect messages sent alone" do
@transport.should_receive(:request).with([{ "channel" => "/meta/connect" }]).once
send_message({ "channel" => "/meta/connect" })
clock.tick(0.01)
end
end
describe "for non-batching transports" do
before do
transport_klass = Class.new(Faye::Transport) do
def batching?
false
end
end
@transport = transport_klass.new(dispatcher, dispatcher.endpoint)
end
it "makes a request immediately" do
@transport.should_receive(:request).with([{ "no" => "batch" }]).once
send_message({ "no" => "batch" })
clock.tick(0.01)
end
end
end
end
|