forked from yutopp/go-rtmp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_client_test.go
140 lines (118 loc) · 3.08 KB
/
server_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Copyright (c) 2018- yutopp ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//
package rtmp
import (
"fmt"
"io"
"net"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/yutopp/go-amf0"
"github.com/yutopp/go-rtmp/message"
)
const (
chunkSize = 128
)
func TestServerCanAcceptConnect(t *testing.T) {
config := &ConnConfig{
Handler: &ServerCanAcceptConnectHandler{},
Logger: logrus.StandardLogger(),
}
prepareConnection(t, config, func(c *ClientConn) {
err := c.Connect(nil)
assert.Nil(t, err)
})
}
type ServerCanAcceptConnectHandler struct {
DefaultHandler
}
func TestServerCanRejectConnect(t *testing.T) {
config := &ConnConfig{
Handler: &ServerCanRejectConnectHandler{},
Logger: logrus.StandardLogger(),
}
prepareConnection(t, config, func(c *ClientConn) {
err := c.Connect(nil)
assert.Equal(t, &ConnectRejectedError{
TransactionID: 1,
Result: &message.NetConnectionConnectResult{
Properties: message.NetConnectionConnectResultProperties{
FMSVer: "GO-RTMP/0,0,0,0",
Capabilities: 31,
Mode: 1,
},
Information: message.NetConnectionConnectResultInformation{
Level: "error",
Code: "NetConnection.Connect.Failed",
Description: "Connection failed.",
Data: amf0.ECMAArray{"type": "go-rtmp", "version": "master"},
},
},
}, err)
})
}
type ServerCanRejectConnectHandler struct {
DefaultHandler
}
func (h *ServerCanRejectConnectHandler) OnConnect(_ uint32, _ *message.NetConnectionConnect) error {
return fmt.Errorf("Reject")
}
func TestServerCanAcceptCreateStream(t *testing.T) {
config := &ConnConfig{
Handler: &ServerCanAcceptCreateStreamHandler{},
Logger: logrus.StandardLogger(),
ControlState: StreamControlStateConfig{
MaxMessageStreams: 2, // Control and another 1 stream
},
}
prepareConnection(t, config, func(c *ClientConn) {
err := c.Connect(nil)
assert.Nil(t, err)
s0, err := c.CreateStream(nil, chunkSize)
assert.Nil(t, err)
defer s0.Close()
// Rejected because a number of message streams is exceeded the limits
s1, err := c.CreateStream(nil, chunkSize)
assert.Equal(t, &CreateStreamRejectedError{
TransactionID: 2,
Result: &message.NetConnectionCreateStreamResult{
StreamID: 0,
},
}, err)
defer s1.Close()
})
}
type ServerCanAcceptCreateStreamHandler struct {
DefaultHandler
}
func prepareConnection(t *testing.T, config *ConnConfig, f func(c *ClientConn)) {
l, err := net.Listen("tcp", "127.0.0.1:")
assert.Nil(t, err)
srv := NewServer(&ServerConfig{
OnConnect: func(conn net.Conn) (io.ReadWriteCloser, *ConnConfig) {
return conn, config
},
})
defer func() {
err := srv.Close()
assert.Nil(t, err)
}()
go func() {
err := srv.Serve(l)
assert.Equal(t, ErrClosed, err)
}()
c, err := Dial("rtmp", l.Addr().String(), &ConnConfig{
Logger: logrus.StandardLogger(),
})
assert.Nil(t, err)
defer func() {
err := c.Close()
assert.Nil(t, err)
}()
f(c)
}