wwl-websocket-rails 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +328 -0
  3. data/Gemfile +27 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +239 -0
  6. data/Rakefile +72 -0
  7. data/bin/thin-socketrails +45 -0
  8. data/lib/assets/javascripts/websocket_rails/abstract_connection.js.coffee +45 -0
  9. data/lib/assets/javascripts/websocket_rails/channel.js.coffee +70 -0
  10. data/lib/assets/javascripts/websocket_rails/event.js.coffee +46 -0
  11. data/lib/assets/javascripts/websocket_rails/http_connection.js.coffee +66 -0
  12. data/lib/assets/javascripts/websocket_rails/main.js +6 -0
  13. data/lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee +29 -0
  14. data/lib/assets/javascripts/websocket_rails/websocket_rails.js.coffee +158 -0
  15. data/lib/config.ru +3 -0
  16. data/lib/generators/websocket_rails/install/install_generator.rb +33 -0
  17. data/lib/generators/websocket_rails/install/templates/events.rb +14 -0
  18. data/lib/generators/websocket_rails/install/templates/websocket_rails.rb +68 -0
  19. data/lib/rails/app/controllers/websocket_rails/delegation_controller.rb +13 -0
  20. data/lib/rails/config/routes.rb +7 -0
  21. data/lib/rails/tasks/websocket_rails.tasks +42 -0
  22. data/lib/spec_helpers/matchers/route_matchers.rb +65 -0
  23. data/lib/spec_helpers/matchers/trigger_matchers.rb +138 -0
  24. data/lib/spec_helpers/spec_helper_event.rb +34 -0
  25. data/lib/websocket-rails.rb +108 -0
  26. data/lib/websocket_rails/base_controller.rb +208 -0
  27. data/lib/websocket_rails/channel.rb +97 -0
  28. data/lib/websocket_rails/channel_manager.rb +55 -0
  29. data/lib/websocket_rails/configuration.rb +177 -0
  30. data/lib/websocket_rails/connection_adapters/http.rb +120 -0
  31. data/lib/websocket_rails/connection_adapters/web_socket.rb +35 -0
  32. data/lib/websocket_rails/connection_adapters.rb +195 -0
  33. data/lib/websocket_rails/connection_manager.rb +119 -0
  34. data/lib/websocket_rails/controller_factory.rb +80 -0
  35. data/lib/websocket_rails/data_store.rb +145 -0
  36. data/lib/websocket_rails/dispatcher.rb +129 -0
  37. data/lib/websocket_rails/engine.rb +26 -0
  38. data/lib/websocket_rails/event.rb +193 -0
  39. data/lib/websocket_rails/event_map.rb +184 -0
  40. data/lib/websocket_rails/event_queue.rb +33 -0
  41. data/lib/websocket_rails/internal_events.rb +37 -0
  42. data/lib/websocket_rails/logging.rb +133 -0
  43. data/lib/websocket_rails/spec_helpers.rb +3 -0
  44. data/lib/websocket_rails/synchronization.rb +178 -0
  45. data/lib/websocket_rails/user_manager.rb +276 -0
  46. data/lib/websocket_rails/version.rb +3 -0
  47. data/spec/dummy/Rakefile +7 -0
  48. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  49. data/spec/dummy/app/controllers/chat_controller.rb +53 -0
  50. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  51. data/spec/dummy/app/models/user.rb +2 -0
  52. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  53. data/spec/dummy/config/application.rb +45 -0
  54. data/spec/dummy/config/boot.rb +10 -0
  55. data/spec/dummy/config/database.yml +22 -0
  56. data/spec/dummy/config/environment.rb +5 -0
  57. data/spec/dummy/config/environments/development.rb +26 -0
  58. data/spec/dummy/config/environments/production.rb +49 -0
  59. data/spec/dummy/config/environments/test.rb +34 -0
  60. data/spec/dummy/config/events.rb +7 -0
  61. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/dummy/config/initializers/inflections.rb +10 -0
  63. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  64. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  65. data/spec/dummy/config/initializers/session_store.rb +8 -0
  66. data/spec/dummy/config/locales/en.yml +5 -0
  67. data/spec/dummy/config/routes.rb +58 -0
  68. data/spec/dummy/config.ru +4 -0
  69. data/spec/dummy/db/development.sqlite3 +0 -0
  70. data/spec/dummy/db/migrate/20130902222552_create_users.rb +10 -0
  71. data/spec/dummy/db/schema.rb +23 -0
  72. data/spec/dummy/db/test.sqlite3 +0 -0
  73. data/spec/dummy/log/development.log +17 -0
  74. data/spec/dummy/log/production.log +0 -0
  75. data/spec/dummy/log/server.log +0 -0
  76. data/spec/dummy/log/test.log +0 -0
  77. data/spec/dummy/public/404.html +26 -0
  78. data/spec/dummy/public/422.html +26 -0
  79. data/spec/dummy/public/500.html +26 -0
  80. data/spec/dummy/public/favicon.ico +0 -0
  81. data/spec/dummy/public/javascripts/application.js +2 -0
  82. data/spec/dummy/public/javascripts/controls.js +965 -0
  83. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  84. data/spec/dummy/public/javascripts/effects.js +1123 -0
  85. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  86. data/spec/dummy/public/javascripts/rails.js +202 -0
  87. data/spec/dummy/script/rails +6 -0
  88. data/spec/integration/connection_manager_spec.rb +135 -0
  89. data/spec/javascripts/support/jasmine.yml +52 -0
  90. data/spec/javascripts/support/jasmine_helper.rb +38 -0
  91. data/spec/javascripts/support/vendor/sinon-1.7.1.js +4343 -0
  92. data/spec/javascripts/websocket_rails/channel_spec.coffee +112 -0
  93. data/spec/javascripts/websocket_rails/event_spec.coffee +81 -0
  94. data/spec/javascripts/websocket_rails/helpers.coffee +6 -0
  95. data/spec/javascripts/websocket_rails/websocket_connection_spec.coffee +158 -0
  96. data/spec/javascripts/websocket_rails/websocket_rails_spec.coffee +273 -0
  97. data/spec/spec_helper.rb +41 -0
  98. data/spec/spec_helpers/matchers/route_matchers_spec.rb +109 -0
  99. data/spec/spec_helpers/matchers/trigger_matchers_spec.rb +358 -0
  100. data/spec/spec_helpers/spec_helper_event_spec.rb +66 -0
  101. data/spec/support/helper_methods.rb +42 -0
  102. data/spec/support/mock_web_socket.rb +41 -0
  103. data/spec/unit/base_controller_spec.rb +74 -0
  104. data/spec/unit/channel_manager_spec.rb +58 -0
  105. data/spec/unit/channel_spec.rb +169 -0
  106. data/spec/unit/connection_adapters/http_spec.rb +88 -0
  107. data/spec/unit/connection_adapters/web_socket_spec.rb +30 -0
  108. data/spec/unit/connection_adapters_spec.rb +259 -0
  109. data/spec/unit/connection_manager_spec.rb +148 -0
  110. data/spec/unit/controller_factory_spec.rb +76 -0
  111. data/spec/unit/data_store_spec.rb +106 -0
  112. data/spec/unit/dispatcher_spec.rb +203 -0
  113. data/spec/unit/event_map_spec.rb +120 -0
  114. data/spec/unit/event_queue_spec.rb +36 -0
  115. data/spec/unit/event_spec.rb +181 -0
  116. data/spec/unit/logging_spec.rb +162 -0
  117. data/spec/unit/synchronization_spec.rb +150 -0
  118. data/spec/unit/target_validator_spec.rb +88 -0
  119. data/spec/unit/user_manager_spec.rb +165 -0
  120. metadata +320 -0
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Route Matchers' do
4
+
5
+ class RouteSpecProductController < WebsocketRails::BaseController
6
+
7
+ def update_product
8
+ end
9
+
10
+ def delete_product
11
+ end
12
+
13
+ end
14
+
15
+ class RouteSpecWarehouseController < WebsocketRails::BaseController
16
+
17
+ def remove_product
18
+ end
19
+
20
+ end
21
+
22
+ def define_route_test_events
23
+ WebsocketRails.config.route_block = nil
24
+ WebsocketRails::EventMap.describe do
25
+
26
+ namespace :product do
27
+ subscribe :update, :to => RouteSpecProductController, :with_method => :update_product
28
+ subscribe :delete, :to => RouteSpecProductController, :with_method => :delete_product
29
+ subscribe :delete, :to => RouteSpecWarehouseController, :with_method => :remove_product
30
+ end
31
+ end
32
+ end
33
+
34
+ before { define_route_test_events }
35
+
36
+ describe 'be_routed_to' do
37
+
38
+ it 'should return true when the event is routed only to the specified controller' do
39
+ create_event('product.update', nil).should be_routed_to 'route_spec_product#update_product'
40
+ end
41
+
42
+ it 'should return true when the event is routed also to the specified controller' do
43
+ create_event('product.delete', nil).should be_routed_to 'route_spec_product#delete_product'
44
+ end
45
+
46
+ it 'should return false when the event is not routed to the specified controller' do
47
+ create_event('product.update', nil).should_not be_routed_to 'route_spec_product#delete_product'
48
+ end
49
+
50
+ it 'should produce the correct failure message' do
51
+ event = create_event('route_spec_product.update', nil)
52
+ matcher = be_routed_to 'route_spec_product#delete_product'
53
+ cache_messages_for_matcher(matcher, event)
54
+ matcher.should produce_as_failure_message 'expected event update to be routed to target route_spec_product#delete_product'
55
+ end
56
+
57
+ it 'should produce the correct negative failure message' do
58
+ event = create_event('product.update', nil)
59
+ matcher = be_routed_to 'route_spec_product#update_product'
60
+ cache_messages_for_matcher(matcher, event)
61
+ matcher.should produce_as_negative_failure_message 'expected event update not to be routed to target route_spec_product#update_product'
62
+ end
63
+
64
+ it 'should produce the correct description' do
65
+ event = create_event('product.update', nil)
66
+ matcher = be_routed_to 'route_spec_product#update_product'
67
+ cache_messages_for_matcher(matcher, event)
68
+ matcher.should produce_as_description 'be routed to target route_spec_product#update_product'
69
+ end
70
+
71
+ end
72
+
73
+ describe 'be_routed_only_to' do
74
+
75
+ it 'should return true when the event is routed only to the specified controller' do
76
+ create_event('product.update', nil).should be_routed_only_to 'route_spec_product#update_product'
77
+ end
78
+
79
+ it 'should return false when the event is routed also to the specified controller' do
80
+ create_event('product.delete', nil).should_not be_routed_only_to 'route_spec_product#delete_product'
81
+ end
82
+
83
+ it 'should return false when the event is not routed to the specified controller' do
84
+ create_event('product.update', nil).should_not be_routed_only_to 'route_spec_product#delete_product'
85
+ end
86
+
87
+ it 'should produce the correct failure message' do
88
+ event = create_event('product.update', nil)
89
+ matcher = be_routed_only_to 'route_spec_product#delete_product'
90
+ cache_messages_for_matcher(matcher, event)
91
+ matcher.should produce_as_failure_message 'expected event update to be routed only to target route_spec_product#delete_product'
92
+ end
93
+
94
+ it 'should produce the correct negative failure message' do
95
+ event = create_event('product.update', nil)
96
+ matcher = be_routed_only_to 'route_spec_product#update_product'
97
+ cache_messages_for_matcher(matcher, event)
98
+ matcher.should produce_as_negative_failure_message 'expected event update not to be routed only to target route_spec_product#update_product'
99
+ end
100
+
101
+ it 'should produce the correct description' do
102
+ event = create_event('product.update', nil)
103
+ matcher = be_routed_only_to 'route_spec_product#update_product'
104
+ cache_messages_for_matcher(matcher, event)
105
+ matcher.should produce_as_description 'be routed only to target route_spec_product#update_product'
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,358 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Trigger Matchers' do
4
+
5
+ class TriggerSpecProductController < WebsocketRails::BaseController
6
+
7
+ # a method that does not trigger messages
8
+ def update_product
9
+ end
10
+
11
+ def delete_product
12
+ data = message[:data] ? 'Return Data' : nil
13
+ if message[:confirm_delete]
14
+ trigger_success(data)
15
+ else
16
+ trigger_failure(data)
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ def define_test_events
23
+ WebsocketRails.config.route_block = nil
24
+ WebsocketRails::EventMap.describe do
25
+
26
+ namespace :product do
27
+ subscribe :update, :to => TriggerSpecProductController, :with_method => :update_product
28
+ subscribe :delete, :to => TriggerSpecProductController, :with_method => :delete_product
29
+ end
30
+ end
31
+ end
32
+
33
+ before { define_test_events }
34
+
35
+ # as we have have 16 possible combinations of trigger messages and data matching pattern (data|no_data, success|failure,
36
+ # no_checking|checking_with_any|checking_with_nil|checking_with_exact_data) plus the case of no message at all
37
+ # for EACH of the matchers, resulting in a total 51 cases, we will not extensively test all cases for all matchers
38
+ # but we just make sure that coverage is 100%
39
+
40
+ describe 'trigger_message' do
41
+
42
+ it 'should trigger a success message with no data even when the event does not trigger any message with the default configuration' do
43
+ expect(create_event('product.update', nil).dispatch).to trigger_message
44
+ end
45
+
46
+ context 'When WebsocketRails.config.trigger_success_by_default = false' do
47
+ before {
48
+ WebsocketRails.config.stub(:trigger_success_by_default) { return false }
49
+ }
50
+
51
+ it 'should trigger a message when the event does not trigger any message' do
52
+ expect(create_event('product.update', nil).dispatch).to trigger_message
53
+ end
54
+ end
55
+
56
+ it 'should return true when the message is a failure' do
57
+ expect(create_event('product.delete', {confirm_delete: false, data: true}).dispatch).to trigger_message
58
+ end
59
+
60
+ it 'should return true when the message is a success' do
61
+ expect(create_event('product.delete', {confirm_delete: true, data: false}).dispatch).to trigger_message
62
+ end
63
+
64
+ context 'when a message is triggered with no data' do
65
+
66
+ it 'should return true when no check is done' do
67
+ create_event('product.delete', {confirm_delete: true, data: false}).dispatch.should trigger_message
68
+ end
69
+
70
+ it 'should produce the correct negative failure message when no check on data is done' do
71
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
72
+ matcher = trigger_message
73
+ cache_messages_for_matcher(matcher, event)
74
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger message'
75
+ end
76
+
77
+ it 'should return true when explicitly checking no data' do
78
+ create_event('product.delete', {confirm_delete: false, data: false}).dispatch.should trigger_message :nil
79
+ end
80
+
81
+ it 'should produce the correct negative failure message when explicitly checking no data' do
82
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
83
+ matcher = trigger_message :nil
84
+ cache_messages_for_matcher(matcher, event)
85
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger message with no data'
86
+ end
87
+
88
+ it 'should return false when checking for some data' do
89
+ create_event('product.delete', {confirm_delete: true, data: false}).dispatch.should_not trigger_message :any
90
+ end
91
+
92
+ it 'should return the correct failure message when checking for some data' do
93
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
94
+ matcher = trigger_message :any
95
+ cache_messages_for_matcher(matcher, event)
96
+ matcher.should produce_as_failure_message 'expected product.delete to trigger message with some data, instead it triggered a success message with no data'
97
+ end
98
+
99
+ it 'should return false when checking for specific data' do
100
+ create_event('product.delete', {confirm_delete: false, data: false}).dispatch.should_not trigger_message 'Expected Data'
101
+ end
102
+
103
+ it 'should return the correct failure message when checking for specific data' do
104
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
105
+ matcher = trigger_message 'Expected Data'
106
+ cache_messages_for_matcher(matcher, event)
107
+ matcher.should produce_as_failure_message 'expected product.delete to trigger message with data Expected Data, instead it triggered a success message with no data'
108
+ end
109
+
110
+ end
111
+
112
+ context 'when a message is triggered with some data' do
113
+
114
+ it 'should return true when no check is done' do
115
+ create_event('product.delete', {confirm_delete: true, data: true}).dispatch.should trigger_message
116
+ end
117
+
118
+ it 'should return false when explicitly checking no data' do
119
+ create_event('product.delete', {confirm_delete: false, data: true}).dispatch.should_not trigger_message :nil
120
+ end
121
+
122
+ it 'should produce the correct failure message when explicitly checking for no data' do
123
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
124
+ matcher = trigger_message :nil
125
+ cache_messages_for_matcher(matcher, event)
126
+ matcher.should produce_as_failure_message 'expected product.delete to trigger message with no data, instead it triggered a failure message with data Return Data'
127
+ end
128
+
129
+ it 'should return true when checking for some data' do
130
+ create_event('product.delete', {confirm_delete: true, data: true}).dispatch.should trigger_message :any
131
+ end
132
+
133
+ it 'should produce the correct negative failure message when checking for some data' do
134
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
135
+ matcher = trigger_message :any
136
+ cache_messages_for_matcher(matcher, event)
137
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger message with some data'
138
+ end
139
+
140
+ it 'should return false when checking for specific data with wrong data' do
141
+ create_event('product.delete', {confirm_delete: false, data: true}).dispatch.should_not trigger_message 'Wrong Data'
142
+ end
143
+
144
+ it 'should produce the correct failure message when checking for specific data' do
145
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
146
+ matcher = trigger_message 'Wrong Data'
147
+ cache_messages_for_matcher(matcher, event)
148
+ matcher.should produce_as_failure_message 'expected product.delete to trigger message with data Wrong Data, instead it triggered a failure message with data Return Data'
149
+ end
150
+
151
+ it 'should return true when checking for specific data with correct data' do
152
+ create_event('product.delete', {confirm_delete: true, data: true}).dispatch.should trigger_message 'Return Data'
153
+ end
154
+
155
+ it 'should produce the correct negative failure message when checking for specific data' do
156
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
157
+ matcher = trigger_message 'Return Data'
158
+ cache_messages_for_matcher(matcher, event)
159
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger message with data Return Data'
160
+ end
161
+
162
+ end
163
+
164
+ it 'should produce the correct description' do
165
+ event = create_event('product.update', nil)
166
+ matcher = trigger_message 'Return Data'
167
+ cache_messages_for_matcher(matcher, event)
168
+ matcher.should produce_as_description 'trigger message with data Return Data'
169
+ end
170
+
171
+
172
+ end
173
+
174
+ describe 'trigger_success_message' do
175
+
176
+ context 'When WebsocketRails.config.trigger_success_by_default is true' do
177
+
178
+ it 'should return true when the method does not trigger any message' do
179
+ expect(create_event('product.update', nil).dispatch).to trigger_success_message
180
+ end
181
+
182
+ it 'should produce the correct failure message when the method does not trigger any message' do
183
+ event = create_event('product.update', nil).dispatch
184
+ matcher = trigger_success_message 'Return Data'
185
+ cache_messages_for_matcher(matcher, event)
186
+ matcher.should produce_as_failure_message 'expected product.update to trigger success message with data Return Data, instead it triggered a success message with no data'
187
+ end
188
+ end
189
+
190
+ context 'When WebsocketRails.config.trigger_success_by_default is false' do
191
+
192
+ before {
193
+ WebsocketRails.config.stub(:trigger_success_by_default) { false }
194
+ }
195
+
196
+ it 'should return false when then method does not trigger any message' do
197
+ expect(create_event('product.update', nil).dispatch).not_to trigger_success_message
198
+ end
199
+
200
+ it 'should produce the correct failure message when the method does not trigger any message' do
201
+ event = create_event('product.update', nil).dispatch
202
+ matcher = trigger_success_message 'Return Data'
203
+ cache_messages_for_matcher(matcher, event)
204
+ matcher.should produce_as_failure_message 'expected product.update to trigger success message with data Return Data, instead it triggered a no result message with no data'
205
+ end
206
+ end
207
+
208
+ it 'should return true when the method triggers a success message' do
209
+ create_event('product.delete', {confirm_delete: true, data: true}).dispatch.should trigger_success_message
210
+ end
211
+
212
+ it 'should produce the correct negative failure message when the method triggers a success message' do
213
+ event = create_event('product.delete', {confirm_delete: true, data: true}).dispatch
214
+ matcher = trigger_success_message :any
215
+ cache_messages_for_matcher(matcher, event)
216
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger success message with some data'
217
+ end
218
+
219
+ it 'should return false when the method triggers a failure message' do
220
+ create_event('product.delete', {confirm_delete: false, data: true}).dispatch.should_not trigger_success_message
221
+ end
222
+
223
+ it 'should produce the correct failure message when the method triggers a failure message' do
224
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
225
+ matcher = trigger_success_message
226
+ cache_messages_for_matcher(matcher, event)
227
+ matcher.should produce_as_failure_message 'expected product.delete to trigger success message, instead it triggered a failure message with data Return Data'
228
+ end
229
+
230
+ end
231
+
232
+ describe 'trigger_failure_message' do
233
+
234
+ it 'should return false when the method does not trigger any message' do
235
+ expect(create_event('product.update', nil).dispatch).not_to trigger_failure_message
236
+ end
237
+
238
+ it 'should produce the correct failure message when the method does not trigger any message' do
239
+ event = create_event('product.update', nil).dispatch
240
+ matcher = trigger_failure_message 'Return Data'
241
+ cache_messages_for_matcher(matcher, event)
242
+ matcher.should produce_as_failure_message 'expected product.update to trigger failure message with data Return Data, instead it triggered a success message with no data'
243
+ end
244
+
245
+ context 'When WebsocketRails.config.trigger_success_by_default is false' do
246
+
247
+ before {
248
+ WebsocketRails.config.stub(:trigger_success_by_default) { false }
249
+ }
250
+
251
+ it 'should produce the correct failure message when the method does not trigger any message' do
252
+ event = create_event('product.update', nil).dispatch
253
+ matcher = trigger_failure_message 'Return Data'
254
+ cache_messages_for_matcher(matcher, event)
255
+ matcher.should produce_as_failure_message 'expected product.update to trigger failure message with data Return Data, instead it triggered a no result message with no data'
256
+ end
257
+
258
+ end
259
+
260
+ it 'should return false when the method triggers a success message' do
261
+ create_event('product.delete', {confirm_delete: true, data: true}).dispatch.should_not trigger_failure_message
262
+ end
263
+
264
+ it 'should produce the correct failure message when the method triggers a success message' do
265
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
266
+ matcher = trigger_failure_message :any
267
+ cache_messages_for_matcher(matcher, event)
268
+ matcher.should produce_as_failure_message 'expected product.delete to trigger failure message with some data, instead it triggered a success message with no data'
269
+ end
270
+
271
+
272
+ it 'should return true when the method triggers a failure message' do
273
+ create_event('product.delete', {confirm_delete: false, data: true}).dispatch.should trigger_failure_message
274
+ end
275
+
276
+ it 'should produce the correct negative failure message when the method triggers a failure message' do
277
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
278
+ matcher = trigger_failure_message
279
+ cache_messages_for_matcher(matcher, event)
280
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger failure message'
281
+ end
282
+
283
+ end
284
+
285
+ describe 'trigger_no_result_message' do
286
+
287
+ it 'should return false when the method does not trigger any message' do
288
+ expect(create_event('product.update', nil).dispatch).not_to trigger_no_result_message
289
+ end
290
+
291
+ it 'should return false when the method triggers a success message' do
292
+ expect(create_event('product.delete', {confirm_delete: true, data: false}).dispatch).not_to trigger_no_result_message
293
+ end
294
+
295
+ it 'should return false when the method triggers a failure message' do
296
+ expect(create_event('product.delete', {confirm_delete: false, data: true}).dispatch).not_to trigger_no_result_message
297
+ end
298
+
299
+ it 'should produce the correct failure message when the method does not trigger any message' do
300
+ event = create_event('product.update', nil).dispatch
301
+ matcher = trigger_no_result_message
302
+ cache_messages_for_matcher(matcher, event)
303
+ matcher.should produce_as_failure_message 'expected product.update to trigger no result message (success == 2), instead it triggered a success message with no data'
304
+ end
305
+
306
+ it 'should produce the correct failure message when the method triggers a success message' do
307
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
308
+ matcher = trigger_no_result_message
309
+ cache_messages_for_matcher(matcher, event)
310
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger no result message (success == 2)'
311
+ end
312
+
313
+ it 'should produce the correct failure message when the method triggers a failure message' do
314
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
315
+ matcher = trigger_no_result_message
316
+ cache_messages_for_matcher(matcher, event)
317
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger no result message (success == 2)'
318
+ end
319
+
320
+ context 'When WebsocketRails.config.trigger_success_by_default is false' do
321
+
322
+ before {
323
+ WebsocketRails.config.stub(:trigger_success_by_default) { false }
324
+ }
325
+
326
+ it 'should return true when the method does not trigger any message' do
327
+ puts WebsocketRails.config.trigger_success_by_default
328
+ puts WebsocketRails.config
329
+ expect(create_event('product.update', nil).dispatch).to trigger_no_result_message
330
+ end
331
+
332
+ it 'should return false when the method triggers a success message' do
333
+ expect(create_event('product.delete', {confirm_delete: true, data: false}).dispatch).not_to trigger_no_result_message
334
+ end
335
+
336
+ it 'should return false when the method triggers a failure message' do
337
+ expect(create_event('product.delete', {confirm_delete: false, data: true}).dispatch).not_to trigger_no_result_message
338
+ end
339
+
340
+ it 'should produce the correct failure message when the method triggers a success message' do
341
+ event = create_event('product.delete', {confirm_delete: true, data: false}).dispatch
342
+ matcher = trigger_no_result_message
343
+ cache_messages_for_matcher(matcher, event)
344
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger no result message (success == 2)'
345
+ end
346
+
347
+ it 'should produce the correct failure message when the method triggers a failure message' do
348
+ event = create_event('product.delete', {confirm_delete: false, data: true}).dispatch
349
+ matcher = trigger_no_result_message
350
+ cache_messages_for_matcher(matcher, event)
351
+ matcher.should produce_as_negative_failure_message 'expected product.delete not to trigger no result message (success == 2)'
352
+ end
353
+
354
+ end
355
+
356
+ end
357
+
358
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ module WebsocketRails
4
+
5
+ describe SpecHelperEvent do
6
+
7
+ before do
8
+ @dispatcher = double(:dispatcher)
9
+ Dispatcher.stub(:new).and_return @dispatcher
10
+ @event = SpecHelperEvent.new('my_event', data: 'my_data')
11
+ end
12
+
13
+ describe 'initialize' do
14
+
15
+ it 'should initialize the name and namespace of the event' do
16
+ @event.namespace.should == [:global]
17
+ @event.name.should == :my_event
18
+ end
19
+
20
+ it 'should initialize the data of the event' do
21
+ @event.data.should == 'my_data'
22
+ end
23
+
24
+ it 'should set the event to not triggered' do
25
+ @event.should_not be_triggered
26
+ end
27
+
28
+ end
29
+
30
+ describe 'trigger' do
31
+
32
+ it 'should set the triggered variable to true' do
33
+ @event.trigger
34
+ @event.should be_triggered
35
+ end
36
+
37
+ end
38
+
39
+ describe 'dispatch' do
40
+
41
+ it 'should invoke dispatch on the dispatcher object' do
42
+ @dispatcher.should_receive(:dispatch).with(@event)
43
+ @event.dispatch
44
+ end
45
+
46
+ it 'should return itself to be able to chain matchers' do
47
+ @dispatcher.stub(:dispatch)
48
+ @event.dispatch.should == @event
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ describe 'create_event' do
58
+
59
+ it 'should create a SpecHelperEvent with the correct parameters' do
60
+ event = create_event('my_event','my_data')
61
+ event.should be_a WebsocketRails::SpecHelperEvent
62
+ event.name.should == :my_event
63
+ event.data.should == 'my_data'
64
+ end
65
+
66
+ end
@@ -0,0 +1,42 @@
1
+ require 'json'
2
+
3
+ module WebsocketRails
4
+ module HelperMethods
5
+ def env
6
+ @_env ||= begin
7
+ env = Rack::MockRequest.env_for('/websocket')
8
+ env['async.callback'] = Proc.new { |response| true }
9
+ env
10
+ end
11
+ end
12
+
13
+ def mock_request
14
+ @_request ||= ActionDispatch::Request.new(env)
15
+ end
16
+
17
+ def encoded_message
18
+ ['test_event',{:data => {:user_name => 'Joe User'}}].to_json
19
+ end
20
+
21
+ def subscribe_encoded_message
22
+ ['websocket_rails.subscribe',:data => nil, :channel => :awesome_channel].to_json
23
+ end
24
+
25
+ def received_encoded_message(connection_id)
26
+ [connection_id,'test_event',{:user_name => 'Joe User'}].to_json
27
+ end
28
+
29
+ MockEvent = Struct.new(:name,:namespace)
30
+ end
31
+ end
32
+
33
+ module EM
34
+ def self.next_tick(&block)
35
+ block.call if block.respond_to?(:call)
36
+ end
37
+ class PeriodicTimer
38
+ def initialize(interval)
39
+ @interval = interval
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module WebsocketRails
4
+
5
+ class MockWebSocket
6
+ attr_writer :onmessage, :onerror, :onclose
7
+
8
+ def env
9
+ env = Rack::MockRequest.env_for('/websocket')
10
+ end
11
+
12
+ def onmessage(event=nil)
13
+ @onmessage.call(event)
14
+ end
15
+
16
+ def onerror(event=nil)
17
+ @onerror.call(event)
18
+ end
19
+
20
+ def onclose(event=nil)
21
+ @onclose.call(event)
22
+ end
23
+
24
+ def rack_response
25
+ [ -1, {}, [] ]
26
+ end
27
+
28
+ def send(*args)
29
+ true
30
+ end
31
+
32
+ def trigger(event)
33
+ true
34
+ end
35
+
36
+ def id
37
+ object_id.to_i
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,74 @@
1
+ require "spec_helper"
2
+
3
+ module WebsocketRails
4
+ describe BaseController do
5
+
6
+ class TestClass; end;
7
+
8
+ describe ".inherited" do
9
+ context "with Rails version 3" do
10
+ before do
11
+ Rails.stub(:version).and_return("3.2.13")
12
+ end
13
+
14
+ it "should call unloadable on the inherited class" do
15
+ Object.should_receive(:unloadable).with(TestClass)
16
+ BaseController.inherited(TestClass)
17
+ end
18
+ end
19
+
20
+ context "with Rails version 4" do
21
+ before do
22
+ Rails.stub(:version).and_return("4.0.0")
23
+ end
24
+
25
+ it "should call unloadable on the inherited class" do
26
+ Object.should_not_receive(:unloadable).with(TestClass)
27
+ BaseController.inherited(TestClass)
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "before actions" do
33
+ class BeforeActionController < WebsocketRails::BaseController
34
+ before_action { self.before_array << :all }
35
+ before_action(:only => :only) { self.before_array << :only_1 }
36
+ before_action(:only => :except) { self.before_array << :only_2 }
37
+ before_action(:only => [:main, :only]) { self.before_array << :only_3 }
38
+ before_action(:only => [:except, :only]) { self.before_array << :only_4 }
39
+ before_action(:except => :except) { self.before_array << :except_1 }
40
+ before_action(:except => :only) { self.before_array << :except_2 }
41
+ before_action(:except => [:main, :except]){ self.before_array << :except_3 }
42
+ before_action(:except => [:only, :except]){ self.before_array << :except_4 }
43
+
44
+ attr_accessor :before_array
45
+
46
+ def initialize
47
+ @before_array = []
48
+ end
49
+ def main;end
50
+ def only;end
51
+ def except;end
52
+ end
53
+
54
+ let(:controller) { BeforeActionController.new }
55
+ it 'should handle before_action with no args' do
56
+ controller.instance_variable_set :@_action_name, 'main'
57
+ controller.process_action(:main, nil)
58
+ controller.before_array.should == [:all, :only_3, :except_1, :except_2, :except_4]
59
+ end
60
+
61
+ it 'should handle before_action with :only flag' do
62
+ controller.instance_variable_set :@_action_name, 'only'
63
+ controller.process_action(:only, nil)
64
+ controller.before_array.should == [:all, :only_1, :only_3, :only_4, :except_1, :except_3]
65
+ end
66
+
67
+ it 'should handle before_action with :except flag' do
68
+ controller.instance_variable_set :@_action_name, 'except'
69
+ controller.process_action(:except, nil)
70
+ controller.before_array.should == [:all, :only_2, :only_4, :except_2]
71
+ end
72
+ end
73
+ end
74
+ end