forked from emiago/sipgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_dialog_test.go
82 lines (67 loc) · 2.1 KB
/
server_dialog_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
package sipgo
import (
"io"
"net"
"testing"
"github.com/livekit/sipgo/fakes"
"github.com/livekit/sipgo/sip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDialog(t *testing.T) {
ua, err := NewUA()
require.Nil(t, err)
srv, err := NewServerDialog(ua)
require.Nil(t, err)
serverReader, serverWriter := io.Pipe()
client1Reader, client1Writer := io.Pipe()
// _, client2Writer := io.Pipe()
//Client1 writes to server and reads response
serverAddr := net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5060}
client1Addr := net.UDPAddr{IP: net.ParseIP("127.0.0.2"), Port: 5060}
// client2Addr := net.UDPAddr{IP: net.ParseIP("127.0.0.3"), Port: 5060}
client1 := &fakes.UDPConn{
LAddr: client1Addr,
RAddr: serverAddr,
Reader: client1Reader,
Writers: map[string]io.Writer{
serverAddr.String(): serverWriter,
},
}
// //Client2 writes to server and reads response
// client2 := &fakes.UDPConn{
// LAddr: client2Addr,
// RAddr: serverAddr,
// Reader: client2Reader,
// Writers: map[string]io.Writer{
// serverAddr.String(): serverWriter,
// },
// }
//Server writes to clients and reads response
serverC := &fakes.UDPConn{
LAddr: serverAddr,
RAddr: client1Addr,
Reader: serverReader,
Writers: map[string]io.Writer{
client1Addr.String(): client1Writer,
// client2Addr.String(): client2Writer,
},
}
go srv.TransportLayer().ServeUDP(serverC)
srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
t.Log("New INVITE request")
// Make all responses
res := sip.NewResponseFromRequest(req, 200, "OK", nil)
tx.Respond(res)
})
ch := make(chan sip.Dialog)
srv.OnDialogChan(ch)
inviteReq, callid, ftag := createTestInvite(t, "sip:[email protected]:5060", "UDP", client1.LocalAddr().String())
client1.TestWriteConn(t, []byte(inviteReq.String()))
d := <-ch
assert.Equal(t, sip.DialogStateEstablished, d.State)
byeReq := createTestBye(t, "sip:[email protected]:5060", "UDP", client1.LocalAddr().String(), callid, ftag, ftag)
client1.TestWriteConn(t, []byte(byeReq.String()))
d = <-ch
assert.Equal(t, sip.DialogStateEnded, d.State)
}