Skip to content

Commit

Permalink
fix parse headers names with numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
staskobzar committed Apr 14, 2024
1 parent fb80aef commit f654ca9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 6 additions & 0 deletions parse.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions parse.re
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func Parse(data string) (*Message, error) {
re2c:tags = 1;
CRLF = "\r\n";
alpha = [a-zA-Z];
name = alpha (alpha | "-")+;
alnum = [a-zA-Z0-9];
name = alnum (alnum | "-")+;
value = [^\r\n]+;
* { break }
Expand Down
24 changes: 24 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const rawPack = "Event: Newstate\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" +
Expand Down Expand Up @@ -58,6 +61,27 @@ func TestParseEvent(t *testing.T) {
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) {
Expand Down

0 comments on commit f654ca9

Please sign in to comment.