forked from goguardian/pusher-ws-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
871 lines (763 loc) · 21 KB
/
client_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
package pusher
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"reflect"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"golang.org/x/net/websocket"
)
// TODO Implement Client.Error channel in all tests and fail the test if any
// errors occur
func TestUnmarshalDataString(t *testing.T) {
dest := map[string]interface{}{}
err := UnmarshalDataString(json.RawMessage(`"{\"foo\":\"A\",\"bar\":1}"`), &dest)
if err != nil {
t.Errorf("Expected error to be `nil`, got %v", err)
}
wantData := map[string]interface{}{
"foo": "A",
"bar": 1.0,
}
if !reflect.DeepEqual(dest, wantData) {
t.Errorf("Expected dest to deep-equal %+v, got %+v", wantData, dest)
}
}
func TestClientIsConnected(t *testing.T) {
t.Run("false", func(t *testing.T) {
client := &Client{connected: false}
if isConnected := client.isConnected(); isConnected != false {
t.Errorf("Expected isConnected to return false, got %v", isConnected)
}
})
t.Run("true", func(t *testing.T) {
client := &Client{connected: true}
if isConnected := client.isConnected(); isConnected != true {
t.Errorf("Expected isConnected to return true, got %v", isConnected)
}
})
}
func TestClientResetActivityTimer(t *testing.T) {
client := &Client{
activityTimerReset: make(chan struct{}, 1),
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
<-client.activityTimerReset
wg.Done()
}()
client.resetActivityTimer()
wg.Wait()
}
func TestClientSendError(t *testing.T) {
errChan := make(chan error)
wantErr := errors.New("foo")
client := &Client{Errors: errChan}
var gotErr error
wg := &sync.WaitGroup{}
wg.Add(1)
go func() { gotErr = <-errChan; wg.Done() }()
runtime.Gosched()
client.sendError(wantErr)
wg.Wait()
if !reflect.DeepEqual(gotErr, wantErr) {
t.Errorf("Expected to value from error chan to be %+v, got %+v", wantErr, gotErr)
}
}
func TestClientBind(t *testing.T) {
wantChan := "foo"
client := Client{boundEvents: map[string]boundEventChans{}}
boundChan := client.Bind(wantChan)
eventBoundChans, ok := client.boundEvents[wantChan]
if !ok {
t.Errorf("Expected client bound events to contain %q, got %+v instead", wantChan, client.boundEvents)
}
_, ok = eventBoundChans[boundChan]
if !ok {
t.Errorf("Expected event bound channels to contain returned channel, got %+v instead", eventBoundChans)
}
}
func TestClientUnbind(t *testing.T) {
wantChan := "foo"
t.Run("eventOnly", func(t *testing.T) {
client := Client{boundEvents: map[string]boundEventChans{
wantChan: {make(chan Event): struct{}{}},
}}
client.Unbind(wantChan)
if _, ok := client.boundEvents[wantChan]; ok {
t.Errorf("Expected client bound events not to contain %q, got %+v instead", wantChan, client.boundEvents)
}
})
t.Run("eventWithChans", func(t *testing.T) {
wantChan := "foo"
ch1 := make(chan Event)
ch2 := make(chan Event)
ch3 := make(chan Event)
client := Client{boundEvents: map[string]boundEventChans{
wantChan: {
ch1: struct{}{},
ch2: struct{}{},
ch3: struct{}{},
},
}}
client.Unbind(wantChan, ch1, ch3)
eventBoundChans, ok := client.boundEvents[wantChan]
if !ok {
t.Errorf("Expected client bound events to contain %q, got %+v instead", wantChan, client.boundEvents)
}
_, ok = eventBoundChans[ch1]
if ok {
t.Errorf("Expected event bound channels not to contain ch1, got %+v instead", eventBoundChans)
}
_, ok = eventBoundChans[ch3]
if ok {
t.Errorf("Expected event bound channels not to contain ch3, got %+v instead", eventBoundChans)
}
_, ok = eventBoundChans[ch2]
if !ok {
t.Errorf("Expected event bound channels to contain ch3, got %+v instead", eventBoundChans)
}
})
}
func TestClientSendEvent(t *testing.T) {
wantEvent := Event{
Channel: "foo",
Event: "bar",
Data: json.RawMessage(`{"bar":1,"foo":"A"}`),
}
wg := &sync.WaitGroup{}
wg.Add(1)
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
event := Event{}
err := websocket.JSON.Receive(ws, &event)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(event, wantEvent) {
t.Errorf("Expected received event to deep-equal %+v, got %+v", wantEvent, event)
}
wg.Done()
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
ws: ws,
activityTimerReset: make(chan struct{}, 1),
}
defer client.Disconnect()
err = client.SendEvent(wantEvent.Event, wantEvent.Data, wantEvent.Channel)
if err != nil {
panic(err)
}
wg.Wait()
<-client.activityTimerReset
}
func TestClientSubscribe(t *testing.T) {
t.Run("existingSubscription", func(t *testing.T) {
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
channelName := "foo"
ch := &channel{name: channelName, subscribed: true}
client := &Client{
subscribedChannels: map[string]internalChannel{channelName: ch},
ws: ws,
}
defer client.Disconnect()
ch.client = client
subCh, err := client.Subscribe(channelName)
if err != nil {
panic(err)
}
if ch != subCh.(*channel) {
t.Errorf("Expected subCh to be %+v, got %+v", ch, subCh)
}
})
t.Run("newPublicChannel", func(t *testing.T) {
channelName := "foo"
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var evt Event
err := websocket.JSON.Receive(ws, &evt)
if err != nil {
panic(err)
}
if evt.Event != pusherSubscribe {
t.Errorf("Expected event to be %q, got %q", pusherSubscribe, evt.Event)
}
err = websocket.JSON.Send(ws, Event{
Event: pusherInternalSubSucceeded,
Channel: channelName,
})
if err != nil {
panic(err)
}
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
subscribedChannels: map[string]internalChannel{},
ws: ws,
connected: true,
}
defer client.Disconnect()
go client.listen()
subCh, err := client.Subscribe(channelName)
if err != nil {
panic(err)
}
baseCh := subCh.(*channel)
if baseCh.name != channelName {
t.Errorf("Expected channel name to be %q, got %q", channelName, baseCh.name)
}
})
t.Run("newPrivateChannel", func(t *testing.T) {
channelName := "private-foo"
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var evt Event
err := websocket.JSON.Receive(ws, &evt)
if err != nil {
panic(err)
}
if evt.Event != pusherSubscribe {
t.Errorf("Expected event to be %q, got %q", pusherSubscribe, evt.Event)
}
err = websocket.JSON.Send(ws, Event{
Event: pusherInternalSubSucceeded,
Channel: channelName,
})
if err != nil {
panic(err)
}
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
authSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{}`))
}))
client := &Client{
subscribedChannels: map[string]internalChannel{},
ws: ws,
connected: true,
AuthURL: authSrv.URL,
}
defer client.Disconnect()
go client.listen()
subCh, err := client.Subscribe(channelName)
if err != nil {
panic(err)
}
baseCh := subCh.(*privateChannel)
if baseCh.name != channelName {
t.Errorf("Expected channel name to be %q, got %q", channelName, baseCh.name)
}
})
t.Run("newPresenceChannel", func(t *testing.T) {
channelName := "presence-foo"
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var evt Event
err := websocket.JSON.Receive(ws, &evt)
if err != nil {
panic(err)
}
if evt.Event != pusherSubscribe {
t.Errorf("Expected event to be %q, got %q", pusherSubscribe, evt.Event)
}
data, err := json.Marshal(`
{
"presence": {
"ids": ["1", "2"],
"hash": {
"1": { "name": "name-1" },
"2": { "name": "name-2" }
},
"count": 2
}
}
`)
if err != nil {
t.Fatal("error marshaling data: ", err)
}
err = websocket.JSON.Send(ws, Event{
Event: pusherInternalSubSucceeded,
Channel: channelName,
Data: data,
})
if err != nil {
panic(err)
}
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
authSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{}`))
}))
client := &Client{
subscribedChannels: map[string]internalChannel{},
ws: ws,
connected: true,
AuthURL: authSrv.URL,
}
defer client.Disconnect()
go client.listen()
subCh, err := client.SubscribePresence(channelName)
if err != nil {
panic(err)
}
baseCh := subCh.(*presenceChannel)
if baseCh.name != channelName {
t.Errorf("Expected channel name to be %q, got %q", channelName, baseCh.name)
}
})
}
func TestClientUnsubscribe(t *testing.T) {
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
ch := &channel{name: "foo"}
client := &Client{
subscribedChannels: map[string]internalChannel{"foo": ch},
ws: ws,
}
defer client.Disconnect()
ch.client = client
err = client.Unsubscribe("foo")
if err != nil {
panic(err)
}
if _, ok := client.subscribedChannels["foo"]; ok {
t.Errorf("Expected client subscribed channels not to contain 'foo', got %+v", client.subscribedChannels)
}
}
func TestClientDisconnect(t *testing.T) {
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
connected: true,
ws: ws,
}
err = client.Disconnect()
if err != nil {
panic(err)
}
if client.connected {
t.Errorf("Expected client connected to be false, got true")
}
if err = ws.Close(); err == nil {
t.Errorf("Expected websocket connection to have been closed, got no error closing again")
}
}
func TestClientHeartbeat(t *testing.T) {
t.Run("notConnected", func(t *testing.T) {
timeChan := make(chan time.Time)
client := &Client{
connected: false,
activityTimerReset: make(chan struct{}, 1),
activityTimer: &time.Timer{C: timeChan},
}
go func() {
client.activityTimerReset <- struct{}{}
client.activityTimerReset <- struct{}{}
t.Errorf("Expected not to block on send to activityTimerReset, but message was received")
}()
go func() {
timeChan <- time.Now()
t.Errorf("Expected not to block on send to activityTimer chan, but message was received")
}()
client.heartbeat()
})
t.Run("timerReset", func(t *testing.T) {
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
connected: true,
activityTimerReset: make(chan struct{}, 1),
activityTimer: time.NewTimer(1 * time.Hour),
activityTimeout: 0,
ws: ws,
}
go client.heartbeat()
runtime.Gosched()
client.Disconnect()
client.activityTimerReset <- struct{}{}
<-client.activityTimer.C
})
t.Run("timerExpire", func(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
var event Event
err := websocket.JSON.Receive(ws, &event)
if err != nil {
panic(err)
}
if event.Event != pusherPing {
t.Errorf("Expected to get ping event, got %+v", event)
}
wg.Done()
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
connected: true,
activityTimer: time.NewTimer(0),
ws: ws,
}
defer client.Disconnect()
go client.heartbeat()
wg.Wait()
})
t.Run("timerReset doesn't block", func(t *testing.T) {
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
connected: true,
activityTimerReset: make(chan struct{}, 1),
activityTimer: time.NewTimer(1024 * time.Hour),
activityTimeout: 0,
ws: ws,
}
go client.heartbeat()
runtime.Gosched()
// If there's a bug in the implementation, this will block forever.
// The test should timeout.
done := make(chan struct{})
go func() {
client.resetActivityTimer()
close(done)
}()
select {
case <-done:
// test passed
case <-time.After(3 * time.Second):
t.Errorf("Timeout waiting for resetActivityTimer to finish")
}
})
}
func TestClientListen(t *testing.T) {
t.Run("notConnected", func(t *testing.T) {
client := &Client{
connected: false,
}
client.heartbeat()
})
t.Run("receivePing", func(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
websocket.Message.Send(ws, pingPayload)
var event Event
err := websocket.JSON.Receive(ws, &event)
if err != nil {
panic(err)
}
if event.Event != pusherPong {
t.Errorf("Expected to get pong event, got %+v", event)
}
wg.Done()
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
connected: true,
ws: ws,
}
defer client.Disconnect()
go client.listen()
wg.Wait()
})
t.Run("receiveEvent", func(t *testing.T) {
wantData := json.RawMessage(`{"hello":"world"}`)
wantEvent := Event{
Event: "foo",
Channel: "bar",
Data: wantData,
}
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
websocket.JSON.Send(ws, wantEvent)
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
eventChan := make(chan Event)
dataChan := make(chan json.RawMessage)
client := &Client{
connected: true,
ws: ws,
boundEvents: map[string]boundEventChans{
wantEvent.Event: {eventChan: struct{}{}},
},
subscribedChannels: map[string]internalChannel{
wantEvent.Channel: &channel{
boundEvents: map[string]boundDataChans{
wantEvent.Event: {dataChan: make(chan struct{})},
},
},
},
}
defer client.Disconnect()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
if gotEvent := <-eventChan; !reflect.DeepEqual(gotEvent, wantEvent) {
t.Errorf("Expected to receive event %+v, got %+v", wantEvent, gotEvent)
}
if gotData := <-dataChan; !reflect.DeepEqual(gotData, wantData) {
t.Errorf("Expected to receive data %+v, got %+v", wantData, gotData)
}
wg.Done()
}()
go client.listen()
wg.Wait()
})
t.Run("receiveError", func(t *testing.T) {
wantError := EventError{
Code: 1234,
Message: "foo",
}
errData, err := json.Marshal(wantError)
if err != nil {
panic(err)
}
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
for {
websocket.JSON.Send(ws, Event{Event: pusherError, Data: errData})
}
}))
defer srv.Close()
wsURL := strings.Replace(srv.URL, "http", "ws", 1)
ws, err := websocket.Dial(wsURL, "ws", localOrigin)
if err != nil {
panic(err)
}
client := &Client{
connected: true,
ws: ws,
Errors: make(chan error),
}
defer client.Disconnect()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
if gotError := <-client.Errors; !reflect.DeepEqual(gotError, wantError) {
t.Errorf("Expected to receive event %+v, got %+v", wantError, gotError)
}
wg.Done()
}()
go client.listen()
wg.Wait()
})
}
func TestClientGenerateConnURL(t *testing.T) {
t.Run("defaults", func(t *testing.T) {
wantAppKey := "foo"
client := &Client{}
gotURL := client.generateConnURL(wantAppKey)
if !strings.Contains(gotURL, secureScheme) {
t.Errorf("Expected connection URL to have secure scheme, got %q", gotURL)
}
if !strings.Contains(gotURL, fmt.Sprint(securePort)) {
t.Errorf("Expected connection URL to have secure port, got %q", gotURL)
}
if !strings.Contains(gotURL, defaultHost) {
t.Errorf("Expected connection URL to have default host, got %q", gotURL)
}
if !strings.Contains(gotURL, wantAppKey) {
t.Errorf("Expected connection URL to have app key, got %q", gotURL)
}
})
t.Run("custom", func(t *testing.T) {
wantAppKey := "foo"
client := &Client{
Insecure: true,
Cluster: "bar",
}
gotURL := client.generateConnURL(wantAppKey)
if !strings.Contains(gotURL, insecureScheme) {
t.Errorf("Expected connection URL to have insecure scheme, got %q", gotURL)
}
if !strings.Contains(gotURL, fmt.Sprint(insecurePort)) {
t.Errorf("Expected connection URL to have insecure port, got %q", gotURL)
}
if !strings.Contains(gotURL, "pusher.com") {
t.Errorf("Expected connection URL to have pusher.com, got %q", gotURL)
}
if !strings.Contains(gotURL, client.Cluster) {
t.Errorf("Expected connection URL to have custom cluster, got %q", gotURL)
}
if !strings.Contains(gotURL, wantAppKey) {
t.Errorf("Expected connection URL to have app key, got %q", gotURL)
}
})
t.Run("override", func(t *testing.T) {
client := &Client{
overrideHost: "foo.bar",
overridePort: 1234,
}
gotURL := client.generateConnURL("")
if !strings.Contains(gotURL, client.overrideHost) {
t.Errorf("Expected connection URL to have override host, got %q", gotURL)
}
if !strings.Contains(gotURL, fmt.Sprint(client.overridePort)) {
t.Errorf("Expected connection URL to have override port, got %q", gotURL)
}
})
}
func TestClientConnect(t *testing.T) {
t.Run("pusherError", func(t *testing.T) {
wantError := EventError{
Code: 1234,
Message: "foo",
}
errData, err := json.Marshal(wantError)
if err != nil {
panic(err)
}
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
for {
websocket.JSON.Send(ws, Event{Event: pusherError, Data: errData})
}
}))
defer srv.Close()
host, port, err := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://"))
if err != nil {
panic(err)
}
portNum, err := strconv.Atoi(port)
if err != nil {
panic(err)
}
client := &Client{
Insecure: true,
overrideHost: host,
overridePort: portNum,
}
defer client.Disconnect()
connectErr := client.Connect("")
if !reflect.DeepEqual(connectErr, wantError) {
t.Errorf("Expected error to deep-equal %+v, got %+v", wantError, connectErr)
}
})
t.Run("connectionEstablished", func(t *testing.T) {
wantConnData := connectionData{
SocketID: "foo",
ActivityTimeout: 1234,
}
connData, err := json.Marshal(wantConnData)
if err != nil {
panic(err)
}
connDataStr, err := json.Marshal(string(connData))
if err != nil {
panic(err)
}
srv := httptest.NewServer(websocket.Handler(func(ws *websocket.Conn) {
for {
websocket.JSON.Send(ws, Event{Event: pusherConnEstablished, Data: connDataStr})
}
}))
defer srv.Close()
host, port, err := net.SplitHostPort(strings.TrimPrefix(srv.URL, "http://"))
if err != nil {
panic(err)
}
portNum, err := strconv.Atoi(port)
if err != nil {
panic(err)
}
client := &Client{
Insecure: true,
overrideHost: host,
overridePort: portNum,
}
defer client.Disconnect()
err = client.Connect("")
if err != nil {
panic(err)
}
if client.connected != true {
t.Errorf("Expected client connected to be true, got false")
}
if client.socketID != wantConnData.SocketID {
t.Errorf("Expected client socket ID to be %v, got %v", wantConnData.SocketID, client.socketID)
}
wantTimeout := time.Duration(wantConnData.ActivityTimeout) * time.Second
if client.activityTimeout != wantTimeout {
t.Errorf("Expected client activity timeout to be %v, got %v", wantTimeout, client.activityTimeout)
}
if client.activityTimer == nil {
t.Errorf("Expected client activity timer to be non-nil")
}
if client.activityTimerReset == nil {
t.Errorf("Expected client activity timer reset channel to be non-nil")
}
if cap(client.activityTimerReset) != 1 {
t.Errorf("Expected client activity timer reset channel to have capacity 1, got %v",
cap(client.activityTimerReset))
}
if client.boundEvents == nil {
t.Errorf("Expected client bound events to be non-nil")
}
if client.subscribedChannels == nil {
t.Errorf("Expected client subscribed channels to be non-nil")
}
})
}