Esempio n. 1
0
        public async Task WithHttpClientFactory_RegistersNecessaryServices(
            [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler,
            [Frozen] ToggleCollection expectedToggleCollection,
            string etag
            )
        {
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddUnleash(s =>
            {
                s.UnleashApi  = new Uri("http://localhost:4242/");
                s.AppName     = "Test";
                s.InstanceTag = "Test";
            })
            .WithNewtonsoftJsonSerializer()
            .WithHttpClientFactory(
                cb =>
            {
                cb.ConfigurePrimaryHttpMessageHandler(() => httpMessageHandler.Object);
            });

            var serviceProvider = serviceCollection.BuildServiceProvider(true);

            var unleashApiClientFactory = serviceProvider.GetRequiredService <IUnleashApiClientFactory>();

            Assert.IsType <HttpClientFactoryApiClientFactory>(unleashApiClientFactory);

            var unleashApiClient = unleashApiClientFactory.CreateClient();

            Assert.IsType <UnleashApiClient>(unleashApiClient);

            var concreteUnleashApiClient = (UnleashApiClient)unleashApiClient;

            var clientRequestHeaders = concreteUnleashApiClient.ClientRequestHeaders;

            httpMessageHandler.SetupGetFeaturesRequestForSuccess(concreteUnleashApiClient.JsonSerializer, expectedToggleCollection, clientRequestHeaders);
            var fetchTogglesResult = await unleashApiClient.FetchToggles(etag, CancellationToken.None);

            var actualToggleCollection = fetchTogglesResult.ToggleCollection;

            AssertionUtils.AssertToggleCollectionsEquivalent(expectedToggleCollection, actualToggleCollection);
        }
        public async Task Load_WhenValidToggleAndEtagFilesExist_ReturnsExpectedResult(
            [Frozen] MemoryDistributedCache distributedCache,
            ToggleCollection toggleCollection,
            string etag
            )
        {
            var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings();
            var jsonSerializer         = new NewtonsoftJsonSerializer(jsonSerializerSettings);

            var settings = new DistributedToggleCollectionCacheSettings
            {
                EtagEntryOptions =
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
                },
                ToggleCollectionEntryOptions =
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
                }
            };

            using (var ms = new MemoryStream())
            {
                jsonSerializer.Serialize(ms, toggleCollection);
                ms.Seek(0, SeekOrigin.Begin);

                await distributedCache.SetAsync(settings.ToggleCollectionKeyName, ms.ToArray(), settings.ToggleCollectionEntryOptions, CancellationToken.None).ConfigureAwait(false);

                await distributedCache.SetStringAsync(settings.EtagKeyName, etag, settings.EtagEntryOptions, CancellationToken.None).ConfigureAwait(false);
            }

            var cache  = new DistributedToggleCollectionCache(settings, distributedCache, jsonSerializer);
            var result = await cache.Load(CancellationToken.None);

            Assert.Equal(etag, result.InitialETag);

            AssertionUtils.AssertToggleCollectionsEquivalent(toggleCollection, result.InitialToggleCollection);
        }