-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
message_test.go
142 lines (130 loc) · 3.86 KB
/
message_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
package neffos
import (
"bytes"
"fmt"
"reflect"
"testing"
)
func TestMessageSerialization(t *testing.T) {
var tests = []struct {
msg Message // in
serialized []byte // out
}{
{ // 0
msg: Message{
Namespace: "default",
Room: "room1",
Event: OnNamespaceConnect,
wait: "0",
},
serialized: []byte("0;default;room1;_OnNamespaceConnect;0;0;"),
},
{ // 1
msg: Message{
Namespace: "default",
Body: []byte("some id"),
Event: OnNamespaceConnect,
},
serialized: []byte(";default;;_OnNamespaceConnect;0;0;some id"),
},
{ // 2
msg: Message{
Namespace: "default",
Event: OnNamespaceDisconnect,
},
serialized: []byte(";default;;_OnNamespaceDisconnect;0;0;"),
},
{ // 3
msg: Message{
Namespace: "default",
Event: "chat",
Body: []byte("text"),
},
serialized: []byte(";default;;chat;0;0;text"),
},
{ // 4
msg: Message{
Namespace: "default",
Event: "chat",
Err: fmt.Errorf("error message"),
isError: true,
},
serialized: []byte(";default;;chat;1;0;error message"),
},
{ // 5
msg: Message{
Namespace: "default",
Event: "chat",
Body: []byte("a body with many ; delimeters; like that;"),
},
serialized: []byte(";default;;chat;0;0;a body with many ; delimeters; like that;"),
},
{ // 6
msg: Message{
Namespace: "",
Event: "chat",
Err: fmt.Errorf("an error message with many ; delimeters; like that;"),
isError: true,
},
serialized: []byte(";;;chat;1;0;an error message with many ; delimeters; like that;"),
},
{ // 7
msg: Message{
Namespace: "default",
Event: "chat",
Body: []byte("body"),
wait: "1",
isNoOp: true,
},
serialized: []byte("1;default;;chat;0;1;body"),
},
}
for i, tt := range tests {
got := serializeMessage(tt.msg)
if !bytes.Equal(got, tt.serialized) {
t.Fatalf("[%d] serialize: expected %s but got %s", i, tt.serialized, got)
}
msg := DeserializeMessage(TextMessage, got, false, false)
if !reflect.DeepEqual(msg, tt.msg) {
t.Fatalf("[%d] deserialize: expected\n%#+v but got\n%#+v", i, tt.msg, msg)
}
}
msg := DeserializeMessage(TextMessage, []byte("default;chat;"), false, false)
if !msg.isInvalid {
t.Fatalf("expected message to be invalid but it seems that it is a valid one")
}
nativeMessage := []byte("a native websocket message")
msg = DeserializeMessage(TextMessage, nativeMessage, true, false)
if msg.isInvalid {
t.Fatalf("expected message to be valid native/raw websocket messageeven")
}
nativeMessage = []byte("0;if;we;have;same;number;of;message;tokens;this should pass")
msg = DeserializeMessage(TextMessage, nativeMessage, true, true)
if msg.isInvalid {
t.Fatalf("expected message to be valid native/raw websocket messageeven")
}
expectedNativeMessage := Message{
Event: OnNativeMessage,
Body: nativeMessage,
IsNative: true,
}
if !reflect.DeepEqual(expectedNativeMessage, msg) {
t.Fatalf("expected a native message to be:\n%#+v\n\tbut got:\n%#+v", expectedNativeMessage, msg)
}
// test escape/unescape.
msg = Message{
Namespace: "contains;semi",
Room: ";this;for sure;",
Event: "thatdoesnot",
}
expectedSerialized := []byte(fmt.Sprintf(";contains%ssemi;%sthis%sfor sure%s;thatdoesnot;0;0;",
messageFieldSeparatorReplacement, messageFieldSeparatorReplacement, messageFieldSeparatorReplacement, messageFieldSeparatorReplacement))
gotSerialized := serializeMessage(msg)
if !bytes.Equal(expectedSerialized, gotSerialized) {
t.Fatalf("expected escaped serialized to be: %s but got: %s", string(expectedSerialized), string(gotSerialized))
}
msgGot := DeserializeMessage(TextMessage, gotSerialized, false, false)
if !reflect.DeepEqual(msg, msgGot) {
t.Fatalf("expected a unescaped message to be:\n%#+v\n\tbut got:\n%#+v", msg, msgGot)
}
}