-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
dialog_client_test.go
94 lines (78 loc) · 2.96 KB
/
dialog_client_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
package sipgo
import (
"context"
"testing"
"github.com/emiago/sipgo/sip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDialogClientRequestRecordRouteHeaders(t *testing.T) {
ua, _ := NewUA()
client, _ := NewClient(ua)
invite := sip.NewRequest(sip.INVITE, sip.Uri{User: "test", Host: "localhost"})
invite.AppendHeader(sip.NewHeader("Contact", "<sip:[email protected]>"))
err := clientRequestBuildReq(client, invite)
require.NoError(t, err)
// assert.Equal(t, "localhost:5060", invite.Source())
assert.Equal(t, "localhost:5060", invite.Destination())
t.Run("LooseRouting", func(t *testing.T) {
resp := sip.NewResponseFromRequest(invite, 200, "OK", nil)
resp.AppendHeader(sip.NewHeader("Contact", "<sip:[email protected]>"))
// Fake some proxy headers
resp.AppendHeader(sip.NewHeader("Record-Route", "<sip:p2.com;lr>"))
resp.AppendHeader(sip.NewHeader("Record-Route", "<sip:p1.com;lr>"))
s := DialogClientSession{
ua: &DialogUA{
Client: client,
},
Dialog: Dialog{
InviteRequest: invite,
InviteResponse: resp,
},
}
// Send canceled request
ctx, cancel := context.WithCancel(context.Background())
cancel()
ack := newAckRequestUAC(s.InviteRequest, s.InviteResponse, nil)
assert.Equal(t, "uas.p2.com:5060", ack.Destination())
s.WriteAck(ctx, ack)
assert.Equal(t, "sip:[email protected]", ack.Recipient.String())
assert.Equal(t, "<sip:p1.com;lr>", ack.Route().Value())
assert.Equal(t, "<sip:p2.com;lr>", ack.GetHeaders("Route")[1].Value())
bye := newByeRequestUAC(s.InviteRequest, s.InviteResponse, nil)
s.Do(ctx, bye)
assert.Equal(t, "sip:[email protected]", bye.Recipient.String())
assert.Equal(t, "<sip:p1.com;lr>", bye.Route().Value())
assert.Equal(t, "<sip:p2.com;lr>", bye.GetHeaders("Route")[1].Value())
})
t.Run("StrictRouting", func(t *testing.T) {
resp := sip.NewResponseFromRequest(invite, 200, "OK", nil)
resp.AppendHeader(sip.NewHeader("Contact", "<sip:[email protected]>"))
// Fake some proxy headers
resp.AppendHeader(sip.NewHeader("Record-Route", "<sip:p2.com;lr>"))
resp.AppendHeader(sip.NewHeader("Record-Route", "<sip:p1.com>"))
s := DialogClientSession{
ua: &DialogUA{
Client: client,
},
Dialog: Dialog{
InviteRequest: invite,
InviteResponse: resp,
},
}
// Send canceled request
ctx, cancel := context.WithCancel(context.Background())
cancel()
ack := newAckRequestUAC(s.InviteRequest, s.InviteResponse, nil)
assert.Equal(t, "uas.p2.com:5060", ack.Destination())
s.WriteAck(ctx, ack)
assert.Equal(t, "sip:p1.com", ack.Recipient.String())
assert.Equal(t, "<sip:p1.com>", ack.Route().Value())
assert.Equal(t, "<sip:p2.com;lr>", ack.GetHeaders("Route")[1].Value())
bye := newByeRequestUAC(s.InviteRequest, s.InviteResponse, nil)
s.Do(ctx, bye)
assert.Equal(t, "sip:p1.com", bye.Recipient.String())
assert.Equal(t, "<sip:p1.com>", bye.Route().Value())
assert.Equal(t, "<sip:p2.com;lr>", bye.GetHeaders("Route")[1].Value())
})
}