-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathdialog_server_test.go
88 lines (73 loc) · 2.69 KB
/
dialog_server_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
package sipgo
import (
"bytes"
"context"
"io"
"log/slog"
"testing"
"github.com/emiago/sipgo/fakes"
"github.com/emiago/sipgo/sip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDialogServerByeRequest(t *testing.T) {
ua, _ := NewUA()
defer ua.Close()
cli, _ := NewClient(ua)
uasContact := sip.ContactHeader{
Address: sip.Uri{User: "test", Host: "127.0.0.200", Port: 5099},
}
dialogSrv := NewDialogServerCache(cli, uasContact)
invite, _, _ := createTestInvite(t, "sip:[email protected]", "udp", "uas.com:5090")
invite.AppendHeader(&sip.ContactHeader{Address: sip.Uri{Host: "uas", Port: 1234}})
invite.AppendHeader(&sip.RecordRouteHeader{Address: sip.Uri{Host: "P1", Port: 5060}})
invite.AppendHeader(&sip.RecordRouteHeader{Address: sip.Uri{Host: "P2", Port: 5060}})
invite.AppendHeader(&sip.RecordRouteHeader{Address: sip.Uri{Host: "P3", Port: 5060}})
dialog, err := dialogSrv.ReadInvite(invite, sip.NewServerTx("test", invite, nil, slog.Default()))
require.NoError(t, err)
res := sip.NewResponseFromRequest(invite, sip.StatusOK, "OK", nil)
res.AppendHeader(&sip.ContactHeader{Address: sip.Uri{Host: "uac", Port: 9876}})
bye := sip.NewRequest(sip.BYE, invite.Contact().Address)
ctxCanceled, cancel := context.WithCancel(context.Background())
cancel()
// No execution
dialog.TransactionRequest(ctxCanceled, bye)
require.Equal(t, invite.CallID(), bye.CallID())
routes := bye.GetHeaders("Route")
assert.Equal(t, "<sip:P1:5060>", routes[0].Value())
assert.Equal(t, "<sip:P2:5060>", routes[1].Value())
assert.Equal(t, "<sip:P3:5060>", routes[2].Value())
}
func TestDialogServerTransactionCanceled(t *testing.T) {
ua, _ := NewUA()
defer ua.Close()
cli, _ := NewClient(ua)
uasContact := sip.ContactHeader{
Address: sip.Uri{User: "test", Host: "127.0.0.200", Port: 5099},
}
dialogSrv := NewDialogServerCache(cli, uasContact)
invite, _, _ := createTestInvite(t, "sip:[email protected]", "udp", "127.0.0.1:5090")
invite.AppendHeader(&sip.ContactHeader{Address: sip.Uri{Host: "uas", Port: 1234}})
t.Run("TerminatedEarly", func(t *testing.T) {
tx := sip.NewServerTx("test", invite, nil, slog.Default())
tx.Terminate()
_, err := dialogSrv.ReadInvite(invite, tx)
require.Error(t, err)
})
t.Run("TerminatedByCancel", func(t *testing.T) {
conn := &sip.UDPConnection{
PacketConn: &fakes.UDPConn{
Writers: map[string]io.Writer{
"127.0.0.1:5090": bytes.NewBuffer(make([]byte, 0)),
},
},
}
tx := sip.NewServerTx("test", invite, conn, slog.Default())
tx.Init()
err := tx.Receive(newCancelRequest(invite))
require.NoError(t, err)
tx.Terminate()
_, err = dialogSrv.ReadInvite(invite, tx)
require.ErrorIs(t, err, sip.ErrTransactionCanceled)
})
}