bushido-faye 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +247 -0
- data/README.rdoc +92 -0
- data/lib/faye-browser-min.js +1 -0
- data/lib/faye.rb +121 -0
- data/lib/faye/adapters/rack_adapter.rb +209 -0
- data/lib/faye/engines/connection.rb +60 -0
- data/lib/faye/engines/memory.rb +112 -0
- data/lib/faye/engines/proxy.rb +111 -0
- data/lib/faye/error.rb +49 -0
- data/lib/faye/mixins/logging.rb +47 -0
- data/lib/faye/mixins/publisher.rb +30 -0
- data/lib/faye/mixins/timeouts.rb +22 -0
- data/lib/faye/protocol/channel.rb +124 -0
- data/lib/faye/protocol/client.rb +378 -0
- data/lib/faye/protocol/extensible.rb +43 -0
- data/lib/faye/protocol/grammar.rb +58 -0
- data/lib/faye/protocol/publication.rb +5 -0
- data/lib/faye/protocol/server.rb +282 -0
- data/lib/faye/protocol/subscription.rb +24 -0
- data/lib/faye/transport/http.rb +76 -0
- data/lib/faye/transport/local.rb +22 -0
- data/lib/faye/transport/transport.rb +115 -0
- data/lib/faye/transport/web_socket.rb +99 -0
- data/lib/faye/util/namespace.rb +20 -0
- data/spec/browser.html +45 -0
- data/spec/encoding_helper.rb +7 -0
- data/spec/install.sh +78 -0
- data/spec/javascript/channel_spec.js +15 -0
- data/spec/javascript/client_spec.js +714 -0
- data/spec/javascript/engine/memory_spec.js +7 -0
- data/spec/javascript/engine_spec.js +417 -0
- data/spec/javascript/faye_spec.js +15 -0
- data/spec/javascript/grammar_spec.js +66 -0
- data/spec/javascript/node_adapter_spec.js +307 -0
- data/spec/javascript/publisher_spec.js +27 -0
- data/spec/javascript/server/connect_spec.js +168 -0
- data/spec/javascript/server/disconnect_spec.js +121 -0
- data/spec/javascript/server/extensions_spec.js +60 -0
- data/spec/javascript/server/handshake_spec.js +145 -0
- data/spec/javascript/server/integration_spec.js +124 -0
- data/spec/javascript/server/publish_spec.js +85 -0
- data/spec/javascript/server/subscribe_spec.js +247 -0
- data/spec/javascript/server/unsubscribe_spec.js +245 -0
- data/spec/javascript/server_spec.js +110 -0
- data/spec/javascript/transport_spec.js +130 -0
- data/spec/node.js +55 -0
- data/spec/phantom.js +17 -0
- data/spec/ruby/channel_spec.rb +17 -0
- data/spec/ruby/client_spec.rb +724 -0
- data/spec/ruby/engine/memory_spec.rb +7 -0
- data/spec/ruby/engine_examples.rb +427 -0
- data/spec/ruby/faye_spec.rb +14 -0
- data/spec/ruby/grammar_spec.rb +68 -0
- data/spec/ruby/publisher_spec.rb +27 -0
- data/spec/ruby/rack_adapter_spec.rb +236 -0
- data/spec/ruby/server/connect_spec.rb +170 -0
- data/spec/ruby/server/disconnect_spec.rb +120 -0
- data/spec/ruby/server/extensions_spec.rb +68 -0
- data/spec/ruby/server/handshake_spec.rb +143 -0
- data/spec/ruby/server/integration_spec.rb +126 -0
- data/spec/ruby/server/publish_spec.rb +81 -0
- data/spec/ruby/server/subscribe_spec.rb +247 -0
- data/spec/ruby/server/unsubscribe_spec.rb +247 -0
- data/spec/ruby/server_spec.rb +110 -0
- data/spec/ruby/transport_spec.rb +134 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/testswarm +29 -0
- data/spec/thin_proxy.rb +37 -0
- metadata +302 -0
@@ -0,0 +1,120 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "server disconnect" do
|
4
|
+
let(:engine) { mock "engine" }
|
5
|
+
let(:server) { Faye::Server.new }
|
6
|
+
|
7
|
+
before do
|
8
|
+
Faye::Engine.stub(:get).and_return engine
|
9
|
+
end
|
10
|
+
|
11
|
+
describe :disconnect do
|
12
|
+
let(:client_id) { "fakeclientid" }
|
13
|
+
let(:message) {{"channel" => "/meta/disconnect",
|
14
|
+
"clientId" => "fakeclientid"
|
15
|
+
}}
|
16
|
+
|
17
|
+
describe "with valid parameters" do
|
18
|
+
before do
|
19
|
+
engine.should_receive(:client_exists).with(client_id).and_yield true
|
20
|
+
end
|
21
|
+
|
22
|
+
it "destroys the client" do
|
23
|
+
engine.should_receive(:destroy_client).with(client_id)
|
24
|
+
server.disconnect(message) {}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns a successful response" do
|
28
|
+
engine.stub(:destroy_client)
|
29
|
+
server.disconnect(message) do |response|
|
30
|
+
response.should == {
|
31
|
+
"channel" => "/meta/disconnect",
|
32
|
+
"successful" => true,
|
33
|
+
"clientId" => client_id
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "with a message id" do
|
39
|
+
before { message["id"] = "foo" }
|
40
|
+
|
41
|
+
it "returns the same id" do
|
42
|
+
engine.stub(:destroy_client)
|
43
|
+
server.disconnect(message) do |response|
|
44
|
+
response.should == {
|
45
|
+
"channel" => "/meta/disconnect",
|
46
|
+
"successful" => true,
|
47
|
+
"clientId" => client_id,
|
48
|
+
"id" => "foo"
|
49
|
+
}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "with an unknown client" do
|
56
|
+
before do
|
57
|
+
engine.should_receive(:client_exists).with(client_id).and_yield false
|
58
|
+
end
|
59
|
+
|
60
|
+
it "does not destroy the client" do
|
61
|
+
engine.should_not_receive(:destroy_client)
|
62
|
+
server.disconnect(message) {}
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns an unsuccessful response" do
|
66
|
+
server.disconnect(message) do |response|
|
67
|
+
response.should == {
|
68
|
+
"channel" => "/meta/disconnect",
|
69
|
+
"successful" => false,
|
70
|
+
"error" => "401:fakeclientid:Unknown client"
|
71
|
+
}
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "missing clientId" do
|
77
|
+
before do
|
78
|
+
message.delete("clientId")
|
79
|
+
engine.should_receive(:client_exists).with(nil).and_yield false
|
80
|
+
end
|
81
|
+
|
82
|
+
it "does not destroy the client" do
|
83
|
+
engine.should_not_receive(:destroy_client)
|
84
|
+
server.disconnect(message) {}
|
85
|
+
end
|
86
|
+
|
87
|
+
it "returns an unsuccessful response" do
|
88
|
+
server.disconnect(message) do |response|
|
89
|
+
response.should == {
|
90
|
+
"channel" => "/meta/disconnect",
|
91
|
+
"successful" => false,
|
92
|
+
"error" => "402:clientId:Missing required parameter"
|
93
|
+
}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "with an error" do
|
99
|
+
before do
|
100
|
+
message["error"] = "invalid"
|
101
|
+
engine.should_receive(:client_exists).with(client_id).and_yield true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "does not destroy the client" do
|
105
|
+
engine.should_not_receive(:destroy_client)
|
106
|
+
server.disconnect(message) {}
|
107
|
+
end
|
108
|
+
|
109
|
+
it "returns an unsuccessful response" do
|
110
|
+
server.disconnect(message) do |response|
|
111
|
+
response.should == {
|
112
|
+
"channel" => "/meta/disconnect",
|
113
|
+
"successful" => false,
|
114
|
+
"error" => "invalid"
|
115
|
+
}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "server extensions" do
|
4
|
+
let(:engine) do
|
5
|
+
engine = mock "engine"
|
6
|
+
engine.stub(:interval).and_return(0)
|
7
|
+
engine.stub(:timeout).and_return(60)
|
8
|
+
engine
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:server) { Faye::Server.new }
|
12
|
+
let(:message) { {"channel" => "/foo", "data" => "hello"} }
|
13
|
+
|
14
|
+
before do
|
15
|
+
Faye::Engine.stub(:get).and_return engine
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "with an incoming extension installed" do
|
19
|
+
before do
|
20
|
+
extension = Class.new do
|
21
|
+
def incoming(message, callback)
|
22
|
+
message["ext"] = {"auth" => "password"}
|
23
|
+
callback.call(message)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
server.add_extension(extension.new)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "passes incoming messages through the extension" do
|
30
|
+
engine.should_receive(:publish).with({"channel" => "/foo", "data" => "hello", "ext" => {"auth" => "password"}})
|
31
|
+
server.process(message, false) {}
|
32
|
+
end
|
33
|
+
|
34
|
+
it "does not pass outgoing messages through the extension" do
|
35
|
+
server.stub(:handshake).and_yield(message)
|
36
|
+
engine.stub(:publish)
|
37
|
+
response = nil
|
38
|
+
server.process({"channel" => "/meta/handshake"}, false) { |r| response = r }
|
39
|
+
response.should == [{"channel" => "/foo", "data" => "hello"}]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "with an outgoing extension installed" do
|
44
|
+
before do
|
45
|
+
extension = Class.new do
|
46
|
+
def outgoing(message, callback)
|
47
|
+
message["ext"] = {"auth" => "password"}
|
48
|
+
callback.call(message)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
server.add_extension(extension.new)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "does not pass incoming messages through the extension" do
|
55
|
+
engine.should_receive(:publish).with({"channel" => "/foo", "data" => "hello"})
|
56
|
+
server.process(message, false) {}
|
57
|
+
end
|
58
|
+
|
59
|
+
it "passes outgoing messages through the extension" do
|
60
|
+
server.stub(:handshake).and_yield(message)
|
61
|
+
engine.stub(:publish)
|
62
|
+
response = nil
|
63
|
+
server.process({"channel" => "/meta/handshake"}, false) { |r| response = r }
|
64
|
+
response.should == [{"channel" => "/foo", "data" => "hello", "ext" => {"auth" => "password"}}]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "server handshake" do
|
4
|
+
let(:engine) { mock "engine" }
|
5
|
+
let(:server) { Faye::Server.new }
|
6
|
+
|
7
|
+
let :connection_types do
|
8
|
+
["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"]
|
9
|
+
end
|
10
|
+
|
11
|
+
before do
|
12
|
+
Faye::Engine.stub(:get).and_return engine
|
13
|
+
end
|
14
|
+
|
15
|
+
describe :handshake do
|
16
|
+
let(:message) {{"channel" => "/meta/handshake",
|
17
|
+
"version" => "1.0",
|
18
|
+
"supportedConnectionTypes" => ["long-polling"]
|
19
|
+
}}
|
20
|
+
|
21
|
+
describe "with valid parameters" do
|
22
|
+
it "creates a client" do
|
23
|
+
engine.should_receive(:create_client)
|
24
|
+
server.handshake(message) {}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns a successful response" do
|
28
|
+
engine.stub(:create_client).and_yield "clientid"
|
29
|
+
server.handshake(message) do |response|
|
30
|
+
response.should == {
|
31
|
+
"channel" => "/meta/handshake",
|
32
|
+
"successful" => true,
|
33
|
+
"version" => "1.0",
|
34
|
+
"supportedConnectionTypes" => connection_types,
|
35
|
+
"clientId" => "clientid"
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "with a message id" do
|
41
|
+
before { message["id"] = "foo" }
|
42
|
+
|
43
|
+
it "returns the same id" do
|
44
|
+
engine.stub(:create_client).and_yield "clientid"
|
45
|
+
server.handshake(message) do |response|
|
46
|
+
response.should == {
|
47
|
+
"channel" => "/meta/handshake",
|
48
|
+
"successful" => true,
|
49
|
+
"version" => "1.0",
|
50
|
+
"supportedConnectionTypes" => connection_types,
|
51
|
+
"clientId" => "clientid",
|
52
|
+
"id" => "foo"
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "missing version" do
|
60
|
+
before { message.delete "version" }
|
61
|
+
|
62
|
+
it "does not create a client" do
|
63
|
+
engine.should_not_receive(:create_client)
|
64
|
+
server.handshake(message) {}
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns an unsuccessful response" do
|
68
|
+
server.handshake(message) do |response|
|
69
|
+
response.should == {
|
70
|
+
"channel" => "/meta/handshake",
|
71
|
+
"successful" => false,
|
72
|
+
"error" => "402:version:Missing required parameter",
|
73
|
+
"version" => "1.0",
|
74
|
+
"supportedConnectionTypes" => connection_types
|
75
|
+
}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "missing supportedConnectionTypes" do
|
81
|
+
before { message.delete "supportedConnectionTypes" }
|
82
|
+
|
83
|
+
it "does not create a client" do
|
84
|
+
engine.should_not_receive(:create_client)
|
85
|
+
server.handshake(message) {}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "returns an unsuccessful response" do
|
89
|
+
server.handshake(message) do |response|
|
90
|
+
response.should == {
|
91
|
+
"channel" => "/meta/handshake",
|
92
|
+
"successful" => false,
|
93
|
+
"error" => "402:supportedConnectionTypes:Missing required parameter",
|
94
|
+
"version" => "1.0",
|
95
|
+
"supportedConnectionTypes" => connection_types
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "with no matching supportedConnectionTypes" do
|
102
|
+
before { message["supportedConnectionTypes"] = ["iframe", "flash"] }
|
103
|
+
|
104
|
+
it "does not create a client" do
|
105
|
+
engine.should_not_receive(:create_client)
|
106
|
+
server.handshake(message) {}
|
107
|
+
end
|
108
|
+
|
109
|
+
it "returns an unsuccessful response" do
|
110
|
+
server.handshake(message) do |response|
|
111
|
+
response.should == {
|
112
|
+
"channel" => "/meta/handshake",
|
113
|
+
"successful" => false,
|
114
|
+
"error" => "301:iframe,flash:Connection types not supported",
|
115
|
+
"version" => "1.0",
|
116
|
+
"supportedConnectionTypes" => connection_types
|
117
|
+
}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "with an error" do
|
123
|
+
before { message["error"] = "invalid" }
|
124
|
+
|
125
|
+
it "does not createa a client" do
|
126
|
+
engine.should_not_receive(:create_client)
|
127
|
+
server.handshake(message) {}
|
128
|
+
end
|
129
|
+
|
130
|
+
it "returns an unsuccessful response" do
|
131
|
+
server.handshake(message) do |response|
|
132
|
+
response.should == {
|
133
|
+
"channel" => "/meta/handshake",
|
134
|
+
"successful" => false,
|
135
|
+
"error" => "invalid",
|
136
|
+
"version" => "1.0",
|
137
|
+
"supportedConnectionTypes" => connection_types
|
138
|
+
}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding=utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
require "thin"
|
6
|
+
Thin::Logging.silent = true
|
7
|
+
|
8
|
+
IntegrationSteps = EM::RSpec.async_steps do
|
9
|
+
def server(port, ssl, &callback)
|
10
|
+
shared = File.dirname(__FILE__) + '/../../../examples/shared'
|
11
|
+
|
12
|
+
options = ssl ?
|
13
|
+
{ :key => shared + '/server.key', :cert => shared + '/server.crt' } :
|
14
|
+
nil
|
15
|
+
|
16
|
+
@adapter = Faye::RackAdapter.new(:mount => "/bayeux", :timeout => 25)
|
17
|
+
@adapter.listen(port, options)
|
18
|
+
@port = port
|
19
|
+
@secure = ssl
|
20
|
+
EM.next_tick(&callback)
|
21
|
+
end
|
22
|
+
|
23
|
+
def stop(&callback)
|
24
|
+
@adapter.stop
|
25
|
+
EM.next_tick(&callback)
|
26
|
+
end
|
27
|
+
|
28
|
+
def client(name, channels, &callback)
|
29
|
+
scheme = @secure ? "https" : "http"
|
30
|
+
@clients ||= {}
|
31
|
+
@inboxes ||= {}
|
32
|
+
@clients[name] = Faye::Client.new("#{scheme}://0.0.0.0:#{@port}/bayeux")
|
33
|
+
@inboxes[name] = {}
|
34
|
+
|
35
|
+
n = channels.size
|
36
|
+
return @clients[name].connect(&callback) if n.zero?
|
37
|
+
|
38
|
+
channels.each do |channel|
|
39
|
+
subscription = @clients[name].subscribe(channel) do |message|
|
40
|
+
@inboxes[name][channel] ||= []
|
41
|
+
@inboxes[name][channel] << message
|
42
|
+
end
|
43
|
+
subscription.callback do
|
44
|
+
n -= 1
|
45
|
+
callback.call if n.zero?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def publish(name, channel, message, &callback)
|
51
|
+
@clients[name].publish(channel, message)
|
52
|
+
EM.add_timer(0.1, &callback)
|
53
|
+
end
|
54
|
+
|
55
|
+
def check_inbox(name, channel, messages, &callback)
|
56
|
+
inbox = @inboxes[name][channel] || []
|
57
|
+
inbox.should == messages
|
58
|
+
callback.call
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "server integration" do
|
63
|
+
include IntegrationSteps
|
64
|
+
include EncodingHelper
|
65
|
+
|
66
|
+
before do
|
67
|
+
Faye.ensure_reactor_running!
|
68
|
+
server 8000, server_options[:ssl]
|
69
|
+
client :alice, []
|
70
|
+
client :bob, ["/foo"]
|
71
|
+
sync
|
72
|
+
end
|
73
|
+
|
74
|
+
after { stop }
|
75
|
+
|
76
|
+
shared_examples_for "message bus" do
|
77
|
+
it "delivers a message between clients" do
|
78
|
+
publish :alice, "/foo", {"hello" => "world"}
|
79
|
+
check_inbox :bob, "/foo", [{"hello" => "world"}]
|
80
|
+
end
|
81
|
+
|
82
|
+
it "does not deliver messages for unsubscribed channels" do
|
83
|
+
publish :alice, "/bar", {"hello" => "world"}
|
84
|
+
check_inbox :bob, "/foo", []
|
85
|
+
end
|
86
|
+
|
87
|
+
it "delivers multiple messages" do
|
88
|
+
publish :alice, "/foo", {"hello" => "world"}
|
89
|
+
publish :alice, "/foo", {"hello" => "world"}
|
90
|
+
check_inbox :bob, "/foo", [{"hello" => "world"}, {"hello" => "world"}]
|
91
|
+
end
|
92
|
+
|
93
|
+
it "delivers multibyte strings" do
|
94
|
+
publish :alice, "/foo", {"hello" => encode("Apple = ")}
|
95
|
+
check_inbox :bob, "/foo", [{"hello" => encode("Apple = ")}]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
shared_examples_for "network transports" do
|
100
|
+
describe "with HTTP transport" do
|
101
|
+
before do
|
102
|
+
Faye::Transport::WebSocket.stub(:usable?).and_yield(false)
|
103
|
+
end
|
104
|
+
|
105
|
+
it_should_behave_like "message bus"
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "with WebSocket transport" do
|
109
|
+
before do
|
110
|
+
Faye::Transport::WebSocket.stub(:usable?).and_yield(false)
|
111
|
+
end
|
112
|
+
|
113
|
+
it_should_behave_like "message bus"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "with HTTP server" do
|
118
|
+
let(:server_options) { {:ssl => false} }
|
119
|
+
it_should_behave_like "network transports"
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "with HTTPS server" do
|
123
|
+
let(:server_options) { {:ssl => true} }
|
124
|
+
it_should_behave_like "network transports"
|
125
|
+
end
|
126
|
+
end
|