-
Notifications
You must be signed in to change notification settings - Fork 64
/
codec_sio_test.go
211 lines (194 loc) · 4.42 KB
/
codec_sio_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package socketio
import (
"testing"
"utf8"
"fmt"
"bytes"
"os"
)
func frame(data string, json bool) string {
utf8str := utf8.NewString(data)
if json {
return fmt.Sprintf("~m~%d~m~~j~%s", 3+utf8str.RuneCount(), data)
}
return fmt.Sprintf("~m~%d~m~%s", utf8str.RuneCount(), data)
}
type encodeTest struct {
in interface{}
out string
}
var encodeTests = []encodeTest{
{
123,
frame("123", false),
},
{
"hello, world",
frame("hello, world", false),
},
{
"öäö¥£♥",
frame("öäö¥£♥", false),
},
{
"öäö¥£♥",
frame("öäö¥£♥", false),
},
{
heartbeat(123456),
frame("~h~123456", false),
},
{
handshake("abcdefg"),
frame("abcdefg", false),
},
{
true,
frame("true", true),
},
{
struct {
Boolean bool
Str string
Array []int
}{
false,
"string♥",
[]int{1, 2, 3, 4},
},
frame(`{"Boolean":false,"Str":"string♥","Array":[1,2,3,4]}`, true),
},
{
[]byte("hello, world"),
frame("hello, world", false),
},
}
type decodeTestMessage struct {
messageType uint8
data string
heartbeat heartbeat
}
type decodeTest struct {
in string
out []decodeTestMessage
}
// NOTE: if you change these -> adjust the benchmarks
var decodeTests = []decodeTest{
{
frame("", false),
[]decodeTestMessage{{MessageText, "", -1}},
},
{
frame("~h~123", false),
[]decodeTestMessage{{MessageHeartbeat, "123", 123}},
},
{
frame("wadap!", false),
[]decodeTestMessage{{MessageText, "wadap!", -1}},
},
{
frame("♥wadap!", true),
[]decodeTestMessage{{MessageJSON, "♥wadap!", -1}},
},
{
frame("hello, world!", true) + frame("~h~313", false) + frame("♥wadap!", false),
[]decodeTestMessage{
{MessageJSON, "hello, world!", -1},
{MessageHeartbeat, "313", 313},
{MessageText, "♥wadap!", -1},
},
},
{
"1:3::fael!,",
nil,
},
{
frame("wadap!", false),
[]decodeTestMessage{{MessageText, "wadap!", -1}},
},
}
func TestEncode(t *testing.T) {
codec := SIOCodec{}
enc := codec.NewEncoder()
buf := new(bytes.Buffer)
for _, test := range encodeTests {
t.Logf("in=%v out=%s", test.in, test.out)
buf.Reset()
if err := enc.Encode(buf, test.in); err != nil {
t.Fatal("Encode:", err)
}
if string(buf.Bytes()) != test.out {
t.Fatalf("Expected %q but got %q from %q", test.out, string(buf.Bytes()), test.in)
}
}
}
func TestDecode(t *testing.T) {
codec := SIOCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
var messages []Message
var err os.Error
for _, test := range decodeTests {
t.Logf("in=%s out=%v", test.in, test.out)
buf.WriteString(test.in)
if messages, err = dec.Decode(); err != nil {
if test.out == nil {
continue
}
t.Fatal("Decode:", err)
}
if test.out == nil {
t.Fatalf("Expected decode error, but got: %v, %v", messages, err)
}
if len(messages) != len(test.out) {
t.Fatalf("Expected %d messages, but got %d", len(test.out), len(messages))
}
for i, msg := range messages {
if test.out[i].messageType != msg.Type() {
t.Logf("Message was: %#v", msg)
t.Fatalf("Expected type %d but got %d", test.out[i].messageType, msg.Type())
}
if test.out[i].data != msg.Data() {
t.Fatalf("Expected data %q but got %q", test.out[i].data, msg.Data())
}
if test.out[i].messageType == MessageHeartbeat {
if hb, ok := msg.heartbeat(); !ok || test.out[i].heartbeat != hb {
t.Fatalf("Expected heartbeat %d but got %d (%v)", test.out[i].heartbeat, hb, err)
}
}
}
}
}
func TestDecodeStreaming(t *testing.T) {
var messages []Message
var err os.Error
codec := SIOCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
expectNothing := func(written string) {
if messages, err = dec.Decode(); err != nil || messages == nil || len(messages) != 0 {
t.Fatalf("Partial decode failed after writing %s. err=%#v messages=%#v", written, err, messages)
}
}
buf.WriteString("~m~")
expectNothing("~m~")
buf.WriteString("6")
expectNothing("~m~6")
buf.WriteString("~m")
expectNothing("~m~6~m")
buf.WriteString("~")
expectNothing("~m~6~m~")
buf.WriteString("12345")
expectNothing("~m~6~m~12345")
buf.WriteString("6~m~")
messages, err = dec.Decode()
if err != nil {
t.Fatalf("Did not expect errors: %s", err)
}
if messages == nil || len(messages) != 1 {
t.Fatalf("Expected 1 message, got: %#v", messages)
}
if messages[0].Type() != MessageText || messages[0].Data() != "123456" {
t.Fatalf("Expected data 123456 and text, got: %#v", messages[0])
}
}