private RequestCount MockItemAndLogHttpRequests(RequestCount actualHttpRequests) { mockHttp.When($"{baseUrl}/items/{"coffee_beverages_explained"}?language=es-ES") .Respond("application/json", (request) => GetMockedJsonAndLog(actualHttpRequests)); return(actualHttpRequests); }
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); }
private FileStream GetMockedJsonAndLog(RequestCount actualHttpRequests) { actualHttpRequests.Value++; return(GetMockedItemJsonStream()); }
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 }; }
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())); }