コード例 #1
0
    public async Task AccessTokenCacheManager_AddOrGet_NotEmptyCache_RemoveItem_ShouldRenewCache()
    {
        var tokenResponse1 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_1", 5);

        var tokenResponse2 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_2", 5);

        var accessTokenCacheManager = new TokenResponseCacheManager(
            new MemoryCache(
                Options.Create(new MemoryCacheOptions())
                )
            );

        var cacheKey = "CacheKey_" + Guid.NewGuid();

        //should cache it and returne tokenResponse1
        var tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse1)); }
            );

        Assert.AreEqual(tokenResponse1, tokenResponseFromCache);

        //remove
        accessTokenCacheManager.Remove(cacheKey);

        //tokenResponse1 should be removed
        tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse2)); }//now it is tokenResponse2
            );

        Assert.AreEqual(tokenResponse2, tokenResponseFromCache);
    }
コード例 #2
0
    public async Task AccessTokenCacheManager_AddOrGet_NotEmptyCache_ShouldRenewExpiredItem()
    {
        var tokenResponse1 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_1", 1);

        var tokenResponse2 = await TokenResponseObjects.GetValidTokenResponseAsync("access_token_2", 1);

        var accessTokenCacheManager = new TokenResponseCacheManager(
            new MemoryCache(
                Options.Create(new MemoryCacheOptions())
                )
            );

        var cacheKey = "CacheKey_" + Guid.NewGuid();

        //should cache it and returne tokenResponse1
        var tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse1)); }
            );

        Assert.AreEqual(tokenResponse1, tokenResponseFromCache);

        //sleep for cache expiration
        Thread.Sleep(1000);

        //should cache it and return tokenResponse2
        tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            cacheKey,
            () => { return(Task.FromResult(tokenResponse2)); }//now it is 2
            );

        Assert.AreEqual(tokenResponse2, tokenResponseFromCache);
    }
コード例 #3
0
    public static async Task <HttpClientService> GetNew(HttpStatusCode coreStatusCode, string coreContent, bool validTokenResponse)
    {
        var httpClientService = new HttpClientServiceFactory(
            new CoreHttpClient(
                IHttpClientFactoryMocks.Get(coreStatusCode, coreContent).CreateClient()
                ),
            new HttpRequestMessageFactory(
                IHttpContextAccessorMocks.Get(),
                Options.Create(new HttpClientServiceOptions())
                ),
            new IdentityServerService(
                new IdentityServerHttpClientSelector(
                    new List <IIdentityServerHttpClient> {
            {
                new ClientCredentialsHttpClient(
                    IHttpClientFactoryMocks.Get(HttpStatusCode.OK).CreateClient()
                    )
            },
            {
                new PasswordHttpClient(
                    IHttpClientFactoryMocks.Get(HttpStatusCode.OK).CreateClient()
                    )
            }
        }
                    ),
                ITokenResponseCacheManagerMocks.Get(
                    validTokenResponse
                    ? await TokenResponseObjects.GetValidTokenResponseAsync("access_token", 5)
                    : await TokenResponseObjects.GetInvalidTokenResponseAsync("invalid_client")
                    )
                )
            ).CreateHttpClientService();

        return(httpClientService);
    }
コード例 #4
0
    public async Task AccessTokenCacheManager_AddOrGet_EmptyCache_ShouldRetrieveNew()
    {
        var tokenResponse = await TokenResponseObjects.GetValidTokenResponseAsync("access_token", 5);

        var accessTokenCacheManager = new TokenResponseCacheManager(
            new MemoryCache(
                Options.Create(new MemoryCacheOptions())
                )
            );

        var tokenResponseFromCache = await accessTokenCacheManager.AddOrGetExistingAsync(
            "CacheKey_" + Guid.NewGuid(),
            () => { return(Task.FromResult(tokenResponse)); }
            );

        Assert.AreEqual(tokenResponse, tokenResponseFromCache);
    }