public IEnumerator RealtimeClient_ConnectedWithExpiringToken_WhenTokenExpired_ShouldNotRetryAndHaveError([ValueSource(nameof(_protocols))] Protocol protocol) { return(UniTask.ToCoroutine(async() => { var helper = new RSA4Helper(this); // Create a token that is valid long enough for a successful connection to occur var authClient = await AblySandbox.GetRestClient(protocol); var almostExpiredToken = await authClient.AblyAuth.RequestTokenAsync(new TokenParams { ClientId = "123", Ttl = TimeSpan.FromMilliseconds(8000), }); // get a realtime client with no Key, AuthUrl, or authCallback var realtimeClient = await helper.GetRealTimeClientWithRequests(protocol, almostExpiredToken, invalidateKey: true); await realtimeClient.WaitForState(ConnectionState.Connected); // assert that there is no pre-existing error realtimeClient.Connection.ErrorReason.Should().BeNull(); await realtimeClient.WaitForState(ConnectionState.Failed); realtimeClient.Connection.State.Should().Be(ConnectionState.Failed); realtimeClient.Connection.ErrorReason.Code.Should().Be(ErrorCodes.NoMeansProvidedToRenewAuthToken); helper.Requests.Count.Should().Be(0); })); }
public IEnumerator RealtimeClient_NewInstanceWithExpiredToken_ShouldNotRetryAndHaveError([ValueSource(nameof(_protocols))] Protocol protocol) { return(UniTask.ToCoroutine(async() => { var helper = new RSA4Helper(this); var authClient = await AblySandbox.GetRestClient(protocol); var almostExpiredToken = await authClient.AblyAuth.RequestTokenAsync(new TokenParams { ClientId = "123", Ttl = TimeSpan.FromMilliseconds(1) }); await Task.Delay(TimeSpan.FromMilliseconds(2)); // Modify the expiry date to fool the client it has a valid token almostExpiredToken.Expires = DateTimeOffset.UtcNow.AddHours(1); // get a realtime client with no key var realtimeClient = await helper.GetRealTimeClientWithRequests(protocol, almostExpiredToken, invalidateKey: true); bool connected = false; realtimeClient.Connection.Once(ConnectionEvent.Connected, (_) => { connected = true; }); // assert that there is no pre-existing error realtimeClient.Connection.ErrorReason.Should().BeNull(); await realtimeClient.WaitForState(ConnectionState.Failed); realtimeClient.Connection.State.Should().Be(ConnectionState.Failed); connected.Should().BeFalse(); realtimeClient.Connection.ErrorReason.Code.Should().Be(ErrorCodes.NoMeansProvidedToRenewAuthToken); helper.Requests.Count.Should().Be(0); })); }
public IEnumerator RestClient_WhenTokenExpired_ShouldNotRetryAndRaiseError([ValueSource(nameof(_protocols))] Protocol protocol) { return(UniTask.ToCoroutine(async() => { var helper = new RSA4Helper(this); // Get a very short lived token and wait for it to expire var authClient = await AblySandbox.GetRestClient(protocol); var almostExpiredToken = await authClient.AblyAuth.RequestTokenAsync(new TokenParams { ClientId = "123", Ttl = TimeSpan.FromMilliseconds(1) }); await Task.Delay(TimeSpan.FromMilliseconds(2)); // Modify the expiry date to fool the client it has a valid token almostExpiredToken.Expires = DateTimeOffset.UtcNow.AddHours(1); // create a new client with the token // set the Key to an empty string to override the sandbox settings var restClient = await helper.GetRestClientWithRequests(protocol, almostExpiredToken, invalidateKey: true); var now = DateTimeOffset.UtcNow; // check the client thinks the token is valid restClient.AblyAuth.CurrentToken.IsValidToken(now).Should().BeTrue(); var channelName = "RSA4a".AddRandomSuffix(); try { await restClient.Channels.Get(channelName).PublishAsync("event", "data"); throw new Exception("Unexpected success, the preceding code should have raised an AblyException"); } catch (AblyException e) { // the server responds with a token error // (401 HTTP status code and an Ably error value 40140 <= code < 40150) // As the token is expired we can expect a specific code "40142": "token expired" e.ErrorInfo.StatusCode.Should().Be(HttpStatusCode.Unauthorized); e.ErrorInfo.Code.Should().Be(ErrorCodes.NoMeansProvidedToRenewAuthToken); } // did not retry the request helper.Requests.Count.Should().Be(1, "only one request should have been attempted"); helper.Requests[0].Url.Should().Be($"/channels/{channelName}/messages", "only the publish request should have been attempted"); })); }
public IEnumerator RSA4Helper_RestClient_ShouldTrackRequests([ValueSource(nameof(_protocols))] Protocol protocol) { return(UniTask.ToCoroutine(async() => { var authClient = await AblySandbox.GetRestClient(protocol); var token = await authClient.Auth.RequestTokenAsync(new TokenParams { ClientId = "123" }); var helper = new RSA4Helper(this); var restClient = await helper.GetRestClientWithRequests(protocol, token, invalidateKey: true); helper.Requests.Count.Should().Be(0); await restClient.TimeAsync(); helper.Requests.Count.Should().Be(1); var realtimeClient = await helper.GetRealTimeClientWithRequests(protocol, token, invalidateKey: true); helper.Requests.Count.Should().Be(1); await realtimeClient.RestClient.TimeAsync(); helper.Requests.Count.Should().Be(2); })); }
public IEnumerator RealtimeWithAuthError_WhenTokenExpired_ShouldRetry_WhenRetryFails_ShouldSetError([ValueSource(nameof(_protocols))] Protocol protocol) { return(UniTask.ToCoroutine(async() => { var helper = new RSA4Helper(this); var restClient = await AblySandbox.GetRestClient(protocol); var token = await restClient.Auth.AuthorizeAsync(new TokenParams { Ttl = TimeSpan.FromMilliseconds(1000), }); // this realtime client will have a key for the sandbox, thus a means to renew var realtimeClient = await AblySandbox.GetRealtimeClient(protocol, (options, _) => { options.TokenDetails = token; options.AutoConnect = false; }); realtimeClient.RestClient.ExecuteHttpRequest = helper.AblyResponseWith500Status; var awaiter = new TaskCompletionAwaiter(5000); realtimeClient.Connection.Once(ConnectionEvent.Disconnected, state => { state.Reason.Code.Should().Be(ErrorCodes.ClientAuthProviderRequestFailed); awaiter.SetCompleted(); }); await Task.Delay(2000); realtimeClient.Connect(); var result = await awaiter.Task; result.Should().BeTrue(); helper.Requests.Count.Should().Be(1); helper.Requests[0].Url.EndsWith("requestToken").Should().BeTrue(); })); }