groem 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,318 @@
1
+ require File.join(File.dirname(__FILE__),'..','spec_helper')
2
+
3
+ # Tests against a real Growl server running locally
4
+ # Testing Client
5
+
6
+ module NotifyTestHelper
7
+
8
+ def register_app_with_notifications(app, *args)
9
+ lines = "GNTP/1.0 REGISTER NONE", "Application-Name: #{app}", ""
10
+ args.each do |arg|
11
+ lines << "Notification-Name: #{arg}"
12
+ lines << "Notification-Display-Name: #{arg}"
13
+ lines << "Notification-Enabled: True"
14
+ end
15
+ raw = lines.join("\r\n") + "\r\n"
16
+ req = load_request(raw)
17
+ EM.run {
18
+ connect = Groem::Client.register(req, 'localhost', 23053)
19
+ connect.callback do |resp|
20
+ puts "Note: REGISTER successful"
21
+ EM.stop
22
+ end
23
+ connect.errback do |resp|
24
+ flunk "Expected successful REGISTER response (0), received failure (#{resp[0]})"
25
+ EM.stop
26
+ end
27
+ }
28
+ end
29
+
30
+ def load_request(str)
31
+ klass = Groem::Client.anonymous_request_class
32
+ klass.load(str)
33
+ end
34
+
35
+ def should_send_and_receive_one_response_successfully(req)
36
+ count = 0
37
+ EM.run {
38
+
39
+ connect = Groem::Client.notify(req, 'localhost', 23053)
40
+ connect.callback do |resp|
41
+ count += 1
42
+ puts "Response received back:\n#{resp.inspect}"
43
+ resp[0].to_i.must_equal 0
44
+ EM.stop
45
+ end
46
+ connect.errback do |resp|
47
+ puts "Response received back:\n#{resp.inspect}"
48
+ flunk "Expected successful NOTIFY response (0), received failure (#{resp[0]})"
49
+ EM.stop
50
+ end
51
+
52
+ }
53
+ count.must_equal 1
54
+ end
55
+
56
+ def should_send_and_receive_response_and_callback_successfully(req)
57
+ count = 0
58
+ EM.run {
59
+
60
+ connect = Groem::Client.notify(req, 'localhost', 23053)
61
+ connect.when_ok do |resp|
62
+ count += 1
63
+ puts "Response received back:\n#{resp.inspect}"
64
+ resp[0].to_i.must_equal 0
65
+ end
66
+ connect.when_callback do |resp|
67
+ count += 1
68
+ resp[2].wont_be_empty
69
+ puts "Callback response received back:\n#{resp.inspect}"
70
+ puts "Does this action match what you did? #{resp[2]['Notification-Callback-Result']}"
71
+ EM.stop
72
+ end
73
+ connect.errback do |resp|
74
+ puts "Response received back:\n#{resp.inspect}"
75
+ flunk "Expected successful NOTIFY response (0), received failure (#{resp[0]})"
76
+ EM.stop
77
+ end
78
+ connect.callback { |resp| EM.stop }
79
+
80
+ }
81
+ count.must_equal 2
82
+ end
83
+
84
+ def should_send_and_receive_only_callback_successfully(req)
85
+ count = 0
86
+ EM.run {
87
+
88
+ connect = Groem::Client.notify(req, 'localhost', 23053)
89
+ connect.when_callback do |resp|
90
+ count += 1
91
+ resp[2].wont_be_empty
92
+ puts "Callback response received back:\n#{resp.inspect}"
93
+ puts "Does this action match what you did? #{resp[2]['Notification-Callback-Result']}"
94
+ EM.stop
95
+ end
96
+ connect.errback do |resp|
97
+ puts "Response received back:\n#{resp.inspect}"
98
+ flunk "Expected successful NOTIFY response (0), received failure (#{resp[0]})"
99
+ EM.stop
100
+ end
101
+ connect.callback { |resp| EM.stop }
102
+
103
+ }
104
+ count.must_equal 1
105
+ end
106
+
107
+ end
108
+
109
+ describe 'Sending a NOTIFY request to Growl' do
110
+ describe 'when no callback' do
111
+ include NotifyTestHelper
112
+
113
+ before do
114
+ @input = <<-__________
115
+ GNTP/1.0 NOTIFY NONE
116
+ Application-Name: Test Ruby App
117
+ Notification-Name: Test Notification
118
+ Notification-Title: Title Goes Here
119
+ Notification-ID: #{rand(9999)}
120
+
121
+ __________
122
+ @input_req = load_request(@input)
123
+ end
124
+
125
+ it 'should send and receive one response successfully' do
126
+ register_app_with_notifications("Test Ruby App", "Test Notification")
127
+ puts "Sending request:\n#{@input_req.inspect}"
128
+ should_send_and_receive_one_response_successfully(@input_req)
129
+ end
130
+
131
+ end
132
+
133
+ describe 'when callback' do
134
+ include NotifyTestHelper
135
+
136
+ before do
137
+ @input = <<-__________
138
+ GNTP/1.0 NOTIFY NONE
139
+ Application-Name: Test Ruby App
140
+ Notification-Name: Test Callback
141
+ Notification-Title: Waiting for your response
142
+ Notification-ID: #{rand(9999)}
143
+ Notification-Callback-Context: Context
144
+ Notification-Callback-Context-Type: Type
145
+
146
+ __________
147
+ @input_req = load_request(@input)
148
+ end
149
+
150
+ it 'should send and receive one response successfully' do
151
+ register_app_with_notifications("Test Ruby App", "Test Callback")
152
+ puts "Sending request:\n#{@input_req.inspect}"
153
+ should_send_and_receive_response_and_callback_successfully(@input_req)
154
+ end
155
+
156
+ it 'should send and receive only a callback successfully' do
157
+ register_app_with_notifications("Test Ruby App", "Test Callback")
158
+ puts "Sending request:\n#{@input_req.inspect}"
159
+ should_send_and_receive_only_callback_successfully(@input_req)
160
+ end
161
+
162
+ end
163
+
164
+
165
+ end
166
+
167
+
168
+ # Testing App
169
+
170
+ describe 'Sending a non-callback notification from an App' do
171
+
172
+ before do
173
+ @app = Groem::App.new('Rapster')
174
+ end
175
+
176
+ it 'should send and receive register and notify response successfully' do
177
+ register_count = 0
178
+ notify_count = 0
179
+ callback_count = 0
180
+ @app.when_register do |resp|
181
+ puts "Response received back:\n#{resp.inspect}"
182
+ register_count += 1
183
+ resp[0].to_i.must_equal 0
184
+ end
185
+
186
+ @app.when_register_failed do |resp|
187
+ puts "Response received back:\n#{resp.inspect}"
188
+ flunk 'Expected OK response from REGISTER, got error connecting or ERROR response'
189
+ end
190
+
191
+ @app.register do
192
+ header 'X-Something', 'Foo'
193
+ notification :starting, :enabled => 'True', :text => "Starting..."
194
+ notification :finished do |n|
195
+ n.enabled = 'True'
196
+ n.text = 'Finished!'
197
+ n.callback :finished, :type => 'Boolean'
198
+ end
199
+ end
200
+
201
+ @app.notify(:starting, 'XYZ has started') do |resp|
202
+ puts "Response received back:\n#{resp.inspect}"
203
+ resp.ok? { notify_count += 1 }
204
+ resp.error? { flunk "Expected OK response from NOTIFY, got ERROR #{resp[0]}" }
205
+ resp.callback? { flunk "Expected OK response from NOTIFY, got CALLBACK" }
206
+ end
207
+
208
+ register_count.must_equal 1
209
+ notify_count.must_equal 1
210
+ callback_count.must_equal 0
211
+ end
212
+
213
+ end
214
+
215
+
216
+ describe 'Sending a callback notification from an App' do
217
+
218
+ before do
219
+ @app = Groem::App.new('Sapster')
220
+ end
221
+
222
+ it 'should send and receive register and notify response successfully' do
223
+ register_count = 0
224
+ notify_count = 0
225
+ callback_count = 0
226
+ @app.when_register do |resp|
227
+ puts "Response received back:\n#{resp.inspect}"
228
+ register_count += 1
229
+ resp[0].to_i.must_equal 0
230
+ end
231
+
232
+ @app.when_register_failed do |resp|
233
+ puts "Response received back:\n#{resp.inspect}"
234
+ flunk 'Expected OK response from REGISTER, got error connecting or ERROR response'
235
+ end
236
+
237
+ @app.register do
238
+ header 'X-Something', 'Foo'
239
+ notification :starting, :enabled => 'True', :text => "Starting..."
240
+ notification :finished do |n|
241
+ n.enabled = 'True'
242
+ n.text = 'Finished!'
243
+ n.callback :finished, :type => 'Boolean'
244
+ end
245
+ end
246
+
247
+ @app.when_click :finished do |resp|
248
+ puts "Callback received back:\n#{resp.inspect}"
249
+ callback_count += 1
250
+ end
251
+
252
+ @app.when_close :finished do |resp|
253
+ puts "Callback received back:\n#{resp.inspect}"
254
+ callback_count += 1
255
+ end
256
+
257
+ @app.when_timedout :finished do |resp|
258
+ puts "Callback received back:\n#{resp.inspect}"
259
+ callback_count += 1
260
+ end
261
+
262
+ @app.notify(:finished, 'XYZ has finished') do |resp|
263
+ puts "Response received back:\n#{resp.inspect}"
264
+ resp.ok? { notify_count += 1 }
265
+ resp.error? { flunk "Expected OK response from NOTIFY, got ERROR #{resp[0]}" }
266
+ resp.callback? { flunk "Expected OK response from NOTIFY, got CALLBACK" }
267
+ end
268
+
269
+ register_count.must_equal 1
270
+ notify_count.must_equal 1
271
+ callback_count.must_equal 1
272
+ end
273
+
274
+ it 'should send and receive ad-hoc callback successfully' do
275
+ callback_count = 0
276
+
277
+ @app.register do
278
+ header 'X-Something', 'Foo'
279
+ notification :starting, :enabled => 'True', :text => "Starting..."
280
+ notification :finished do |n|
281
+ n.enabled = 'True'
282
+ n.text = 'Finished!'
283
+ n.callback :finished, :type => 'Boolean'
284
+ end
285
+ end
286
+
287
+ @app.when_click :starting do |resp|
288
+ puts "Callback received back:\n#{resp.inspect}"
289
+ resp.context.must_equal 'XYZ'
290
+ resp.context_type.must_equal 'starting'
291
+ callback_count += 1
292
+ end
293
+
294
+ @app.when_close :starting do |resp|
295
+ puts "Callback received back:\n#{resp.inspect}"
296
+ resp.context.must_equal 'XYZ'
297
+ resp.context_type.must_equal 'starting'
298
+ callback_count += 1
299
+ end
300
+
301
+ @app.when_timedout :starting do |resp|
302
+ puts "Callback received back:\n#{resp.inspect}"
303
+ resp.context.must_equal 'XYZ'
304
+ resp.context_type.must_equal 'starting'
305
+ callback_count += 1
306
+ end
307
+
308
+ @app.notify(:starting, 'XYZ is starting',
309
+ :callback => {:context => 'XYZ'})
310
+
311
+ callback_count.must_equal 1
312
+ @app.notifications[:starting].callback_context.must_be_nil
313
+ @app.notifications[:starting].callback_type.must_be_nil
314
+ end
315
+
316
+ end
317
+
318
+
@@ -0,0 +1,133 @@
1
+ require File.join(File.dirname(__FILE__),'..','spec_helper')
2
+
3
+ # Tests against a real Growl server running locally
4
+
5
+ module RegisterTestHelper
6
+
7
+ def load_request(str)
8
+ klass = Groem::Client.anonymous_request_class
9
+ klass.load(str)
10
+ end
11
+
12
+ def should_send_and_receive_one_response_successfully(req)
13
+ count = 0
14
+ EM.run {
15
+
16
+ connect = Groem::Client.register(req, 'localhost', 23053)
17
+ connect.callback do |resp|
18
+ count += 1
19
+ puts "Response received back:\n#{resp.inspect}"
20
+ resp[0].to_i.must_equal 0
21
+ EM.stop
22
+ end
23
+ connect.errback do |resp|
24
+ puts "Response received back:\n#{resp.inspect}"
25
+ flunk "Expected successful REGISTER response (0), received failure (#{resp[0]})"
26
+ end
27
+
28
+ }
29
+ count.must_equal 1
30
+ end
31
+
32
+ end
33
+
34
+ describe 'Sending a REGISTER request to Growl' do
35
+ describe 'when one notification' do
36
+ include RegisterTestHelper
37
+
38
+ before do
39
+ @input = <<-__________
40
+ GNTP/1.0 REGISTER NONE
41
+ Application-Name: Test Ruby App
42
+
43
+ Notification-Name: Download Complete
44
+ Notification-Display-Name: Download completed
45
+ Notification-Enabled:True
46
+ X-Language : English
47
+ X-Timezone: EDT
48
+
49
+ __________
50
+ @input_req = load_request(@input)
51
+ puts "Sending request:\n#{@input_req.inspect}"
52
+ end
53
+
54
+ it 'should send and receive one response successfully' do
55
+ should_send_and_receive_one_response_successfully(@input_req)
56
+ end
57
+
58
+ end
59
+
60
+ describe 'when three notifications' do
61
+ include RegisterTestHelper
62
+
63
+ before do
64
+ @input = <<-__________
65
+ GNTP/1.0 REGISTER NONE
66
+ Application-Name: Test Ruby App
67
+
68
+ Notification-Name: Download Complete
69
+ Notification-Display-Name: Download completed
70
+ Notification-Enabled:True
71
+ X-Language : English
72
+ X-Timezone: EDT
73
+
74
+ Notification-Name: Download Started
75
+ Notification-Display-Name: Download starting
76
+ Notification-Enabled: False
77
+ Notification-Sticky: True
78
+
79
+ Notification-Name: Download Halfway
80
+ Notification-Display-Name: Almost there...
81
+ Notification-Enabled: True
82
+ Notification-Sticky: False
83
+ Notification-Priority: 0
84
+
85
+ __________
86
+ @input_req = load_request(@input)
87
+ puts "Sending request:\n#{@input_req.inspect}"
88
+ end
89
+
90
+ it 'should send and receive one response successfully' do
91
+ should_send_and_receive_one_response_successfully(@input_req)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ # Testing App
99
+
100
+ describe 'Registering an App with Growl' do
101
+
102
+ before do
103
+ @app = Groem::App.new('Apster')
104
+ end
105
+
106
+ it 'should send and receive one response successfully' do
107
+ count = 0
108
+ @app.when_register do |resp|
109
+ puts "Response received back:\n#{resp.inspect}"
110
+ count += 1
111
+ resp[0].to_i.must_equal 0
112
+ end
113
+
114
+ @app.when_register_failed do |resp|
115
+ puts "Response received back:\n#{resp.inspect}"
116
+ flunk 'Expected OK response, got error connecting or ERROR response'
117
+ end
118
+
119
+ @app.register do
120
+ header 'X-Something', 'Foo'
121
+ notification :starting, :enabled => 'False', :text => "Starting..."
122
+ notification :finished do |n|
123
+ n.enabled = 'True'
124
+ n.text = 'Finished!'
125
+ n.callback :finished, :type => 'Boolean'
126
+ end
127
+ end
128
+
129
+ count.must_equal 1
130
+ end
131
+
132
+
133
+ end