face-faye 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/History.txt +304 -0
  2. data/README.rdoc +83 -0
  3. data/lib/faye-browser-min.js +2 -0
  4. data/lib/faye-browser-min.js.map +8 -0
  5. data/lib/faye-browser.js +2194 -0
  6. data/lib/faye.rb +122 -0
  7. data/lib/faye/adapters/rack_adapter.rb +216 -0
  8. data/lib/faye/adapters/static_server.rb +56 -0
  9. data/lib/faye/engines/connection.rb +60 -0
  10. data/lib/faye/engines/memory.rb +112 -0
  11. data/lib/faye/engines/proxy.rb +121 -0
  12. data/lib/faye/error.rb +49 -0
  13. data/lib/faye/mixins/logging.rb +47 -0
  14. data/lib/faye/mixins/publisher.rb +30 -0
  15. data/lib/faye/mixins/timeouts.rb +22 -0
  16. data/lib/faye/protocol/channel.rb +124 -0
  17. data/lib/faye/protocol/client.rb +376 -0
  18. data/lib/faye/protocol/extensible.rb +43 -0
  19. data/lib/faye/protocol/grammar.rb +58 -0
  20. data/lib/faye/protocol/publication.rb +5 -0
  21. data/lib/faye/protocol/server.rb +293 -0
  22. data/lib/faye/protocol/socket.rb +23 -0
  23. data/lib/faye/protocol/subscription.rb +24 -0
  24. data/lib/faye/transport/http.rb +76 -0
  25. data/lib/faye/transport/local.rb +22 -0
  26. data/lib/faye/transport/transport.rb +116 -0
  27. data/lib/faye/transport/web_socket.rb +92 -0
  28. data/lib/faye/util/namespace.rb +20 -0
  29. data/spec/browser.html +45 -0
  30. data/spec/encoding_helper.rb +7 -0
  31. data/spec/install.sh +78 -0
  32. data/spec/javascript/channel_spec.js +15 -0
  33. data/spec/javascript/client_spec.js +729 -0
  34. data/spec/javascript/engine/memory_spec.js +7 -0
  35. data/spec/javascript/engine_spec.js +417 -0
  36. data/spec/javascript/faye_spec.js +34 -0
  37. data/spec/javascript/grammar_spec.js +66 -0
  38. data/spec/javascript/node_adapter_spec.js +307 -0
  39. data/spec/javascript/publisher_spec.js +27 -0
  40. data/spec/javascript/server/connect_spec.js +168 -0
  41. data/spec/javascript/server/disconnect_spec.js +121 -0
  42. data/spec/javascript/server/extensions_spec.js +60 -0
  43. data/spec/javascript/server/handshake_spec.js +145 -0
  44. data/spec/javascript/server/integration_spec.js +131 -0
  45. data/spec/javascript/server/publish_spec.js +85 -0
  46. data/spec/javascript/server/subscribe_spec.js +247 -0
  47. data/spec/javascript/server/unsubscribe_spec.js +245 -0
  48. data/spec/javascript/server_spec.js +121 -0
  49. data/spec/javascript/transport_spec.js +135 -0
  50. data/spec/node.js +55 -0
  51. data/spec/phantom.js +17 -0
  52. data/spec/ruby/channel_spec.rb +17 -0
  53. data/spec/ruby/client_spec.rb +741 -0
  54. data/spec/ruby/engine/memory_spec.rb +7 -0
  55. data/spec/ruby/engine_examples.rb +427 -0
  56. data/spec/ruby/faye_spec.rb +30 -0
  57. data/spec/ruby/grammar_spec.rb +68 -0
  58. data/spec/ruby/publisher_spec.rb +27 -0
  59. data/spec/ruby/rack_adapter_spec.rb +236 -0
  60. data/spec/ruby/server/connect_spec.rb +170 -0
  61. data/spec/ruby/server/disconnect_spec.rb +120 -0
  62. data/spec/ruby/server/extensions_spec.rb +68 -0
  63. data/spec/ruby/server/handshake_spec.rb +143 -0
  64. data/spec/ruby/server/integration_spec.rb +133 -0
  65. data/spec/ruby/server/publish_spec.rb +81 -0
  66. data/spec/ruby/server/subscribe_spec.rb +247 -0
  67. data/spec/ruby/server/unsubscribe_spec.rb +247 -0
  68. data/spec/ruby/server_spec.rb +121 -0
  69. data/spec/ruby/transport_spec.rb +136 -0
  70. data/spec/spec_helper.rb +11 -0
  71. data/spec/testswarm +42 -0
  72. data/spec/thin_proxy.rb +37 -0
  73. metadata +441 -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,133 @@
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
+ class Tagger
10
+ def outgoing(message, callback)
11
+ message["data"]["tagged"] = true if message["data"]
12
+ callback.call(message)
13
+ end
14
+ end
15
+
16
+ def server(port, ssl, &callback)
17
+ shared = File.dirname(__FILE__) + '/../../../examples/shared'
18
+
19
+ options = ssl ?
20
+ { :key => shared + '/server.key', :cert => shared + '/server.crt' } :
21
+ nil
22
+
23
+ @adapter = Faye::RackAdapter.new(:mount => "/bayeux", :timeout => 25)
24
+ @adapter.add_extension(Tagger.new)
25
+ @adapter.listen(port, options)
26
+
27
+ @port = port
28
+ @secure = ssl
29
+ EM.next_tick(&callback)
30
+ end
31
+
32
+ def stop(&callback)
33
+ @adapter.stop
34
+ EM.next_tick(&callback)
35
+ end
36
+
37
+ def client(name, channels, &callback)
38
+ scheme = @secure ? "https" : "http"
39
+ @clients ||= {}
40
+ @inboxes ||= {}
41
+ @clients[name] = Faye::Client.new("#{scheme}://0.0.0.0:#{@port}/bayeux")
42
+ @inboxes[name] = {}
43
+
44
+ n = channels.size
45
+ return @clients[name].connect(&callback) if n.zero?
46
+
47
+ channels.each do |channel|
48
+ subscription = @clients[name].subscribe(channel) do |message|
49
+ @inboxes[name][channel] ||= []
50
+ @inboxes[name][channel] << message
51
+ end
52
+ subscription.callback do
53
+ n -= 1
54
+ callback.call if n.zero?
55
+ end
56
+ end
57
+ end
58
+
59
+ def publish(name, channel, message, &callback)
60
+ @clients[name].publish(channel, message)
61
+ EM.add_timer(0.1, &callback)
62
+ end
63
+
64
+ def check_inbox(name, channel, messages, &callback)
65
+ inbox = @inboxes[name][channel] || []
66
+ inbox.should == messages
67
+ callback.call
68
+ end
69
+ end
70
+
71
+ describe "server integration" do
72
+ include IntegrationSteps
73
+ include EncodingHelper
74
+
75
+ before do
76
+ server 8000, server_options[:ssl]
77
+ client :alice, []
78
+ client :bob, ["/foo"]
79
+ end
80
+
81
+ after { stop }
82
+
83
+ shared_examples_for "message bus" do
84
+ it "delivers a message between clients" do
85
+ publish :alice, "/foo", {"hello" => "world", "extra" => nil}
86
+ check_inbox :bob, "/foo", [{"hello" => "world", "extra" => nil, "tagged" => true}]
87
+ end
88
+
89
+ it "does not deliver messages for unsubscribed channels" do
90
+ publish :alice, "/bar", {"hello" => "world"}
91
+ check_inbox :bob, "/foo", []
92
+ end
93
+
94
+ it "delivers multiple messages" do
95
+ publish :alice, "/foo", {"hello" => "world"}
96
+ publish :alice, "/foo", {"hello" => "world"}
97
+ check_inbox :bob, "/foo", [{"hello" => "world", "tagged" => true}, {"hello" => "world", "tagged" => true}]
98
+ end
99
+
100
+ it "delivers multibyte strings" do
101
+ publish :alice, "/foo", {"hello" => encode("Apple = "), "tagged" => true}
102
+ check_inbox :bob, "/foo", [{"hello" => encode("Apple = "), "tagged" => true}]
103
+ end
104
+ end
105
+
106
+ shared_examples_for "network transports" do
107
+ describe "with HTTP transport" do
108
+ before do
109
+ Faye::Transport::WebSocket.stub(:usable?).and_yield(false)
110
+ end
111
+
112
+ it_should_behave_like "message bus"
113
+ end
114
+
115
+ describe "with WebSocket transport" do
116
+ before do
117
+ Faye::Transport::WebSocket.stub(:usable?).and_yield(true)
118
+ end
119
+
120
+ it_should_behave_like "message bus"
121
+ end
122
+ end
123
+
124
+ describe "with HTTP server" do
125
+ let(:server_options) { {:ssl => false} }
126
+ it_should_behave_like "network transports"
127
+ end
128
+
129
+ describe "with HTTPS server" do
130
+ let(:server_options) { {:ssl => true} }
131
+ it_should_behave_like "network transports"
132
+ end
133
+ end