public async Task WhenErrorCodeIsTokenSpecific_ShouldAutomaticallyTryToRenewTokenIfRequestFails(int errorCode) { //Now = DateTimeOffset.Now; var tokenDetails = new TokenDetails("id") { Expires = Now.AddHours(1) }; //Had to inline the method otherwise the tests intermittently fail. bool firstAttempt = true; var client = GetRestClient(request => { if (request.Url.Contains("/keys")) { return(_returnedDummyTokenDetails.ToJson().ToAblyResponse()); } if (firstAttempt) { firstAttempt = false; throw new AblyException(new ErrorInfo("", errorCode, HttpStatusCode.Unauthorized)); } return(AblyResponse.EmptyResponse.ToTask()); }, opts => opts.TokenDetails = tokenDetails); await client.StatsAsync(); client.AblyAuth.CurrentToken.Expires.Should().BeCloseTo(_returnedDummyTokenDetails.Expires); client.AblyAuth.CurrentToken.ClientId.Should().Be(_returnedDummyTokenDetails.ClientId); }
public async Task WithTokenErrorAndRenewableToken_ShouldRenewTokenAutomaticallyWithoutEmittingError() { var tokenDetails = new TokenDetails("id") { Expires = Now.AddHours(1) }; bool renewTokenCalled = false; var client = GetClientWithFakeTransport( opts => { opts.TokenDetails = tokenDetails; opts.UseBinaryProtocol = false; opts.AutoConnect = false; }, request => { if (request.Url.Contains("/keys")) { if (renewTokenCalled == false) { renewTokenCalled = true; } return(_returnedDummyTokenDetails.ToJson().ToAblyResponse()); } return(AblyResponse.EmptyResponse.ToTask()); }); client.Connect(); List <ErrorInfo> raisedErrors = new List <ErrorInfo>(); client.Connection.On((args) => { if (args.HasError) { raisedErrors.Add(args.Reason); } }); await client.WaitForState(ConnectionState.Connecting); client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Error) { Error = new ErrorInfo("Unauthorised", ErrorCodes.TokenError, HttpStatusCode.Unauthorized) }); await client.ProcessCommands(); renewTokenCalled.Should().BeTrue(); var currentToken = client.RestClient.AblyAuth.CurrentToken; currentToken.Token.Should().Be(_returnedDummyTokenDetails.Token); currentToken.ClientId.Should().Be(_returnedDummyTokenDetails.ClientId); currentToken.Expires.Should().BeCloseTo(_returnedDummyTokenDetails.Expires, TimeSpan.FromMilliseconds(20)); raisedErrors.Should().BeEmpty("No errors should be raised!"); }
private Task <AblyRealtime> SetupConnectedClient(ConnectedClientErrors errors = ConnectedClientErrors.None) { return(GetConnectedClient( opts => { if (errors.HasFlag(ConnectedClientErrors.RenewalNotSupported)) { opts.Key = string.Empty; // clear the key to make the token non renewable } opts.TokenDetails = _validToken; opts.UseBinaryProtocol = false; }, request => { if (request.Url.Contains("/keys")) { if (errors.HasFlag(ConnectedClientErrors.FailRenewal)) { throw new AblyException(new ErrorInfo("Failed to renew token", FailedRenewalErrorCode)); } _renewTokenCalled = true; return _returnedDummyTokenDetails.ToJson().ToAblyResponse(); } return AblyResponse.EmptyResponse.ToTask(); })); }
public AblyRealtime SetupConnectedClient(bool failRenewal = false, bool renewable = true) { return(GetConnectedClient(opts => { if (renewable == false) { opts.Key = ""; //clear the key to make the token non renewable } opts.TokenDetails = _validToken; opts.UseBinaryProtocol = false; }, request => { if (request.Url.Contains("/keys")) { if (failRenewal) { throw new AblyException(new ErrorInfo("Failed to renew token", _failedRenewalErorrCode)); } _renewTokenCalled = true; return _returnedDummyTokenDetails.ToJson().ToAblyResponse(); } return AblyResponse.EmptyResponse.ToTask(); })); }
public async Task WithTokenErrorAndRenewableToken_ShouldRenewTokenAutomaticallyWithoutEmittingError() { Now = DateTimeOffset.Now; var tokenDetails = new TokenDetails("id") { Expires = Now.AddHours(1) }; bool renewTokenCalled = false; var client = GetClientWithFakeTransport(opts => { opts.TokenDetails = tokenDetails; opts.UseBinaryProtocol = false; }, request => { if (request.Url.Contains("/keys")) { renewTokenCalled = true; return(_returnedDummyTokenDetails.ToJson().ToAblyResponse()); } return(AblyResponse.EmptyResponse.ToTask()); }); List <ErrorInfo> raisedErrors = new List <ErrorInfo>(); client.Connection.InternalStateChanged += (sender, args) => { if (args.HasError) { raisedErrors.Add(args.Reason); } }; await client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Error) { Error = new ErrorInfo("Unauthorised", _tokenErrorCode, HttpStatusCode.Unauthorized) }); renewTokenCalled.Should().BeTrue(); var currentToken = client.RestClient.AblyAuth.CurrentToken; currentToken.Token.Should().Be(_returnedDummyTokenDetails.Token); currentToken.ClientId.Should().Be(_returnedDummyTokenDetails.ClientId); currentToken.Expires.Should().BeCloseTo(_returnedDummyTokenDetails.Expires); raisedErrors.Should().BeEmpty("No errors should be raised!"); }