mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 19:10:51 +08:00 
			
		
		
		
	* #280 can now add response headers inc trace id, now need to consolidate the header place holder stuff * #280 changed port for linux tests * #280 lots of hacking around to handle errors and consolidate placeholders into one class
This commit is contained in:
		@@ -34,10 +34,10 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
            _fileConfig = new FileConfiguration();
 | 
			
		||||
            _config = new Mock<IConsulPollerConfiguration>();
 | 
			
		||||
            _repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(_fileConfig));
 | 
			
		||||
            _config.Setup(x => x.Delay).Returns(10);
 | 
			
		||||
            _config.Setup(x => x.Delay).Returns(100);
 | 
			
		||||
            _poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object, _config.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _poller.Dispose();
 | 
			
		||||
 
 | 
			
		||||
@@ -826,7 +826,8 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
                result.DownstreamReRoute[0].ClaimsToHeaders.Count.ShouldBe(expected.DownstreamReRoute[0].ClaimsToHeaders.Count);
 | 
			
		||||
                result.DownstreamReRoute[0].ClaimsToQueries.Count.ShouldBe(expected.DownstreamReRoute[0].ClaimsToQueries.Count);
 | 
			
		||||
                result.DownstreamReRoute[0].RequestIdKey.ShouldBe(expected.DownstreamReRoute[0].RequestIdKey);   
 | 
			
		||||
                result.DownstreamReRoute[0].DelegatingHandlers.ShouldBe(expected.DownstreamReRoute[0].DelegatingHandlers);           
 | 
			
		||||
                result.DownstreamReRoute[0].DelegatingHandlers.ShouldBe(expected.DownstreamReRoute[0].DelegatingHandlers);      
 | 
			
		||||
                result.DownstreamReRoute[0].AddHeadersToDownstream.ShouldBe(expected.DownstreamReRoute[0].AddHeadersToDownstream);           
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -909,7 +910,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
 | 
			
		||||
        private void GivenTheHeaderFindAndReplaceCreatorReturns()
 | 
			
		||||
        {
 | 
			
		||||
            _headerFindAndReplaceCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(new HeaderTransformations(new List<HeaderFindAndReplace>(), new List<HeaderFindAndReplace>()));
 | 
			
		||||
            _headerFindAndReplaceCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(new HeaderTransformations(new List<HeaderFindAndReplace>(), new List<HeaderFindAndReplace>(), new List<AddHeader>()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheFollowingIsReturned(ServiceProviderConfiguration serviceProviderConfiguration)
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,11 @@ using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Configuration.Builder;
 | 
			
		||||
using Ocelot.Configuration.Creator;
 | 
			
		||||
using Ocelot.Configuration.File;
 | 
			
		||||
using Ocelot.Infrastructure;
 | 
			
		||||
using Ocelot.Logging;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Ocelot.UnitTests.Responder;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using TestStack.BDDfy;
 | 
			
		||||
using Xunit;
 | 
			
		||||
@@ -17,12 +21,17 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
        private HeaderFindAndReplaceCreator _creator;
 | 
			
		||||
        private FileReRoute _reRoute;
 | 
			
		||||
        private HeaderTransformations _result;
 | 
			
		||||
        private Mock<IBaseUrlFinder> _finder;
 | 
			
		||||
        private Mock<IPlaceholders> _placeholders;
 | 
			
		||||
        private Mock<IOcelotLoggerFactory> _factory;
 | 
			
		||||
        private Mock<IOcelotLogger> _logger;
 | 
			
		||||
 | 
			
		||||
        public HeaderFindAndReplaceCreatorTests()
 | 
			
		||||
        {
 | 
			
		||||
            _finder = new Mock<IBaseUrlFinder>();
 | 
			
		||||
            _creator = new HeaderFindAndReplaceCreator(_finder.Object);
 | 
			
		||||
            _logger = new Mock<IOcelotLogger>();
 | 
			
		||||
            _factory = new Mock<IOcelotLoggerFactory>();
 | 
			
		||||
            _factory.Setup(x => x.CreateLogger<HeaderFindAndReplaceCreator>()).Returns(_logger.Object);
 | 
			
		||||
            _placeholders = new Mock<IPlaceholders>();
 | 
			
		||||
            _creator = new HeaderFindAndReplaceCreator(_placeholders.Object, _factory.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
@@ -84,6 +93,40 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_log_errors_and_not_add_headers()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new FileReRoute
 | 
			
		||||
            {
 | 
			
		||||
                DownstreamHeaderTransform = new Dictionary<string, string>
 | 
			
		||||
                {
 | 
			
		||||
                    {"Location", "http://www.bbc.co.uk/, {BaseUrl}"},
 | 
			
		||||
                },
 | 
			
		||||
                UpstreamHeaderTransform = new Dictionary<string, string>
 | 
			
		||||
                {
 | 
			
		||||
                    {"Location", "http://www.bbc.co.uk/, {BaseUrl}"},
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var expected = new List<HeaderFindAndReplace>
 | 
			
		||||
            {
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(x => GivenTheReRoute(reRoute))
 | 
			
		||||
                .And(x => GivenTheBaseUrlErrors())
 | 
			
		||||
                .When(x => WhenICreate())
 | 
			
		||||
                .Then(x => ThenTheFollowingDownstreamIsReturned(expected))
 | 
			
		||||
                .And(x => ThenTheFollowingUpstreamIsReturned(expected))
 | 
			
		||||
                .And(x => ThenTheLoggerIsCalledCorrectly("Unable to add DownstreamHeaderTransform Location: http://www.bbc.co.uk/, {BaseUrl}"))
 | 
			
		||||
                .And(x => ThenTheLoggerIsCalledCorrectly("Unable to add UpstreamHeaderTransform Location: http://www.bbc.co.uk/, {BaseUrl}"))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheLoggerIsCalledCorrectly(string message)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Verify(x => x.LogError(message), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_use_base_url_partial_placeholder()
 | 
			
		||||
        {
 | 
			
		||||
@@ -107,9 +150,41 @@ namespace Ocelot.UnitTests.Configuration
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_trace_id_header()
 | 
			
		||||
        {
 | 
			
		||||
            var reRoute = new FileReRoute
 | 
			
		||||
            {
 | 
			
		||||
                 DownstreamHeaderTransform = new Dictionary<string, string>
 | 
			
		||||
                {
 | 
			
		||||
                    {"Trace-Id", "{TraceId}"},
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            var expected = new AddHeader("Trace-Id", "{TraceId}");
 | 
			
		||||
 | 
			
		||||
            this.Given(x => GivenTheReRoute(reRoute))
 | 
			
		||||
                .And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
 | 
			
		||||
                .When(x => WhenICreate())
 | 
			
		||||
                .Then(x => ThenTheFollowingAddHeaderIsReturned(expected))
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheBaseUrlIs(string baseUrl)
 | 
			
		||||
        {
 | 
			
		||||
            _finder.Setup(x => x.Find()).Returns(baseUrl);
 | 
			
		||||
            _placeholders.Setup(x => x.Get(It.IsAny<string>())).Returns(new OkResponse<string>(baseUrl));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheBaseUrlErrors()
 | 
			
		||||
        {
 | 
			
		||||
            _placeholders.Setup(x => x.Get(It.IsAny<string>())).Returns(new ErrorResponse<string>(new AnyError()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheFollowingAddHeaderIsReturned(AddHeader addHeader)
 | 
			
		||||
        {
 | 
			
		||||
            _result.AddHeadersToDownstream[0].Key.ShouldBe(addHeader.Key);
 | 
			
		||||
            _result.AddHeadersToDownstream[0].Value.ShouldBe(addHeader.Value);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheFollowingDownstreamIsReturned(List<HeaderFindAndReplace> downstream)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user