|
1
|
|
|
package goagi |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"testing" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/stretchr/testify/assert" |
|
7
|
|
|
) |
|
8
|
|
|
|
|
9
|
|
|
func TestSessionSetup(t *testing.T) { |
|
10
|
|
|
agi := &AGI{} |
|
11
|
|
|
|
|
12
|
|
|
envLen := len(agiSetupInput) - 2 |
|
13
|
|
|
agi.sessionSetup(agiSetupInput) |
|
14
|
|
|
assert.Equal(t, envLen, len(agi.env)) |
|
15
|
|
|
assert.Equal(t, 2, len(agi.arg)) |
|
16
|
|
|
|
|
17
|
|
|
input := agiSetupInput[:5] |
|
18
|
|
|
input = append(input, "fooo: bar") |
|
19
|
|
|
agi.sessionSetup(input) |
|
20
|
|
|
assert.Equal(t, 5, len(agi.env)) |
|
21
|
|
|
|
|
22
|
|
|
input = agiSetupInput[:5] |
|
23
|
|
|
input = append(input, "agi_foo bar") |
|
24
|
|
|
agi.sessionSetup(input) |
|
25
|
|
|
assert.Equal(t, 5, len(agi.env)) |
|
26
|
|
|
|
|
27
|
|
|
input = agiSetupInput[:5] |
|
28
|
|
|
input = append(input, "agi_foo: bar") |
|
29
|
|
|
agi.sessionSetup(input) |
|
30
|
|
|
assert.Equal(t, 6, len(agi.env)) |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
func TestParseResponse(t *testing.T) { |
|
34
|
|
|
tests := []struct { |
|
35
|
|
|
input string |
|
36
|
|
|
code int |
|
37
|
|
|
result int |
|
38
|
|
|
value string |
|
39
|
|
|
data string |
|
40
|
|
|
endpos int64 |
|
41
|
|
|
digit string |
|
42
|
|
|
sres int |
|
43
|
|
|
}{ |
|
44
|
|
|
{"100 result=1 Trying...\n", 100, 1, "Trying...", "Trying...", 0, "", 0}, |
|
45
|
|
|
{"100 result=0\n", 100, 0, "", "", 0, "", 0}, |
|
46
|
|
|
{"100 Trying\n", 100, 0, "Trying", "Trying", 0, "", 0}, |
|
47
|
|
|
{"200 result=1\n", 200, 1, "", "", 0, "", 0}, |
|
48
|
|
|
{"200 result=\n", 200, 0, "", "", 0, "", 0}, |
|
49
|
|
|
|
|
50
|
|
|
{"200 result=1 (hangup)\n", 200, 1, "hangup", "", 0, "", 0}, |
|
51
|
|
|
{"200 result=-1 endpos=11223344\n", 200, -1, "", "", 11223344, "", 0}, |
|
52
|
|
|
{"200 result=-1 endpos=\n", 200, -1, "", "", 0, "", 0}, |
|
53
|
|
|
{"200 result=-1 endpos=asf\n", 200, -1, "", "", 0, "", 0}, |
|
54
|
|
|
{"200 result=1 (en)\n", 200, 1, "en", "", 0, "", 0}, |
|
55
|
|
|
{ |
|
56
|
|
|
"200 result=1 (\"Alice Johnson\" <2233>)\n", |
|
57
|
|
|
200, 1, "\"Alice Johnson\" <2233>", "", 0, "", 0, |
|
58
|
|
|
}, { |
|
59
|
|
|
"200 result=1 (Alice Johnson)\n", |
|
60
|
|
|
200, 1, "Alice Johnson", "", 0, "", 0, |
|
61
|
|
|
}, { |
|
62
|
|
|
"200 result=1 (SIP/9170-12-00000008)\n", |
|
63
|
|
|
200, 1, "SIP/9170-12-00000008", "", 0, "", 0, |
|
64
|
|
|
}, { |
|
65
|
|
|
"200 result=1 (digit) digit=* endpos=998877660\n", |
|
66
|
|
|
200, 1, "digit", "", 998877660, "*", 0, |
|
67
|
|
|
}, { |
|
68
|
|
|
"503 result=-2 Memory allocation failure\n", |
|
69
|
|
|
503, -2, |
|
70
|
|
|
"Memory allocation failure", "Memory allocation failure", |
|
71
|
|
|
0, "", 0, |
|
72
|
|
|
}, { |
|
73
|
|
|
"200 result=1 (speech) endpos=918273 results=123 \n", |
|
74
|
|
|
200, 1, "speech", "", 918273, "", 123, |
|
75
|
|
|
}, { |
|
76
|
|
|
"510 Invalid or unknown command\n", |
|
77
|
|
|
510, 0, |
|
78
|
|
|
"Invalid or unknown command", "Invalid or unknown command", |
|
79
|
|
|
0, "", 0, |
|
80
|
|
|
}, { |
|
81
|
|
|
"510 Error", 510, 0, "Error", "Error", 0, "", 0, |
|
82
|
|
|
}, { |
|
83
|
|
|
"511 Command Not Permitted on a dead channel or intercept routine\n", |
|
84
|
|
|
511, 0, |
|
85
|
|
|
"Command Not Permitted on a dead channel or intercept routine", |
|
86
|
|
|
"Command Not Permitted on a dead channel or intercept routine", |
|
87
|
|
|
0, "", 0, |
|
88
|
|
|
}, { |
|
89
|
|
|
"520 Invalid command syntax. Proper usage not available.\n", |
|
90
|
|
|
520, 0, |
|
91
|
|
|
"Invalid command syntax. Proper usage not available.", |
|
92
|
|
|
"Invalid command syntax. Proper usage not available.", |
|
93
|
|
|
0, "", 0, |
|
94
|
|
|
}, { |
|
95
|
|
|
"520-Invalid command syntax. Proper usage follows:\n" + |
|
96
|
|
|
"Usage: database put <family> <key> <value>\n" + |
|
97
|
|
|
"Adds or updates an entry in the Asterisk database for\n" + |
|
98
|
|
|
"a given family, key, and value.\n" + |
|
99
|
|
|
"520 End of proper usage.\n", |
|
100
|
|
|
520, 0, |
|
101
|
|
|
"Usage: database put <family> <key> <value>\n" + |
|
102
|
|
|
"Adds or updates an entry in the Asterisk database for\n" + |
|
103
|
|
|
"a given family, key, and value.", |
|
104
|
|
|
"Usage: database put <family> <key> <value>\n" + |
|
105
|
|
|
"Adds or updates an entry in the Asterisk database for\n" + |
|
106
|
|
|
"a given family, key, and value.", |
|
107
|
|
|
0, "", 0, |
|
108
|
|
|
}, |
|
109
|
|
|
} |
|
110
|
|
|
agi := &AGI{} |
|
111
|
|
|
|
|
112
|
|
|
for _, tc := range tests { |
|
113
|
|
|
resp, err := agi.parseResponse(tc.input, tc.code) |
|
114
|
|
|
assert.Nil(t, err) |
|
115
|
|
|
assert.Equal(t, tc.code, resp.Code()) |
|
116
|
|
|
assert.Equal(t, tc.result, resp.Result()) |
|
117
|
|
|
assert.Equal(t, tc.value, resp.Value()) |
|
118
|
|
|
assert.Equal(t, tc.data, resp.Data()) |
|
119
|
|
|
assert.Equal(t, tc.input, resp.RawResponse()) |
|
120
|
|
|
assert.Equal(t, tc.endpos, resp.EndPos()) |
|
121
|
|
|
assert.Equal(t, tc.digit, resp.Digit()) |
|
122
|
|
|
assert.Equal(t, tc.sres, resp.SResults()) |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
func TestParseResponseProblems(t *testing.T) { |
|
126
|
|
|
tests := []string{ |
|
127
|
|
|
"", "510", "620 FOO", "30000", |
|
128
|
|
|
} |
|
129
|
|
|
agi := &AGI{} |
|
130
|
|
|
|
|
131
|
|
|
for _, test := range tests { |
|
132
|
|
|
_, err := agi.parseResponse(test, 0) |
|
133
|
|
|
assert.NotNil(t, err) |
|
134
|
|
|
} |
|
135
|
|
|
resp, err := agi.parseResponse("200 result=1 (hangup\n", 200) |
|
136
|
|
|
assert.Nil(t, err) |
|
137
|
|
|
assert.Equal(t, 1, resp.Result()) |
|
138
|
|
|
assert.Empty(t, resp.Value()) |
|
139
|
|
|
|
|
140
|
|
|
resp, err = agi.parseResponse("200 result=1 (dtmf) endpos=3 results=q \n", 200) |
|
141
|
|
|
assert.Nil(t, err) |
|
142
|
|
|
assert.Equal(t, 1, resp.Result()) |
|
143
|
|
|
assert.Equal(t, "dtmf", resp.Value()) |
|
144
|
|
|
assert.Equal(t, 0, resp.SResults()) |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
func TestScanResult(t *testing.T) { |
|
148
|
|
|
tests := []struct { |
|
149
|
|
|
input string |
|
150
|
|
|
remain string |
|
151
|
|
|
result int |
|
152
|
|
|
}{ |
|
153
|
|
|
{"result=0 Trying...\n", "Trying...\n", 0}, |
|
154
|
|
|
{"Command Not Premitted", "Command Not Premitted", 0}, |
|
155
|
|
|
{"result=2", "", 2}, |
|
156
|
|
|
{"result=", "", 0}, |
|
157
|
|
|
{"result= More data", "More data", 0}, |
|
158
|
|
|
{"result=-1 (timeout) Foo", "(timeout) Foo", -1}, |
|
159
|
|
|
{"result=*45#3 some data", "some data", 0}, |
|
160
|
|
|
} |
|
161
|
|
|
for _, test := range tests { |
|
162
|
|
|
remain, result := scanResult(test.input) |
|
163
|
|
|
assert.Equal(t, test.remain, remain, test.input) |
|
164
|
|
|
assert.Equal(t, test.result, result, test.input) |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|