public UnleashServiceFixture() { Settings = new UnleashSettings { UnleashApi = new Uri("http://localhost:4242/"), AppName = "IntegrationTest", InstanceTag = "Test" }; UnleashServices = new DefaultUnleashServices(Settings); ContextProvider = new DefaultUnleashContextProvider(); Unleash = new Unleash(Settings, UnleashServices, ContextProvider); UnleashServices?.FeatureToggleLoadComplete(false, CancellationToken.None).Wait(); var httpClient = new HttpClient { BaseAddress = new Uri("http://localhost:4242/admin/") }; httpClient.DefaultRequestHeaders.ConnectionClose = false; httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); AdminApiClient = new UnleashAdminApiClient(httpClient, JsonSerializer); }
public static IUnleashServiceCollection WithNewtonsoftJsonSerializer(this IUnleashServiceCollection serviceCollection, Action <NewtonsoftJsonSerializerSettings> settingsConfigurator = null) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } var settings = new NewtonsoftJsonSerializerSettings(); if (serviceCollection.UnleashConfiguration != null) { var section = serviceCollection.UnleashConfiguration.GetSection("Serialization:NewtonsoftJson"); section.Bind(settings); } settingsConfigurator?.Invoke(settings); SettingsValidator.Validate(settings); serviceCollection.AddSingleton(settings); serviceCollection.WithJsonSerializer <NewtonsoftJsonSerializer>(); return(serviceCollection); }
internal async Task FetchToggles_WhenHttpExceptionOccurs_ReturnsEmptyResult( HttpStatusCode responseStatusCode, string responseBody, string responseContentType, UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] UnleashApiClientRequestHeaders requestHeaders, string etag ) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); httpMessageHandler.SetupGetFeaturesRequestForException(requestHeaders, responseStatusCode, responseBody, responseContentType); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); var actualFetchTogglesResult = await apiClient.FetchToggles(etag, CancellationToken.None); Assert.Null(actualFetchTogglesResult.ToggleCollection); Assert.Null(actualFetchTogglesResult.Etag); Assert.False(actualFetchTogglesResult.HasChanged); httpMessageHandler.VerifyAll(); }
internal async Task ExecuteAsync_WhenHttpExceptionOccurs_CompletesSuccessfully( HttpStatusCode statusCode, string responseBody, string responseContentType, [Frozen] UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] Mock <IUnleashApiClientFactory> apiClientFactoryMock, [Frozen] UnleashApiClientRequestHeaders requestHeaders, List <string> strategies) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); var backgroundTask = new ClientRegistrationBackgroundTask(apiClientFactoryMock.Object, settings, strategies); httpMessageHandler.SetupPostRegisterClientRequestForException(requestHeaders, statusCode, responseBody, responseContentType); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); apiClientFactoryMock.Setup(cf => cf.CreateClient()).Returns(apiClient); await backgroundTask.ExecuteAsync(CancellationToken.None); httpMessageHandler.VerifyAll(); apiClientFactoryMock.VerifyAll(); }
internal async Task RegisterMetrics_WhenHttpExceptionOccurs_ReturnsFalse( HttpStatusCode responseStatusCode, string responseBody, string responseContentType, UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] UnleashApiClientRequestHeaders requestHeaders, MetricsBucket metricsBucket ) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); httpMessageHandler.SetupPostSendMetricsRequestForException(requestHeaders, responseStatusCode, responseBody, responseContentType); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); var threadSafeMetricsBucket = new ThreadSafeMetricsBucket(metricsBucket); var successResult = await apiClient.SendMetrics(threadSafeMetricsBucket, CancellationToken.None); Assert.False(successResult); httpMessageHandler.VerifyAll(); }
internal async Task FetchToggles_WhenServerReturns304NotModified_ReturnsCacheHitResult( UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] UnleashApiClientRequestHeaders requestHeaders, string etagMinusQuotes ) { var etag = $"\"{etagMinusQuotes}\""; var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); httpMessageHandler.SetupGetFeaturesRequestForCacheHit(requestHeaders, etag); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); var actualFetchTogglesResult = await apiClient.FetchToggles(etag, CancellationToken.None); Assert.Null(actualFetchTogglesResult.ToggleCollection); Assert.False(actualFetchTogglesResult.HasChanged); Assert.Equal(etag, actualFetchTogglesResult.Etag); httpMessageHandler.VerifyAll(); }
internal async Task FetchToggles_WhenServerReturnsExpectedToggles_ReturnsValidResult( UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] UnleashApiClientRequestHeaders requestHeaders, string etag ) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); var expectedToggles = new ToggleCollection(); httpMessageHandler.SetupGetFeaturesRequestForSuccess(jsonSerializer, expectedToggles, requestHeaders); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); var actualFetchTogglesResult = await apiClient.FetchToggles(etag, CancellationToken.None); var actualToggles = actualFetchTogglesResult.ToggleCollection; Assert.Equal(expectedToggles.Features, actualToggles.Features); Assert.True(actualFetchTogglesResult.HasChanged); Assert.NotEqual(etag, actualFetchTogglesResult.Etag); httpMessageHandler.VerifyAll(); }
internal async Task RegisterMetrics_WhenServerReturns200Ok_ReturnsTrue( UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] UnleashApiClientRequestHeaders requestHeaders, MetricsBucket metricsBucket ) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); httpMessageHandler.SetupPostSendMetricsRequestForSuccess(requestHeaders); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); var threadSafeMetricsBucket = new ThreadSafeMetricsBucket(metricsBucket); var successResult = await apiClient.SendMetrics(threadSafeMetricsBucket, CancellationToken.None); Assert.True(successResult); httpMessageHandler.VerifyAll(); }
internal async Task ExecuteAsync_WhenApiReturns200Ok_CompletesSuccessfully( [Frozen] UnleashSettings settings, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] Mock <IUnleashApiClientFactory> apiClientFactoryMock, [Frozen] UnleashApiClientRequestHeaders requestHeaders, [Frozen] MetricsBucket metricsBucket) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); var threadSafeMetricsBucket = new ThreadSafeMetricsBucket(metricsBucket); var backgroundTask = new ClientMetricsBackgroundTask(apiClientFactoryMock.Object, settings, threadSafeMetricsBucket); httpMessageHandler.SetupPostSendMetricsRequestForSuccess(requestHeaders); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); apiClientFactoryMock.Setup(cf => cf.CreateClient()).Returns(apiClient); await backgroundTask.ExecuteAsync(CancellationToken.None); httpMessageHandler.VerifyAll(); apiClientFactoryMock.VerifyAll(); }
internal async Task ExecuteAsync_WhenHttpExceptionOccurs_CompletesSuccessfully( HttpStatusCode statusCode, string responseBody, string responseContentType, [Frozen] UnleashSettings settings, [Frozen] ThreadSafeToggleCollection toggleCollection, [Frozen] Mock <IToggleCollectionCache> toggleCollectionCache, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] Mock <IUnleashApiClientFactory> apiClientFactoryMock, [Frozen] UnleashApiClientRequestHeaders requestHeaders, TaskCompletionSource <object> taskCompletionSource) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); var backgroundTask = new FetchFeatureTogglesTask(apiClientFactoryMock.Object, toggleCollection, toggleCollectionCache.Object, taskCompletionSource); httpMessageHandler.SetupGetFeaturesRequestForException(requestHeaders, statusCode, responseBody, responseContentType); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); apiClientFactoryMock.Setup(cf => cf.CreateClient()).Returns(apiClient); await backgroundTask.ExecuteAsync(CancellationToken.None); Assert.True(taskCompletionSource.Task.IsCompletedSuccessfully); httpMessageHandler.VerifyAll(); apiClientFactoryMock.VerifyAll(); toggleCollectionCache.VerifyNoOtherCalls(); }
internal async Task ExecuteAsync_WhenApiReturns200Ok_CompletesSuccessfully( [Frozen] UnleashSettings settings, [Frozen] ThreadSafeToggleCollection toggleCollection, [Frozen] Mock <IToggleCollectionCache> toggleCollectionCache, [Frozen] Mock <FakeHttpMessageHandler> httpMessageHandler, [Frozen] Mock <IUnleashApiClientFactory> apiClientFactoryMock, [Frozen] UnleashApiClientRequestHeaders requestHeaders, TaskCompletionSource <object> taskCompletionSource) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); var toggleCollectionInstance = toggleCollection.Instance; toggleCollectionCache .Setup( x => x.Save( It.Is <ToggleCollection>( y => y.Version.Equals(toggleCollectionInstance.Version) && y.Features.All( z => toggleCollectionInstance.Features.Any(zz => zz.Name.Equals(z.Name)) ) ), It.IsAny <string>(), CancellationToken.None ) ) .Returns(Task.CompletedTask); var backgroundTask = new FetchFeatureTogglesTask(apiClientFactoryMock.Object, toggleCollection, toggleCollectionCache.Object, taskCompletionSource); httpMessageHandler.SetupGetFeaturesRequestForSuccess(jsonSerializer, toggleCollectionInstance, requestHeaders); var httpClient = new HttpClient(httpMessageHandler.Object) { BaseAddress = settings.UnleashApi }; var apiClient = new UnleashApiClient(httpClient, jsonSerializer, requestHeaders); apiClientFactoryMock.Setup(cf => cf.CreateClient()).Returns(apiClient); await backgroundTask.ExecuteAsync(CancellationToken.None); Assert.True(taskCompletionSource.Task.IsCompletedSuccessfully); httpMessageHandler.VerifyAll(); apiClientFactoryMock.VerifyAll(); toggleCollectionCache.VerifyAll(); }
public async Task Load_WhenTogglesOrEtagFileDoesNotExists_ReturnsEmptyResult( bool toggleCollectionExists, bool etagExists, [Frozen] DistributedToggleCollectionCacheSettings settings, [Frozen] MemoryDistributedCache distributedCache, DistributedToggleCollectionCache cache ) { var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings(); var jsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); settings.EtagKeyName = "Etag"; settings.ToggleCollectionKeyName = "Toggles"; if (toggleCollectionExists) { var toggleCollection = new ToggleCollection(); 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); } } if (toggleCollectionExists && etagExists) { var etag = Guid.NewGuid().ToString(); distributedCache.SetString(settings.EtagKeyName, etag); } var result = await cache.Load(CancellationToken.None); Assert.Equal(string.Empty, result.InitialETag); Assert.Null(result.InitialToggleCollection); }
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); }
public override NewtonsoftJsonSerializer CreateSerializer() { var settings = new NewtonsoftJsonSerializerSettings(); return(new NewtonsoftJsonSerializer(settings)); }