mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 16:10:50 +08:00 
			
		
		
		
	tests for version creator
This commit is contained in:
		@@ -43,7 +43,7 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
        private bool _dangerousAcceptAnyServerCertificateValidator;
 | 
					        private bool _dangerousAcceptAnyServerCertificateValidator;
 | 
				
			||||||
        private SecurityOptions _securityOptions;
 | 
					        private SecurityOptions _securityOptions;
 | 
				
			||||||
        private string _downstreamHttpMethod;
 | 
					        private string _downstreamHttpMethod;
 | 
				
			||||||
        private string _downstreamHttpVersion;
 | 
					        private Version _downstreamHttpVersion;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public DownstreamReRouteBuilder()
 | 
					        public DownstreamReRouteBuilder()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -257,9 +257,9 @@ namespace Ocelot.Configuration.Builder
 | 
				
			|||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public DownstreamReRouteBuilder WithHttpVersion(string httpVersion)
 | 
					        public DownstreamReRouteBuilder WithHttpVersion(Version downstreamHttpVersion)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _downstreamHttpVersion = httpVersion;
 | 
					            _downstreamHttpVersion = downstreamHttpVersion;
 | 
				
			||||||
            return this;
 | 
					            return this;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										9
									
								
								src/Ocelot/Configuration/Creator/IVersionCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Ocelot/Configuration/Creator/IVersionCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					namespace Ocelot.Configuration.Creator
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    using System;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public interface IVersionCreator
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        Version Create(string downstreamHttpVersion);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -22,6 +22,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
        private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
 | 
					        private readonly IDownstreamAddressesCreator _downstreamAddressesCreator;
 | 
				
			||||||
        private readonly IReRouteKeyCreator _reRouteKeyCreator;
 | 
					        private readonly IReRouteKeyCreator _reRouteKeyCreator;
 | 
				
			||||||
        private readonly ISecurityOptionsCreator _securityOptionsCreator;
 | 
					        private readonly ISecurityOptionsCreator _securityOptionsCreator;
 | 
				
			||||||
 | 
					        private readonly IVersionCreator _versionCreator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public ReRoutesCreator(
 | 
					        public ReRoutesCreator(
 | 
				
			||||||
            IClaimsToThingCreator claimsToThingCreator,
 | 
					            IClaimsToThingCreator claimsToThingCreator,
 | 
				
			||||||
@@ -37,7 +38,8 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            IDownstreamAddressesCreator downstreamAddressesCreator,
 | 
					            IDownstreamAddressesCreator downstreamAddressesCreator,
 | 
				
			||||||
            ILoadBalancerOptionsCreator loadBalancerOptionsCreator,
 | 
					            ILoadBalancerOptionsCreator loadBalancerOptionsCreator,
 | 
				
			||||||
            IReRouteKeyCreator reRouteKeyCreator,
 | 
					            IReRouteKeyCreator reRouteKeyCreator,
 | 
				
			||||||
            ISecurityOptionsCreator securityOptionsCreator
 | 
					            ISecurityOptionsCreator securityOptionsCreator,
 | 
				
			||||||
 | 
					            IVersionCreator versionCreator
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _reRouteKeyCreator = reRouteKeyCreator;
 | 
					            _reRouteKeyCreator = reRouteKeyCreator;
 | 
				
			||||||
@@ -55,6 +57,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            _httpHandlerOptionsCreator = httpHandlerOptionsCreator;
 | 
					            _httpHandlerOptionsCreator = httpHandlerOptionsCreator;
 | 
				
			||||||
            _loadBalancerOptionsCreator = loadBalancerOptionsCreator;
 | 
					            _loadBalancerOptionsCreator = loadBalancerOptionsCreator;
 | 
				
			||||||
            _securityOptionsCreator = securityOptionsCreator;
 | 
					            _securityOptionsCreator = securityOptionsCreator;
 | 
				
			||||||
 | 
					            _versionCreator = versionCreator;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public List<ReRoute> Create(FileConfiguration fileConfiguration)
 | 
					        public List<ReRoute> Create(FileConfiguration fileConfiguration)
 | 
				
			||||||
@@ -104,6 +107,8 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var securityOptions = _securityOptionsCreator.Create(fileReRoute.SecurityOptions);
 | 
					            var securityOptions = _securityOptionsCreator.Create(fileReRoute.SecurityOptions);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var downstreamHttpVersion = _versionCreator.Create(fileReRoute.DownstreamHttpVersion);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
					            var reRoute = new DownstreamReRouteBuilder()
 | 
				
			||||||
                .WithKey(fileReRoute.Key)
 | 
					                .WithKey(fileReRoute.Key)
 | 
				
			||||||
                .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
 | 
					                .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
 | 
				
			||||||
@@ -138,7 +143,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
                .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
 | 
					                .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
 | 
				
			||||||
                .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
 | 
					                .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
 | 
				
			||||||
                .WithSecurityOptions(securityOptions)
 | 
					                .WithSecurityOptions(securityOptions)
 | 
				
			||||||
                .WithHttpVersion(fileReRoute.DownstreamHttpVersion)
 | 
					                .WithHttpVersion(downstreamHttpVersion)
 | 
				
			||||||
                .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod)
 | 
					                .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod)
 | 
				
			||||||
                .Build();
 | 
					                .Build();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										17
									
								
								src/Ocelot/Configuration/Creator/VersionCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/Ocelot/Configuration/Creator/VersionCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					namespace Ocelot.Configuration.Creator
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    using System;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public class VersionCreator : IVersionCreator
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public Version Create(string downstreamHttpVersion)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (!Version.TryParse(downstreamHttpVersion, out Version version))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                version = new Version(1, 1);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return version;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -41,7 +41,7 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
            bool dangerousAcceptAnyServerCertificateValidator,
 | 
					            bool dangerousAcceptAnyServerCertificateValidator,
 | 
				
			||||||
            SecurityOptions securityOptions,
 | 
					            SecurityOptions securityOptions,
 | 
				
			||||||
            string downstreamHttpMethod,
 | 
					            string downstreamHttpMethod,
 | 
				
			||||||
            string downstreamHttpVersion)
 | 
					            Version downstreamHttpVersion)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
 | 
					            DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
 | 
				
			||||||
            AddHeadersToDownstream = addHeadersToDownstream;
 | 
					            AddHeadersToDownstream = addHeadersToDownstream;
 | 
				
			||||||
