예제 #1
0
        private RequestCount MockItemAndLogHttpRequests(RequestCount actualHttpRequests)
        {
            mockHttp.When($"{baseUrl}/items/{"coffee_beverages_explained"}?language=es-ES")
            .Respond("application/json", (request) => GetMockedJsonAndLog(actualHttpRequests));

            return(actualHttpRequests);
        }
예제 #2
0
        public async void GetEntryFromCacheAfterFirstRequest()
        {
            var plannedHttpRequests = 2;
            var actualHttpRequests  = new RequestCount();
            var cachedClient        = GetCachedDeliveryClient(mockFunc: MockItemAndLogHttpRequests, actualHttpRequests: actualHttpRequests);

            for (int i = 0; i < plannedHttpRequests; i++)
            {
                var response = await cachedClient.GetItemAsync("coffee_beverages_explained", new LanguageParameter("es-ES"));
            }

            Assert.Equal(1, actualHttpRequests.Value);
        }
예제 #3
0
        private FileStream GetMockedJsonAndLog(RequestCount actualHttpRequests)
        {
            actualHttpRequests.Value++;

            return(GetMockedItemJsonStream());
        }
예제 #4
0
        private void InitClientPrerequisites(out HttpClient httpClient, out DeliveryOptions deliveryOptions, Action mockAction = null, Func <RequestCount, RequestCount> mockFunc = null, RequestCount actualHttpRequests = null)
        {
            if (mockAction != null)
            {
                mockAction();
            }
            else
            {
                mockFunc?.Invoke(actualHttpRequests);
            }

            httpClient = mockHttp.ToHttpClient();

            deliveryOptions = new DeliveryOptions
            {
                ProjectId = guid
            };
        }
예제 #5
0
        private CachedDeliveryClient GetCachedDeliveryClient(Action mockAction = null, Func <RequestCount, RequestCount> mockFunc = null, RequestCount actualHttpRequests = null)
        {
            HttpClient      httpClient      = null;
            DeliveryOptions deliveryOptions = null;

            if (mockAction != null)
            {
                InitClientPrerequisites(out httpClient, out deliveryOptions, mockAction: mockAction);
            }
            else if (mockFunc != null && actualHttpRequests != null)
            {
                InitClientPrerequisites(out httpClient, out deliveryOptions, mockFunc: mockFunc, actualHttpRequests: actualHttpRequests);
            }

            var projectOptions = Options.Create(new ProjectOptions
            {
                CacheTimeoutSeconds = 60,
                DeliveryOptions     = deliveryOptions
            });

            var memoryCacheOptions = Options.Create(new MemoryCacheOptions
            {
                Clock = new TestClock(),
                ExpirationScanFrequency = new TimeSpan(0, 0, 5)
            });

            var cacheManager = new ReactiveCacheManager(projectOptions, new MemoryCache(memoryCacheOptions), new DependentFormatResolver(), new WebhookListener());

            return(new CachedDeliveryClient(projectOptions, cacheManager
                                            , DeliveryClientBuilder.WithOptions(o => deliveryOptions).WithCodeFirstTypeProvider(new Models.CustomTypeProvider()).WithHttpClient(httpClient).Build()));
        }