-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_test.go
161 lines (140 loc) · 4.19 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package goami2
import (
"testing"
"github.com/stretchr/testify/assert"
)
const rawPack = "Event: Newstate\r\n" +
"Privilege: call,all\r\n" +
"Channel: SIP/9170-12-0000003c\r\n" +
"ChannelState: 4\r\n" +
"ChannelStateDesc: Ring\r\n" +
"CallerIDNum: 9170\r\n" +
"CallerIDName: Bud Heller\r\n" +
"ConnectedLineNum: <unknown>\r\n" +
"ConnectedLineName: <unknown>\r\n" +
"Language: fr\r\n" +
"AccountCode: \r\n" +
"Context: default\r\n" +
"Exten: 9898\r\n" +
"Priority: 1\r\n" +
"Report-005: 52212\r\n" +
"Report-006: 65gff5\r\n" +
"00A-LRP-001: local\r\n" +
"Address-IP: 10.0.0.1\r\n" +
"Uniqueid: 1598887681.60\r\n" +
"Linkedid: 1598887681.60\r\n" +
"ChanVariable: realm=\r\n" +
"ChanVariable: SIPDOMAIN=okon.sip.com\r\n" +
"ChanVariable: SIPCALLID=b5ser03agv7huo7faai5\r\n\r\n"
func TestParseEvent(t *testing.T) {
input := "Event: DeviceStateChange\r\n" +
"Privilege: call,all\r\n" +
"Device: SIP/9170-12\r\n" +
"State: INUSE\r\n\r\n"
msg, err := Parse(input)
assert.Nil(t, err)
assert.Equal(t, 4, msg.Len())
t.Run("bool event or response", func(t *testing.T) {
assert.True(t, msg.IsEvent())
assert.False(t, msg.IsResponse())
assert.False(t, msg.IsSuccess())
})
t.Run("fields values", func(t *testing.T) {
assert.Equal(t, "DeviceStateChange", msg.Field("event"))
assert.Equal(t, "call,all", msg.Field("Privilege"))
assert.Equal(t, "SIP/9170-12", msg.Field("device"))
assert.Equal(t, "INUSE", msg.Field("State"))
})
t.Run("Message to string match input", func(t *testing.T) {
assert.Equal(t, input, msg.String())
})
t.Run("Message to bytes match input", func(t *testing.T) {
assert.Equal(t, []byte(input), msg.Byte())
})
t.Run("parse all headers with numbers and dashes in names", func(t *testing.T) {
msg, err := Parse(rawPack)
assert.Nil(t, err)
assert.Equal(t, 23, msg.Len())
assert.Equal(t, "call,all", msg.Field("Privilege"))
assert.Equal(t, "SIP/9170-12-0000003c", msg.Field("Channel"))
assert.Equal(t, "4", msg.Field("ChannelState"))
assert.Equal(t, "Ring", msg.Field("ChannelStateDesc"))
assert.Equal(t, "9170", msg.Field("CallerIDNum"))
sipdomain, _ := msg.Var("SIPDOMAIN")
assert.Equal(t, "okon.sip.com", sipdomain)
sipcallid, _ := msg.Var("SIPCALLID")
assert.Equal(t, "b5ser03agv7huo7faai5", sipcallid)
assert.Equal(t, "Newstate", msg.Field("event"))
assert.Equal(t, "local", msg.Field("00A-LRP-001"))
assert.Equal(t, "52212", msg.Field("report-005"))
assert.Equal(t, "65gff5", msg.Field("Report-006"))
})
}
func TestParseEventOK(t *testing.T) {
tests := map[string][]string{
`endpoint call`: getAmiFixtureCall(),
`call to queue`: getAmiFixtureQueues(),
`queue memeber pause`: getAmiFixtureQueueMembers(),
`conference bridge`: getAmiFixtureConfBridge(),
}
for name, packets := range tests {
t.Run(name, func(t *testing.T) {
for _, input := range packets {
msg, err := Parse(input)
assert.Nil(t, err)
assert.NotNil(t, msg)
}
})
}
}
func TestParseEventFail(t *testing.T) {
tests := map[string]struct {
input string
want string
}{
`empty string`: {
"", "unexpected end of input",
},
`incomplete packet input`: {
"Event: MusicOnHoldStop\r\nPrivilege: call,all\r\nChannel: SIP/1031-12-0000006b\r\nChannelState: 6\r\nChannelStateDesc: Up",
"unexpected end of input",
},
`invalid string`: {
"foo bar", "invalid input",
},
`invalid header`: {
"Event: MoH\r\nPriv:all\r\nChan\r\n\r\n", "invalid input",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
msg, err := Parse(tc.input)
assert.ErrorIs(t, err, ErrAMI)
assert.ErrorContains(t, err, tc.want)
assert.Nil(t, msg)
})
}
}
func TestParseResponse(t *testing.T) {
input := "Response: Success\r\n" +
"ActionID: [email protected]\r\n" +
"EventList: start\r\n" +
"Message: Queue status will follow\r\n\r\n"
msg, err := Parse(input)
assert.Nil(t, err)
assert.Equal(t, 4, msg.Len())
assert.True(t, msg.IsResponse())
assert.True(t, msg.IsSuccess())
}
func BenchmarkParse(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Parse(rawPack)
}
}
func BenchmarkMessageToString(b *testing.B) {
msg, _ := Parse(rawPack)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = msg.String()
}
}