mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:35:27 +08:00 
			
		
		
		
	extracted thing that creates request id key
This commit is contained in:
		@@ -31,6 +31,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
        private readonly IClaimsToThingCreator _claimsToThingCreator;
 | 
					        private readonly IClaimsToThingCreator _claimsToThingCreator;
 | 
				
			||||||
        private readonly IAuthenticationOptionsCreator _authOptionsCreator;
 | 
					        private readonly IAuthenticationOptionsCreator _authOptionsCreator;
 | 
				
			||||||
        private IUpstreamTemplatePatternCreator _upstreamTemplatePatternCreator;
 | 
					        private IUpstreamTemplatePatternCreator _upstreamTemplatePatternCreator;
 | 
				
			||||||
 | 
					        private IRequestIdKeyCreator _requestIdKeyCreator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public FileOcelotConfigurationCreator(
 | 
					        public FileOcelotConfigurationCreator(
 | 
				
			||||||
            IOptions<FileConfiguration> options, 
 | 
					            IOptions<FileConfiguration> options, 
 | 
				
			||||||
@@ -42,8 +43,10 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            IQosProviderHouse qosProviderHouse,
 | 
					            IQosProviderHouse qosProviderHouse,
 | 
				
			||||||
            IClaimsToThingCreator claimsToThingCreator,
 | 
					            IClaimsToThingCreator claimsToThingCreator,
 | 
				
			||||||
            IAuthenticationOptionsCreator authOptionsCreator,
 | 
					            IAuthenticationOptionsCreator authOptionsCreator,
 | 
				
			||||||
            IUpstreamTemplatePatternCreator upstreamTemplatePatternCreator)
 | 
					            IUpstreamTemplatePatternCreator upstreamTemplatePatternCreator,
 | 
				
			||||||
 | 
					            IRequestIdKeyCreator requestIdKeyCreator)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
 | 
					            _requestIdKeyCreator = requestIdKeyCreator;
 | 
				
			||||||
            _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
 | 
					            _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
 | 
				
			||||||
            _authOptionsCreator = authOptionsCreator;
 | 
					            _authOptionsCreator = authOptionsCreator;
 | 
				
			||||||
            _loadBalanceFactory = loadBalancerFactory;
 | 
					            _loadBalanceFactory = loadBalancerFactory;
 | 
				
			||||||
