Skip to content

Commit cfce4b2

Browse files
committed
Added integration test for server initiated auth, refactored related test name
1 parent 947de24 commit cfce4b2

3 files changed

+58
-1
lines changed

ably/ably_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ func (rec *MessageRecorder) CheckIfReceived(action ably.ProtoAction, times int)
272272
}
273273
}
274274
}
275+
if times == 0 && times == counter {
276+
return true
277+
}
275278
return false
276279
}
277280
}

ably/proto_protocol_message.go

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ func (msg *protocolMessage) String() string {
183183
case actionMessage:
184184
return fmt.Sprintf("(action=%q, id=%q, messages=%v)", msg.Action,
185185
msg.ConnectionID, msg.Messages)
186+
case actionAuth:
187+
return fmt.Sprintf("(action=%q, id=%q, auth=%v)", msg.Action,
188+
msg.ConnectionID, msg.Auth)
186189
default:
187190
return fmt.Sprintf("%#v", msg)
188191
}

ably/realtime_conn_spec_integration_test.go

+52-1
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ func TestRealtimeConn_RTN15h3_Success(t *testing.T) {
17831783
ablytest.Instantly.NoRecv(t, nil, stateChanges, t.Fatalf)
17841784
}
17851785

1786-
func TestRealtimeConn_RTN15h_Integration_ClientInitiatedAuth(t *testing.T) {
1786+
func TestRealtimeConn_RTN22a_RTN15h2_Integration_ServerInitiatedAuth(t *testing.T) {
17871787
t.Parallel()
17881788
app, restClient := ablytest.NewREST()
17891789
defer safeclose(t, app)
@@ -1833,6 +1833,57 @@ func TestRealtimeConn_RTN15h_Integration_ClientInitiatedAuth(t *testing.T) {
18331833
assert.ElementsMatch(t, authCallbackTokens, tokens)
18341834
}
18351835

1836+
func TestRealtimeConn_RTN22_RTC8_Integration_ServerInitiatedAuth(t *testing.T) {
1837+
app, restClient := ablytest.NewREST()
1838+
defer safeclose(t, app)
1839+
1840+
recorder := NewMessageRecorder()
1841+
authCallbackTokens := []string{}
1842+
1843+
// Server sends AUTH message 30 seconds before token expiry.
1844+
// So sending client token with expiry of 33 seconds, server will send AUTH msg after 3 seconds.
1845+
authCallback := func(ctx context.Context, tp ably.TokenParams) (ably.Tokener, error) {
1846+
tokenExpiry := 33000
1847+
token, err := restClient.Auth.RequestToken(context.Background(), &ably.TokenParams{TTL: int64(tokenExpiry)})
1848+
authCallbackTokens = append(authCallbackTokens, token.Token)
1849+
return token, err
1850+
}
1851+
1852+
realtime, err := ably.NewRealtime(
1853+
ably.WithAutoConnect(false),
1854+
ably.WithDial(recorder.Dial),
1855+
ably.WithUseBinaryProtocol(false),
1856+
ably.WithEnvironment(ablytest.Environment),
1857+
ably.WithAuthCallback(authCallback))
1858+
1859+
assert.NoError(t, err)
1860+
defer realtime.Close()
1861+
1862+
err = ablytest.Wait(ablytest.ConnWaiter(realtime, realtime.Connect, ably.ConnectionEventConnected), nil)
1863+
assert.NoError(t, err)
1864+
1865+
for i := 0; i < 3; i++ {
1866+
// auth msg sent by ably server every 3 seconds, so connection is updated by client
1867+
err = ablytest.Wait(ablytest.ConnWaiter(realtime, nil, ably.ConnectionEventUpdate), nil)
1868+
assert.NoError(t, err)
1869+
assert.Equal(t, ably.ConnectionStateConnected, realtime.Connection.State())
1870+
assert.True(t, ablytest.Instantly.IsTrue(recorder.CheckIfReceived(ably.ActionAuth, i+1)))
1871+
}
1872+
assert.True(t, ablytest.Instantly.IsTrue(recorder.CheckIfReceived(ably.ActionDisconnected, 0)))
1873+
1874+
// Only one dial attempt
1875+
tokens := []string{}
1876+
assert.Len(t, recorder.URLs(), 1)
1877+
for _, url := range recorder.URLs() {
1878+
tokens = append(tokens, url.Query().Get("access_token"))
1879+
}
1880+
assert.Len(t, tokens, 1)
1881+
1882+
assert.Len(t, authCallbackTokens, 4)
1883+
assert.Equal(t, tokens[0], authCallbackTokens[0])
1884+
assertUnique(t, authCallbackTokens)
1885+
}
1886+
18361887
func TestRealtimeConn_RTN15i_OnErrorWhenConnected(t *testing.T) {
18371888

18381889
in := make(chan *ably.ProtocolMessage, 1)

0 commit comments

Comments
 (0)