mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	#296 started writing merge code
This commit is contained in:
		@@ -18,7 +18,6 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class FileInternalConfigurationCreator : IInternalConfigurationCreator
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IOptions<FileConfiguration> _options;
 | 
			
		||||
        private readonly IConfigurationValidator _configurationValidator;
 | 
			
		||||
        private readonly IOcelotLogger _logger;
 | 
			
		||||
        private readonly IClaimsToThingCreator _claimsToThingCreator;
 | 
			
		||||
@@ -36,7 +35,6 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
        private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
 | 
			
		||||
 | 
			
		||||
        public FileInternalConfigurationCreator(
 | 
			
		||||
            IOptions<FileConfiguration> options, 
 | 
			
		||||
            IConfigurationValidator configurationValidator,
 | 
			
		||||
            IOcelotLoggerFactory loggerFactory,
 | 
			
		||||
            IClaimsToThingCreator claimsToThingCreator,
 | 
			
		||||
@@ -62,7 +60,6 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
            _requestIdKeyCreator = requestIdKeyCreator;
 | 
			
		||||
            _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
 | 
			
		||||
            _authOptionsCreator = authOptionsCreator;
 | 
			
		||||
            _options = options;
 | 
			
		||||
            _configurationValidator = configurationValidator;
 | 
			
		||||
            _logger = loggerFactory.CreateLogger<FileInternalConfigurationCreator>();
 | 
			
		||||
            _claimsToThingCreator = claimsToThingCreator;
 | 
			
		||||
 
 | 
			
		||||
@@ -18,5 +18,14 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
            builder.Add(memorySource);
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            //var load all files with ocelot*.json
 | 
			
		||||
            //merge these files into one
 | 
			
		||||
            //save it as ocelot.json
 | 
			
		||||
            builder.AddJsonFile("ocelot.json");
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,18 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Castle.Components.DictionaryAdapter;
 | 
			
		||||
using Microsoft.Extensions.Options;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Cache;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.Configuration.Creator;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Configuration.Validator;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Cache;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Configuration.Builder;
 | 
			
		||||
    using Ocelot.Configuration.Creator;
 | 
			
		||||
    using Ocelot.Configuration.File;
 | 
			
		||||
    using Ocelot.Configuration.Validator;
 | 
			
		||||
    using Ocelot.Logging;
 | 
			
		||||
    using Ocelot.Responses;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
    using Ocelot.DependencyInjection;
 | 
			
		||||
    using Ocelot.Errors;
 | 
			
		||||
    using Ocelot.UnitTests.TestData;
 | 
			
		||||
@@ -24,23 +20,22 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
 | 
			
		||||
    public class FileConfigurationCreatorTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IOptions<FileConfiguration>> _fileConfig;
 | 
			
		||||
        private readonly Mock<IConfigurationValidator> _validator;
 | 
			
		||||
        private Response<IInternalConfiguration> _config;
 | 
			
		||||
        private FileConfiguration _fileConfiguration;
 | 
			
		||||
        private readonly Mock<IOcelotLoggerFactory> _logger;
 | 
			
		||||
        private readonly FileInternalConfigurationCreator _internalConfigurationCreator;
 | 
			
		||||
        private Mock<IClaimsToThingCreator> _claimsToThingCreator;
 | 
			
		||||
        private Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
 | 
			
		||||
        private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
 | 
			
		||||
        private Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
 | 
			
		||||
        private Mock<IServiceProviderConfigurationCreator> _serviceProviderConfigCreator;
 | 
			
		||||
        private Mock<IQoSOptionsCreator> _qosOptionsCreator;
 | 
			
		||||
        private Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
 | 
			
		||||
        private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
 | 
			
		||||
        private Mock<IRegionCreator> _regionCreator;
 | 
			
		||||
        private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
 | 
			
		||||
        private Mock<IAdministrationPath> _adminPath;
 | 
			
		||||
        private readonly Mock<IClaimsToThingCreator> _claimsToThingCreator;
 | 
			
		||||
        private readonly Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
 | 
			
		||||
        private readonly Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
 | 
			
		||||
        private readonly Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
 | 
			
		||||
        private readonly Mock<IServiceProviderConfigurationCreator> _serviceProviderConfigCreator;
 | 
			
		||||
        private readonly Mock<IQoSOptionsCreator> _qosOptionsCreator;
 | 
			
		||||
        private readonly Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
 | 
			
		||||
        private readonly Mock<IRateLimitOptionsCreator> _rateLimitOptions;
 | 
			
		||||
        private readonly Mock<IRegionCreator> _regionCreator;
 | 
			
		||||
        private readonly Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
 | 
			
		||||
        private readonly Mock<IAdministrationPath> _adminPath;
 | 
			
		||||
        private readonly Mock<IHeaderFindAndReplaceCreator> _headerFindAndReplaceCreator;
 | 
			
		||||
        private readonly Mock<IDownstreamAddressesCreator> _downstreamAddressesCreator;
 | 
			
		||||
 | 
			
		||||
@@ -48,7 +43,6 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
        {
 | 
			
		||||
            _logger = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _validator = new Mock<IConfigurationValidator>();
 | 
			
		||||
            _fileConfig = new Mock<IOptions<FileConfiguration>>();
 | 
			
		||||
            _claimsToThingCreator = new Mock<IClaimsToThingCreator>();
 | 
			
		||||
            _authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
 | 
			
		||||
            _upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
 | 
			
		||||
@@ -64,7 +58,6 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
            _downstreamAddressesCreator = new Mock<IDownstreamAddressesCreator>();
 | 
			
		||||
 | 
			
		||||
            _internalConfigurationCreator = new FileInternalConfigurationCreator( 
 | 
			
		||||
                _fileConfig.Object,
 | 
			
		||||
                _validator.Object, 
 | 
			
		||||
                _logger.Object,
 | 
			
		||||
                _claimsToThingCreator.Object,
 | 
			
		||||
@@ -262,7 +255,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
                .And(x => x.GivenTheConfigIsValid())
 | 
			
		||||
                .And(x => x.GivenTheFollowingRegionIsReturned("region"))
 | 
			
		||||
                .When(x => x.WhenICreateTheConfig())
 | 
			
		||||
                .Then(x => x.ThenTheRegionCreatorIsCalledCorrectly("region"))
 | 
			
		||||
                .Then(x => x.ThenTheRegionCreatorIsCalledCorrectly())
 | 
			
		||||
                .And(x => x.ThenTheHeaderFindAndReplaceCreatorIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
@@ -800,9 +793,6 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
        private void GivenTheConfigIs(FileConfiguration fileConfiguration)
 | 
			
		||||
        {
 | 
			
		||||
            _fileConfiguration = fileConfiguration;
 | 
			
		||||
            _fileConfig
 | 
			
		||||
                .Setup(x => x.Value)
 | 
			
		||||
                .Returns(_fileConfiguration);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenICreateTheConfig()
 | 
			
		||||
@@ -927,7 +917,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
                .Returns(region);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheRegionCreatorIsCalledCorrectly(string expected)
 | 
			
		||||
        private void ThenTheRegionCreatorIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            _regionCreator
 | 
			
		||||
                .Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once);
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,11 @@ using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.IO;
 | 
			
		||||
    using Newtonsoft.Json;
 | 
			
		||||
    using Ocelot.Configuration.File;
 | 
			
		||||
 | 
			
		||||
    public class ConfigurationBuilderExtensionsTests
 | 
			
		||||
    {
 | 
			
		||||
        private IConfigurationRoot _configuration;
 | 
			
		||||
@@ -20,6 +25,61 @@ namespace Ocelot.UnitTests.DependencyInjection
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_merge_files()
 | 
			
		||||
        {
 | 
			
		||||
            var globalConfig = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                GlobalConfiguration = new FileGlobalConfiguration
 | 
			
		||||
                {
 | 
			
		||||
                    BaseUrl = "BaseUrl",
 | 
			
		||||
                    RateLimitOptions = new FileRateLimitOptions
 | 
			
		||||
                    {
 | 
			
		||||
                        HttpStatusCode = 500,
 | 
			
		||||
                        ClientIdHeader = "ClientIdHeader",
 | 
			
		||||
                        DisableRateLimitHeaders = true,
 | 
			
		||||
                        QuotaExceededMessage = "QuotaExceededMessage",
 | 
			
		||||
                        RateLimitCounterPrefix = "RateLimitCounterPrefix"
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var reRoute = new FileConfiguration
 | 
			
		||||
            {
 | 
			
		||||
                ReRoutes = new List<FileReRoute>
 | 
			
		||||
                {
 | 
			
		||||
                    new FileReRoute
 | 
			
		||||
                    {
 | 
			
		||||
                        DownstreamScheme = "DownstreamScheme",
 | 
			
		||||
                        Key = "Key",
 | 
			
		||||
                        UpstreamHost = "UpstreamHost",
 | 
			
		||||
                        UpstreamHttpMethod = new List<string>
 | 
			
		||||
                        {
 | 
			
		||||
                            "UpstreamHttpMethod"
 | 
			
		||||
                        },
 | 
			
		||||
                        DownstreamHostAndPorts = new List<FileHostAndPort>
 | 
			
		||||
                        {
 | 
			
		||||
                            new FileHostAndPort
 | 
			
		||||
                            {
 | 
			
		||||
                                Host = "Host",
 | 
			
		||||
                                Port = 80
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var globalJson = JsonConvert.SerializeObject(globalConfig);
 | 
			
		||||
            //File.WriteAllText("ocelot.global.json", globalJson);
 | 
			
		||||
 | 
			
		||||
            var reRouteJson = JsonConvert.SerializeObject(reRoute);
 | 
			
		||||
            //File.WriteAllText("ocelot.reRoute.json", reRouteJson);
 | 
			
		||||
 | 
			
		||||
            IConfigurationBuilder builder = new ConfigurationBuilder();
 | 
			
		||||
            //builder.AddOcelot();
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheBaseUrl(string baseUrl)
 | 
			
		||||
        {
 | 
			
		||||
            #pragma warning disable CS0618
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user