@@ -105,7 +108,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var isCached = IsCached(fileReRoute);
 | 
					            var isCached = IsCached(fileReRoute);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var requestIdKey = BuildRequestId(fileReRoute, globalConfiguration);
 | 
					            var requestIdKey = _requestIdKeyCreator.Create(fileReRoute, globalConfiguration);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var reRouteKey = BuildReRouteKey(fileReRoute);
 | 
					            var reRouteKey = BuildReRouteKey(fileReRoute);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -210,17 +213,6 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            return fileReRoute.FileCacheOptions.TtlSeconds > 0;
 | 
					            return fileReRoute.FileCacheOptions.TtlSeconds > 0;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private string BuildRequestId(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            var globalRequestIdConfiguration = !string.IsNullOrEmpty(globalConfiguration?.RequestIdKey);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
             var requestIdKey = globalRequestIdConfiguration
 | 
					 | 
				
			||||||
                ? globalConfiguration.RequestIdKey
 | 
					 | 
				
			||||||
                : fileReRoute.RequestIdKey;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                return requestIdKey;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        private string BuildReRouteKey(FileReRoute fileReRoute)
 | 
					        private string BuildReRouteKey(FileReRoute fileReRoute)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            //note - not sure if this is the correct key, but this is probably the only unique key i can think of given my poor brain
 | 
					            //note - not sure if this is the correct key, but this is probably the only unique key i can think of given my poor brain
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										9
									
								
								src/Ocelot/Configuration/Creator/IRequestIdKeyCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Ocelot/Configuration/Creator/IRequestIdKeyCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					using Ocelot.Configuration.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Ocelot.Configuration.Creator
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public interface IRequestIdKeyCreator
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        string Create(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										18
									
								
								src/Ocelot/Configuration/Creator/RequestIdKeyCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/Ocelot/Configuration/Creator/RequestIdKeyCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					using Ocelot.Configuration.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Ocelot.Configuration.Creator
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class RequestIdKeyCreator : IRequestIdKeyCreator
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public string Create(FileReRoute fileReRoute, FileGlobalConfiguration globalConfiguration)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var globalRequestIdConfiguration = !string.IsNullOrEmpty(globalConfiguration?.RequestIdKey);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					             var requestIdKey = globalRequestIdConfiguration
 | 
				
			||||||
 | 
					                ? globalConfiguration.RequestIdKey
 | 
				
			||||||
 | 
					                : fileReRoute.RequestIdKey;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                return requestIdKey;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -63,6 +63,7 @@ namespace Ocelot.DependencyInjection
 | 
				
			|||||||
            services.AddSingleton<IClaimsToThingCreator, ClaimsToThingCreator>();
 | 
					            services.AddSingleton<IClaimsToThingCreator, ClaimsToThingCreator>();
 | 
				
			||||||
            services.AddSingleton<IAuthenticationOptionsCreator, AuthenticationOptionsCreator>();
 | 
					            services.AddSingleton<IAuthenticationOptionsCreator, AuthenticationOptionsCreator>();
 | 
				
			||||||
            services.AddSingleton<IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
 | 
					            services.AddSingleton<IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
 | 
				
			||||||
 | 
					            services.AddSingleton<IRequestIdKeyCreator, RequestIdKeyCreator>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
 | 
					            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,6 +34,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
        private Mock<IClaimsToThingCreator> _claimsToThingCreator;
 | 
					        private Mock<IClaimsToThingCreator> _claimsToThingCreator;
 | 
				
			||||||
        private Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
 | 
					        private Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
 | 
				
			||||||
        private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
 | 
					        private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
 | 
				
			||||||
 | 
					        private Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public FileConfigurationCreatorTests()
 | 
					        public FileConfigurationCreatorTests()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -49,12 +50,13 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
            _claimsToThingCreator = new Mock<IClaimsToThingCreator>();
 | 
					            _claimsToThingCreator = new Mock<IClaimsToThingCreator>();
 | 
				
			||||||
            _authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
 | 
					            _authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
 | 
				
			||||||
            _upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
 | 
					            _upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
 | 
				
			||||||
 | 
					            _requestIdKeyCreator = new Mock<IRequestIdKeyCreator>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( 
 | 
					            _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( 
 | 
				
			||||||
                _fileConfig.Object, _validator.Object, _logger.Object,
 | 
					                _fileConfig.Object, _validator.Object, _logger.Object,
 | 
				
			||||||
                _loadBalancerFactory.Object, _loadBalancerHouse.Object, 
 | 
					                _loadBalancerFactory.Object, _loadBalancerHouse.Object, 
 | 
				
			||||||
                _qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
 | 
					                _qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
 | 
				
			||||||
                _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object);
 | 
					                _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
@@ -279,7 +281,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void should_set_global_request_id_key()
 | 
					        public void should_call_request_id_creator()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            this.Given(x => x.GivenTheConfigIs(new FileConfiguration
 | 
					            this.Given(x => x.GivenTheConfigIs(new FileConfiguration
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
@@ -299,6 +301,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }))
 | 
					            }))
 | 
				
			||||||
                .And(x => x.GivenTheConfigIsValid())    
 | 
					                .And(x => x.GivenTheConfigIsValid())    
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheRequestIdCreatorReturns("blahhhh"))
 | 
				
			||||||
                .When(x => x.WhenICreateTheConfig())
 | 
					                .When(x => x.WhenICreateTheConfig())
 | 
				
			||||||
                .Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
 | 
					                .Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
