AsteriskRuby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,519 @@
1
+ #!/usr/local/bin/ruby -wKU -I ../../lib
2
+ ## Copyright (c) 2007, Vonage Holdings
3
+ ##
4
+ ## All rights reserved.
5
+ ##
6
+ ## Redistribution and use in source and binary forms, with or without
7
+ ## modification, are permitted provided that the following conditions are met:
8
+ ##
9
+ ## * Redistributions of source code must retain the above copyright
10
+ ## notice, this list of conditions and the following disclaimer.
11
+ ## * Redistributions in binary form must reproduce the above copyright
12
+ ## notice, this list of conditions and the following disclaimer in the
13
+ ## documentation and/or other materials provided with the distribution.
14
+ ## * Neither the name of Vonage Holdings nor the names of its
15
+ ## contributors may be used to endorse or promote products derived from this
16
+ ## software without specific prior written permission.
17
+ ##
18
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ ## POSSIBILITY OF SUCH DAMAGE.
29
+ ##
30
+ ## Author: Michael Komitee
31
+
32
+
33
+ require 'test/unit'
34
+ require 'stringio'
35
+ require 'AGI'
36
+
37
+ class TestAGI < Test::Unit::TestCase
38
+
39
+ def test_response
40
+ agi = get_dummy_agi(['200 result=012345','200 result=0','200 result=12345','200 result=1 (hello)'])
41
+ lzresponse = agi.get_data('audiofile')
42
+ oresponse = agi.get_data('audiofile')
43
+ iresponse = agi.get_data('audiofile')
44
+ sresponse = agi.get_variable('foo')
45
+ assert_equal('012345', lzresponse)
46
+ assert_equal(0, oresponse)
47
+ assert_equal(12345, iresponse.to_i)
48
+ assert_equal('hello', sresponse.to_str)
49
+ assert(sresponse =~ /el/)
50
+ assert_nil(sresponse =~ 12)
51
+ assert(!sresponse.nil?)
52
+ assert_equal(12346, iresponse + 1)
53
+ assert_equal(12344, iresponse - 1)
54
+ assert_equal(0, iresponse * 0)
55
+ assert_raise(ZeroDivisionError) { iresponse / 0 }
56
+ assert_equal(12345**2, iresponse **2 )
57
+ assert_equal(12345%2, iresponse%2)
58
+ end
59
+ def test_timeout
60
+ agi = get_dummy_agi(['200 result=(timeout)'])
61
+ assert_raise(AGITimeoutError) { agi.stream_file('myfile') }
62
+ end
63
+ def test_init
64
+ agi = get_dummy_agi
65
+ params = agi.channel_params
66
+ assert_equal('agi://localhost:4573/sample/1/?foo=bar', agi.channel_params['request'])
67
+ assert_equal('en', agi.channel_params['language'])
68
+ assert_equal('"SIPPhone" <1234>', agi.channel_params['callerid'])
69
+ assert_equal('ruby-agi', agi.channel_params['context'])
70
+ assert_equal('s', agi.channel_params['extension'])
71
+ assert_equal('2', agi.channel_params['priority'])
72
+ assert_equal('agi://localhost:4573/sample/1/?foo=bar', params['request'])
73
+ assert_equal('en', params['language'])
74
+ assert_equal('"SIPPhone" <1234>', params['callerid'])
75
+ assert_equal('ruby-agi', params['context'])
76
+ assert_equal('s', params['extension'])
77
+ assert_equal('2', params['priority'])
78
+ assert_raise(AGIInitializeError) { agi.init }
79
+ end
80
+
81
+ def test_reinit
82
+ agi_h = get_dummy_agi
83
+ agi_g = get_dummy_agi(["agi_network: yes",""])
84
+ params = agi_g.channel_params
85
+ assert_equal('agi://localhost:4573/sample/1/?foo=bar', agi_g.channel_params['request'])
86
+ agi_g.reinit
87
+ assert_raise(AGIHangupError) { agi_h.reinit }
88
+ assert_equal('yes', agi_g.channel_params['network'])
89
+ assert_nil(agi_g.channel_params['request'])
90
+ end
91
+ def test_answer
92
+ agi_h = get_dummy_agi
93
+ agi_s = get_dummy_agi(["200 result=0"])
94
+ agi_f = get_dummy_agi(["200 result=1"])
95
+ agi_c = get_dummy_agi(["200 result=-1"])
96
+ response_s = agi_s.answer
97
+ response_f = agi_f.answer
98
+ assert_raise(AGIHangupError) { agi_h.answer }
99
+ assert_equal(0, response_s.native)
100
+ assert_equal(1, response_f.native)
101
+ assert(response_s.success?)
102
+ assert(! response_f.success?)
103
+ assert_raise(AGIChannelError) { agi_c.answer }
104
+ end
105
+
106
+ def test_say_datetime
107
+ agi_h = get_dummy_agi
108
+ agi_f = get_dummy_agi(["200 result=-1"])
109
+ agi_s = get_dummy_agi(["200 result=0"])
110
+ agi_d = get_dummy_agi(["200 result=35"])
111
+ assert_raise(AGIHangupError) { agi_h.say_datetime(Time.now, '#') }
112
+ assert_raise(AGIChannelError) { agi_f.say_datetime(Time.now, '#') }
113
+ assert(agi_s.say_datetime(Time.now, '#').success?)
114
+ assert_equal('#', agi_d.say_datetime(Time.now, '#'))
115
+ end
116
+
117
+ def test_say_date
118
+ agi_h = get_dummy_agi
119
+ agi_f = get_dummy_agi(["200 result=-1"])
120
+ agi_s = get_dummy_agi(["200 result=0"])
121
+ agi_d = get_dummy_agi(["200 result=35"])
122
+ assert_raise(AGIHangupError) { agi_h.say_date(Time.now, '#') }
123
+ assert_raise(AGIChannelError) { agi_f.say_date(Time.now, '#') }
124
+ assert(agi_s.say_date(Time.now, '#').success?)
125
+ assert_equal('#', agi_d.say_date(Time.now, '#'))
126
+ end
127
+
128
+ def test_say_time
129
+ agi_h = get_dummy_agi
130
+ agi_f = get_dummy_agi(["200 result=-1"])
131
+ agi_s = get_dummy_agi(["200 result=0"])
132
+ agi_d = get_dummy_agi(["200 result=35"])
133
+ assert_raise(AGIHangupError) { agi_h.say_time(Time.now, '#') }
134
+ assert_raise(AGIChannelError) { agi_f.say_time(Time.now, '#') }
135
+ assert(agi_s.say_time(Time.now, '#').success?)
136
+ assert_equal('#', agi_d.say_time(Time.now, '#'))
137
+ end
138
+
139
+
140
+ def test_say_alpha
141
+ agi_h = get_dummy_agi
142
+ agi_f = get_dummy_agi(["200 result=-1"])
143
+ agi_s = get_dummy_agi(["200 result=0"])
144
+ agi_d = get_dummy_agi(["200 result=35"])
145
+ assert_raise(AGIHangupError) { agi_h.say_alpha("hi", '#') }
146
+ assert_raise(AGIChannelError) { agi_f.say_alpha("hi", '#') }
147
+ assert(agi_s.say_alpha("hi", '#').success?)
148
+ assert_equal('#', agi_d.say_alpha("hi", '#'))
149
+ end
150
+
151
+ def test_say_phonetic
152
+ agi_h = get_dummy_agi
153
+ agi_f = get_dummy_agi(["200 result=-1"])
154
+ agi_s = get_dummy_agi(["200 result=0"])
155
+ agi_d = get_dummy_agi(["200 result=35"])
156
+ assert_raise(AGIHangupError) { agi_h.say_phonetic("hi", '#') }
157
+ assert_raise(AGIChannelError) { agi_f.say_phonetic("hi", '#') }
158
+ assert(agi_s.say_phonetic("hi", '#').success?)
159
+ assert_equal('#', agi_d.say_phonetic("hi", '#'))
160
+ end
161
+
162
+ def test_say_digits
163
+ agi_h = get_dummy_agi
164
+ agi_f = get_dummy_agi(["200 result=-1"])
165
+ agi_s = get_dummy_agi(["200 result=0"])
166
+ agi_d = get_dummy_agi(["200 result=35"])
167
+ assert_raise(AGIHangupError) { agi_h.say_digits(12345, '#') }
168
+ assert_raise(AGIChannelError) { agi_f.say_digits(12345, '#') }
169
+ assert(agi_s.say_digits(12345, '#').success?)
170
+ assert_equal('#', agi_d.say_digits(12345, '#'))
171
+ end
172
+
173
+ def test_say_number
174
+ agi_h = get_dummy_agi
175
+ agi_f = get_dummy_agi(["200 result=-1"])
176
+ agi_s = get_dummy_agi(["200 result=0"])
177
+ agi_d = get_dummy_agi(["200 result=35"])
178
+ assert_raise(AGIHangupError) { agi_h.say_number(12345, '#') }
179
+ assert_raise(AGIChannelError) { agi_f.say_number(12345, '#') }
180
+ assert(agi_s.say_number(12345, '#').success?)
181
+ assert_equal('#', agi_d.say_number(12345, '#'))
182
+ end
183
+
184
+ def test_receive_char
185
+ agi_h = get_dummy_agi
186
+ agi_f = get_dummy_agi(["200 result=-1"])
187
+ agi_s = get_dummy_agi(["200 result=0"])
188
+ agi_d = get_dummy_agi(["200 result=35"])
189
+ assert_raise(AGIHangupError) { agi_h.receive_char() }
190
+ assert_raise(AGIChannelError) { agi_f.receive_char() }
191
+ assert_raise(AGICommandError) { agi_s.receive_char() }
192
+ assert_equal('#', agi_d.receive_char())
193
+ end
194
+ def test_record_file
195
+ agi_h = get_dummy_agi
196
+ agi_f = get_dummy_agi(["200 result=-1"])
197
+ agi_s = get_dummy_agi(["200 result=0"])
198
+ agi_d = get_dummy_agi(["200 result=35"])
199
+ assert_raise(AGIHangupError) { agi_h.record_file("myfile", "gsm", "#") }
200
+ assert_raise(AGIChannelError) { agi_f.record_file("myfile", "gsm", "#") }
201
+ assert(agi_s.record_file("myfile", "gsm", "#").success?)
202
+ assert_equal('#', agi_d.record_file("myfile", "gsm", "#"))
203
+ end
204
+
205
+ def test_stream_file
206
+ agi_h = get_dummy_agi
207
+ agi_f = get_dummy_agi(["200 result=-1"])
208
+ agi_s = get_dummy_agi(["200 result=0"])
209
+ agi_d = get_dummy_agi(["200 result=35"])
210
+ assert_raise(AGIHangupError) { agi_h.stream_file(["myfile1",'myfile2'], "#") }
211
+ assert_raise(AGIChannelError) { agi_f.stream_file(["myfile1",'myfile2'], "#") }
212
+ assert(agi_s.stream_file(["myfile1",'myfile2'], "#").success?)
213
+ assert_equal('#', agi_d.stream_file(["myfile1",'myfile2'], "#"))
214
+ end
215
+
216
+ def test_get_option
217
+ agi_h = get_dummy_agi
218
+ agi_f = get_dummy_agi(["200 result=-1"])
219
+ agi_s = get_dummy_agi(["200 result=0"])
220
+ agi_d = get_dummy_agi(["200 result=35"])
221
+ assert_raise(AGIHangupError) { agi_h.get_option("myfile", "#") }
222
+ assert_raise(AGIChannelError) { agi_f.get_option("myfile", "#") }
223
+ assert(agi_s.get_option("myfile", "#").success?)
224
+ assert_equal('#', agi_d.get_option("myfile", "#"))
225
+ end
226
+
227
+ def test_wait_for_digit
228
+ agi_h = get_dummy_agi
229
+ agi_f = get_dummy_agi(["200 result=-1"])
230
+ agi_s = get_dummy_agi(["200 result=0"])
231
+ agi_d = get_dummy_agi(["200 result=35"])
232
+ assert_raise(AGIHangupError) { agi_h.wait_for_digit() }
233
+ assert_raise(AGIChannelError) { agi_f.wait_for_digit() }
234
+ assert(agi_s.wait_for_digit().success?)
235
+ assert_equal('#', agi_d.wait_for_digit())
236
+ end
237
+
238
+ def test_control_stream_file
239
+ agi_h = get_dummy_agi
240
+ agi_f = get_dummy_agi(["200 result=-1"])
241
+ agi_s = get_dummy_agi(["200 result=0"])
242
+ agi_d = get_dummy_agi(["200 result=48"])
243
+ assert_raise(AGIHangupError) { agi_h.control_stream_file("myfile", "#") }
244
+ assert_raise(AGIChannelError) { agi_f.control_stream_file("myfile", "#") }
245
+ assert(agi_s.control_stream_file("myfile", "#").success?)
246
+ assert_equal('0', agi_d.control_stream_file("myfile", "0", 3000, '*', '#', '5'))
247
+ end
248
+
249
+ def test_get_variable
250
+ agi_h = get_dummy_agi
251
+ agi_f = get_dummy_agi(["200 result=-1"])
252
+ agi_n = get_dummy_agi(["200 result=0"])
253
+ agi_s = get_dummy_agi(["200 result=1 (variablevalue)"])
254
+ assert_raise(AGIHangupError) { agi_h.get_variable('VARIABLENAME') }
255
+ assert_raise(AGIChannelError) { agi_f.get_variable('VARIABLENAME') }
256
+ assert(! agi_n.get_variable('VARIABLENAME').success?)
257
+ assert_equal('variablevalue', agi_s.get_variable('VARIABLENAME'))
258
+ end
259
+
260
+ def test_get_full_variable
261
+ agi_h = get_dummy_agi
262
+ agi_f = get_dummy_agi(["200 result=-1"])
263
+ agi_n = get_dummy_agi(["200 result=0"])
264
+ agi_s = get_dummy_agi(["200 result=1 (variablevalue)"])
265
+ assert_raise(AGIHangupError) { agi_h.get_full_variable('VARIABLENAME') }
266
+ assert_raise(AGIChannelError) { agi_f.get_full_variable('VARIABLENAME') }
267
+ assert(! agi_n.get_full_variable('VARIABLENAME').success?)
268
+ assert_equal('variablevalue', agi_s.get_full_variable('VARIABLENAME'))
269
+ end
270
+
271
+ def test_database_get
272
+ agi_h = get_dummy_agi
273
+ agi_f = get_dummy_agi(["200 result=-1"])
274
+ agi_n = get_dummy_agi(["200 result=0"])
275
+ agi_s = get_dummy_agi(["200 result=1 (dbvarvalue)"])
276
+ assert_raise(AGIHangupError) { agi_h.database_get('family', 'key') }
277
+ assert_raise(AGIChannelError) { agi_f.database_get('family', 'key') }
278
+ assert(! agi_n.database_get('family', 'key').success?)
279
+ assert_equal('dbvarvalue', agi_s.database_get('family', 'key'))
280
+ end
281
+
282
+ def test_channel_status
283
+ agi_h = get_dummy_agi
284
+ agi_f = get_dummy_agi(["200 result=-1"])
285
+ agi_s = get_dummy_agi(["200 result=0", "200 result=1", "200 result=2", "200 result=3",
286
+ "200 result=4", "200 result=5", "200 result=6", "200 result=7"])
287
+ assert_raise(AGIHangupError) { agi_h.channel_status }
288
+ assert_raise(AGIChannelError) { agi_f.channel_status }
289
+ assert_equal("DOWN, AVAILABLE", agi_s.channel_status)
290
+ assert_equal("DOWN, RESERVED", agi_s.channel_status)
291
+ assert_equal("OFF HOOK", agi_s.channel_status)
292
+ assert_equal("DIGITS DIALED", agi_s.channel_status)
293
+ assert_equal("LINE RINGING", agi_s.channel_status)
294
+ assert_equal("REMOTE RINGING", agi_s.channel_status)
295
+ assert_equal("UP", agi_s.channel_status)
296
+ assert_equal("BUSY", agi_s.channel_status("foo"))
297
+ end
298
+
299
+ def test_database_del
300
+ agi = get_dummy_agi(["200 result=0", "200 result=1", "200 result=-1"])
301
+ assert(! agi.database_del('family', 'key').success?)
302
+ assert(agi.database_del('family', 'key').success?)
303
+ assert_raise(AGIChannelError) { agi.database_del('family', 'key') }
304
+ assert_raise(AGIHangupError) { agi.database_del('family', 'key') }
305
+ end
306
+
307
+ def test_database_deltree
308
+ agi = get_dummy_agi(["200 result=0", "200 result=1", "200 result=-1"])
309
+ assert(! agi.database_deltree('family', 'key').success?)
310
+ assert(agi.database_deltree('family', 'key').success?)
311
+ assert_raise(AGIChannelError) { agi.database_deltree('family', 'key') }
312
+ assert_raise(AGIHangupError) { agi.database_deltree('family') }
313
+ end
314
+
315
+ def test_database_put
316
+ agi = get_dummy_agi(["200 result=0", "200 result=1", "200 result=-1"])
317
+ assert(! agi.database_put('family', 'key','value').success?)
318
+ assert(agi.database_put('family', 'key','value').success?)
319
+ assert_raise(AGIChannelError) { agi.database_put('family', 'key','value') }
320
+ assert_raise(AGIHangupError) { agi.database_put('family', 'key','value') }
321
+ end
322
+
323
+ def test_noop
324
+ agi = get_dummy_agi(["200 result=0", "200 result=1", "200 result=-1","200 result=1"])
325
+ assert(agi.noop().success?)
326
+ assert(agi.noop().success?)
327
+ assert_raise(AGIChannelError) { agi.noop() }
328
+ assert(agi.noop("test").success?)
329
+ assert_raise(AGIHangupError) { agi.noop() }
330
+ end
331
+
332
+ def test_exec
333
+ agi = get_dummy_agi(["200 result=1", "200 result=1 (foo)", "200 result=83", "200 result=-1", "200 result=-2", "500 Invalid Command"])
334
+ assert(agi.exec("testing testing 123...").success?)
335
+ assert_equal('foo', agi.exec("testing testing 123..."))
336
+ assert_equal(83, agi.exec("testing testing 123..."))
337
+ assert_raise(AGIChannelError) { agi.exec("testing testing 123...") }
338
+ assert_raise(AGICommandError) { agi.exec("testing testing 123...") }
339
+ assert_raise(AGICommandError) { agi.exec("testing testing 123...") }
340
+ assert_raise(AGIHangupError) { agi.exec("testing testing 123...") }
341
+ end
342
+
343
+ def test_get_data
344
+ agi = get_dummy_agi(["200 result=123456", "200 result=-1", "200 result=123456", "200 result=12", "200 result=0123"])
345
+ assert_equal(123456, agi.get_data("testaudio"))
346
+ assert_raise(AGIChannelError) { agi.get_data("testaudio") }
347
+ assert_equal(123456, agi.get_data("testaudio", 10))
348
+ assert_equal(12, agi.get_data("testaudio", 10, 2))
349
+ # assert_equal('0123', agi.get_data("testaudio").to_s)
350
+ # assert_raise(AGIHangupError) { agi.get_data("testaudio") }
351
+ end
352
+
353
+ def test_hangup
354
+ agi = get_dummy_agi(["200 result=0", "200 result=1", "200 result=-1"])
355
+ assert(! agi.hangup().success?)
356
+ assert(agi.hangup().success?)
357
+ assert_raise(AGIChannelError) { agi.hangup() }
358
+ assert_raise(AGIHangupError) { agi.hangup() }
359
+ end
360
+
361
+ def test_send_image
362
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
363
+ assert(agi.send_image('testimage').success?)
364
+ assert_raise(AGIChannelError) { agi.send_image('testimage') }
365
+ assert_raise(AGIHangupError) { agi.send_image('testimage') }
366
+ end
367
+
368
+ def test_send_text
369
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
370
+ assert(agi.send_text('testtext').success?)
371
+ assert_raise(AGIChannelError) { agi.send_text('testtext') }
372
+ assert_raise(AGIHangupError) { agi.send_text('testtext') }
373
+ end
374
+
375
+ def test_set_autohangup
376
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
377
+ assert(agi.set_autohangup(100).success?)
378
+ assert_raise(AGIChannelError) { agi.set_autohangup(100) }
379
+ assert_raise(AGIHangupError) { agi.set_autohangup(100) }
380
+ end
381
+
382
+ def test_set_callerid
383
+ agi = get_dummy_agi(["200 result=1", "200 result=-1"])
384
+ assert(agi.set_callerid(12345).success?)
385
+ assert_raise(AGIChannelError) { agi.set_callerid(12345) }
386
+ assert_raise(AGIHangupError) { agi.set_callerid(12345) }
387
+ end
388
+
389
+ def test_set_context
390
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
391
+ assert(agi.set_context('testcontext').success?)
392
+ assert_raise(AGIChannelError) { agi.set_context('testcontext') }
393
+ assert_raise(AGIHangupError) { agi.set_context('testcontext') }
394
+ end
395
+
396
+ def test_set_extension
397
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
398
+ assert(agi.set_extension('testextension').success?)
399
+ assert_raise(AGIChannelError) { agi.set_extension('testextension') }
400
+ assert_raise(AGIHangupError) { agi.set_extension('testextension') }
401
+ end
402
+
403
+ def test_set_priority
404
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
405
+ assert(agi.set_priority('testpriority').success?)
406
+ assert_raise(AGIChannelError) { agi.set_priority('testpriority') }
407
+ assert_raise(AGIHangupError) { agi.set_priority('testpriority') }
408
+ end
409
+
410
+ def test_set_music
411
+ agi = get_dummy_agi(["200 result=0", "200 result=-1"])
412
+ assert(agi.set_music('on').success?)
413
+ assert_raise(AGIChannelError) { agi.set_music('on') }
414
+ assert_raise(AGIHangupError) { agi.set_music('on') }
415
+ end
416
+
417
+ def test_set_variable
418
+ agi = get_dummy_agi(["200 result=1", "200 result=-1"])
419
+ assert(agi.set_variable('variable','value').success?)
420
+ assert_raise(AGIChannelError) { agi.set_variable('variable','value') }
421
+ assert_raise(AGIHangupError) { agi.set_variable('variable','value') }
422
+ end
423
+
424
+ def test_tdd_mode
425
+ agi = get_dummy_agi(["200 result=0", "200 result=-1", "200 result=1"])
426
+ assert_raise(AGIChannelError) { agi.tdd_mode('on') }
427
+ assert_raise(AGIChannelError) { agi.tdd_mode('on') }
428
+ assert(agi.tdd_mode('on').success?)
429
+ assert_raise(AGIHangupError) { agi.tdd_mode('on') }
430
+ end
431
+
432
+ def test_verbose
433
+ agi = get_dummy_agi(["200 result=1", "200 result=-1"])
434
+ assert(agi.verbose('message','level').success?)
435
+ assert_raise(AGIChannelError) { agi.verbose('message','level') }
436
+ assert_raise(AGIHangupError) { agi.verbose('message','level') }
437
+ end
438
+
439
+ def test_background
440
+ agi_h = get_dummy_agi
441
+ agi_f = get_dummy_agi(["200 result=-1"])
442
+ agi_s = get_dummy_agi(["200 result=0"])
443
+ agi_d = get_dummy_agi(["200 result=35"])
444
+ agi_m = get_dummy_agi(["200 result=0","200 result=0","200 result=35"])
445
+ agi_ms = get_dummy_agi(["200 result=0","200 result=0","200 result=0"])
446
+ agi_e = get_dummy_agi(["200 result=(timeout)"])
447
+ assert_raise(AGITimeoutError) { agi_e.background("myfile", "#")}
448
+ assert_raise(AGIHangupError) { agi_h.background("myfile", "#") }
449
+ assert_raise(AGIChannelError) { agi_f.background("myfile", "#") }
450
+ assert_equal('#', agi_m.background(['myfile1','myfile2','myfile3'], '#'))
451
+ assert(agi_s.background("myfile", "#").success?)
452
+ assert(agi_ms.background(["myfile","myfile2",'myfile3'], "#").success?)
453
+ assert_equal('#', agi_d.background("myfile", "#"))
454
+ end
455
+
456
+ def test_background_digits
457
+ agi_h = get_dummy_agi
458
+ agi_s = get_dummy_agi(["200 result=0"])
459
+ assert_raise(AGIHangupError) {agi_h.background_digits("12345")}
460
+ assert(agi_s.background_digits("1").success?)
461
+ end
462
+ private
463
+ def get_dummy_agi(input=nil)
464
+ asterisk_io_in = StringIO.new
465
+ asterisk_io_in << "agi_network: yes\n"
466
+ asterisk_io_in << "agi_request: agi://localhost:4573/sample/1/?foo=bar\n"
467
+ asterisk_io_in << "agi_channel: SIP/5061-00000000\n"
468
+ asterisk_io_in << "agi_language: en\n"
469
+ asterisk_io_in << "agi_type: SIP\n"
470
+ asterisk_io_in << "agi_type: SIP\n"
471
+ asterisk_io_in << "agi_uniqueid: 1234567890\n"
472
+ asterisk_io_in << "agi_callerid: \"SIPPhone\" <1234>\n"
473
+ asterisk_io_in << "agi_dnid: 1\n"
474
+ asterisk_io_in << "agi_rdnis: unknown\n"
475
+ asterisk_io_in << "agi_context: ruby-agi\n"
476
+ asterisk_io_in << "agi_extension: s\n"
477
+ asterisk_io_in << "agi_priority: 2\n"
478
+ asterisk_io_in << "agi_enhanced: 0.0\n"
479
+ asterisk_io_in << "\n"
480
+ unless input.nil?
481
+ input.each { |data| asterisk_io_in << "#{data}\n"}
482
+ end
483
+ asterisk_io_in.rewind
484
+ logger = Logger.new('/dev/null')
485
+ #logger = Logger.new(STDERR)
486
+ agi = AGI.new({:input => asterisk_io_in, :output => StringIO.new, :logger => logger})
487
+ agi.init
488
+ return agi
489
+ end
490
+ end
491
+
492
+
493
+ ## Copyright (c) 2007, Vonage Holdings
494
+ ##
495
+ ## All rights reserved.
496
+ ##
497
+ ## Redistribution and use in source and binary forms, with or without
498
+ ## modification, are permitted provided that the following conditions are met:
499
+ ##
500
+ ## * Redistributions of source code must retain the above copyright
501
+ ## notice, this list of conditions and the following disclaimer.
502
+ ## * Redistributions in binary form must reproduce the above copyright
503
+ ## notice, this list of conditions and the following disclaimer in the
504
+ ## documentation and/or other materials provided with the distribution.
505
+ ## * Neither the name of Vonage Holdings nor the names of its
506
+ ## contributors may be used to endorse or promote products derived from this
507
+ ## software without specific prior written permission.
508
+ ##
509
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
510
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
511
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
512
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
513
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
514
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
515
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
516
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
517
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
518
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
519
+ ## POSSIBILITY OF SUCH DAMAGE.