forked from googollee/go-engine.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport_test.go
More file actions
99 lines (83 loc) · 2.32 KB
/
transport_test.go
File metadata and controls
99 lines (83 loc) · 2.32 KB
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
package engineio
import (
"fmt"
"io"
"net/http"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type fakeTransport struct {
name string
conn Conn
isClosed bool
encoder *payloadEncoder
}
func newFakeTransportCreater(ok bool, name string) transportCreateFunc {
return func(http.ResponseWriter, *http.Request) (transport, error) {
if !ok {
return nil, fmt.Errorf("transport %s error", name)
}
return &fakeTransport{
name: name,
encoder: newStringPayloadEncoder(),
}, nil
}
}
func (t *fakeTransport) Name() string {
return t.name
}
func (t *fakeTransport) SetConn(conn Conn) {
t.conn = conn
}
func (t *fakeTransport) ServeHTTP(http.ResponseWriter, *http.Request) {
}
func (t *fakeTransport) Close() error {
t.isClosed = true
return nil
}
func (t *fakeTransport) NextWriter(messageType MessageType, packetType packetType) (io.WriteCloser, error) {
if messageType == MessageText {
return t.encoder.NextString(packetType)
}
return t.encoder.NextBinary(packetType)
}
func TestTransport(t *testing.T) {
t1 := newFakeTransportCreater(true, "t1")
t2 := newFakeTransportCreater(true, "t2")
t3 := newFakeTransportCreater(false, "t3")
registerTransport("t1", true, t1)
registerTransport("t2", false, t2)
registerTransport("t3", true, t3)
tt, _ := newTransportsType([]string{"t1", "t2"})
Convey("Create transports type", t, func() {
t, err := newTransportsType(nil)
So(err, ShouldBeNil)
So(len(t), ShouldEqual, 5)
var names []string
for n := range t {
names = append(names, n)
}
So(names, ShouldContain, "t1")
So(names, ShouldContain, "t2")
So(names, ShouldContain, "t3")
So(names, ShouldContain, "polling")
So(names, ShouldContain, "websocket")
_, err = newTransportsType([]string{"t1", "t2"})
So(err, ShouldBeNil)
_, err = newTransportsType([]string{"t1", "nonexist"})
So(err.Error(), ShouldEqual, "invalid transport name nonexist")
})
Convey("Test upgrades", t, func() {
So(tt.Upgrades(), ShouldResemble, []string{"t1"})
})
Convey("Test get creater", t, func() {
So(tt.GetCreater("t1"), ShouldEqual, t1)
So(tt.GetCreater("t2"), ShouldEqual, t2)
So(tt.GetCreater("nonexit"), ShouldBeNil)
})
Convey("Test get upgrade", t, func() {
So(tt.GetUpgrade("t1"), ShouldEqual, t1)
So(tt.GetUpgrade("t2"), ShouldBeNil)
So(tt.GetUpgrade("nonexit"), ShouldBeNil)
})
}