public async Task Correctly_Detects_Stale_Token_Refreshes_And_Retries_Original_Request()
        {
            var originalToken = new OAuth2AccessToken()
            {
                Token = "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MzAzNDM3MzUsInNjb3BlcyI6Indwcm8gd2xvYyB3bnV0IHdzbGUgd3NldCB3aHIgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiJBQkNERUYiLCJhdWQiOiJJSktMTU4iLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0MzAzNDAxMzV9.z0VHrIEzjsBnjiNMBey6wtu26yHTnSWz_qlqoEpUlpc"
            };
            var refreshedToken = new OAuth2AccessToken()
            {
                Token = "Refreshed"
            };

            //mocking our implementation of token manager. This test is concerned with ensuring the wiring is done correctly. Not the actual refresh process.
            var fakeManager = new Mock <ITokenManager>();

            fakeManager.Setup(m => m.RefreshTokenAsync(It.IsAny <FitbitClient>())).Returns(() => Task.Run(() => refreshedToken));

            //we shortcircuit the request to fake an expired token on the first request, and assuming the token is different the second time we let the request through
            var fakeServer = new StaleTokenFaker();

            var sut = new FitbitClient(dummyCredentials, originalToken, fakeServer, /*Explicity activate autorefresh, default is true*/ true, fakeManager.Object);

            //Act
            var actualResponse = await sut.HttpClient.GetAsync("https://dev.fitbit.com/");

            //Assert
            Assert.AreEqual(refreshedToken, sut.AccessToken);

            fakeManager.Verify(m => m.RefreshTokenAsync(It.IsAny <FitbitClient>()), Times.Once);
            //Expecte two interceptions. First when we get the 401 refresh, and second when we retry after refreshing the stale token
            Assert.AreEqual(2, fakeServer.requestCount, "It looks like either the client did not retry after the token was refreshed, or the stale token was not detected");
        }
        public void Can_Handle_Failed_Refresh_Operation()
        {
            const int EXPECT_TWO_COUNT_STALE_AND_RETRY = 2;
            var       originalToken = new OAuth2AccessToken()
            {
                Token = "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MzAzNDM3MzUsInNjb3BlcyI6Indwcm8gd2xvYyB3bnV0IHdzbGUgd3NldCB3aHIgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiJBQkNERUYiLCJhdWQiOiJJSktMTU4iLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0MzAzNDAxMzV9.z0VHrIEzjsBnjiNMBey6wtu26yHTnSWz_qlqoEpUlpc"
            };
            var refreshedToken = new OAuth2AccessToken()
            {
                Token = "Refreshed"
            };

            //mocking our implementation of token manager. This test is concerned with ensuring the wiring is done correctly. Not the actual refresh process.
            var fakeManager = new Mock <ITokenManager>();

            fakeManager.Setup(m => m.RefreshTokenAsync(It.IsAny <FitbitClient>())).Returns(() => Task.Run(() => refreshedToken));

            //simulate failed refresh token.
            var fakeServer = new StaleTokenFaker(10);

            var sut = new FitbitClient(dummyCredentials, originalToken, fakeServer, fakeManager.Object);

            //Act
            var r = sut.HttpClient.GetAsync("https://dev.fitbit.com/");


            Assert.Throws <System.AggregateException>(() => r.Wait());
        }
        public async Task Disable_Automatic_Token_Refresh()
        {
            var originalToken = new OAuth2AccessToken()
            {
                Token = "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MzAzNDM3MzUsInNjb3BlcyI6Indwcm8gd2xvYyB3bnV0IHdzbGUgd3NldCB3aHIgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiJBQkNERUYiLCJhdWQiOiJJSktMTU4iLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0MzAzNDAxMzV9.z0VHrIEzjsBnjiNMBey6wtu26yHTnSWz_qlqoEpUlpc"
            };
            var refreshedToken = new OAuth2AccessToken()
            {
                Token = "Refreshed"
            };

            //mocking our implementation of token manager. This test is concerned with ensuring the wiring is done correctly. Not the actual refresh process.
            var fakeManager = new Mock <ITokenManager>();

            fakeManager.Setup(m => m.RefreshTokenAsync(It.IsAny <FitbitClient>())).Returns(() => Task.Run(() => refreshedToken));

            //we shortcircuit the request to return a stale token and ensure that the client lets the stale token response through
            var fakeServer = new StaleTokenFaker();

            var sut = new FitbitClient(dummyCredentials, originalToken, fakeServer, false, fakeManager.Object);

            //Act
            var actualResponse = await sut.HttpClient.GetAsync("https://dev.fitbit.com/");

            //Assert
            Assert.AreEqual(fakeServer.staleTokenresponse, actualResponse);
        }