-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathdialog_client_test.go
125 lines (105 loc) · 3.72 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
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
package sipgo
import (
"context"
"testing"
"github.com/emiago/sipgo/sip"
"github.com/emiago/sipgo/siptest"
"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())
})
}
func BenchmarkDialogDo(b *testing.B) {
ua, _ := NewUA()
cli, _ := NewClient(ua)
cli.TxRequester = &siptest.ClientTxRequester{
OnRequest: func(req *sip.Request) *sip.Response {
return sip.NewResponseFromRequest(req, 200, "OK", nil)
},
}
dua := &DialogUA{
Client: cli,
}
dialog, err := dua.Invite(context.TODO(), sip.Uri{User: "test", Host: "localhost"}, nil)
require.NoError(b, err)
dialog.WaitAnswer(context.TODO(), AnswerOptions{})
b.Run("ACK", func(b *testing.B) {
for i := 0; i < b.N; i++ {
dialog.Ack(context.TODO())
}
})
b.Run("NotSupported", func(b *testing.B) {
for i := 0; i < b.N; i++ {
req := sip.NewRequest(sip.REFER, sip.Uri{User: "refer", Host: "localhost"})
dialog.Do(context.TODO(), req)
}
})
}