using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using CacheManager.Core; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Ocelot.Cache; using Ocelot.Configuration; using Ocelot.Configuration.File; using Ocelot.Configuration.Setter; using Ocelot.DependencyInjection; using Ocelot.Requester; using Ocelot.UnitTests.Requester; using Shouldly; using IdentityServer4.AccessTokenValidation; using TestStack.BDDfy; using Xunit; using static Ocelot.UnitTests.Middleware.UserDefinedResponseAggregatorTests; using Ocelot.Middleware.Multiplexer; namespace Ocelot.UnitTests.DependencyInjection { public class OcelotBuilderTests { private readonly IServiceCollection _services; private IServiceProvider _serviceProvider; private readonly IConfiguration _configRoot; private IOcelotBuilder _ocelotBuilder; private readonly int _maxRetries; private Exception _ex; public OcelotBuilderTests() { _configRoot = new ConfigurationRoot(new List()); _services = new ServiceCollection(); _services.AddSingleton(); _services.AddSingleton(_configRoot); _maxRetries = 100; } [Fact] public void should_add_specific_delegating_handlers_transient() { this.Given(x => WhenISetUpOcelotServices()) .When(x => AddSpecificTransientDelegatingHandler()) .And(x => AddSpecificTransientDelegatingHandler()) .Then(x => ThenTheProviderIsRegisteredAndReturnsSpecificHandlers()) .And(x => ThenTheSpecificHandlersAreTransient()) .BDDfy(); } [Fact] public void should_add_global_delegating_handlers_transient() { this.Given(x => WhenISetUpOcelotServices()) .When(x => AddTransientGlobalDelegatingHandler()) .And(x => AddTransientGlobalDelegatingHandler()) .Then(x => ThenTheProviderIsRegisteredAndReturnsHandlers()) .And(x => ThenTheGlobalHandlersAreTransient()) .BDDfy(); } [Fact] public void should_set_up_services() { this.When(x => WhenISetUpOcelotServices()) .Then(x => ThenAnExceptionIsntThrown()) .BDDfy(); } [Fact] public void should_return_ocelot_builder() { this.When(x => WhenISetUpOcelotServices()) .Then(x => ThenAnOcelotBuilderIsReturned()) .BDDfy(); } [Fact] public void should_set_up_cache_manager() { this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenISetUpCacheManager()) .Then(x => ThenAnExceptionIsntThrown()) .And(x => OnlyOneVersionOfEachCacheIsRegistered()) .BDDfy(); } [Fact] public void should_set_up_consul() { this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenISetUpConsul()) .Then(x => ThenAnExceptionIsntThrown()) .BDDfy(); } [Fact] public void should_set_up_rafty() { this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenISetUpRafty()) .Then(x => ThenAnExceptionIsntThrown()) .Then(x => ThenTheCorrectAdminPathIsRegitered()) .BDDfy(); } [Fact] public void should_set_up_administration_with_identity_server_options() { Action options = o => {}; this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenISetUpAdministration(options)) .Then(x => ThenAnExceptionIsntThrown()) .Then(x => ThenTheCorrectAdminPathIsRegitered()) .BDDfy(); } [Fact] public void should_set_up_administration() { this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenISetUpAdministration()) .Then(x => ThenAnExceptionIsntThrown()) .Then(x => ThenTheCorrectAdminPathIsRegitered()) .BDDfy(); } [Fact] public void should_use_logger_factory() { this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenIValidateScopes()) .When(x => WhenIAccessLoggerFactory()) .Then(x => ThenAnExceptionIsntThrown()) .BDDfy(); } [Fact] public void should_set_up_tracing() { this.Given(x => WhenISetUpOcelotServices()) .When(x => WhenISetUpOpentracing()) .When(x => WhenIAccessOcelotHttpTracingHandler()) .BDDfy(); } [Fact] public void should_set_up_without_passing_in_config() { this.When(x => WhenISetUpOcelotServicesWithoutConfig()) .Then(x => ThenAnExceptionIsntThrown()) .BDDfy(); } [Fact] public void should_add_singleton_defined_aggregators() { this.Given(x => WhenISetUpOcelotServices()) .When(x => AddSingletonDefinedAggregator()) .When(x => AddSingletonDefinedAggregator()) .Then(x => ThenTheProviderIsRegisteredAndReturnsSpecificAggregators()) .And(x => ThenTheAggregatorsAreSingleton()) .BDDfy(); } [Fact] public void should_add_transient_defined_aggregators() { this.Given(x => WhenISetUpOcelotServices()) .When(x => AddTransientDefinedAggregator()) .When(x => AddTransientDefinedAggregator()) .Then(x => ThenTheProviderIsRegisteredAndReturnsSpecificAggregators()) .And(x => ThenTheAggregatorsAreTransient()) .BDDfy(); } private void AddSingletonDefinedAggregator() where T : class, IDefinedAggregator { _ocelotBuilder.AddSingletonDefinedAggregator(); } private void AddTransientDefinedAggregator() where T : class, IDefinedAggregator { _ocelotBuilder.AddTransientDefinedAggregator(); } private void ThenTheSpecificHandlersAreSingleton() { var handlers = _serviceProvider.GetServices().ToList(); var first = handlers[0]; handlers = _serviceProvider.GetServices().ToList(); var second = handlers[0]; first.ShouldBe(second); } private void ThenTheSpecificHandlersAreTransient() { var handlers = _serviceProvider.GetServices().ToList(); var first = handlers[0]; handlers = _serviceProvider.GetServices().ToList(); var second = handlers[0]; first.ShouldNotBe(second); } private void ThenTheGlobalHandlersAreTransient() { var handlers = _serviceProvider.GetServices().ToList(); var first = handlers[0].DelegatingHandler; handlers = _serviceProvider.GetServices().ToList(); var second = handlers[0].DelegatingHandler; first.ShouldNotBe(second); } private void WhenISetUpAdministration() { _ocelotBuilder.AddAdministration("/administration", "secret"); } private void WhenISetUpAdministration(Action options) { _ocelotBuilder.AddAdministration("/administration", options); } private void AddTransientGlobalDelegatingHandler() where T : DelegatingHandler { _ocelotBuilder.AddDelegatingHandler(true); } private void AddSpecificTransientDelegatingHandler() where T : DelegatingHandler { _ocelotBuilder.AddDelegatingHandler(); } private void ThenTheCorrectAdminPathIsRegitered() { _serviceProvider = _services.BuildServiceProvider(); var path = _serviceProvider.GetService(); path.Path.ShouldBe("/administration"); } private void ThenTheProviderIsRegisteredAndReturnsHandlers() { _serviceProvider = _services.BuildServiceProvider(); var handlers = _serviceProvider.GetServices().ToList(); handlers[0].DelegatingHandler.ShouldBeOfType(); handlers[1].DelegatingHandler.ShouldBeOfType(); } private void ThenTheProviderIsRegisteredAndReturnsSpecificHandlers() { _serviceProvider = _services.BuildServiceProvider(); var handlers = _serviceProvider.GetServices().ToList(); handlers[0].ShouldBeOfType(); handlers[1].ShouldBeOfType(); } private void ThenTheProviderIsRegisteredAndReturnsSpecificAggregators() { _serviceProvider = _services.BuildServiceProvider(); var handlers = _serviceProvider.GetServices().ToList(); handlers[0].ShouldBeOfType(); handlers[1].ShouldBeOfType(); } private void ThenTheAggregatorsAreTransient() { var aggregators = _serviceProvider.GetServices().ToList(); var first = aggregators[0]; aggregators = _serviceProvider.GetServices().ToList(); var second = aggregators[0]; first.ShouldNotBe(second); } private void ThenTheAggregatorsAreSingleton() { var aggregators = _serviceProvider.GetServices().ToList(); var first = aggregators[0]; aggregators = _serviceProvider.GetServices().ToList(); var second = aggregators[0]; first.ShouldBe(second); } private void OnlyOneVersionOfEachCacheIsRegistered() { var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager)); var instance = (ICacheManager)outputCacheManager.ImplementationInstance; var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager)); var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache)); var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager)); instance.Configuration.MaxRetries.ShouldBe(_maxRetries); outputCache.ShouldNotBeNull(); ocelotConfigCache.ShouldNotBeNull(); ocelotConfigCacheManager.ShouldNotBeNull(); fileConfigCache.ShouldNotBeNull(); fileConfigCacheManager.ShouldNotBeNull(); } private void WhenISetUpConsul() { try { _ocelotBuilder.AddStoreOcelotConfigurationInConsul(); } catch (Exception e) { _ex = e; } } private void WhenISetUpRafty() { try { _ocelotBuilder.AddAdministration("/administration", "secret").AddRafty(); } catch (Exception e) { _ex = e; } } private void AddGlobalDelegatingHandler() where T : DelegatingHandler { _ocelotBuilder.AddDelegatingHandler(true); } private void AddSpecificDelegatingHandler() where T : DelegatingHandler { _ocelotBuilder.AddDelegatingHandler(); } private void ThenAnOcelotBuilderIsReturned() { _ocelotBuilder.ShouldBeOfType(); } private void WhenISetUpOcelotServices() { try { _ocelotBuilder = _services.AddOcelot(_configRoot); } catch (Exception e) { _ex = e; } } private void WhenISetUpOcelotServicesWithoutConfig() { try { _ocelotBuilder = _services.AddOcelot(); } catch (Exception e) { _ex = e; } } private void WhenISetUpCacheManager() { try { _ocelotBuilder.AddCacheManager(x => { x.WithMaxRetries(_maxRetries); x.WithDictionaryHandle(); }); } catch (Exception e) { _ex = e; } } private void WhenISetUpOpentracing() { try { _ocelotBuilder.AddOpenTracing( option => { option.CollectorUrl = "http://localhost:9618"; option.Service = "Ocelot.ManualTest"; } ); } catch (Exception e) { _ex = e; } } private void WhenIAccessLoggerFactory() { try { _serviceProvider = _services.BuildServiceProvider(); var logger = _serviceProvider.GetService(); logger.ShouldNotBeNull(); } catch (Exception e) { _ex = e; } } private void WhenIAccessOcelotHttpTracingHandler() { try { var tracingHandler = _serviceProvider.GetService(); tracingHandler.ShouldNotBeNull(); } catch (Exception e) { _ex = e; } } private void WhenIValidateScopes() { try { _serviceProvider = _services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true }); } catch (Exception e) { _ex = e; } } private void ThenAnExceptionIsntThrown() { _ex.ShouldBeNull(); } } }