@@ -112,6 +112,6 @@ namespace Ocelot.Configuration
 | 
				
			|||||||
        public bool DangerousAcceptAnyServerCertificateValidator { get; }
 | 
					        public bool DangerousAcceptAnyServerCertificateValidator { get; }
 | 
				
			||||||
        public SecurityOptions SecurityOptions { get; }
 | 
					        public SecurityOptions SecurityOptions { get; }
 | 
				
			||||||
        public string DownstreamHttpMethod { get; }
 | 
					        public string DownstreamHttpMethod { get; }
 | 
				
			||||||
        public string DownstreamHttpVersion { get;  }
 | 
					        public Version DownstreamHttpVersion { get;  }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -136,6 +136,7 @@ namespace Ocelot.DependencyInjection
 | 
				
			|||||||
            Services.TryAddSingleton<IFrameworkDescription, FrameworkDescription>();
 | 
					            Services.TryAddSingleton<IFrameworkDescription, FrameworkDescription>();
 | 
				
			||||||
            Services.TryAddSingleton<IQoSFactory, QoSFactory>();
 | 
					            Services.TryAddSingleton<IQoSFactory, QoSFactory>();
 | 
				
			||||||
            Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
 | 
					            Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
 | 
				
			||||||
 | 
					            Services.TryAddSingleton<IVersionCreator, VersionCreator>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //add security
 | 
					            //add security
 | 
				
			||||||
            this.AddSecurity();
 | 
					            this.AddSecurity();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,17 +20,12 @@
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            try
 | 
					            try
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                if (!Version.TryParse(downstreamReRoute.DownstreamHttpVersion, out Version version))
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    version = new Version(1, 1);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                
 | 
					 | 
				
			||||||
                var requestMessage = new HttpRequestMessage()
 | 
					                var requestMessage = new HttpRequestMessage()
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    Content = await MapContent(request),
 | 
					                    Content = await MapContent(request),
 | 
				
			||||||
                    Method = MapMethod(request, downstreamReRoute),
 | 
					                    Method = MapMethod(request, downstreamReRoute),
 | 
				
			||||||
                    RequestUri = MapUri(request),
 | 
					                    RequestUri = MapUri(request),
 | 
				
			||||||
                    Version = version,
 | 
					                    Version = downstreamReRoute.DownstreamHttpVersion,
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                MapHeaders(request, requestMessage);
 | 
					                MapHeaders(request, requestMessage);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
