forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
191 lines (162 loc) · 3.81 KB
/
parser_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package socketio
import (
"bytes"
"github.com/googollee/go-engine.io"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestPacketType(t *testing.T) {
Convey("Type string", t, func() {
So(_CONNECT, ShouldEqual, 0)
So(_CONNECT.String(), ShouldEqual, "connect")
So(_DISCONNECT, ShouldEqual, 1)
So(_DISCONNECT.String(), ShouldEqual, "disconnect")
So(_EVENT, ShouldEqual, 2)
So(_EVENT.String(), ShouldEqual, "event")
So(_ACK, ShouldEqual, 3)
So(_ACK.String(), ShouldEqual, "ack")
So(_ERROR, ShouldEqual, 4)
So(_ERROR.String(), ShouldEqual, "error")
So(_BINARY_EVENT, ShouldEqual, 5)
So(_BINARY_EVENT.String(), ShouldEqual, "binary_event")
So(_BINARY_ACK, ShouldEqual, 6)
So(_BINARY_ACK.String(), ShouldEqual, "binary_ack")
})
}
func TestParser(t *testing.T) {
p := packet{}
var decodeData interface{}
output := ""
message := ""
test := func() {
saver := &FrameSaver{}
encoder := newEncoder(saver)
err := encoder.Encode(p)
So(err, ShouldBeNil)
So(len(saver.data), ShouldBeGreaterThan, 0)
So(saver.data[0].Buffer.String(), ShouldEqual, output)
So(saver.data[0].Type, ShouldEqual, engineio.MessageText)
if len(saver.data) > 1 {
So(saver.data[1].Buffer.String(), ShouldEqual, "data")
So(saver.data[1].Type, ShouldEqual, engineio.MessageBinary)
}
d := packet{Data: decodeData}
decoder := newDecoder(saver)
err = decoder.Decode(&d)
So(err, ShouldBeNil)
So(d.Id, ShouldEqual, p.Id)
So(d.NSP, ShouldEqual, p.NSP)
if decodeData == nil {
So(d.Data, ShouldBeNil)
}
So(decoder.Message(), ShouldEqual, message)
err = decoder.DecodeData(&d)
So(err, ShouldBeNil)
So(d.Type, ShouldEqual, p.Type)
So(decoder.current, ShouldBeNil)
}
Convey("Only type", t, func() {
p = packet{
Type: _CONNECT,
Id: -1,
}
decodeData = nil
output = "0"
message = ""
test()
})
Convey("Type and id", t, func() {
p = packet{
Type: _EVENT,
Id: 1,
}
decodeData = nil
output = "21"
message = ""
test()
})
Convey("Type and namespace", t, func() {
p = packet{
Type: _EVENT,
Id: -1,
NSP: "/abc",
}
decodeData = nil
output = "2/abc"
message = ""
test()
})
Convey("Type, id and namespace", t, func() {
p = packet{
Type: _EVENT,
Id: 1,
NSP: "/abc",
}
decodeData = nil
output = "2/abc,1"
message = ""
test()
})
Convey("Type, namespace and data", t, func() {
p = packet{
Type: _EVENT,
Id: -1,
NSP: "/abc",
Data: []interface{}{"numbers", 1, 2, 3},
}
var i1, i2, i3 int
decodeData = &[]interface{}{&i1, &i2, &i3}
output = "2/abc,[\"numbers\",1,2,3]"
message = "numbers"
test()
So(i1, ShouldEqual, 1)
So(i2, ShouldEqual, 2)
So(i3, ShouldEqual, 3)
})
Convey("Type, namespace, id and data", t, func() {
p = packet{
Type: _EVENT,
Id: 1,
NSP: "/abc",
Data: []interface{}{"numbers", 1, 2, 3},
}
var i1, i2, i3 int
decodeData = &[]interface{}{&i1, &i2, &i3}
output = "2/abc,1[\"numbers\",1,2,3]"
message = "numbers"
test()
So(i1, ShouldEqual, 1)
So(i2, ShouldEqual, 2)
So(i3, ShouldEqual, 3)
})
Convey("Type, namespace, id and data(ack)", t, func() {
p = packet{
Type: _ACK,
Id: 1,
NSP: "/abc",
Data: []interface{}{1, 2, 3},
}
var i1, i2, i3 int
decodeData = &[]interface{}{&i1, &i2, &i3}
output = "3/abc,1[1,2,3]"
message = ""
test()
So(i1, ShouldEqual, 1)
So(i2, ShouldEqual, 2)
So(i3, ShouldEqual, 3)
})
Convey("Binary type with attachment", t, func() {
p = packet{
Type: _EVENT,
Id: 1,
NSP: "/abc",
Data: []interface{}{"binary", &Attachment{Data: bytes.NewBufferString("data")}},
}
buf := bytes.NewBuffer(nil)
decodeData = &[]interface{}{&Attachment{Data: buf}}
output = `51-/abc,1["binary",{"_placeholder":true,"num":0}]`
message = "binary"
test()
So(buf.String(), ShouldEqual, "data")
})
}