public async Task DoNotRequestNewTokenWhenRefreshedTokenIsNotExpired()
        {
            MockHttp.Clear();

            var getToken = MockHttp.When(AuthUrl)
                           .WithFormData("grant_type", "password")
                           .Respond(HttpStatusCode.OK, "application/json",
                                    @"{
                        'access_token': 'old_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 0,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var refreshToken = MockHttp.When(AuthUrl)
                               .WithFormData("grant_type", "refresh_token")
                               .Respond(HttpStatusCode.OK, "application/json",
                                        @"{
                        'access_token': 'new_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 1000,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var provider = CreateTokenProvider();

            await provider.GetAuthorizationHeader(false);

            await provider.GetAuthorizationHeader(false);

            var header = await provider.GetAuthorizationHeader(false);

            Assert.Equal("Authorization", header.Key);
            Assert.Equal("token_type new_token", header.Value);

            Assert.Equal(1, MockHttp.GetMatchCount(getToken));
            Assert.Equal(1, MockHttp.GetMatchCount(refreshToken));
        }
        public async Task ProvideAccessToken()
        {
            MockHttp.Clear();

            MockHttp
            .When(AuthUrl)
            .Respond(HttpStatusCode.OK, "application/json",
                     @"{
                    'access_token': 'access_token_value',
                    'token_type': 'token_type_value',
                    'refresh_token': 'refresh_token_value',
                    'expires_in': 1000,
                    'scope': 'scim.me openid password.write approvals.me oauth.approvals',
                    'jti': '11111111-1111-1111-1111-111111111111'
                }");

            var header = await CreateTokenProvider().GetAuthorizationHeader(false);

            Assert.Equal("Authorization", header.Key);
            Assert.Equal("token_type_value access_token_value", header.Value);
        }
        public async Task SendAdditionalRequestIfTokenExpired()
        {
            var json = @"[
                    'name1',
                    'name2',
                    'name3'
                ]".QuotesToDoubleQuotes();

            var expectedList = new List <string>()
            {
                "name1",
                "name2",
                "name3"
            };

            var url = BaseUrl + $"/v1/o/{OrgName}/apiproducts?count={EntitiesLimit}";

            MockHttp.Clear();

            var getProductsUnauthorized = MockHttp.When(url)
                                          .WithHeaders("Authorization", "token_type access_token")
                                          .Respond(HttpStatusCode.Unauthorized);

            var getToken = MockHttp.When(AuthUrl)
                           .WithFormData("grant_type", "password")
                           .Respond(HttpStatusCode.OK, "application/json",
                                    @"{
                        'access_token': 'access_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 1000,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var refreshToken = MockHttp.When(AuthUrl)
                               .WithFormData("grant_type", "refresh_token")
                               .Respond(HttpStatusCode.OK, "application/json",
                                        @"{
                        'access_token': 'new_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 1000,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var getProductsSuccessful = MockHttp.When(url)
                                        .WithHeaders(@"Authorization: token_type new_token")
                                        .Respond("application/json", json);

            var apiProductNames = await GetApigeeClient().GetApiProductNames();

            Assert.Equal(expectedList.Count, apiProductNames.Count);
            Assert.Equal(expectedList[0], apiProductNames[0]);
            Assert.Equal(expectedList[1], apiProductNames[1]);
            Assert.Equal(expectedList[2], apiProductNames[2]);

            Assert.Equal(1, MockHttp.GetMatchCount(getProductsUnauthorized));
            Assert.Equal(1, MockHttp.GetMatchCount(getProductsSuccessful));
            Assert.Equal(1, MockHttp.GetMatchCount(getToken));
            Assert.Equal(1, MockHttp.GetMatchCount(refreshToken));
        }