namespace Ocelot.UnitTests.Configuration
 | 
					namespace Ocelot.UnitTests.Configuration
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    using System;
 | 
				
			||||||
    using Moq;
 | 
					    using Moq;
 | 
				
			||||||
    using Ocelot.Cache;
 | 
					    using Ocelot.Cache;
 | 
				
			||||||
    using Ocelot.Configuration;
 | 
					    using Ocelot.Configuration;
 | 
				
			||||||
@@ -30,6 +31,7 @@
 | 
				
			|||||||
        private Mock<ILoadBalancerOptionsCreator> _lboCreator;
 | 
					        private Mock<ILoadBalancerOptionsCreator> _lboCreator;
 | 
				
			||||||
        private Mock<IReRouteKeyCreator> _rrkCreator;
 | 
					        private Mock<IReRouteKeyCreator> _rrkCreator;
 | 
				
			||||||
        private Mock<ISecurityOptionsCreator> _soCreator;
 | 
					        private Mock<ISecurityOptionsCreator> _soCreator;
 | 
				
			||||||
 | 
					        private Mock<IVersionCreator> _versionCreator;
 | 
				
			||||||
        private FileConfiguration _fileConfig;
 | 
					        private FileConfiguration _fileConfig;
 | 
				
			||||||
        private ReRouteOptions _rro;
 | 
					        private ReRouteOptions _rro;
 | 
				
			||||||
        private string _requestId;
 | 
					        private string _requestId;
 | 
				
			||||||
