@@ -131,6 +131,35 @@ describe('connect with WebSocket', () => {
131
131
socket . connect ( )
132
132
assert . deepStrictEqual ( conn , socket . conn )
133
133
} )
134
+
135
+ test ( 'handles setAuth errors gracefully during connection' , async ( ) => {
136
+ const errorMessage = 'Token fetch failed'
137
+ const accessToken = sinon . spy ( ( ) => Promise . reject ( new Error ( errorMessage ) ) )
138
+ const logSpy = sinon . spy ( )
139
+
140
+ const socketWithError = new RealtimeClient ( url , {
141
+ transport : MockWebSocket ,
142
+ accessToken,
143
+ logger : logSpy ,
144
+ } )
145
+
146
+ socketWithError . connect ( )
147
+
148
+ await new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( undefined ) , 100 ) )
149
+
150
+ // Verify that the error was logged
151
+ assert . ok (
152
+ logSpy . calledWith (
153
+ 'error' ,
154
+ 'error setting auth' ,
155
+ sinon . match . instanceOf ( Error )
156
+ )
157
+ )
158
+
159
+ // Verify that the connection was still established despite the error
160
+ assert . ok ( socketWithError . conn , 'connection should still exist' )
161
+ assert . equal ( socketWithError . conn ! . url , socketWithError . endpointURL ( ) )
162
+ } )
134
163
} )
135
164
136
165
describe ( 'disconnect' , ( ) => {
@@ -210,7 +239,7 @@ describe('channel', () => {
210
239
let channel
211
240
212
241
test ( 'returns channel with given topic and params' , ( ) => {
213
- channel = socket . channel ( 'topic' , { one : 'two' } )
242
+ channel = socket . channel ( 'topic' )
214
243
215
244
assert . deepStrictEqual ( channel . socket , socket )
216
245
assert . equal ( channel . topic , 'realtime:topic' )
@@ -220,12 +249,11 @@ describe('channel', () => {
220
249
presence : { key : '' , enabled : false } ,
221
250
private : false ,
222
251
} ,
223
- one : 'two' ,
224
252
} )
225
253
} )
226
254
227
255
test ( 'returns channel with given topic and params for a private channel' , ( ) => {
228
- channel = socket . channel ( 'topic' , { config : { private : true } , one : 'two' } )
256
+ channel = socket . channel ( 'topic' , { config : { private : true } } )
229
257
230
258
assert . deepStrictEqual ( channel . socket , socket )
231
259
assert . equal ( channel . topic , 'realtime:topic' )
@@ -235,7 +263,6 @@ describe('channel', () => {
235
263
presence : { key : '' , enabled : false } ,
236
264
private : true ,
237
265
} ,
238
- one : 'two' ,
239
266
} )
240
267
} )
241
268
@@ -540,7 +567,7 @@ describe('setAuth', () => {
540
567
let new_token = generateJWT ( '1h' )
541
568
let new_socket = new RealtimeClient ( url , {
542
569
transport : MockWebSocket ,
543
- accessToken : ( ) => Promise . resolve ( token ) ,
570
+ accessToken : ( ) => Promise . resolve ( new_token ) ,
544
571
} )
545
572
546
573
const channel1 = new_socket . channel ( 'test-topic1' )
@@ -725,15 +752,18 @@ describe('flushSendBuffer', () => {
725
752
} )
726
753
} )
727
754
728
- describe ( '_onConnClose' , ( ) => {
729
- beforeEach ( ( ) => {
730
- socket . connect ( )
731
- } )
755
+ describe ( 'socket close event' , ( ) => {
756
+ beforeEach ( ( ) => socket . connect ( ) )
732
757
733
758
test ( 'schedules reconnectTimer timeout' , ( ) => {
734
759
const spy = sinon . spy ( socket . reconnectTimer , 'scheduleTimeout' )
735
760
736
- socket . _onConnClose ( )
761
+ const closeEvent = new CloseEvent ( 'close' , {
762
+ code : 1000 ,
763
+ reason : '' ,
764
+ wasClean : true ,
765
+ } )
766
+ socket . conn ?. onclose ?.( closeEvent )
737
767
738
768
assert . ok ( spy . calledOnce )
739
769
} )
@@ -742,10 +772,39 @@ describe('_onConnClose', () => {
742
772
const channel = socket . channel ( 'topic' )
743
773
const spy = sinon . spy ( channel , '_trigger' )
744
774
745
- socket . _onConnClose ( )
775
+ const closeEvent = new CloseEvent ( 'close' , {
776
+ code : 1000 ,
777
+ reason : '' ,
778
+ wasClean : true ,
779
+ } )
780
+ socket . conn ?. onclose ?.( closeEvent )
746
781
747
782
assert . ok ( spy . calledWith ( 'phx_error' ) )
748
783
} )
784
+
785
+ test ( 'should use a new token after reconnect' , async ( ) => {
786
+ const tokens = [ 'initial-token' , 'refreshed-token' ]
787
+
788
+ let callCount = 0
789
+ const accessToken = sinon . spy ( ( ) => Promise . resolve ( tokens [ callCount ++ ] ) )
790
+
791
+ const socket = new RealtimeClient ( url , {
792
+ transport : MockWebSocket ,
793
+ accessToken,
794
+ } )
795
+ socket . connect ( )
796
+
797
+ // Wait for the async setAuth call to complete
798
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
799
+ assert . strictEqual ( accessToken . callCount , 1 )
800
+ expect ( socket . accessTokenValue ) . toBe ( tokens [ 0 ] )
801
+
802
+ // Call the callback and wait for async operations to complete
803
+ await socket . reconnectTimer . callback ( )
804
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
805
+ expect ( socket . accessTokenValue ) . toBe ( tokens [ 1 ] )
806
+ assert . strictEqual ( accessToken . callCount , 2 )
807
+ } )
749
808
} )
750
809
751
810
describe ( '_onConnError' , ( ) => {
@@ -757,7 +816,7 @@ describe('_onConnError', () => {
757
816
const channel = socket . channel ( 'topic' )
758
817
const spy = sinon . spy ( channel , '_trigger' )
759
818
760
- socket . _onConnError ( 'error' )
819
+ socket . conn ?. onerror ?. ( new Event ( 'error' ) )
761
820
762
821
assert . ok ( spy . calledWith ( 'phx_error' ) )
763
822
} )
@@ -780,7 +839,8 @@ describe('onConnMessage', () => {
780
839
const otherSpy = sinon . spy ( otherChannel , '_trigger' )
781
840
782
841
socket . pendingHeartbeatRef = '3'
783
- socket . _onConnMessage ( data )
842
+ const messageEvent = new MessageEvent ( 'message' , { data : message } )
843
+ socket . conn ?. onmessage ?.( messageEvent )
784
844
785
845
// assert.ok(targetSpy.calledWith('INSERT', {type: 'INSERT'}, 'ref'))
786
846
assert . strictEqual ( targetSpy . callCount , 1 )
@@ -792,12 +852,15 @@ describe('onConnMessage', () => {
792
852
let socket = new RealtimeClient ( url )
793
853
socket . onHeartbeat ( ( message : HeartbeatStatus ) => ( called = message == 'ok' ) )
794
854
855
+ socket . connect ( )
856
+
795
857
const message =
796
858
'{"ref":"1","event":"phx_reply","payload":{"status":"ok","response":{}},"topic":"phoenix"}'
797
859
const data = { data : message }
798
860
799
861
socket . pendingHeartbeatRef = '3'
800
- socket . _onConnMessage ( data )
862
+ const messageEvent = new MessageEvent ( 'message' , { data : message } )
863
+ socket . conn ?. onmessage ?.( messageEvent )
801
864
802
865
assert . strictEqual ( called , true )
803
866
} )
@@ -808,12 +871,15 @@ describe('onConnMessage', () => {
808
871
( message : HeartbeatStatus ) => ( called = message == 'error' )
809
872
)
810
873
874
+ socket . connect ( )
875
+
811
876
const message =
812
877
'{"ref":"1","event":"phx_reply","payload":{"status":"error","response":{}},"topic":"phoenix"}'
813
878
const data = { data : message }
814
879
815
880
socket . pendingHeartbeatRef = '3'
816
- socket . _onConnMessage ( data )
881
+ const messageEvent = new MessageEvent ( 'message' , { data : message } )
882
+ socket . conn ?. onmessage ?.( messageEvent )
817
883
818
884
assert . strictEqual ( called , true )
819
885
} )
0 commit comments