mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 15:50:49 +08:00 
			
		
		
		
	#296 will now do a crappy merge on the configuration
This commit is contained in:
		@@ -22,12 +22,6 @@ namespace Ocelot.ManualTest
 | 
			
		||||
                        .AddJsonFile("appsettings.json", true, true)
 | 
			
		||||
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
 | 
			
		||||
                        .AddJsonFile("ocelot.json")
 | 
			
		||||
 | 
			
		||||
                        //.AddOcelot();
 | 
			
		||||
                        //load all the ocelot.xxx.json files that are not environments from asp.net core
 | 
			
		||||
                        //merge them into megaconfig
 | 
			
		||||
                        //save megaconfig to disk as ocelot.json
 | 
			
		||||
                        //then add to asp.net config stuff..
 | 
			
		||||
                        .AddEnvironmentVariables();
 | 
			
		||||
                })
 | 
			
		||||
                .ConfigureServices(s => {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,20 +15,33 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
    {
 | 
			
		||||
        private IConfigurationRoot _configuration;
 | 
			
		||||
        private string _result;
 | 
			
		||||
        private IConfigurationRoot _configRoot;
 | 
			
		||||
        private FileConfiguration _globalConfig;
 | 
			
		||||
        private FileConfiguration _reRoute;
 | 
			
		||||
        private FileConfiguration _reRouteB;
 | 
			
		||||
        private FileConfiguration _aggregate;
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_base_url_to_config()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(x => GivenTheBaseUrl("test"))
 | 
			
		||||
                .When(x => WhenIGet("BaseUrl"))
 | 
			
		||||
                .Then(x => ThenTheResultIs("test"))
 | 
			
		||||
            this.Given(_ => GivenTheBaseUrl("test"))
 | 
			
		||||
                .When(_ => WhenIGet("BaseUrl"))
 | 
			
		||||
                .Then(_ => ThenTheResultIs("test"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_merge_files()
 | 
			
		||||
        {
 | 
			
		||||
            var globalConfig = new FileConfiguration
 | 
			
		||||
            this.Given(_ => GivenMultipleConfigurationFiles())
 | 
			
		||||
                .When(_ => WhenIAddOcelotConfiguration())
 | 
			
		||||
                .Then(_ => ThenTheConfigsAreMerged())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenMultipleConfigurationFiles()
 | 
			
		||||
        {
 | 
			
		||||
            _globalConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                GlobalConfiguration = new FileGlobalConfiguration
 | 
			
		||||
                {
 | 
			
		||||
@@ -40,17 +53,25 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
                        DisableRateLimitHeaders = true,
 | 
			
		||||
                        QuotaExceededMessage = "QuotaExceededMessage",
 | 
			
		||||
                        RateLimitCounterPrefix = "RateLimitCounterPrefix"
 | 
			
		||||
                    }
 | 
			
		||||
                    },
 | 
			
		||||
                    ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
 | 
			
		||||
                    {
 | 
			
		||||
                        Host = "Host",
 | 
			
		||||
                        Port = 80,
 | 
			
		||||
                        Type = "Type"
 | 
			
		||||
                    },
 | 
			
		||||
                    RequestIdKey = "RequestIdKey"
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var reRoute = new FileConfiguration
 | 
			
		||||
            _reRoute = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamScheme = "DownstreamScheme",
 | 
			
		||||
                        DownstreamPathTemplate = "DownstreamPathTemplate",
 | 
			
		||||
                        Key = "Key",
 | 
			
		||||
                        UpstreamHost = "UpstreamHost",
 | 
			
		||||
                        UpstreamHttpMethod = new List<string>
 | 
			
		||||
@@ -69,15 +90,130 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var globalJson = JsonConvert.SerializeObject(globalConfig);
 | 
			
		||||
            _reRouteB = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamScheme = "DownstreamSchemeB",
 | 
			
		||||
                        DownstreamPathTemplate = "DownstreamPathTemplateB",
 | 
			
		||||
                        Key = "KeyB",
 | 
			
		||||
                        UpstreamHost = "UpstreamHostB",
 | 
			
		||||
                        UpstreamHttpMethod = new List<string>
 | 
			
		||||
                        {
 | 
			
		||||
                            "UpstreamHttpMethodB"
 | 
			
		||||
                        },
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "HostB",
 | 
			
		||||
                                Port = 80
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    },
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamScheme = "DownstreamSchemeBB",
 | 
			
		||||
                        DownstreamPathTemplate = "DownstreamPathTemplateBB",
 | 
			
		||||
                        Key = "KeyBB",
 | 
			
		||||
                        UpstreamHost = "UpstreamHostBB",
 | 
			
		||||
                        UpstreamHttpMethod = new List<string>
 | 
			
		||||
                        {
 | 
			
		||||
                            "UpstreamHttpMethodBB"
 | 
			
		||||
                        },
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "HostBB",
 | 
			
		||||
                                Port = 80
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            _aggregate = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                Aggregates = new List<FileAggregateReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileAggregateReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        ReRouteKeys = new List<string> 
 | 
			
		||||
                        {
 | 
			
		||||
                            "KeyB",
 | 
			
		||||
                            "KeyBB"
 | 
			
		||||
                        },
 | 
			
		||||
                        UpstreamPathTemplate = "UpstreamPathTemplate",
 | 
			
		||||
                    },
 | 
			
		||||
                    new FileAggregateReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        ReRouteKeys = new List<string> 
 | 
			
		||||
                        {
 | 
			
		||||
                            "KeyB",
 | 
			
		||||
                            "KeyBB"
 | 
			
		||||
                        },
 | 
			
		||||
                        UpstreamPathTemplate = "UpstreamPathTemplate",
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var globalJson = JsonConvert.SerializeObject(_globalConfig);
 | 
			
		||||
            File.WriteAllText("ocelot.global.json", globalJson);
 | 
			
		||||
 | 
			
		||||
            var reRouteJson = JsonConvert.SerializeObject(reRoute);
 | 
			
		||||
            var reRouteJson = JsonConvert.SerializeObject(_reRoute);
 | 
			
		||||
            File.WriteAllText("ocelot.reRoutes.json", reRouteJson);
 | 
			
		||||
 | 
			
		||||
            var reRouteJsonB = JsonConvert.SerializeObject(_reRouteB);
 | 
			
		||||
            File.WriteAllText("ocelot.reRoutesB.json", reRouteJsonB);
 | 
			
		||||
 | 
			
		||||
            var aggregates = JsonConvert.SerializeObject(_aggregate);
 | 
			
		||||
            File.WriteAllText("ocelot.aggregates.json", aggregates);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIAddOcelotConfiguration()
 | 
			
		||||
        {
 | 
			
		||||
            IConfigurationBuilder builder = new ConfigurationBuilder();
 | 
			
		||||
            builder.AddOcelot();
 | 
			
		||||
            var configRoot = builder.Build();
 | 
			
		||||
            _configRoot = builder.Build();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheConfigsAreMerged()
 | 
			
		||||
        {
 | 
			
		||||
            var fc = (FileConfiguration)_configRoot.Get(typeof(FileConfiguration));
 | 
			
		||||
 | 
			
		||||
            fc.GlobalConfiguration.BaseUrl.ShouldBe(_globalConfig.GlobalConfiguration.BaseUrl);
 | 
			
		||||
            fc.GlobalConfiguration.RateLimitOptions.ClientIdHeader.ShouldBe(_globalConfig.GlobalConfiguration.RateLimitOptions.ClientIdHeader);
 | 
			
		||||
            fc.GlobalConfiguration.RateLimitOptions.DisableRateLimitHeaders.ShouldBe(_globalConfig.GlobalConfiguration.RateLimitOptions.DisableRateLimitHeaders);
 | 
			
		||||
            fc.GlobalConfiguration.RateLimitOptions.HttpStatusCode.ShouldBe(_globalConfig.GlobalConfiguration.RateLimitOptions.HttpStatusCode);
 | 
			
		||||
            fc.GlobalConfiguration.RateLimitOptions.QuotaExceededMessage.ShouldBe(_globalConfig.GlobalConfiguration.RateLimitOptions.QuotaExceededMessage);
 | 
			
		||||
            fc.GlobalConfiguration.RateLimitOptions.RateLimitCounterPrefix.ShouldBe(_globalConfig.GlobalConfiguration.RateLimitOptions.RateLimitCounterPrefix);
 | 
			
		||||
            fc.GlobalConfiguration.RequestIdKey.ShouldBe(_globalConfig.GlobalConfiguration.RequestIdKey);
 | 
			
		||||
            fc.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(_globalConfig.GlobalConfiguration.ServiceDiscoveryProvider.Host);
 | 
			
		||||
            fc.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(_globalConfig.GlobalConfiguration.ServiceDiscoveryProvider.Port);
 | 
			
		||||
            fc.GlobalConfiguration.ServiceDiscoveryProvider.Type.ShouldBe(_globalConfig.GlobalConfiguration.ServiceDiscoveryProvider.Type);
 | 
			
		||||
 | 
			
		||||
            fc.ReRoutes.Count.ShouldBe(_reRoute.ReRoutes.Count + _reRouteB.ReRoutes.Count);
 | 
			
		||||
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.DownstreamPathTemplate == _reRoute.ReRoutes[0].DownstreamPathTemplate);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.DownstreamPathTemplate == _reRouteB.ReRoutes[0].DownstreamPathTemplate);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.DownstreamPathTemplate == _reRouteB.ReRoutes[1].DownstreamPathTemplate);
 | 
			
		||||
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.DownstreamScheme == _reRoute.ReRoutes[0].DownstreamScheme);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.DownstreamScheme == _reRouteB.ReRoutes[0].DownstreamScheme);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.DownstreamScheme == _reRouteB.ReRoutes[1].DownstreamScheme);
 | 
			
		||||
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.Key == _reRoute.ReRoutes[0].Key);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.Key == _reRouteB.ReRoutes[0].Key);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.Key == _reRouteB.ReRoutes[1].Key);
 | 
			
		||||
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.UpstreamHost == _reRoute.ReRoutes[0].UpstreamHost);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.UpstreamHost == _reRouteB.ReRoutes[0].UpstreamHost);
 | 
			
		||||
            fc.ReRoutes.ShouldContain(x => x.UpstreamHost == _reRouteB.ReRoutes[1].UpstreamHost);
 | 
			
		||||
 | 
			
		||||
            fc.Aggregates.Count.ShouldBe(_aggregate.Aggregates.Count);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheBaseUrl(string baseUrl)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user