sipatra 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/base_spec.rb ADDED
@@ -0,0 +1,530 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestApp < Sipatra::Base
4
+ invite /^sip:test_uri$/ do
5
+ block_called
6
+ end
7
+ end
8
+
9
+ def mock_request(method, uri, headers = {})
10
+ unless @mock_request
11
+ @mock_request = mock('MockSipRequest')
12
+ @mock_request.stub!(:method => method, :requestURI => uri)
13
+
14
+ @mock_request.stub!(:getHeader).and_return(nil)
15
+ headers.each_pair { |name, value|
16
+ @mock_request.should_receive(:getHeader).with(name.to_s).any_number_of_times.and_return(value)
17
+ }
18
+ end
19
+ @mock_request.stub!(:respond_to?).with(:getRequest).and_return(false)
20
+ @app.message = @mock_request
21
+ end
22
+
23
+ def mock_response(method, status_code, headers = {})
24
+ unless @mock_response
25
+ @mock_response = mock('MockSipResponse')
26
+ @mock_response.stub!(:method => method, :status => status_code)
27
+ @mock_response.stub!(:getHeader).and_return(nil)
28
+ headers.each_pair { |name, value|
29
+ @mock_response.should_receive(:getHeader).with(name.to_s).any_number_of_times.and_return(value)
30
+ }
31
+ end
32
+ @mock_response.stub! :getRequest # needed to decide if the message is a response or a request
33
+ @app.message = @mock_response
34
+ end
35
+
36
+ describe Sipatra::Base do
37
+ [:ack, :bye, :cancel, :info, :invite, :message,
38
+ :notify, :options, :prack, :publish, :refer,
39
+ :register, :subscribe, :update,
40
+ :request, :response, :helpers, :before, :after].each do |name|
41
+ it "should accept method handler '#{name}'" do
42
+ Sipatra::Base.respond_to?(name).should be_true
43
+ TOPLEVEL_BINDING.eval("private_methods.include? '#{name}'").should be_true
44
+ end
45
+ end
46
+
47
+ it "passes the subclass to configure blocks" do
48
+ ref = nil
49
+ TestApp.configure { |app| ref = app }
50
+ ref.should == TestApp
51
+ end
52
+ end
53
+
54
+ describe 'Sipatra::Base subclasses' do
55
+
56
+ before do
57
+ @app = Class::new(Sipatra::Base).new
58
+ @app.set_bindings nil, nil, mock('SipSessionMock'), mock_request('INVITE', 'sip:uri')
59
+ end
60
+
61
+ subject do
62
+ @app
63
+ end
64
+
65
+ it 'processes requests with do_request' do
66
+ subject.respond_to?(:do_request).should be_true
67
+ end
68
+
69
+ it 'processes responses with do_response' do
70
+ subject.respond_to?(:do_response).should be_true
71
+ end
72
+
73
+ describe '#helpers' do
74
+ it "adds a helper method" do
75
+ subject.class.helpers do
76
+ def a_helper
77
+ a_helper_body
78
+ end
79
+ end
80
+
81
+ subject.respond_to? :a_helper
82
+ end
83
+ end
84
+
85
+ it "session[] should return a session attribute value" do
86
+ @app.session.should_receive(:getAttribute).with('foo').twice.and_return('bar')
87
+
88
+ @app.session['foo'].should == 'bar'
89
+ @app.session[:foo].should == 'bar'
90
+ end
91
+
92
+ it "session[]= should set session attribute value" do
93
+ @app.session.should_receive(:setAttribute).with('foo', 'bar').twice
94
+
95
+ @app.session['foo'] = 'bar'
96
+ @app.session[:foo] = 'bar'
97
+ end
98
+
99
+ it "session[]= should remove a session attribute if value is nil" do
100
+ @app.session.should_receive(:removeAttribute).with('foo').twice
101
+
102
+ @app.session['foo'] = nil
103
+ @app.session[:foo] = nil
104
+ end
105
+
106
+ it "message.uri should return msg.requestUri" do
107
+ @app.message.uri.should == "sip:uri"
108
+ end
109
+
110
+ describe "when receiving do_request (with URI sip:uri)" do
111
+ after do
112
+ subject.do_request
113
+ end
114
+
115
+ it "should pass processing to the next matching handler" do
116
+ subject.class.invite(/sip:uri/) do
117
+ must_be_called1
118
+ pass
119
+ must_not_be_called
120
+ end
121
+ subject.class.invite(/sip:uri/) do
122
+ must_be_called2
123
+ end
124
+
125
+ subject.should_receive(:must_be_called1)
126
+ subject.should_not_receive(:must_not_be_called)
127
+ subject.should_receive(:must_be_called2)
128
+ end
129
+
130
+ it "should stop processing" do
131
+ subject.class.invite(/sip:uri/) do
132
+ must_be_called
133
+ halt
134
+ must_not_be_called1
135
+ end
136
+ subject.class.invite(/sip:uri/) do
137
+ must_not_be_called2
138
+ end
139
+
140
+ subject.should_receive(:must_be_called)
141
+ subject.should_not_receive(:must_not_be_called1)
142
+ subject.should_not_receive(:must_not_be_called2)
143
+ end
144
+ end
145
+ end
146
+
147
+ describe TestApp do
148
+
149
+ describe "when calling do_request" do
150
+ subject do
151
+ @app = TestApp::new
152
+ end
153
+
154
+ after(:each) do
155
+ subject.do_request
156
+ end
157
+
158
+ it "should invoke the handler" do
159
+ subject.message = mock_request('INVITE', 'sip:test_uri')
160
+
161
+ subject.should_receive(:block_called)
162
+ end
163
+
164
+ it "should not invoke the handler" do
165
+ subject.message = mock_request('INVITE', 'sip:wrong_test_uri')
166
+
167
+ subject.should_not_receive(:block_called)
168
+ end
169
+ end
170
+ end
171
+
172
+ describe TestApp do
173
+ it "should add a request handler" do
174
+ TestApp.invite(/sip:new_uri/) {}
175
+ TestApp.register(/sip:new_uri/) {}
176
+
177
+ TestApp.instance_variable_get(:@req_handlers)['INVITE'].size.should == 2
178
+ TestApp.instance_variable_get(:@req_handlers)['REGISTER'].size.should == 1
179
+ end
180
+
181
+ it "should add a response handler" do
182
+ TestApp.response(:invite, /sip:new_uri/) {}
183
+
184
+ TestApp.instance_variable_get(:@resp_handlers)['INVITE'].size.should == 1
185
+ end
186
+
187
+ it "should add a default request handler" do
188
+ TestApp.request {}
189
+ TestApp.instance_variable_get(:@req_handlers)['_'].size.should == 1
190
+ end
191
+
192
+ it "should add a default response handler" do
193
+ TestApp.response {}
194
+ TestApp.instance_variable_get(:@resp_handlers)['_'].size.should == 1
195
+ end
196
+ end
197
+
198
+ describe Sipatra::Base do
199
+ before do
200
+ @app = Class::new(Sipatra::Base).new
201
+ end
202
+
203
+ subject { @app }
204
+
205
+ describe 'params' do
206
+
207
+ before do
208
+ @app.message = mock_request('INVITE', 'sip:+uri-1-2-3:pass@domain.com;params1=test')
209
+ end
210
+
211
+ it 'should not have an empty size by default' do
212
+ subject.params.size.should == 0
213
+ end
214
+
215
+ describe "when receiving do_request (with URI sip:+uri-1-2-3:pass@domain.com;params1=test)" do
216
+ after do
217
+ subject.do_request
218
+ end
219
+
220
+ it "should pass processing through a right matching string " do
221
+ subject.class.invite('sip:(:uri):(:pass)@(:domain);.*') do
222
+ must_be_called
223
+ end
224
+
225
+ subject.should_receive(:must_be_called)
226
+ end
227
+
228
+ it "should pass processing through a right regexp " do
229
+ subject.class.invite(/sip:.*:.*@.*;.*/) do
230
+ must_be_called
231
+ end
232
+
233
+ subject.should_receive(:must_be_called)
234
+ end
235
+
236
+ it "should not be processed through a wrong regexp " do
237
+ subject.class.invite('sip:domain.com') do
238
+ must_not_be_called
239
+ end
240
+
241
+ subject.should_not_receive(:must_not_be_called)
242
+ end
243
+ end
244
+
245
+ describe "when receiving do_request" do
246
+ it "should have access to params between brackets" do
247
+ subject.class.invite('sip:(:user):(:pass)?@(:domain);.*') do
248
+ must_be_called
249
+ end
250
+ subject.should_receive(:must_be_called)
251
+ subject.do_request
252
+
253
+ subject.params[:user].should == "+uri-1-2-3"
254
+ subject.params[:pass].should == "pass"
255
+ subject.params[:domain].should == "domain.com"
256
+ subject.params[:uri].should == nil
257
+ end
258
+ end
259
+
260
+ describe "when receiving do_request" do
261
+ it "should have access to params " do
262
+ subject.class.invite(/sip:(.*):(.*)@([^;]*)(;([^;=]*)=([^;=]*))?/) do
263
+ must_be_called
264
+ end
265
+ subject.should_receive(:must_be_called)
266
+ subject.do_request
267
+
268
+ subject.params[:uri].should == %w(sip:+uri-1-2-3:pass@domain.com;params1=test +uri-1-2-3
269
+ pass domain.com ;params1=test params1 test)
270
+ end
271
+ end
272
+ end
273
+
274
+
275
+ describe 'params with conditions' do
276
+
277
+ before do
278
+ @app.message = mock_request('INVITE', 'sip:user@domain.com',
279
+ :Header1 => 'sip:user:pass@domain.com',
280
+ :Header2 => 'sip:user:pass@domain.com')
281
+ end
282
+
283
+ describe "when receiving do_request" do
284
+ it "handler with conditions on Header1 and Header2 should be called" do
285
+ subject.class.invite /sip:(.*)@(.*)/,
286
+ :Header1 => /sip:(.*):(.*)@(.*)/,
287
+ :Header2 => /sip:(.*)@(.*)/ do
288
+ must_be_called
289
+ end
290
+ subject.should_receive(:must_be_called)
291
+ subject.do_request
292
+
293
+ subject.params.size.should == 3
294
+ subject.params[:uri].should == %w(sip:user@domain.com user domain.com)
295
+ subject.params[:Header1].should == %w(sip:user:pass@domain.com user pass domain.com)
296
+ subject.params[:Header2].should == %w(sip:user:pass@domain.com user:pass domain.com)
297
+ subject.params[:Header3].should == nil
298
+ end
299
+
300
+ it "handler with conditions on Header1 and Header3 should NOT be called" do
301
+ subject.class.invite /sip:(.*)@(.*)/,
302
+ :Header1 => /sip:(.*):(.*)@(.*)/,
303
+ :Header3 => /.*/ do
304
+ must_not_be_called
305
+ end
306
+ subject.should_not_receive(:must_be_called)
307
+ subject.do_request
308
+ end
309
+ end
310
+ end
311
+
312
+ describe 'responses with params' do
313
+
314
+ before do
315
+ @app.message = mock_response 'INVITE', 200,
316
+ :Header1 => 'sip:user:pass@domain.com',
317
+ :Header2 => 'sip:user:pass@domain.com'
318
+ end
319
+
320
+ it 'should have an empty size by default' do
321
+ subject.params.size.should == 0
322
+ end
323
+
324
+ describe "when receiving do_response" do
325
+ after do
326
+ subject.do_response
327
+ end
328
+
329
+ it "should pass processing through a response line " do
330
+ subject.class.response do
331
+ must_be_called
332
+ end
333
+
334
+ subject.should_receive(:must_be_called)
335
+ end
336
+
337
+ it "should pass processing through a response line with the right method " do
338
+ subject.class.response(:INVITE) do
339
+ must_be_called
340
+ end
341
+
342
+ subject.should_receive(:must_be_called)
343
+ end
344
+
345
+ it "should not pass processing through a response line with the wrong method " do
346
+ subject.class.response(:REGISTER) do
347
+ must_not_be_called
348
+ end
349
+
350
+ subject.should_not_receive(:must_not_be_called)
351
+ end
352
+
353
+ it "should pass processing through a response line with the right status code " do
354
+ subject.class.response(:INVITE, 200) do
355
+ must_be_called
356
+ end
357
+
358
+ subject.should_receive(:must_be_called)
359
+ end
360
+
361
+ it "should not pass processing through a response line with a wrong status code " do
362
+ subject.class.response(:INVITE, 400) do
363
+ must_not_be_called
364
+ end
365
+
366
+ subject.should_not_receive(:must_not_be_called)
367
+ end
368
+
369
+ it "should pass processing through a response line with the right status code and no method " do
370
+ subject.class.response(200) do
371
+ must_be_called
372
+ end
373
+
374
+ subject.should_receive(:must_be_called)
375
+ end
376
+
377
+ it "should pass processing through a response line with the wrong status code and no method " do
378
+ subject.class.response(600) do
379
+ must_not_be_called
380
+ end
381
+
382
+ subject.should_not_receive(:must_not_be_called)
383
+ end
384
+
385
+ it "should pass processing through a response line with the wrong status code and no method " do
386
+ subject.class.response(300, :Header => /sip:(.*):(.*)@(.*)/) do
387
+ must_not_be_called
388
+ end
389
+
390
+ subject.should_not_receive(:must_not_be_called)
391
+ end
392
+ end
393
+
394
+ describe "when receiving do_response" do
395
+ it "should have access to params " do
396
+ subject.class.response :INVITE, 200,
397
+ :Header1 => /sip:(.*):(.*)@(.*)/,
398
+ :Header2 => /sip:(.*)@(.*)/ do
399
+ must_be_called
400
+ end
401
+ subject.should_receive(:must_be_called)
402
+ subject.do_response
403
+
404
+ subject.params.size.should == 2
405
+ subject.params[:Header1].should == %w(sip:user:pass@domain.com user pass domain.com)
406
+ subject.params[:Header2].should == %w(sip:user:pass@domain.com user:pass domain.com)
407
+ subject.params[:Header3].should == nil
408
+ end
409
+
410
+ it "should NOT have access to params " do
411
+ subject.class.response :INVITE, 200,
412
+ :Header1 => /sip:(.*):(.*)@(.*)/,
413
+ :Header2 => /sip:(.*)@(.*)/,
414
+ :Header3 => /tel:(.*)/ do
415
+ must_not_be_called
416
+ end
417
+ subject.should_not_receive(:must_not_be_called)
418
+ subject.do_response
419
+ end
420
+
421
+ it "should have access to params when no status nor method are set " do
422
+ subject.class.response(:Header1 => /sip:(.*):(.*)@.*/,
423
+ :Header2 => /sip:(.*)@.*/) do
424
+ must_be_called
425
+ end
426
+ subject.should_receive(:must_be_called)
427
+ subject.do_response
428
+
429
+ subject.params.size.should == 2
430
+ subject.params[:Header1].size.should == 3
431
+ subject.params[:Header2].size.should == 2
432
+ subject.params[:Header3].should == nil
433
+ end
434
+
435
+ it "should have access to params only status is set " do
436
+ subject.class.response(200, :Header1 => /sip:(.*):(.*)@.*/, :Header2 => /sip:(.*)@(.*)/) do
437
+ must_be_called
438
+ end
439
+ subject.should_receive(:must_be_called)
440
+ subject.do_response
441
+
442
+ subject.params.size.should == 2
443
+ subject.params[:Header1].size.should == 3
444
+ subject.params[:Header2].size.should == 3
445
+ end
446
+ end
447
+ end
448
+
449
+ describe "#before" do
450
+ it "should add a before filter" do
451
+ subject.class.before { before_call }
452
+
453
+ subject.should_receive(:before_call).exactly(2)
454
+ subject.class.after_filters.should == []
455
+ subject.class.before_filters.size.should == 1
456
+ mock_request('INVITE', 'sip:uri')
457
+ subject.do_request
458
+ mock_response('INVITE', 200)
459
+ subject.do_response
460
+ end
461
+
462
+ it "should add a before request filter" do
463
+ subject.class.before :request do before_call end
464
+
465
+ subject.should_receive(:before_call).exactly(2)
466
+ subject.class.after_filters.should == []
467
+ subject.class.before_filters.size.should == 1
468
+ mock_request('INVITE', 'sip:uri')
469
+ subject.do_request
470
+ subject.do_request
471
+ mock_response('INVITE', 200)
472
+ subject.do_response
473
+ end
474
+
475
+ it "should add a before response filter" do
476
+ subject.class.before :response do before_call end
477
+
478
+ subject.should_receive(:before_call).exactly(2)
479
+ subject.class.after_filters.should == []
480
+ subject.class.before_filters.size.should == 1
481
+ mock_request('INVITE', 'sip:uri')
482
+ subject.do_request
483
+ mock_response('INVITE', 200)
484
+ subject.do_response
485
+ subject.do_response
486
+ end
487
+ end
488
+
489
+ describe "#after" do
490
+ it "should add an after filter" do
491
+ subject.class.after { after_call }
492
+
493
+ subject.should_receive(:after_call).exactly(2)
494
+ subject.class.before_filters.should == []
495
+ subject.class.after_filters.size.should == 1
496
+ mock_request('INVITE', 'sip:uri')
497
+ subject.do_request
498
+ mock_response('INVITE', 200)
499
+ subject.do_response
500
+ end
501
+
502
+ it "should add an after request filter" do
503
+ subject.class.after :request do after_call end
504
+
505
+ subject.should_receive(:after_call).exactly(2)
506
+ subject.class.before_filters.should == []
507
+ subject.class.after_filters.size.should == 1
508
+ mock_request('INVITE', 'sip:uri')
509
+ subject.do_request
510
+ subject.do_request
511
+ mock_response('INVITE', 200)
512
+ subject.do_response
513
+ end
514
+
515
+ it "should add an after response filter" do
516
+ subject.class.after :response do after_call end
517
+
518
+ subject.should_receive(:after_call).exactly(2)
519
+ subject.class.before_filters.should == []
520
+ subject.class.after_filters.size.should == 1
521
+ mock_request('INVITE', 'sip:uri')
522
+ subject.do_request
523
+ mock_response('INVITE', 200)
524
+ subject.do_response
525
+ subject.do_response
526
+
527
+ end
528
+ end
529
+
530
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ describe Object do
4
+ it_should_behave_like ExtensionsMethods
5
+
6
+ it "should add a request handler to Sipatra::Application" do
7
+ invite(/sip:new_uri/) {}
8
+
9
+ Sipatra::Application.instance_variable_get(:@req_handlers)['INVITE'].size.should == 1
10
+ end
11
+
12
+ it "should add a request handler to Sipatra::Application" do
13
+ response :invite , /sip:new_uri/ do end
14
+
15
+ Sipatra::Application.instance_variable_get(:@resp_handlers)['INVITE'].size.should == 1
16
+ end
17
+
18
+
19
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,46 @@
1
+ $top_level = self
2
+
3
+ $LOAD_PATH << "src/main/resources"
4
+ require 'sipatra'
5
+
6
+ share_as :ExtensionsMethods do
7
+ describe "#helpers" do
8
+ module TestModule1
9
+ def helper1
10
+ end
11
+ end
12
+
13
+ module TestModule2
14
+ def helper2
15
+ end
16
+ end
17
+
18
+ it "should add helpers defined in the block" do
19
+ subject.send :helpers do
20
+ def a_helper
21
+ # TODO
22
+ end
23
+ end
24
+
25
+ Sipatra::Application.instance_methods.include?('a_helper').should be_true
26
+ end
27
+
28
+ it "should add helpers defined in the module" do
29
+ subject.send :helpers, TestModule1, TestModule2
30
+
31
+ Sipatra::Application.instance_methods.include?('helper1').should be_true
32
+ Sipatra::Application.instance_methods.include?('helper2').should be_true
33
+ end
34
+
35
+ it "should add helpers defined in the module and the block" do
36
+ subject.send :helpers, TestModule1, TestModule2 do
37
+ def helper3
38
+ end
39
+ end
40
+
41
+ Sipatra::Application.instance_methods.include?('helper1').should be_true
42
+ Sipatra::Application.instance_methods.include?('helper2').should be_true
43
+ Sipatra::Application.instance_methods.include?('helper3').should be_true
44
+ end
45
+ end
46
+ end