AsteriskRuby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,79 @@
1
+ ## Copyright (c) 2007, Vonage Holdings
2
+ ##
3
+ ## All rights reserved.
4
+ ##
5
+ ## Redistribution and use in source and binary forms, with or without
6
+ ## modification, are permitted provided that the following conditions are met:
7
+ ##
8
+ ## * Redistributions of source code must retain the above copyright
9
+ ## notice, this list of conditions and the following disclaimer.
10
+ ## * Redistributions in binary form must reproduce the above copyright
11
+ ## notice, this list of conditions and the following disclaimer in the
12
+ ## documentation and/or other materials provided with the distribution.
13
+ ## * Neither the name of Vonage Holdings nor the names of its
14
+ ## contributors may be used to endorse or promote products derived from this
15
+ ## software without specific prior written permission.
16
+ ##
17
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ ## POSSIBILITY OF SUCH DAMAGE.
28
+ ##
29
+ ## Author: Michael Komitee
30
+ ---
31
+ :choices:
32
+ - :dtmf: *
33
+ :audio: press-star-to-go-back
34
+ - :dtmf: 1
35
+ :audio:
36
+ - press
37
+ - digits/1
38
+ - for-option-1
39
+ - :dtmf: 2
40
+ :audio:
41
+ - press
42
+ - digits/2
43
+ - for-option-2
44
+ - :dtmf: '#'
45
+ :audio: or-press-pound-to-repeat
46
+ :conclusion: what-is-your-choice
47
+ :introduction:
48
+ - welcome
49
+ - instructions
50
+ :timeout: 5
51
+ :title: "Test Menu"
52
+
53
+ ## Copyright (c) 2007, Vonage Holdings
54
+ ##
55
+ ## All rights reserved.
56
+ ##
57
+ ## Redistribution and use in source and binary forms, with or without
58
+ ## modification, are permitted provided that the following conditions are met:
59
+ ##
60
+ ## * Redistributions of source code must retain the above copyright
61
+ ## notice, this list of conditions and the following disclaimer.
62
+ ## * Redistributions in binary form must reproduce the above copyright
63
+ ## notice, this list of conditions and the following disclaimer in the
64
+ ## documentation and/or other materials provided with the distribution.
65
+ ## * Neither the name of Vonage Holdings nor the names of its
66
+ ## contributors may be used to endorse or promote products derived from this
67
+ ## software without specific prior written permission.
68
+ ##
69
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
70
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
71
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
72
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
73
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
74
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
75
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
77
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
78
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
79
+ ## POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,179 @@
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 'AGIMenu'
36
+
37
+ class TestAGIMenu < Test::Unit::TestCase
38
+ def test_menu_from_filename
39
+ check_menu(get_menu_from_filename)
40
+ end
41
+ def test_menu_from_file
42
+ check_menu(get_menu_from_file)
43
+ end
44
+ def test_menu_from_yaml
45
+ check_menu(get_menu_from_yaml)
46
+ end
47
+ def test_menu_from_hash
48
+ check_menu(get_menu_from_hash)
49
+ end
50
+ def test_menu_build
51
+ check_menu(get_menu_build)
52
+ end
53
+ private
54
+ def check_menu(menu)
55
+ assert_equal(["*", "1", "2", "#"], menu.order)
56
+ initial_order = menu.order
57
+ # invalid order
58
+ assert_raise(AGIMenuFailure) { menu.reorder(['1']) }
59
+ # force it
60
+ menu.reorder(['1'], true)
61
+ assert_equal(['1'], menu.order)
62
+ # reset it back so its useful in future tests
63
+ menu.reorder(initial_order, true)
64
+ assert_equal(["*", "1", "2", "#"], menu.order)
65
+ assert_raise(ArgumentError) { menu.play() }
66
+ # duplicate dtmf
67
+ assert_raise(AGIMenuFailure) { menu.add('*', "duplicate-dtmf") }
68
+ agi, io = get_dummy_agi(15, '*')
69
+ assert_equal('*', menu.play(:agi => agi).data, :introduction => true, :conclusion => true )
70
+ io.rewind
71
+ expected = get_expected_results(:length => 15, :introduction => true, :conclusion => true)
72
+ while line = io.gets
73
+ assert_equal(expected.gets, line)
74
+ end
75
+ agi, io = get_dummy_agi(15, '*')
76
+ assert_equal('*', menu.play(:agi => agi, :introduction => false, :conclusion => false).data )
77
+ io.rewind
78
+ expected = get_expected_results(:length => 15, :introduction => false, :conclusion => false)
79
+ while line = io.gets
80
+ assert_equal(expected.gets, line)
81
+ end
82
+ end
83
+ def get_menu_from_filename
84
+ AGIMenu.new(File.expand_path(File.join(File.dirname(__FILE__), 'config/menu.yaml')))
85
+ end
86
+ def get_menu_from_file
87
+ AGIMenu.new(File.open(File.expand_path(File.join(File.dirname(__FILE__), 'config/menu.yaml'))))
88
+ end
89
+ def get_menu_from_yaml
90
+ AGIMenu.new(File.open( File.expand_path(File.join(File.dirname(__FILE__), 'config/menu.yaml')) ) { |f| YAML::load( f ) })
91
+ end
92
+ def get_menu_from_hash
93
+ hash = {:introduction=>["welcome", "instructions"],
94
+ :conclusion=>"what-is-your-choice",
95
+ :timeout=>5,
96
+ :choices=>
97
+ [{:dtmf=>"*", :audio=>["press-star-to-go-back"]},
98
+ {:dtmf=>'1', :audio=>["press", "digits/1", "for-option-1"]},
99
+ {:dtmf=>'2', :audio=>["press", "digits/2", "for-option-2"]},
100
+ {:dtmf=>"#", :audio=>"or-press-pound-to-repeat"}]}
101
+ return AGIMenu.new(hash)
102
+ end
103
+ def get_menu_build
104
+ menu = AGIMenu.new()
105
+ menu.conclusion = 'what-is-your-choice'
106
+ menu.introduction = ['welcome', 'instructions']
107
+ menu.timeout = 5
108
+ menu.add('*', 'press-star-to-go-back')
109
+ menu.add('1', ['press','digits/1', 'for-option-1'])
110
+ menu.add('2', ['press','digits/2', 'for-option-2'])
111
+ menu.add('#', 'or-press-pound-to-repeat')
112
+ return menu
113
+ end
114
+ def get_dummy_agi(length, char='*')
115
+ dec = char[0]
116
+ asterisk_io_in = StringIO.new
117
+ asterisk_io_out = StringIO.new
118
+ length.times { asterisk_io_in << "200 result=0\n" }
119
+ asterisk_io_in << "200 result=#{dec}\n"
120
+ asterisk_io_in.rewind
121
+ # logger = Logger.new(STDERR)
122
+ logger = Logger.new('/dev/null')
123
+ agi = AGI.new({:input => asterisk_io_in, :output => asterisk_io_out, :logger => logger})
124
+ return agi, asterisk_io_out
125
+ end
126
+ def get_expected_results(conf={})
127
+ length = conf[:length]
128
+ expected = StringIO.new
129
+ if conf[:introduction] then
130
+ expected << "STREAM FILE welcome '*12#'\n" && length -= 1 unless length <= 0
131
+ expected << "STREAM FILE instructions '*12#'\n" && length -= 1 unless length <= 0
132
+ end
133
+ expected << "STREAM FILE press-star-to-go-back '*12#'\n" && length -= 1 unless length <= 0
134
+ expected << "STREAM FILE press '*12#'\n" && length -= 1 unless length <= 0
135
+ expected << "STREAM FILE digits/1 '*12#'\n" && length -= 1 unless length <= 0
136
+ expected << "STREAM FILE for-option-1 '*12#'\n" && length -= 1 unless length <= 0
137
+ expected << "STREAM FILE press '*12#'\n" && length -= 1 unless length <= 0
138
+ expected << "STREAM FILE digits/2 '*12#'\n" && length -= 1 unless length <= 0
139
+ expected << "STREAM FILE for-option-2 '*12#'\n" && length -= 1 unless length <= 0
140
+ expected << "STREAM FILE or-press-pound-to-repeat '*12#'\n" && length -= 1 unless length <= 0
141
+ if conf[:conclusion]
142
+ expected << "STREAM FILE what-is-your-choice '*12#'\n" && length -= 1 unless length <= 0
143
+ end
144
+ while length >= 0
145
+ expected << "WAIT FOR DIGIT 5000\n" && length -= 1
146
+ end
147
+ expected.rewind
148
+ return expected
149
+ end
150
+
151
+ end
152
+
153
+ ## Copyright (c) 2007, Vonage Holdings
154
+ ##
155
+ ## All rights reserved.
156
+ ##
157
+ ## Redistribution and use in source and binary forms, with or without
158
+ ## modification, are permitted provided that the following conditions are met:
159
+ ##
160
+ ## * Redistributions of source code must retain the above copyright
161
+ ## notice, this list of conditions and the following disclaimer.
162
+ ## * Redistributions in binary form must reproduce the above copyright
163
+ ## notice, this list of conditions and the following disclaimer in the
164
+ ## documentation and/or other materials provided with the distribution.
165
+ ## * Neither the name of Vonage Holdings nor the names of its
166
+ ## contributors may be used to endorse or promote products derived from this
167
+ ## software without specific prior written permission.
168
+ ##
169
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
170
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
173
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
174
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
175
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
176
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
177
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
178
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
179
+ ## POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,104 @@
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
+ # Unit test for AGISelection
33
+ require 'test/unit'
34
+ require 'stringio'
35
+ require 'AGISelection'
36
+
37
+
38
+ class TestAGISelection < Test::Unit::TestCase
39
+
40
+ # agi = AGI.new({})
41
+ # # Initialize the AGI or bad things will happen
42
+ # agi.init()
43
+ # yaml_hash = {:audio => 'tt-monkeys', :max_digits => 4}
44
+ # foo = AGISelection.new(yaml_hash)
45
+ # # Override the Audio file from the YAML hash
46
+ # foo.audio = 'somethingelse'
47
+ # foo.read(:agi => agi)
48
+
49
+ def test_read
50
+ agi = get_dummy_agi(["200 result=123456", "200 result=-1", "200 result=1234", "200 result=12"])
51
+ yaml_hash = {:audio => 'tt-monkeys', :max_digits => 6}
52
+ agi_sel = AGISelection.new(yaml_hash)
53
+ assert_equal(123456, agi_sel.read(:agi => agi))
54
+ assert_raise(AGIChannelError) {agi_sel.read(:agi => agi)}
55
+ yaml_hash = {:audio => 'tt-monkeys', :max_digits => 4, :timeout => 60}
56
+ agi_sel = AGISelection.new(yaml_hash)
57
+ assert_equal(1234, agi_sel.read(:agi => agi))
58
+ assert_raise(ArgumentError){agi_sel.read()}
59
+ yaml_hash = {:max_digits => 4, :timeout => 60}
60
+ agi_sel = AGISelection.new(yaml_hash)
61
+ assert_raise(ArgumentError){agi_sel.read(:agi => agi)}
62
+ end
63
+
64
+ private
65
+ def get_dummy_agi(input=nil)
66
+ asterisk_io_in = StringIO.new
67
+ unless input.nil?
68
+ input.each { |data| asterisk_io_in << "#{data}\n"}
69
+ end
70
+ asterisk_io_in.rewind
71
+ agi = AGI.new({ :input => asterisk_io_in,
72
+ :logger => Logger.new('/dev/null'),
73
+ :output => StringIO.new})
74
+ return agi
75
+ end
76
+
77
+ end
78
+ ## Copyright (c) 2007, Vonage Holdings
79
+ ##
80
+ ## All rights reserved.
81
+ ##
82
+ ## Redistribution and use in source and binary forms, with or without
83
+ ## modification, are permitted provided that the following conditions are met:
84
+ ##
85
+ ## * Redistributions of source code must retain the above copyright
86
+ ## notice, this list of conditions and the following disclaimer.
87
+ ## * Redistributions in binary form must reproduce the above copyright
88
+ ## notice, this list of conditions and the following disclaimer in the
89
+ ## documentation and/or other materials provided with the distribution.
90
+ ## * Neither the name of Vonage Holdings nor the names of its
91
+ ## contributors may be used to endorse or promote products derived from this
92
+ ## software without specific prior written permission.
93
+ ##
94
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
95
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
98
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
99
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
100
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
101
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
102
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
103
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
104
+ ## POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,224 @@
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 'stringio'
34
+ require 'AGIServer'
35
+ require "test/unit"
36
+ require 'pp'
37
+
38
+ class TestController < AGIRoute
39
+ def test_route
40
+ agi.answer
41
+ agi.set_variable('foo', params.pretty_inspect.chomp)
42
+ agi.hangup
43
+ end
44
+ private
45
+ def priv_route
46
+ agi.answer
47
+ agi.set_variable('foo', params.pretty_inspect.chomp)
48
+ agi.hangup
49
+ end
50
+ end
51
+
52
+ class NotAController
53
+ def test_route
54
+ agi.answer
55
+ agi.set_variable('foo', params.pretty_inspect.chomp)
56
+ agi.hangup
57
+ end
58
+ private
59
+ def priv_route
60
+ agi.answer
61
+ agi.set_variable('foo', params.pretty_inspect.chomp)
62
+ agi.hangup
63
+ end
64
+ end
65
+
66
+ class TestAsteriskAGIServer < Test::Unit::TestCase
67
+ def test_routed_agiserver
68
+ port = 4573
69
+ agiserver = get_agiserver(port)
70
+ agiserver.start
71
+ client = TCPSocket.new('localhost', port)
72
+ client << "agi_request: agi://localhost/TestController/test_route/1/?foo=bar&foobar=baz\n"
73
+ client << "\n"
74
+ assert_equal("ANSWER\n", client.gets)
75
+ client << "200 result=0\n"
76
+ assert_equal("SET VARIABLE foo {:param1=>\"param1_text\"}\n", client.gets)
77
+ client << "200 result=0\n"
78
+ assert_equal("HANGUP\n", client.gets)
79
+ client << "200 result=1\n"
80
+ client.close
81
+ agiserver.shutdown
82
+ agiserver.finish
83
+
84
+ agiserver = get_agiserver(port)
85
+ agiserver.start
86
+ client = TCPSocket.new('localhost', port)
87
+ client << "agi_request: agi://localhost/NotAController/test_route/1/?foo=bar&foobar=baz\n"
88
+ client << "\n"
89
+ assert_equal("SET EXTENSION i\n", client.gets)
90
+ client << "200 result=1\n"
91
+ assert_equal("SET PRIORITY 1\n", client.gets)
92
+ client << "200 result=1\n"
93
+ client.close
94
+ agiserver.shutdown
95
+ agiserver.finish
96
+
97
+ agiserver = get_agiserver(port)
98
+ agiserver.start
99
+ client = TCPSocket.new('localhost', port)
100
+ client << "agi_request: agi://localhost/NoController/test_route/1/?foo=bar&foobar=baz\n"
101
+ client << "\n"
102
+ assert_equal("SET EXTENSION i\n", client.gets)
103
+ client << "200 result=1\n"
104
+ assert_equal("SET PRIORITY 1\n", client.gets)
105
+ client << "200 result=1\n"
106
+ client.close
107
+ agiserver.shutdown
108
+ agiserver.finish
109
+
110
+ agiserver = get_agiserver(port)
111
+ agiserver.start
112
+ client = TCPSocket.new('localhost', port)
113
+ client << "agi_request: agi://localhost/TestController/no_route/1/?foo=bar&foobar=baz\n"
114
+ client << "\n"
115
+ assert_equal("SET EXTENSION i\n", client.gets)
116
+ client << "200 result=1\n"
117
+ assert_equal("SET PRIORITY 1\n", client.gets)
118
+ client << "200 result=1\n"
119
+ client.close
120
+ agiserver.shutdown
121
+ agiserver.finish
122
+
123
+ agiserver = get_agiserver(port)
124
+ agiserver.start
125
+ client = TCPSocket.new('localhost', port)
126
+ client << "agi_request: agi://localhost/TestController/priv_route/1/?foo=bar&foobar=baz\n"
127
+ client << "\n"
128
+ assert_equal("SET EXTENSION i\n", client.gets)
129
+ client << "200 result=1\n"
130
+ assert_equal("SET PRIORITY 1\n", client.gets)
131
+ client << "200 result=1\n"
132
+ client.close
133
+ agiserver.shutdown
134
+ agiserver.finish
135
+ end
136
+ def test_agiblock_agiserver
137
+ port = 4574
138
+ agiserver = get_agiserver(port)
139
+ agiserver.start do |agi|
140
+ agi.answer
141
+ agi.hangup
142
+ end
143
+ client = TCPSocket.new('localhost', port)
144
+ client << "agi_request: agi://localhost/\n"
145
+ client << "\n"
146
+ assert_equal("ANSWER\n", client.gets)
147
+ client << "200 result=0\n"
148
+ assert_equal("HANGUP\n", client.gets)
149
+ client << "200 result=1\n"
150
+ client.close
151
+ agiserver.shutdown
152
+ agiserver.finish
153
+ end
154
+ def test_agiparamblock_agiserver
155
+ port = 4574
156
+ agiserver = get_agiserver(port)
157
+ agiserver.start do |agi,params|
158
+ agi.answer
159
+ assert_equal("{:param1=>\"param1_text\"}", params.pretty_inspect.chomp)
160
+ agi.hangup
161
+ end
162
+ client = TCPSocket.new('localhost', port)
163
+ client << "agi_request: agi://localhost/\n"
164
+ client << "\n"
165
+ assert_equal("ANSWER\n", client.gets)
166
+ client << "200 result=0\n"
167
+ assert_equal("HANGUP\n", client.gets)
168
+ client << "200 result=1\n"
169
+ client.close
170
+ agiserver.shutdown
171
+ agiserver.finish
172
+ end
173
+ def test_exceptions
174
+ port = 4575
175
+ agiserver1 = get_agiserver(port)
176
+ assert_raise(Errno::EADDRINUSE) { agiserver2 = get_agiserver(port) }
177
+ agiserver1.run
178
+ client = TCPSocket.new('localhost', port)
179
+ client.close
180
+ sleep 10
181
+ agiserver1.shutdown
182
+ end
183
+ private
184
+ def get_agiserver(port=4573)
185
+ config = {:stats => true,
186
+ :bind_port => port,
187
+ :min_workers => 5,
188
+ :jobs_per_worker => 2,
189
+ :max_workers => 10,
190
+ :bind_host => "127.0.0.1",
191
+ :logger => Logger.new('/dev/null'),
192
+ :params => {:param1 => 'param1_text'}
193
+ }
194
+ server = AGIServer.new(config)
195
+ end
196
+ end
197
+
198
+ ## Copyright (c) 2007, Vonage Holdings
199
+ ##
200
+ ## All rights reserved.
201
+ ##
202
+ ## Redistribution and use in source and binary forms, with or without
203
+ ## modification, are permitted provided that the following conditions are met:
204
+ ##
205
+ ## * Redistributions of source code must retain the above copyright
206
+ ## notice, this list of conditions and the following disclaimer.
207
+ ## * Redistributions in binary form must reproduce the above copyright
208
+ ## notice, this list of conditions and the following disclaimer in the
209
+ ## documentation and/or other materials provided with the distribution.
210
+ ## * Neither the name of Vonage Holdings nor the names of its
211
+ ## contributors may be used to endorse or promote products derived from this
212
+ ## software without specific prior written permission.
213
+ ##
214
+ ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
215
+ ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
216
+ ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
217
+ ## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
218
+ ## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
219
+ ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
220
+ ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
221
+ ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
222
+ ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
223
+ ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
224
+ ## POSSIBILITY OF SUCH DAMAGE.