mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 16:10:50 +08:00 
			
		
		
		
	Merge branch 'develop' into feature/config_grow_when_merged
# Conflicts: # src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs # test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs
This commit is contained in:
		@@ -46,7 +46,7 @@
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_merge_files()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenMultipleConfigurationFiles(false))
 | 
			
		||||
            this.Given(_ => GivenMultipleConfigurationFiles("", false))
 | 
			
		||||
                .When(_ => WhenIAddOcelotConfiguration(false))
 | 
			
		||||
                .Then(_ => ThenTheConfigsAreMerged())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
@@ -55,15 +55,30 @@
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_merge_files_except_env()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(_ => GivenMultipleConfigurationFiles(true))
 | 
			
		||||
            this.Given(_ => GivenMultipleConfigurationFiles("", true))
 | 
			
		||||
                .When(_ => WhenIAddOcelotConfiguration(true))
 | 
			
		||||
                .Then(_ => ThenTheConfigsAreMerged())
 | 
			
		||||
                .And(_ => NotContainsEnvSpecificConfig())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenMultipleConfigurationFiles(bool addEnvSpecificConfig)
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_merge_files_in_specific_folder()
 | 
			
		||||
        {
 | 
			
		||||
            string configFolder = "ConfigFiles";
 | 
			
		||||
            this.Given(_ => GivenMultipleConfigurationFiles(configFolder, false))
 | 
			
		||||
                .When(_ => WhenIAddOcelotConfigurationWithSpecificFolder(configFolder))
 | 
			
		||||
                .Then(_ => ThenTheConfigsAreMerged())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenMultipleConfigurationFiles(string folder, bool addEnvSpecificConfig)
 | 
			
		||||
        {
 | 
			
		||||
            if (!string.IsNullOrEmpty(folder))
 | 
			
		||||
            {
 | 
			
		||||
                Directory.CreateDirectory(folder);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _globalConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                GlobalConfiguration = new FileGlobalConfiguration
 | 
			
		||||
@@ -209,14 +224,20 @@
 | 
			
		||||
                    }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            File.WriteAllText("ocelot.global.json", JsonConvert.SerializeObject(_globalConfig));
 | 
			
		||||
            File.WriteAllText("ocelot.reRoutesA.json", JsonConvert.SerializeObject(_reRouteA));
 | 
			
		||||
            File.WriteAllText("ocelot.reRoutesB.json", JsonConvert.SerializeObject(_reRouteB));
 | 
			
		||||
            File.WriteAllText("ocelot.aggregates.json", JsonConvert.SerializeObject(_aggregate));
 | 
			
		||||
            string globalFilename = Path.Combine(folder, "ocelot.global.json");
 | 
			
		||||
            string reroutesAFilename = Path.Combine(folder, "ocelot.reRoutesA.json");
 | 
			
		||||
            string reroutesBFilename = Path.Combine(folder, "ocelot.reRoutesB.json");
 | 
			
		||||
            string aggregatesFilename = Path.Combine(folder, "ocelot.aggregates.json");
 | 
			
		||||
 | 
			
		||||
            File.WriteAllText(globalFilename, JsonConvert.SerializeObject(_globalConfig));
 | 
			
		||||
            File.WriteAllText(reroutesAFilename, JsonConvert.SerializeObject(_reRouteA));
 | 
			
		||||
            File.WriteAllText(reroutesBFilename, JsonConvert.SerializeObject(_reRouteB));
 | 
			
		||||
            File.WriteAllText(aggregatesFilename, JsonConvert.SerializeObject(_aggregate));
 | 
			
		||||
 | 
			
		||||
            if (addEnvSpecificConfig)
 | 
			
		||||
            {
 | 
			
		||||
                File.WriteAllText("ocelot.Env.json", JsonConvert.SerializeObject(_envSpecific));
 | 
			
		||||
                string envSpecificFilename = Path.Combine(folder, "ocelot.Env.json");
 | 
			
		||||
                File.WriteAllText(envSpecificFilename, JsonConvert.SerializeObject(_envSpecific));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -232,6 +253,13 @@
 | 
			
		||||
            _configRoot = builder.Build();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIAddOcelotConfigurationWithSpecificFolder(string folder)
 | 
			
		||||
        {
 | 
			
		||||
            IConfigurationBuilder builder = new ConfigurationBuilder();
 | 
			
		||||
            builder.AddOcelot(folder);
 | 
			
		||||
            _configRoot = builder.Build();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheConfigsAreMerged()
 | 
			
		||||
        {
 | 
			
		||||
            var fc = (FileConfiguration)_configRoot.Get(typeof(FileConfiguration));
 | 
			
		||||
 
 | 
			
		||||
@@ -1,28 +1,23 @@
 | 
			
		||||
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
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
    using Microsoft.AspNetCore.Hosting.Internal;
 | 
			
		||||
    using Microsoft.Extensions.Configuration;
 | 
			
		||||
    using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
    using Ocelot.Configuration.Setter;
 | 
			
		||||
    using Ocelot.DependencyInjection;
 | 
			
		||||
    using Ocelot.Requester;
 | 
			
		||||
    using Ocelot.UnitTests.Requester;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
    using static Ocelot.UnitTests.Middleware.UserDefinedResponseAggregatorTests;
 | 
			
		||||
    using Ocelot.Middleware.Multiplexer;
 | 
			
		||||
 | 
			
		||||
    public class OcelotBuilderTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IServiceCollection _services;
 | 
			
		||||
@@ -79,57 +74,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
                .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<IdentityServerAuthenticationOptions> 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()
 | 
			
		||||
        {
 | 
			
		||||
@@ -140,15 +84,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
                .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()
 | 
			
		||||
        {
 | 
			
		||||
@@ -191,15 +126,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            _ocelotBuilder.AddTransientDefinedAggregator<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheSpecificHandlersAreSingleton()
 | 
			
		||||
        {
 | 
			
		||||
            var handlers = _serviceProvider.GetServices<DelegatingHandler>().ToList();
 | 
			
		||||
            var first = handlers[0];
 | 
			
		||||
            handlers = _serviceProvider.GetServices<DelegatingHandler>().ToList();
 | 
			
		||||
            var second = handlers[0];
 | 
			
		||||
            first.ShouldBe(second);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheSpecificHandlersAreTransient()
 | 
			
		||||
        {
 | 
			
		||||
            var handlers = _serviceProvider.GetServices<DelegatingHandler>().ToList();
 | 
			
		||||
@@ -218,16 +144,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            first.ShouldNotBe(second);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenISetUpAdministration()
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.AddAdministration("/administration", "secret");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenISetUpAdministration(Action<IdentityServerAuthenticationOptions> options)
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.AddAdministration("/administration", options);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddTransientGlobalDelegatingHandler<T>()
 | 
			
		||||
            where T : DelegatingHandler
 | 
			
		||||
        {
 | 
			
		||||
@@ -240,13 +156,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            _ocelotBuilder.AddDelegatingHandler<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheCorrectAdminPathIsRegitered()
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProvider = _services.BuildServiceProvider();
 | 
			
		||||
            var path = _serviceProvider.GetService<IAdministrationPath>();
 | 
			
		||||
            path.Path.ShouldBe("/administration");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheProviderIsRegisteredAndReturnsHandlers<TOne, TWo>()
 | 
			
		||||
        {
 | 
			
		||||
            _serviceProvider = _services.BuildServiceProvider();
 | 
			
		||||
@@ -289,60 +198,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            first.ShouldBe(second);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void OnlyOneVersionOfEachCacheIsRegistered()
 | 
			
		||||
        {
 | 
			
		||||
            var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>));
 | 
			
		||||
            var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<CachedResponse>));
 | 
			
		||||
            var instance = (ICacheManager<CachedResponse>)outputCacheManager.ImplementationInstance;
 | 
			
		||||
            var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<IInternalConfiguration>));
 | 
			
		||||
            var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<IInternalConfiguration>));
 | 
			
		||||
            var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<FileConfiguration>));
 | 
			
		||||
            var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<FileConfiguration>));
 | 
			
		||||
 | 
			
		||||
            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<T>()
 | 
			
		||||
            where T : DelegatingHandler
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.AddDelegatingHandler<T>(true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void AddSpecificDelegatingHandler<T>()
 | 
			
		||||
            where T : DelegatingHandler
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.AddDelegatingHandler<T>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenAnOcelotBuilderIsReturned()
 | 
			
		||||
        {
 | 
			
		||||
            _ocelotBuilder.ShouldBeOfType<OcelotBuilder>();
 | 
			
		||||
@@ -372,39 +227,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
@@ -419,19 +241,6 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIAccessOcelotHttpTracingHandler()
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                var tracingHandler = _serviceProvider.GetService<OcelotHttpTracingHandler>();
 | 
			
		||||
                tracingHandler.ShouldNotBeNull();
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e)
 | 
			
		||||
            {
 | 
			
		||||
                _ex = e;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIValidateScopes()
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user