-
Notifications
You must be signed in to change notification settings - Fork 8
/
message_test.go
320 lines (302 loc) · 6.99 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package gobayeux
import (
"testing"
"time"
)
func TestMessage_TimestampAsTime(t *testing.T) {
m := Message{Timestamp: "2020-05-01T06:28:51.00"}
got, err := m.TimestampAsTime()
if err != nil {
t.Errorf("expected a valid timestamp, got err %q", err)
}
if want := time.Date(2020, time.May, 1, 6, 28, 51, 0, time.UTC); want != got {
t.Errorf("unexpected time parse; want %v, got %v", want, got)
}
}
func TestMessage_ParseError(t *testing.T) {
testCases := []struct {
name string
errorStr string
expected MessageError
shouldErr bool
}{
// Examples taken from specification
{
"no error args",
"401::No client ID",
MessageError{401, []string{""}, "No client ID"},
false,
},
{
"one nonsense error arg",
"402:xj3sjdsjdsjad:Unknown Client ID",
MessageError{402, []string{"xj3sjdsjdsjad"}, "Unknown Client ID"},
false,
},
{
"two args",
"403:xj3sjdsjdsjad,/foo/bar:Subscription denied",
MessageError{403, []string{"xj3sjdsjdsjad", "/foo/bar"}, "Subscription denied"},
false,
},
{
"one channel name arg",
"404:/foo/bar:Unknown Channel",
MessageError{404, []string{"/foo/bar"}, "Unknown Channel"},
false,
},
// Following cases aren't from the specification directly
{
"invalid status code",
"4o4:/foo/bar:Broken Error Code",
MessageError{},
true,
},
{
"invalid error string",
"404-/foo/bar-Unknown Channel",
MessageError{},
true,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
m := Message{Error: tc.errorStr}
got, err := m.ParseError()
if err != nil && tc.shouldErr {
return
}
if err != nil && !tc.shouldErr {
t.Errorf("expected a parsed MessageError but got an err: %q", err)
}
if err == nil && tc.shouldErr {
t.Error("expected an error but didn't get one")
}
want := tc.expected
if want.ErrorCode != got.ErrorCode {
t.Errorf("error parsing error code; want %v, got %v", want.ErrorCode, got.ErrorCode)
}
if want.ErrorMessage != got.ErrorMessage {
t.Errorf("error parsing error message; want %v, got %v", want.ErrorMessage, got.ErrorMessage)
}
if len(want.ErrorArgs) != len(got.ErrorArgs) {
t.Errorf("error parsing error args (found different lengths); want %v, got %v", want.ErrorArgs, got.ErrorArgs)
}
for index, arg := range want.ErrorArgs {
if arg != got.ErrorArgs[index] {
t.Errorf("error parsing error args (found different items at same position %d); want %v, got %v", index, want.ErrorArgs, got.ErrorArgs)
}
}
})
}
}
func TestMessage_GetExt(t *testing.T) {
testCases := []struct {
name string
message *Message
shouldCreate bool
want map[string]interface{}
}{
{
name: "nil extension is initialized as a map with create=true",
message: &Message{},
shouldCreate: true,
want: make(map[string]interface{}),
},
{
name: "nil extension is not initialized with create=false",
message: &Message{},
shouldCreate: false,
want: nil,
},
{
name: "non-nil extension is not overwritten with create=true",
message: &Message{Ext: map[string]interface{}{"foo": "bar"}},
shouldCreate: true,
want: map[string]interface{}{"foo": "bar"},
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := tc.message.GetExt(tc.shouldCreate)
if tc.want == nil && got != nil {
t.Errorf("expected GetExt(%v) to return nil, got %v", tc.shouldCreate, got)
}
if tc.want != nil && got == nil {
t.Errorf("expected GetExt(%v) to return %v, got nil", tc.shouldCreate, tc.want)
}
if len(tc.want) == len(got) {
for k, vi := range tc.want {
wantv, _ := vi.(string)
gotv, _ := got[k].(string)
if wantv != gotv {
t.Errorf("expected Ext[%s] == %s, got %s", k, wantv, gotv)
}
}
}
})
}
}
func TestAdvice_MustNotRetryOrHandshake(t *testing.T) {
testCases := []struct {
name string
reconnect string
expected bool
}{
{
"reconnect advice is none",
"none",
true,
},
{
"reconnect advice is retry",
"retry",
false,
},
{
"reconnect advice is handshake",
"handshake",
false,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
a := Advice{Reconnect: tc.reconnect}
if got, want := a.MustNotRetryOrHandshake(), tc.expected; want != got {
t.Errorf("expected MustNotRetryOrHandshake() = %v, got %v", want, got)
}
})
}
}
func TestAdvice_ShouldRetry(t *testing.T) {
testCases := []struct {
name string
reconnect string
expected bool
}{
{
"reconnect advice is none",
"none",
false,
},
{
"reconnect advice is retry",
"retry",
true,
},
{
"reconnect advice is handshake",
"handshake",
false,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
a := Advice{Reconnect: tc.reconnect}
if got, want := a.ShouldRetry(), tc.expected; want != got {
t.Errorf("expected ShouldRetry() = %v, got %v", want, got)
}
})
}
}
func TestAdvice_ShouldHandshake(t *testing.T) {
testCases := []struct {
name string
reconnect string
expected bool
}{
{
"reconnect advice is none",
"none",
false,
},
{
"reconnect advice is retry",
"retry",
false,
},
{
"reconnect advice is handshake",
"handshake",
true,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
a := Advice{Reconnect: tc.reconnect}
if got, want := a.ShouldHandshake(), tc.expected; want != got {
t.Errorf("expected ShouldHandshake() = %v, got %v", want, got)
}
})
}
}
func TestAdvice_TimeoutAsDuration(t *testing.T) {
testCases := []struct {
name string
timeout int
expected time.Duration
}{
{
"two seconds",
2000,
time.Duration(2) * time.Second,
},
{
"two hundred milliseconds",
200,
time.Duration(200) * time.Millisecond,
},
{
"three minutes",
180000,
time.Duration(3) * time.Minute,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
a := Advice{Timeout: tc.timeout}
if got, want := a.TimeoutAsDuration(), tc.expected; want != got {
t.Errorf("expected TimeoutAsDuration() = %v, got %v", want, got)
}
})
}
}
func TestAdvice_IntervalAsDuration(t *testing.T) {
testCases := []struct {
name string
interval int
expected time.Duration
}{
{
"two seconds",
2000,
time.Duration(2) * time.Second,
},
{
"two hundred milliseconds",
200,
time.Duration(200) * time.Millisecond,
},
{
"three minutes",
180000,
time.Duration(3) * time.Minute,
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
a := Advice{Interval: tc.interval}
if got, want := a.IntervalAsDuration(), tc.expected; want != got {
t.Errorf("expected IntervalAsDuration() = %v, got %v", want, got)
}
})
}
}