Test Failed
Push — master ( d4cdd3...569253 )
by Vyacheslav
03:17 queued 01:50
created

goagi.TestCmdVerbose   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 1
dl 0
loc 16
rs 9.75
c 0
b 0
f 0
1
package goagi
2
3
import (
4
	"bytes"
5
	"strings"
6
	"testing"
7
8
	"github.com/stretchr/testify/assert"
9
)
10
11
func stubReaderWriter(response string) (*bytes.Buffer, *stubReader, *stubWriter) {
12
	buf := new(bytes.Buffer)
13
	response += "\n"
14
	reader := &stubReader{strings.NewReader(response), 0}
15
	writer := &stubWriter{buf, 0}
16
17
	return buf, reader, writer
18
}
19
20
func mockAGI(response string) (*AGI, *bytes.Buffer) {
21
	buf, r, w := stubReaderWriter(response)
22
	return &AGI{reader: r, writer: w, rwtout: rwDefaultTimeout}, buf
23
}
24
25
const respOk = "200 result=1"
26
27
func TestCmdCommand(t *testing.T) {
28
	buf, r, w := stubReaderWriter(respOk)
29
	agi := &AGI{reader: r, writer: w, rwtout: rwDefaultTimeout * 5}
30
31
	resp, err := agi.Command("ANSWER")
32
	assert.Nil(t, err)
33
	assert.Equal(t, 200, resp.Code())
34
	assert.EqualValues(t, 0, r.tout, "No timeout when reading")
35
	assert.Equal(t, "ANSWER\n", buf.String())
36
}
37
38
func TestCmdAnswer(t *testing.T) {
39
	agi, buf := mockAGI("511 Command Not Permitted")
40
	resp, err := agi.Answer()
41
	assert.Nil(t, err)
42
	assert.Equal(t, 511, resp.Code())
43
	assert.Equal(t, "ANSWER\n", buf.String())
44
}
45
46
func TestCmdAsyncAGIBreak(t *testing.T) {
47
	agi, buf := mockAGI(respOk)
48
	resp, err := agi.AsyncAGIBreak()
49
	assert.Nil(t, err)
50
	assert.Equal(t, 200, resp.Code())
51
	assert.Equal(t, "ASYNCAGI BREAK\n", buf.String())
52
}
53
54
func TestCmdChannelStatus(t *testing.T) {
55
	agi, buf := mockAGI(respOk)
56
	resp, err := agi.ChannelStatus("SIP/0001-FA878E0")
57
	assert.Nil(t, err)
58
	assert.Equal(t, 200, resp.Code())
59
	assert.Equal(t, "CHANNEL STATUS SIP/0001-FA878E0\n", buf.String())
60
}
61
62
func TestCmdControlStreamFile(t *testing.T) {
63
	agi, buf := mockAGI("200 result=1 endpos=10")
64
	resp, err := agi.ControlStreamFile("welcome", "")
65
	assert.Nil(t, err)
66
	assert.Equal(t, 200, resp.Code())
67
	assert.EqualValues(t, 10, resp.EndPos())
68
	assert.Equal(t, "CONTROL STREAM FILE welcome \"\"\n", buf.String())
69
70
	agi, buf = mockAGI("200 result=1 endpos=998877")
71
	resp, _ = agi.ControlStreamFile("welcome", "123", "1500")
72
	assert.EqualValues(t, 998877, resp.EndPos())
73
	assert.Equal(t, `CONTROL STREAM FILE welcome "123" "1500"`+"\n", buf.String())
74
75
	agi, buf = mockAGI("200 result=1 endpos=10")
76
	resp, _ = agi.ControlStreamFile("welcome", "123", "1500", "#", "0", "*", "1600")
77
	assert.Equal(t, `CONTROL STREAM FILE welcome "123" "1500" "#" "0" "*" "1600"`+"\n",
78
		buf.String())
79
80
	_, err = agi.ControlStreamFile("welcome", "123", "1500", "#", "0", "*", "1600", "a", "1")
81
	assert.NotNil(t, err)
82
	assert.Contains(t, err.Error(), "[a 1]")
83
}
84
85
func TestCmdDatabaseDel(t *testing.T) {
86
	agi, buf := mockAGI(respOk)
87
	resp, err := agi.DatabaseDel("channel/sip", "foo")
88
	assert.Nil(t, err)
89
	assert.Equal(t, 200, resp.Code())
90
	assert.Equal(t, "DATABASE DEL channel/sip foo\n", buf.String())
91
}
92
93
func TestCmdDatabaseDelTree(t *testing.T) {
94
	agi, buf := mockAGI(respOk)
95
	resp, err := agi.DatabaseDelTree("channel", "sip")
96
	assert.Nil(t, err)
97
	assert.Equal(t, 200, resp.Code())
98
	assert.Equal(t, "DATABASE DELTREE channel sip\n", buf.String())
99
}
100
101
func TestCmdDatabaseGet(t *testing.T) {
102
	agi, buf := mockAGI("200 result=1 (SIP/router01-000e57a5)\n")
103
	resp, err := agi.DatabaseGet("channel", "sip/111")
104
	assert.Nil(t, err)
105
	assert.Equal(t, 200, resp.Code())
106
	assert.Equal(t, "SIP/router01-000e57a5", resp.Value())
107
	assert.Equal(t, "DATABASE GET channel sip/111\n", buf.String())
108
}
109
110
func TestCmdDatabasePut(t *testing.T) {
111
	agi, buf := mockAGI(respOk)
112
	resp, err := agi.DatabasePut("callwait", "sip/1111", "on")
113
	assert.Nil(t, err)
114
	assert.Equal(t, 200, resp.Code())
115
	assert.Equal(t, "DATABASE PUT callwait sip/1111 on\n", buf.String())
116
}
117
118
func TestCmdExec(t *testing.T) {
119
	agi, buf := mockAGI(respOk)
120
	resp, err := agi.Exec("GoTo", "default,s,1")
121
	assert.Nil(t, err)
122
	assert.Equal(t, 200, resp.Code())
123
	assert.Equal(t, "EXEC GoTo \"default,s,1\"\n", buf.String())
124
}
125
126
func TestCmdGetData(t *testing.T) {
127
	tests := []struct {
128
		response string
129
		file     string
130
		tout     int
131
		max      int
132
		code     int
133
		result   int
134
		value    string
135
		data     string
136
		exec     string
137
	}{
138
		{
139
			"200 result= (timeout)\n",
140
			"mainmenu", 0, 1,
141
			200, 0, "timeout", "",
142
			"GET DATA mainmenu 0 1\n",
143
		}, {
144
			"200 result=*123 (timeout)\n",
145
			"mainmenu", 1500, 4,
146
			200, 0, "timeout", "*123",
147
			"GET DATA mainmenu 1500 4\n",
148
		}, {
149
			"200 result=23\n",
150
			"hollidays", 2000, 2,
151
			200, 0, "", "23",
152
			"GET DATA hollidays 2000 2\n",
153
		}, {
154
			"200 result=-1\n",
155
			"hollidays", 3000, 3,
156
			200, -1, "", "",
157
			"GET DATA hollidays 3000 3\n",
158
		}, {
159
			"200 result=\n", // user press # only
160
			"hollidays", 3000, 3,
161
			200, 0, "", "",
162
			"GET DATA hollidays 3000 3\n",
163
		}, {
164
			"511 Command Not Permitted on a dead channel or intercept routine\n",
165
			"hollidays", 3000, 3,
166
			511, 0, "Command Not Permitted on a dead channel or intercept routine",
167
			"Command Not Permitted on a dead channel or intercept routine",
168
			"GET DATA hollidays 3000 3\n",
169
		},
170
	}
171
172
	for _, tc := range tests {
173
		agi, buf := mockAGI(tc.response)
174
		resp, err := agi.GetData(tc.file, tc.tout, tc.max)
175
		assert.Nil(t, err)
176
		assert.Equal(t, tc.code, resp.Code(), "Code:"+tc.response)
177
		assert.Equal(t, tc.result, resp.Result(), "Result:"+tc.response)
178
		assert.Equal(t, tc.value, resp.Value(), "Value:"+tc.response)
179
		assert.Equal(t, tc.data, resp.Data(), "Data:"+tc.response)
180
		assert.Equal(t, tc.exec, buf.String(), tc.response)
181
	}
182
183
	agi, _ := mockAGI("foo")
184
	resp, err := agi.GetData("hello", 1000, 3)
185
	assert.NotNil(t, err)
186
	assert.Contains(t, err.Error(), "Invalid input")
187
	assert.Nil(t, resp)
188
}
189
190
func TestCmdGetFullVariable(t *testing.T) {
191
	agi, buf := mockAGI("200 result=1 (fr)")
192
	resp, err := agi.GetFullVariable("CHANNEL(language)", "")
193
	assert.Nil(t, err)
194
	assert.Equal(t, 200, resp.Code())
195
	assert.Equal(t, 1, resp.Result())
196
	assert.Equal(t, "fr", resp.Value())
197
	assert.Equal(t, "GET FULL VARIABLE CHANNEL(language)\n", buf.String())
198
199
	agi, buf = mockAGI("200 result=1 (Today is sunny)")
200
	resp, err = agi.GetFullVariable("WEATHER", "SIP/2222-12-00000008")
201
	assert.Nil(t, err)
202
	assert.Equal(t, "Today is sunny", resp.Value())
203
	assert.Equal(t, "GET FULL VARIABLE WEATHER SIP/2222-12-00000008\n", buf.String())
204
}
205
206
func TestCmdGetOption(t *testing.T) {
207
	agi, buf := mockAGI("200 result=0 endpos=7680")
208
	resp, err := agi.GetOption("welcome_menu", "", 500)
209
	assert.Nil(t, err)
210
	assert.Equal(t, 200, resp.Code())
211
	assert.Equal(t, 0, resp.Result())
212
	assert.Equal(t, "GET OPTION welcome_menu \"\" 500\n", buf.String())
213
}
214
215
func TestCmdGetVariable(t *testing.T) {
216
	agi, buf := mockAGI(`200 result=1 ("Alice Foo" <5145553322>)`)
217
	resp, err := agi.GetVariable("CALLERID(all)")
218
	assert.Nil(t, err)
219
	assert.Equal(t, 200, resp.Code())
220
	assert.Equal(t, 1, resp.Result())
221
	assert.Equal(t, "\"Alice Foo\" <5145553322>", resp.Value())
222
	assert.Equal(t, "GET VARIABLE CALLERID(all)\n", buf.String())
223
}
224
225
func TestCmdHangup(t *testing.T) {
226
	agi, buf := mockAGI(respOk)
227
	resp, err := agi.Hangup()
228
	assert.Nil(t, err)
229
	assert.Equal(t, 1, resp.Result())
230
	assert.Equal(t, "HANGUP\n", buf.String())
231
232
	agi, buf = mockAGI(respOk)
233
	resp, err = agi.Hangup("SIP/111-222")
234
	assert.Nil(t, err)
235
	assert.Equal(t, "HANGUP SIP/111-222\n", buf.String())
236
237
	agi, buf = mockAGI(respOk)
238
	resp, err = agi.Hangup("IAX/333-444", "SIP/111-222")
239
	assert.Nil(t, err)
240
	assert.Equal(t, "HANGUP IAX/333-444\n", buf.String())
241
}
242
243
func TestCmdReceiveChar(t *testing.T) {
244
	agi, buf := mockAGI(respOk)
245
	resp, err := agi.ReceiveChar(2000)
246
	assert.Nil(t, err)
247
	assert.Equal(t, 1, resp.Result())
248
	assert.Equal(t, "RECEIVE CHAR 2000\n", buf.String())
249
}
250
251
func TestCmdReceiveText(t *testing.T) {
252
	agi, buf := mockAGI("200 result=1 (Hello Bob)")
253
	resp, err := agi.ReceiveText(2000)
254
	assert.Nil(t, err)
255
	assert.Equal(t, 1, resp.Result())
256
	assert.Equal(t, "Hello Bob", resp.Value())
257
	assert.Equal(t, "RECEIVE TEXT 2000\n", buf.String())
258
}
259
260
func TestCmdRecordFile(t *testing.T) {
261
	tests := []struct {
262
		input              string
263
		file, ftype, digit string
264
		tout, offset       int
265
		beep               bool
266
		silence            int
267
		result             int
268
		value, data        string
269
		endpos             int64
270
		cmd                string
271
	}{
272
		{
273
			"200 result=1 (timeout) endpos=86435\n",
274
			"new_rec", "wav", "029", -1, 0, false, 0,
275
			1, "timeout", "", 86435,
276
			"RECORD FILE new_rec wav \"029\" -1\n",
277
		}, {
278
			"200 result=* (dtmf) endpos=1554\n",
279
			"new_rec", "wav", "*#", 1000, 600, false, 0,
280
			1, "dtmf", "*", 1554,
281
			"RECORD FILE new_rec wav \"*#\" 1000 600\n",
282
		}, {
283
			"200 result=-1 (hangup) endpos=0\n",
284
			"new_rec", "wav", "*#", 1000, 600, true, 500,
285
			-1, "hangup", "", 0,
286
			"RECORD FILE new_rec wav \"*#\" 1000 600 BEEP s=500\n",
287
		}, {
288
			"200 result=4 (dtmf) endpos=0\n",
289
			"new_rec", "wav", "*#4", 1000, 600, false, 500,
290
			1, "dtmf", "4", 0,
291
			"RECORD FILE new_rec wav \"*#4\" 1000 600 s=500\n",
292
		},
293
	}
294
295
	for _, tc := range tests {
296
		agi, buf := mockAGI(tc.input)
297
		resp, err := agi.RecordFile(tc.file, tc.ftype, tc.digit, tc.tout, tc.offset,
298
			tc.beep, tc.silence)
299
		assert.Nil(t, err)
300
		assert.Equal(t, tc.result, resp.Result())
301
		assert.Equal(t, tc.value, resp.Value())
302
		assert.Equal(t, tc.data, resp.Data())
303
		assert.Equal(t, tc.endpos, resp.EndPos())
304
		assert.Equal(t, tc.cmd, buf.String())
305
	}
306
307
	agi, buf := mockAGI("")
308
	resp, err := agi.RecordFile("new_rec", "wav", "", 100, 0, false, 0)
309
	assert.NotNil(t, err)
310
	assert.Nil(t, resp)
311
	assert.Equal(t, "RECORD FILE new_rec wav \"\" 100\n", buf.String())
312
}
313
314
func TestCmdSayAlpha(t *testing.T) {
315
	agi, buf := mockAGI(respOk)
316
	resp, err := agi.SayAlpha("abc", "*1")
317
	assert.Nil(t, err)
318
	assert.Equal(t, 1, resp.Result())
319
	assert.Equal(t, "SAY ALPHA abc \"*1\"\n", buf.String())
320
}
321
322
func TestCmdSayDate(t *testing.T) {
323
	agi, buf := mockAGI(respOk)
324
	resp, err := agi.SayDate("1563844045", "")
325
	assert.Nil(t, err)
326
	assert.Equal(t, 1, resp.Result())
327
	assert.Equal(t, "SAY DATE 1563844045 \"\"\n", buf.String())
328
}
329
330
func TestCmdSayDatetime(t *testing.T) {
331
	agi, buf := mockAGI(respOk)
332
	resp, err := agi.SayDatetime("1563844045", "*#0", "dB", "UTC")
333
	assert.Nil(t, err)
334
	assert.Equal(t, 1, resp.Result())
335
	assert.Equal(t, "SAY DATETIME 1563844045 \"*#0\" \"dB\" \"UTC\"\n", buf.String())
336
}
337
338
func TestCmdSayDigits(t *testing.T) {
339
	agi, buf := mockAGI(respOk)
340
	resp, err := agi.SayDigits("2234", "")
341
	assert.Nil(t, err)
342
	assert.Equal(t, 1, resp.Result())
343
	assert.Equal(t, "SAY DIGITS 2234 \"\"\n", buf.String())
344
}
345
346
func TestCmdSayNumber(t *testing.T) {
347
	agi, buf := mockAGI(respOk)
348
	resp, err := agi.SayNumber("1000", "01")
349
	assert.Nil(t, err)
350
	assert.Equal(t, 1, resp.Result())
351
	assert.Equal(t, "SAY NUMBER 1000 \"01\"\n", buf.String())
352
}
353
354
func TestCmdSayPhonetic(t *testing.T) {
355
	agi, buf := mockAGI(respOk)
356
	resp, err := agi.SayPhonetic("welcome", "")
357
	assert.Nil(t, err)
358
	assert.Equal(t, 1, resp.Result())
359
	assert.Equal(t, "SAY PHONETIC welcome \"\"\n", buf.String())
360
}
361
362
func TestCmdSayTime(t *testing.T) {
363
	agi, buf := mockAGI(respOk)
364
	resp, err := agi.SayTime("1563844046", "*#0123456789")
365
	assert.Nil(t, err)
366
	assert.Equal(t, 1, resp.Result())
367
	assert.Equal(t, "SAY TIME 1563844046 \"*#0123456789\"\n", buf.String())
368
}
369
370
func TestCmdSendImage(t *testing.T) {
371
	agi, buf := mockAGI(respOk)
372
	resp, err := agi.SendImage("logo.png")
373
	assert.Nil(t, err)
374
	assert.Equal(t, 1, resp.Result())
375
	assert.Equal(t, "SEND IMAGE \"logo.png\"\n", buf.String())
376
}
377
378
func TestCmdSendText(t *testing.T) {
379
	agi, buf := mockAGI(respOk)
380
	resp, err := agi.SendText("Hello there!")
381
	assert.Nil(t, err)
382
	assert.Equal(t, 1, resp.Result())
383
	assert.Equal(t, "SEND TEXT \"Hello there!\"\n", buf.String())
384
}
385
386
func TestCmdSetAutoHangup(t *testing.T) {
387
	agi, buf := mockAGI(respOk)
388
	resp, err := agi.SetAutoHangup(0)
389
	assert.Nil(t, err)
390
	assert.Equal(t, 1, resp.Result())
391
	assert.Equal(t, "SET AUTOHANGUP 0\n", buf.String())
392
}
393
394
func TestCmdSetCallerid(t *testing.T) {
395
	agi, buf := mockAGI(respOk)
396
	resp, err := agi.SetCallerid("5145553322")
397
	assert.Nil(t, err)
398
	assert.Equal(t, 1, resp.Result())
399
	assert.Equal(t, "SET CALLERID \"5145553322\"\n", buf.String())
400
}
401
402
func TestCmdSetContext(t *testing.T) {
403
	agi, buf := mockAGI("200 result=0")
404
	resp, err := agi.SetContext("default")
405
	assert.Nil(t, err)
406
	assert.Equal(t, 0, resp.Result())
407
	assert.Equal(t, "SET CONTEXT default\n", buf.String())
408
}
409
410
func TestCmdSetExtension(t *testing.T) {
411
	agi, buf := mockAGI("200 result=0")
412
	resp, err := agi.SetExtension("s")
413
	assert.Nil(t, err)
414
	assert.Equal(t, 0, resp.Result())
415
	assert.Equal(t, "SET EXTENSION s\n", buf.String())
416
}
417
418
func TestCmdSetMusic(t *testing.T) {
419
	tests := []struct {
420
		enable bool
421
		class  string
422
		cmd    string
423
	}{
424
		{true, "", "SET MUSIC on \"\"\n"},
425
		{true, "jazz", "SET MUSIC on \"jazz\"\n"},
426
		{false, "lounge", "SET MUSIC off \"lounge\"\n"},
427
	}
428
429
	for _, tc := range tests {
430
		agi, buf := mockAGI(respOk)
431
		resp, err := agi.SetMusic(tc.enable, tc.class)
432
		assert.Nil(t, err)
433
		assert.Equal(t, 1, resp.Result())
434
		assert.Equal(t, tc.cmd, buf.String())
435
	}
436
}
437
438
func TestCmdSetPriority(t *testing.T) {
439
	agi, buf := mockAGI("200 result=0")
440
	resp, err := agi.SetPriority("1")
441
	assert.Nil(t, err)
442
	assert.Equal(t, 0, resp.Result())
443
	assert.Equal(t, "SET PRIORITY 1\n", buf.String())
444
}
445
446
func TestCmdSetVariable(t *testing.T) {
447
	agi, buf := mockAGI(respOk)
448
	resp, err := agi.SetVariable("CITY", "Toronto")
449
	assert.Nil(t, err)
450
	assert.Equal(t, 1, resp.Result())
451
	assert.Equal(t, "SET VARIABLE CITY \"Toronto\"\n", buf.String())
452
453
	agi, buf = mockAGI(respOk)
454
	resp, err = agi.SetVariable("CALLERID(all)", `"Alice Foo" <5145553322>`)
455
	assert.Nil(t, err)
456
	assert.Equal(t, 1, resp.Result())
457
	assert.Equal(t, `SET VARIABLE CALLERID(all) "\"Alice Foo\" <5145553322>"`+"\n",
458
		buf.String())
459
}
460
461
func TestCmdStreamFile(t *testing.T) {
462
	agi, buf := mockAGI(respOk)
463
	resp, err := agi.StreamFile("rec109234", "", 0)
464
	assert.Nil(t, err)
465
	assert.Equal(t, 1, resp.Result())
466
	assert.Equal(t, "STREAM FILE rec109234 \"\" 0\n", buf.String())
467
}
468
469
func TestCmdTDDMode(t *testing.T) {
470
	tests := []struct {
471
		mode, cmd string
472
	}{
473
		{"on", "TDD MODE on\n"},
474
		{"off", "TDD MODE off\n"},
475
		{"mate", "TDD MODE mate\n"},
476
		{"tdd", "TDD MODE tdd\n"},
477
		{"foo", "TDD MODE off\n"},
478
	}
479
480
	for _, tc := range tests {
481
		agi, buf := mockAGI(respOk)
482
		resp, err := agi.TDDMode(tc.mode)
483
		assert.Nil(t, err)
484
		assert.Equal(t, 1, resp.Result())
485
		assert.Equal(t, tc.cmd, buf.String())
486
	}
487
}
488
489
func TestCmdVerbose(t *testing.T) {
490
	agi, buf := mockAGI(respOk)
491
	agi.Verbose("Debug message", 3)
492
	assert.Equal(t, "VERBOSE \"Debug message\" 3\n", buf.String())
493
494
	agi, buf = mockAGI(respOk)
495
	agi.Verbose("Debug message")
496
	assert.Equal(t, "VERBOSE \"Debug message\" 1\n", buf.String())
497
498
	agi, buf = mockAGI(respOk)
499
	agi.Verbose("Debug message", 0)
500
	assert.Equal(t, "VERBOSE \"Debug message\" 1\n", buf.String())
501
502
	agi, buf = mockAGI(respOk)
503
	agi.Verbose("Debug message", 10)
504
	assert.Equal(t, "VERBOSE \"Debug message\" 1\n", buf.String())
505
}
506
507
func TestCmdWaitForDigit(t *testing.T) {
508
	agi, buf := mockAGI(respOk)
509
	resp, err := agi.WaitForDigit(1000)
510
	assert.Nil(t, err)
511
	assert.Equal(t, 1, resp.Result())
512
	assert.Equal(t, "WAIT FOR DIGIT 1000\n", buf.String())
513
}
514