@@ -46,6 +48,7 @@
 | 
				
			|||||||
        private LoadBalancerOptions _lbo;
 | 
					        private LoadBalancerOptions _lbo;
 | 
				
			||||||
        private List<ReRoute> _result;
 | 
					        private List<ReRoute> _result;
 | 
				
			||||||
        private SecurityOptions _securityOptions;
 | 
					        private SecurityOptions _securityOptions;
 | 
				
			||||||
 | 
					        private Version _expectedVersion;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public ReRoutesCreatorTests()
 | 
					        public ReRoutesCreatorTests()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -63,6 +66,7 @@
 | 
				
			|||||||
            _lboCreator = new Mock<ILoadBalancerOptionsCreator>();
 | 
					            _lboCreator = new Mock<ILoadBalancerOptionsCreator>();
 | 
				
			||||||
            _rrkCreator = new Mock<IReRouteKeyCreator>();
 | 
					            _rrkCreator = new Mock<IReRouteKeyCreator>();
 | 
				
			||||||
            _soCreator = new Mock<ISecurityOptionsCreator>();
 | 
					            _soCreator = new Mock<ISecurityOptionsCreator>();
 | 
				
			||||||
 | 
					            _versionCreator = new Mock<IVersionCreator>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _creator = new ReRoutesCreator(
 | 
					            _creator = new ReRoutesCreator(
 | 
				
			||||||
                _cthCreator.Object,
 | 
					                _cthCreator.Object,
 | 
				
			||||||
@@ -78,7 +82,8 @@
 | 
				
			|||||||
                _daCreator.Object,
 | 
					                _daCreator.Object,
 | 
				
			||||||
                _lboCreator.Object,
 | 
					                _lboCreator.Object,
 | 
				
			||||||
                _rrkCreator.Object,
 | 
					                _rrkCreator.Object,
 | 
				
			||||||
                _soCreator.Object
 | 
					                _soCreator.Object,
 | 
				
			||||||
 | 
					                _versionCreator.Object
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -155,6 +160,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        private void GivenTheDependenciesAreSetUpCorrectly()
 | 
					        private void GivenTheDependenciesAreSetUpCorrectly()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            _expectedVersion = new Version("1.1");
 | 
				
			||||||
            _rro = new ReRouteOptions(false, false, false, false, false);
 | 
					            _rro = new ReRouteOptions(false, false, false, false, false);
 | 
				
			||||||
            _requestId = "testy";
 | 
					            _requestId = "testy";
 | 
				
			||||||
            _rrk = "besty";
 | 
					            _rrk = "besty";
 | 
				
			||||||
@@ -182,6 +188,7 @@
 | 
				
			|||||||
            _hfarCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_ht);
 | 
					            _hfarCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_ht);
 | 
				
			||||||
            _daCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_dhp);
 | 
					            _daCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_dhp);
 | 
				
			||||||
            _lboCreator.Setup(x => x.Create(It.IsAny<FileLoadBalancerOptions>())).Returns(_lbo);
 | 
					            _lboCreator.Setup(x => x.Create(It.IsAny<FileLoadBalancerOptions>())).Returns(_lbo);
 | 
				
			||||||
 | 
					            _versionCreator.Setup(x => x.Create(It.IsAny<string>())).Returns(_expectedVersion);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private void ThenTheReRoutesAreCreated()
 | 
					        private void ThenTheReRoutesAreCreated()
 | 
				
			||||||
@@ -209,6 +216,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        private void ThenTheReRouteIsSet(FileReRoute expected, int reRouteIndex)
 | 
					        private void ThenTheReRouteIsSet(FileReRoute expected, int reRouteIndex)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            _result[reRouteIndex].DownstreamReRoute[0].DownstreamHttpVersion.ShouldBe(_expectedVersion);
 | 
				
			||||||
            _result[reRouteIndex].DownstreamReRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated);
 | 
					            _result[reRouteIndex].DownstreamReRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated);
 | 
				
			||||||
            _result[reRouteIndex].DownstreamReRoute[0].IsAuthorised.ShouldBe(_rro.IsAuthorised);
 | 
					            _result[reRouteIndex].DownstreamReRoute[0].IsAuthorised.ShouldBe(_rro.IsAuthorised);
 | 
				
			||||||
            _result[reRouteIndex].DownstreamReRoute[0].IsCached.ShouldBe(_rro.IsCached);
 | 
					            _result[reRouteIndex].DownstreamReRoute[0].IsCached.ShouldBe(_rro.IsCached);
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										34
									
								
								test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					namespace Ocelot.UnitTests.Configuration
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    using Ocelot.Configuration.Creator;
 | 
				
			||||||
 | 
					    using Shouldly;
 | 
				
			||||||
 | 
					    using Xunit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public class VersionCreatorTests
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        private readonly VersionCreator _creator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public VersionCreatorTests()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _creator = new VersionCreator();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_create_version_based_on_input()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var input = "2.0";
 | 
				
			||||||
 | 
					            var result = _creator.Create(input);
 | 
				
			||||||
 | 
					            result.Major.ShouldBe(2);
 | 
				
			||||||
 | 
					            result.Minor.ShouldBe(0);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_default_to_version_one_point_one()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var input = "";
 | 
				
			||||||
 | 
					            var result = _creator.Create(input);
 | 
				
			||||||
 | 
					            result.Major.ShouldBe(1);
 | 
				
			||||||
 | 
					            result.Minor.ShouldBe(1);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user