@@ -309,6 +312,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
                        .WithRequestIdKey("blahhhh")
 | 
					                        .WithRequestIdKey("blahhhh")
 | 
				
			||||||
                        .Build()
 | 
					                        .Build()
 | 
				
			||||||
                }))
 | 
					                }))
 | 
				
			||||||
 | 
					                .And(x => x.ThenTheRequestIdKeyCreatorIsCalledCorrectly())
 | 
				
			||||||
                .BDDfy();
 | 
					                .BDDfy();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -465,6 +469,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
                result.ClaimsToClaims.Count.ShouldBe(expected.ClaimsToClaims.Count);
 | 
					                result.ClaimsToClaims.Count.ShouldBe(expected.ClaimsToClaims.Count);
 | 
				
			||||||
                result.ClaimsToHeaders.Count.ShouldBe(expected.ClaimsToHeaders.Count);
 | 
					                result.ClaimsToHeaders.Count.ShouldBe(expected.ClaimsToHeaders.Count);
 | 
				
			||||||
                result.ClaimsToQueries.Count.ShouldBe(expected.ClaimsToQueries.Count);
 | 
					                result.ClaimsToQueries.Count.ShouldBe(expected.ClaimsToQueries.Count);
 | 
				
			||||||
 | 
					                result.RequestIdKey.ShouldBe(expected.RequestIdKey);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -549,5 +554,19 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
                .Setup(x => x.Create(It.IsAny<FileReRoute>()))
 | 
					                .Setup(x => x.Create(It.IsAny<FileReRoute>()))
 | 
				
			||||||
                .Returns(pattern);
 | 
					                .Returns(pattern);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void ThenTheRequestIdKeyCreatorIsCalledCorrectly()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _requestIdKeyCreator
 | 
				
			||||||
 | 
					                .Verify(x => x.Create(_fileConfiguration.ReRoutes[0], _fileConfiguration.GlobalConfiguration), Times.Once);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void GivenTheRequestIdCreatorReturns(string requestId)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _requestIdKeyCreator
 | 
				
			||||||
 | 
					                .Setup(x => x.Create(It.IsAny<FileReRoute>(), It.IsAny<FileGlobalConfiguration>()))
 | 
				
			||||||
 | 
					                .Returns(requestId);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,91 @@
 | 
				
			|||||||
 | 
					using Ocelot.Configuration.Creator;
 | 
				
			||||||
 | 
					using Ocelot.Configuration.File;
 | 
				
			||||||
 | 
					using Shouldly;
 | 
				
			||||||
 | 
					using TestStack.BDDfy;
 | 
				
			||||||
 | 
					using Xunit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Ocelot.UnitTests.Configuration
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class RequestIdKeyCreatorTests
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        private FileReRoute _fileReRoute;
 | 
				
			||||||
 | 
					        private FileGlobalConfiguration _fileGlobalConfig;
 | 
				
			||||||
 | 
					        private string _result;
 | 
				
			||||||
 | 
					        private RequestIdKeyCreator _creator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public RequestIdKeyCreatorTests()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _creator = new RequestIdKeyCreator();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_use_global_configuration()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var reRoute = new FileReRoute();
 | 
				
			||||||
 | 
					            var globalConfig = new FileGlobalConfiguration
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                RequestIdKey = "cheese"
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            this.Given(x => x.GivenTheFollowingReRoute(reRoute))
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
 | 
				
			||||||
 | 
					                .When(x => x.WhenICreate())
 | 
				
			||||||
 | 
					                .Then(x => x.ThenTheFollowingIsReturned("cheese"))
 | 
				
			||||||
 | 
					                .BDDfy();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_use_re_route_specific()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var reRoute = new FileReRoute
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                RequestIdKey = "cheese"
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					            var globalConfig = new FileGlobalConfiguration();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            this.Given(x => x.GivenTheFollowingReRoute(reRoute))
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
 | 
				
			||||||
 | 
					                .When(x => x.WhenICreate())
 | 
				
			||||||
 | 
					                .Then(x => x.ThenTheFollowingIsReturned("cheese"))
 | 
				
			||||||
 | 
					                .BDDfy();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_use_global_cofiguration_over_re_route_specific()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var reRoute = new FileReRoute
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                RequestIdKey = "cheese"
 | 
				
			||||||
 | 
					            };            var globalConfig = new FileGlobalConfiguration
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                RequestIdKey = "cheese"
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            this.Given(x => x.GivenTheFollowingReRoute(reRoute))
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
 | 
				
			||||||
 | 
					                .When(x => x.WhenICreate())
 | 
				
			||||||
 | 
					                .Then(x => x.ThenTheFollowingIsReturned("cheese"))
 | 
				
			||||||
 | 
					                .BDDfy();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _fileReRoute = fileReRoute;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void GivenTheFollowingGlobalConfig(FileGlobalConfiguration globalConfig)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _fileGlobalConfig = globalConfig;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void WhenICreate()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _result = _creator.Create(_fileReRoute, _fileGlobalConfig);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void ThenTheFollowingIsReturned(string expected)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _result.ShouldBe(expected);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user