mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 20: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 SecurityOptions _securityOptions;
 | 
			
		||||
        private string _downstreamHttpMethod;
 | 
			
		||||
        private string _downstreamHttpVersion;
 | 
			
		||||
        private Version _downstreamHttpVersion;
 | 
			
		||||
 | 
			
		||||
        public DownstreamReRouteBuilder()
 | 
			
		||||
        {
 | 
			
		||||
@@ -257,9 +257,9 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public DownstreamReRouteBuilder WithHttpVersion(string httpVersion)
 | 
			
		||||
        public DownstreamReRouteBuilder WithHttpVersion(Version downstreamHttpVersion)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamHttpVersion = httpVersion;
 | 
			
		||||
            _downstreamHttpVersion = downstreamHttpVersion;
 | 
			
		||||
            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 IReRouteKeyCreator _reRouteKeyCreator;
 | 
			
		||||
        private readonly ISecurityOptionsCreator _securityOptionsCreator;
 | 
			
		||||
        private readonly IVersionCreator _versionCreator;
 | 
			
		||||
 | 
			
		||||
        public ReRoutesCreator(
 | 
			
		||||
            IClaimsToThingCreator claimsToThingCreator,
 | 
			
		||||
@@ -37,7 +38,8 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
            IDownstreamAddressesCreator downstreamAddressesCreator,
 | 
			
		||||
            ILoadBalancerOptionsCreator loadBalancerOptionsCreator,
 | 
			
		||||
            IReRouteKeyCreator reRouteKeyCreator,
 | 
			
		||||
            ISecurityOptionsCreator securityOptionsCreator
 | 
			
		||||
            ISecurityOptionsCreator securityOptionsCreator,
 | 
			
		||||
            IVersionCreator versionCreator
 | 
			
		||||
            )
 | 
			
		||||
        {
 | 
			
		||||
            _reRouteKeyCreator = reRouteKeyCreator;
 | 
			
		||||
@@ -55,6 +57,7 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
            _httpHandlerOptionsCreator = httpHandlerOptionsCreator;
 | 
			
		||||
            _loadBalancerOptionsCreator = loadBalancerOptionsCreator;
 | 
			
		||||
            _securityOptionsCreator = securityOptionsCreator;
 | 
			
		||||
            _versionCreator = versionCreator;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<ReRoute> Create(FileConfiguration fileConfiguration)
 | 
			
		||||
@@ -104,6 +107,8 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
 | 
			
		||||
            var securityOptions = _securityOptionsCreator.Create(fileReRoute.SecurityOptions);
 | 
			
		||||
 | 
			
		||||
            var downstreamHttpVersion = _versionCreator.Create(fileReRoute.DownstreamHttpVersion);
 | 
			
		||||
 | 
			
		||||
            var reRoute = new DownstreamReRouteBuilder()
 | 
			
		||||
                .WithKey(fileReRoute.Key)
 | 
			
		||||
                .WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
 | 
			
		||||
@@ -138,7 +143,7 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
                .WithAddHeadersToUpstream(hAndRs.AddHeadersToUpstream)
 | 
			
		||||
                .WithDangerousAcceptAnyServerCertificateValidator(fileReRoute.DangerousAcceptAnyServerCertificateValidator)
 | 
			
		||||
                .WithSecurityOptions(securityOptions)
 | 
			
		||||
                .WithHttpVersion(fileReRoute.DownstreamHttpVersion)
 | 
			
		||||
                .WithHttpVersion(downstreamHttpVersion)
 | 
			
		||||
                .WithDownStreamHttpMethod(fileReRoute.DownstreamHttpMethod)
 | 
			
		||||
                .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,
 | 
			
		||||
            SecurityOptions securityOptions,
 | 
			
		||||
            string downstreamHttpMethod,
 | 
			
		||||
            string downstreamHttpVersion)
 | 
			
		||||
            Version downstreamHttpVersion)
 | 
			
		||||
        {
 | 
			
		||||
            DangerousAcceptAnyServerCertificateValidator = dangerousAcceptAnyServerCertificateValidator;
 | 
			
		||||
            AddHeadersToDownstream = addHeadersToDownstream;
 | 
			
		||||
@@ -112,6 +112,6 @@ namespace Ocelot.Configuration
 | 
			
		||||
        public bool DangerousAcceptAnyServerCertificateValidator { get; }
 | 
			
		||||
        public SecurityOptions SecurityOptions { 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<IQoSFactory, QoSFactory>();
 | 
			
		||||
            Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
 | 
			
		||||
            Services.TryAddSingleton<IVersionCreator, VersionCreator>();
 | 
			
		||||
 | 
			
		||||
            //add security
 | 
			
		||||
            this.AddSecurity();
 | 
			
		||||
 
 | 
			
		||||
@@ -20,17 +20,12 @@
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if (!Version.TryParse(downstreamReRoute.DownstreamHttpVersion, out Version version))
 | 
			
		||||
                {
 | 
			
		||||
                    version = new Version(1, 1);
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                var requestMessage = new HttpRequestMessage()
 | 
			
		||||
                {
 | 
			
		||||
                    Content = await MapContent(request),
 | 
			
		||||
                    Method = MapMethod(request, downstreamReRoute),
 | 
			
		||||
                    RequestUri = MapUri(request),
 | 
			
		||||
                    Version = version,
 | 
			
		||||
                    Version = downstreamReRoute.DownstreamHttpVersion,
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
                MapHeaders(request, requestMessage);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.Cache;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
@@ -30,6 +31,7 @@
 | 
			
		||||
        private Mock<ILoadBalancerOptionsCreator> _lboCreator;
 | 
			
		||||
        private Mock<IReRouteKeyCreator> _rrkCreator;
 | 
			
		||||
        private Mock<ISecurityOptionsCreator> _soCreator;
 | 
			
		||||
        private Mock<IVersionCreator> _versionCreator;
 | 
			
		||||
        private FileConfiguration _fileConfig;
 | 
			
		||||
        private ReRouteOptions _rro;
 | 
			
		||||
        private string _requestId;
 | 
			
		||||
@@ -46,6 +48,7 @@
 | 
			
		||||
        private LoadBalancerOptions _lbo;
 | 
			
		||||
        private List<ReRoute> _result;
 | 
			
		||||
        private SecurityOptions _securityOptions;
 | 
			
		||||
        private Version _expectedVersion;
 | 
			
		||||
 | 
			
		||||
        public ReRoutesCreatorTests()
 | 
			
		||||
        {
 | 
			
		||||
@@ -63,6 +66,7 @@
 | 
			
		||||
            _lboCreator = new Mock<ILoadBalancerOptionsCreator>();
 | 
			
		||||
            _rrkCreator = new Mock<IReRouteKeyCreator>();
 | 
			
		||||
            _soCreator = new Mock<ISecurityOptionsCreator>();
 | 
			
		||||
            _versionCreator = new Mock<IVersionCreator>();
 | 
			
		||||
 | 
			
		||||
            _creator = new ReRoutesCreator(
 | 
			
		||||
                _cthCreator.Object,
 | 
			
		||||
@@ -78,7 +82,8 @@
 | 
			
		||||
                _daCreator.Object,
 | 
			
		||||
                _lboCreator.Object,
 | 
			
		||||
                _rrkCreator.Object,
 | 
			
		||||
                _soCreator.Object
 | 
			
		||||
                _soCreator.Object,
 | 
			
		||||
                _versionCreator.Object
 | 
			
		||||
                );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -155,6 +160,7 @@
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDependenciesAreSetUpCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            _expectedVersion = new Version("1.1");
 | 
			
		||||
            _rro = new ReRouteOptions(false, false, false, false, false);
 | 
			
		||||
            _requestId = "testy";
 | 
			
		||||
            _rrk = "besty";
 | 
			
		||||
@@ -182,6 +188,7 @@
 | 
			
		||||
            _hfarCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_ht);
 | 
			
		||||
            _daCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_dhp);
 | 
			
		||||
            _lboCreator.Setup(x => x.Create(It.IsAny<FileLoadBalancerOptions>())).Returns(_lbo);
 | 
			
		||||
            _versionCreator.Setup(x => x.Create(It.IsAny<string>())).Returns(_expectedVersion);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheReRoutesAreCreated()
 | 
			
		||||
@@ -209,6 +216,7 @@
 | 
			
		||||
 | 
			
		||||
        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].IsAuthorised.ShouldBe(_rro.IsAuthorised);
 | 
			
		||||
            _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