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); })); }