Feature/refactoring internal config creation stack (#600)

* tidy up some code so I can understand it

* broke al the things that make config out into their own classes etc..sigh

* fix the things i broked

* #597 test for issue, works on this branch :E

* #597 removed comments

* added tests for new load balancer creator..basic

* added tests for lb creator and aggregates creator

* added tests for config creator

* boiler plate for tests

* added dynamics tests

* wip

* finished refactoring for now
This commit is contained in:
Tom Pallister
2018-09-11 21:41:58 +01:00
committed by GitHub
parent b491bd50a8
commit 44f8e312a8
34 changed files with 2086 additions and 2012 deletions

View File

@ -0,0 +1,164 @@
namespace Ocelot.UnitTests.Configuration
{
using System.Collections.Generic;
using System.Net.Http;
using Moq;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class AggregatesCreatorTests
{
private readonly AggregatesCreator _creator;
private readonly Mock<IUpstreamTemplatePatternCreator> _utpCreator;
private FileConfiguration _fileConfiguration;
private List<ReRoute> _reRoutes;
private List<ReRoute> _result;
private UpstreamPathTemplate _aggregate1Utp;
private UpstreamPathTemplate _aggregate2Utp;
public AggregatesCreatorTests()
{
_utpCreator = new Mock<IUpstreamTemplatePatternCreator>();
_creator = new AggregatesCreator(_utpCreator.Object);
}
[Fact]
public void should_return_no_aggregates()
{
var fileConfig = new FileConfiguration
{
Aggregates = new List<FileAggregateReRoute>
{
new FileAggregateReRoute
{
ReRouteKeys = new List<string>{"key1"}
}
}
};
var reRoutes = new List<ReRoute>();
this.Given(_ => GivenThe(fileConfig))
.And(_ => GivenThe(reRoutes))
.When(_ => WhenICreate())
.Then(_ => TheUtpCreatorIsNotCalled())
.And(_ => ThenTheResultIsNotNull())
.And(_ => ThenTheResultIsEmpty())
.BDDfy();
}
[Fact]
public void should_create_aggregates()
{
var fileConfig = new FileConfiguration
{
Aggregates = new List<FileAggregateReRoute>
{
new FileAggregateReRoute
{
ReRouteKeys = new List<string>{"key1", "key2"},
UpstreamHost = "hosty",
UpstreamPathTemplate = "templatey",
Aggregator = "aggregatory",
ReRouteIsCaseSensitive = true
},
new FileAggregateReRoute
{
ReRouteKeys = new List<string>{"key3", "key4"},
UpstreamHost = "hosty",
UpstreamPathTemplate = "templatey",
Aggregator = "aggregatory",
ReRouteIsCaseSensitive = true
}
}
};
var reRoutes = new List<ReRoute>
{
new ReRouteBuilder().WithDownstreamReRoute(new DownstreamReRouteBuilder().WithKey("key1").Build()).Build(),
new ReRouteBuilder().WithDownstreamReRoute(new DownstreamReRouteBuilder().WithKey("key2").Build()).Build(),
new ReRouteBuilder().WithDownstreamReRoute(new DownstreamReRouteBuilder().WithKey("key3").Build()).Build(),
new ReRouteBuilder().WithDownstreamReRoute(new DownstreamReRouteBuilder().WithKey("key4").Build()).Build()
};
this.Given(_ => GivenThe(fileConfig))
.And(_ => GivenThe(reRoutes))
.And(_ => GivenTheUtpCreatorReturns())
.When(_ => WhenICreate())
.Then(_ => ThenTheUtpCreatorIsCalledCorrectly())
.And(_ => ThenTheAggregatesAreCreated())
.BDDfy();
}
private void ThenTheAggregatesAreCreated()
{
_result.ShouldNotBeNull();
_result.Count.ShouldBe(2);
_result[0].UpstreamHttpMethod.ShouldContain(x => x == HttpMethod.Get);
_result[0].UpstreamHost.ShouldBe(_fileConfiguration.Aggregates[0].UpstreamHost);
_result[0].UpstreamTemplatePattern.ShouldBe(_aggregate1Utp);
_result[0].Aggregator.ShouldBe(_fileConfiguration.Aggregates[0].Aggregator);
_result[0].DownstreamReRoute.ShouldContain(x => x == _reRoutes[0].DownstreamReRoute[0]);
_result[0].DownstreamReRoute.ShouldContain(x => x == _reRoutes[1].DownstreamReRoute[0]);
_result[1].UpstreamHttpMethod.ShouldContain(x => x == HttpMethod.Get);
_result[1].UpstreamHost.ShouldBe(_fileConfiguration.Aggregates[1].UpstreamHost);
_result[1].UpstreamTemplatePattern.ShouldBe(_aggregate2Utp);
_result[1].Aggregator.ShouldBe(_fileConfiguration.Aggregates[1].Aggregator);
_result[1].DownstreamReRoute.ShouldContain(x => x == _reRoutes[2].DownstreamReRoute[0]);
_result[1].DownstreamReRoute.ShouldContain(x => x == _reRoutes[3].DownstreamReRoute[0]);
}
private void ThenTheUtpCreatorIsCalledCorrectly()
{
_utpCreator.Verify(x => x.Create(_fileConfiguration.Aggregates[0]), Times.Once);
_utpCreator.Verify(x => x.Create(_fileConfiguration.Aggregates[1]), Times.Once);
}
private void GivenTheUtpCreatorReturns()
{
_aggregate1Utp = new UpstreamPathTemplateBuilder().Build();
_aggregate2Utp = new UpstreamPathTemplateBuilder().Build();
_utpCreator.SetupSequence(x => x.Create(It.IsAny<IReRoute>()))
.Returns(_aggregate1Utp)
.Returns(_aggregate2Utp);
}
private void ThenTheResultIsEmpty()
{
_result.Count.ShouldBe(0);
}
private void ThenTheResultIsNotNull()
{
_result.ShouldNotBeNull();
}
private void TheUtpCreatorIsNotCalled()
{
_utpCreator.Verify(x => x.Create(It.IsAny<FileAggregateReRoute>()), Times.Never);
}
private void GivenThe(FileConfiguration fileConfiguration)
{
_fileConfiguration = fileConfiguration;
}
private void GivenThe(List<ReRoute> reRoutes)
{
_reRoutes = reRoutes;
}
private void WhenICreate()
{
_result = _creator.Create(_fileConfiguration, _reRoutes);
}
}
}

View File

@ -0,0 +1,124 @@
namespace Ocelot.UnitTests.Configuration
{
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Configuration;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.DependencyInjection;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class ConfigurationCreatorTests
{
private ConfigurationCreator _creator;
private InternalConfiguration _result;
private readonly Mock<IServiceProviderConfigurationCreator> _spcCreator;
private readonly Mock<IQoSOptionsCreator> _qosCreator;
private readonly Mock<IHttpHandlerOptionsCreator> _hhoCreator;
private readonly Mock<ILoadBalancerOptionsCreator> _lboCreator;
private FileConfiguration _fileConfig;
private List<ReRoute> _reRoutes;
private ServiceProviderConfiguration _spc;
private LoadBalancerOptions _lbo;
private QoSOptions _qoso;
private HttpHandlerOptions _hho;
private AdministrationPath _adminPath;
private readonly ServiceCollection _serviceCollection;
public ConfigurationCreatorTests()
{
_lboCreator = new Mock<ILoadBalancerOptionsCreator>();
_hhoCreator = new Mock<IHttpHandlerOptionsCreator>();
_qosCreator = new Mock<IQoSOptionsCreator>();
_spcCreator = new Mock<IServiceProviderConfigurationCreator>();
_serviceCollection = new ServiceCollection();
}
[Fact]
public void should_build_configuration_with_no_admin_path()
{
this.Given(_ => GivenTheDependenciesAreSetUp())
.When(_ => WhenICreate())
.Then(_ => ThenTheDepdenciesAreCalledCorrectly())
.And(_ => ThenThePropertiesAreSetCorrectly())
.And(_ => ThenTheAdminPathIsNull())
.BDDfy();
}
[Fact]
public void should_build_configuration_with_admin_path()
{
this.Given(_ => GivenTheDependenciesAreSetUp())
.And(_ => GivenTheAdminPath())
.When(_ => WhenICreate())
.Then(_ => ThenTheDepdenciesAreCalledCorrectly())
.And(_ => ThenThePropertiesAreSetCorrectly())
.And(_ => ThenTheAdminPathIsSet())
.BDDfy();
}
private void ThenTheAdminPathIsNull()
{
_result.AdministrationPath.ShouldBeNull();
}
private void ThenThePropertiesAreSetCorrectly()
{
_result.ShouldNotBeNull();
_result.ServiceProviderConfiguration.ShouldBe(_spc);
_result.LoadBalancerOptions.ShouldBe(_lbo);
_result.QoSOptions.ShouldBe(_qoso);
_result.HttpHandlerOptions.ShouldBe(_hho);
_result.ReRoutes.ShouldBe(_reRoutes);
_result.RequestId.ShouldBe(_fileConfig.GlobalConfiguration.RequestIdKey);
_result.DownstreamScheme.ShouldBe(_fileConfig.GlobalConfiguration.DownstreamScheme);
}
private void ThenTheAdminPathIsSet()
{
_result.AdministrationPath.ShouldBe("wooty");
}
private void ThenTheDepdenciesAreCalledCorrectly()
{
_spcCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration), Times.Once);
_lboCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration.LoadBalancerOptions), Times.Once);
_qosCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration.QoSOptions), Times.Once);
_hhoCreator.Verify(x => x.Create(_fileConfig.GlobalConfiguration.HttpHandlerOptions), Times.Once);
}
private void GivenTheAdminPath()
{
_adminPath = new AdministrationPath("wooty");
_serviceCollection.AddSingleton<IAdministrationPath>(_adminPath);
}
private void GivenTheDependenciesAreSetUp()
{
_fileConfig = new FileConfiguration
{
GlobalConfiguration = new FileGlobalConfiguration()
};
_reRoutes = new List<ReRoute>();
_spc = new ServiceProviderConfiguration("", "", 1, "", "", 1);
_lbo = new LoadBalancerOptionsBuilder().Build();
_qoso = new QoSOptions(1, 1, 1, "");
_hho = new HttpHandlerOptionsBuilder().Build();
_spcCreator.Setup(x => x.Create(It.IsAny<FileGlobalConfiguration>())).Returns(_spc);
_lboCreator.Setup(x => x.Create(It.IsAny<FileLoadBalancerOptions>())).Returns(_lbo);
_qosCreator.Setup(x => x.Create(It.IsAny<FileQoSOptions>())).Returns(_qoso);
_hhoCreator.Setup(x => x.Create(It.IsAny<FileHttpHandlerOptions>())).Returns(_hho);
}
private void WhenICreate()
{
var serviceProvider = _serviceCollection.BuildServiceProvider();
_creator = new ConfigurationCreator(_spcCreator.Object, _qosCreator.Object, _hhoCreator.Object, serviceProvider, _lboCreator.Object);
_result = _creator.Create(_fileConfig, _reRoutes);
}
}
}

View File

@ -0,0 +1,126 @@
namespace Ocelot.UnitTests.Configuration
{
using System.Collections.Generic;
using Moq;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class DynamicsCreatorTests
{
private readonly DynamicsCreator _creator;
private readonly Mock<IRateLimitOptionsCreator> _rloCreator;
private List<ReRoute> _result;
private FileConfiguration _fileConfig;
private RateLimitOptions _rlo1;
private RateLimitOptions _rlo2;
public DynamicsCreatorTests()
{
_rloCreator = new Mock<IRateLimitOptionsCreator>();
_creator = new DynamicsCreator(_rloCreator.Object);
}
[Fact]
public void should_return_nothing()
{
var fileConfig = new FileConfiguration();
this.Given(_ => GivenThe(fileConfig))
.When(_ => WhenICreate())
.Then(_ => ThenNothingIsReturned())
.And(_ => ThenTheRloCreatorIsNotCalled())
.BDDfy();
}
[Fact]
public void should_return_re_routes()
{
var fileConfig = new FileConfiguration
{
DynamicReRoutes = new List<FileDynamicReRoute>
{
new FileDynamicReRoute
{
ServiceName = "1",
RateLimitRule = new FileRateLimitRule
{
EnableRateLimiting = false
}
},
new FileDynamicReRoute
{
ServiceName = "2",
RateLimitRule = new FileRateLimitRule
{
EnableRateLimiting = true
}
}
}
};
this.Given(_ => GivenThe(fileConfig))
.And(_ => GivenTheRloCreatorReturns())
.When(_ => WhenICreate())
.Then(_ => ThenTheReRoutesAreReturned())
.And(_ => ThenTheRloCreatorIsCalledCorrectly())
.BDDfy();
}
private void ThenTheRloCreatorIsCalledCorrectly()
{
_rloCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[0].RateLimitRule,
_fileConfig.GlobalConfiguration), Times.Once);
_rloCreator.Verify(x => x.Create(_fileConfig.DynamicReRoutes[1].RateLimitRule,
_fileConfig.GlobalConfiguration), Times.Once);
}
private void ThenTheReRoutesAreReturned()
{
_result.Count.ShouldBe(2);
_result[0].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeFalse();
_result[0].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo1);
_result[0].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[0].ServiceName);
_result[1].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBeTrue();
_result[1].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo2);
_result[1].DownstreamReRoute[0].ServiceName.ShouldBe(_fileConfig.DynamicReRoutes[1].ServiceName);
}
private void GivenTheRloCreatorReturns()
{
_rlo1 = new RateLimitOptionsBuilder().Build();
_rlo2 = new RateLimitOptionsBuilder().WithEnableRateLimiting(true).Build();
_rloCreator
.SetupSequence(x => x.Create(It.IsAny<FileRateLimitRule>(), It.IsAny<FileGlobalConfiguration>()))
.Returns(_rlo1)
.Returns(_rlo2);
}
private void ThenTheRloCreatorIsNotCalled()
{
_rloCreator.Verify(x => x.Create(It.IsAny<FileRateLimitRule>(), It.IsAny<FileGlobalConfiguration>()), Times.Never);
}
private void ThenNothingIsReturned()
{
_result.Count.ShouldBe(0);
}
private void WhenICreate()
{
_result = _creator.Create(_fileConfig);
}
private void GivenThe(FileConfiguration fileConfig)
{
_fileConfig = fileConfig;
}
}
}

View File

@ -0,0 +1,54 @@
namespace Ocelot.UnitTests.Configuration
{
using Ocelot.Configuration;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class LoadBalancerOptionsCreatorTests
{
private readonly ILoadBalancerOptionsCreator _creator;
private FileLoadBalancerOptions _fileLoadBalancerOptions;
private LoadBalancerOptions _result;
public LoadBalancerOptionsCreatorTests()
{
_creator = new LoadBalancerOptionsCreator();
}
[Fact]
public void should_create()
{
var fileLoadBalancerOptions = new FileLoadBalancerOptions
{
Type = "test",
Key = "west",
Expiry = 1
};
this.Given(_ => GivenThe(fileLoadBalancerOptions))
.When(_ => WhenICreate())
.Then(_ => ThenTheOptionsAreCreated(fileLoadBalancerOptions))
.BDDfy();
}
private void ThenTheOptionsAreCreated(FileLoadBalancerOptions expected)
{
_result.Type.ShouldBe(expected.Type);
_result.Key.ShouldBe(expected.Key);
_result.ExpiryInMs.ShouldBe(expected.Expiry);
}
private void WhenICreate()
{
_result = _creator.Create(_fileLoadBalancerOptions);
}
private void GivenThe(FileLoadBalancerOptions fileLoadBalancerOptions)
{
_fileLoadBalancerOptions = fileLoadBalancerOptions;
}
}
}

View File

@ -0,0 +1,85 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.LoadBalancer.LoadBalancers;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Configuration
{
public class ReRouteKeyCreatorTests
{
private ReRouteKeyCreator _creator;
private FileReRoute _reRoute;
private string _result;
public ReRouteKeyCreatorTests()
{
_creator = new ReRouteKeyCreator();
}
[Fact]
public void should_return_sticky_session_key()
{
var reRoute = new FileReRoute
{
LoadBalancerOptions = new FileLoadBalancerOptions
{
Key = "testy",
Type = nameof(CookieStickySessions)
}
};
this.Given(_ => GivenThe(reRoute))
.When(_ => WhenICreate())
.Then(_ => ThenTheResultIs($"{nameof(CookieStickySessions)}:{reRoute.LoadBalancerOptions.Key}"))
.BDDfy();
}
[Fact]
public void should_return_re_route_key()
{
var reRoute = new FileReRoute
{
UpstreamPathTemplate = "/api/product",
UpstreamHttpMethod = new List<string> {"GET", "POST", "PUT"},
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort
{
Host = "localhost",
Port = 123
},
new FileHostAndPort
{
Host = "localhost",
Port = 123
}
}
};
this.Given(_ => GivenThe(reRoute))
.When(_ => WhenICreate())
.Then(_ => ThenTheResultIs($"{reRoute.UpstreamPathTemplate}|{string.Join(",", reRoute.UpstreamHttpMethod)}|{string.Join(",", reRoute.DownstreamHostAndPorts.Select(x => $"{x.Host}:{x.Port}"))}"))
.BDDfy();
}
private void GivenThe(FileReRoute reRoute)
{
_reRoute = reRoute;
}
private void WhenICreate()
{
_result = _creator.Create(_reRoute);
}
private void ThenTheResultIs(string expected)
{
_result.ShouldBe(expected);
}
}
}

View File

@ -1,77 +1,80 @@
using System.Collections.Generic;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Configuration
{
public class ReRouteOptionsCreatorTests
{
private ReRouteOptionsCreator _creator;
private FileReRoute _reRoute;
private ReRouteOptions _result;
public ReRouteOptionsCreatorTests()
{
_creator = new ReRouteOptionsCreator();
}
[Fact]
public void should_create_re_route_options()
{
var reRoute = new FileReRoute
{
RateLimitOptions = new FileRateLimitRule
{
EnableRateLimiting = true
},
AuthenticationOptions = new FileAuthenticationOptions()
{
AuthenticationProviderKey = "Test"
},
RouteClaimsRequirement = new Dictionary<string, string>()
{
{"",""}
},
FileCacheOptions = new FileCacheOptions
{
TtlSeconds = 1
}
};
var expected = new ReRouteOptionsBuilder()
.WithIsAuthenticated(true)
.WithIsAuthorised(true)
.WithIsCached(true)
.WithRateLimiting(true)
.Build();
this.Given(x => x.GivenTheFollowing(reRoute))
.When(x => x.WhenICreate())
.Then(x => x.ThenTheFollowingIsReturned(expected))
.BDDfy();
}
private void GivenTheFollowing(FileReRoute reRoute)
{
_reRoute = reRoute;
}
private void WhenICreate()
{
_result = _creator.Create(_reRoute);
}
private void ThenTheFollowingIsReturned(ReRouteOptions expected)
{
_result.IsAuthenticated.ShouldBe(expected.IsAuthenticated);
_result.IsAuthorised.ShouldBe(expected.IsAuthorised);
_result.IsCached.ShouldBe(expected.IsCached);
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
}
}
namespace Ocelot.UnitTests.Configuration
{
using System.Collections.Generic;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class ReRouteOptionsCreatorTests
{
private readonly ReRouteOptionsCreator _creator;
private FileReRoute _reRoute;
private ReRouteOptions _result;
public ReRouteOptionsCreatorTests()
{
_creator = new ReRouteOptionsCreator();
}
[Fact]
public void should_create_re_route_options()
{
var reRoute = new FileReRoute
{
RateLimitOptions = new FileRateLimitRule
{
EnableRateLimiting = true
},
AuthenticationOptions = new FileAuthenticationOptions()
{
AuthenticationProviderKey = "Test"
},
RouteClaimsRequirement = new Dictionary<string, string>()
{
{"",""}
},
FileCacheOptions = new FileCacheOptions
{
TtlSeconds = 1
},
ServiceName = "west"
};
var expected = new ReRouteOptionsBuilder()
.WithIsAuthenticated(true)
.WithIsAuthorised(true)
.WithIsCached(true)
.WithRateLimiting(true)
.WithUseServiceDiscovery(true)
.Build();
this.Given(x => x.GivenTheFollowing(reRoute))
.When(x => x.WhenICreate())
.Then(x => x.ThenTheFollowingIsReturned(expected))
.BDDfy();
}
private void GivenTheFollowing(FileReRoute reRoute)
{
_reRoute = reRoute;
}
private void WhenICreate()
{
_result = _creator.Create(_reRoute);
}
private void ThenTheFollowingIsReturned(ReRouteOptions expected)
{
_result.IsAuthenticated.ShouldBe(expected.IsAuthenticated);
_result.IsAuthorised.ShouldBe(expected.IsAuthorised);
_result.IsCached.ShouldBe(expected.IsCached);
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
_result.UseServiceDiscovery.ShouldBe(expected.UseServiceDiscovery);
}
}
}

View File

@ -0,0 +1,271 @@
namespace Ocelot.UnitTests.Configuration
{
using System;
using System.Collections.Generic;
using System.Linq;
using Moq;
using Ocelot.Cache;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.Values;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class ReRoutesCreatorTests
{
private ReRoutesCreator _creator;
private Mock<IClaimsToThingCreator> _cthCreator;
private Mock<IAuthenticationOptionsCreator> _aoCreator;
private Mock<IUpstreamTemplatePatternCreator> _utpCreator;
private Mock<IRequestIdKeyCreator> _ridkCreator;
private Mock<IQoSOptionsCreator> _qosoCreator;
private Mock<IReRouteOptionsCreator> _rroCreator;
private Mock<IRateLimitOptionsCreator> _rloCreator;
private Mock<IRegionCreator> _rCreator;
private Mock<IHttpHandlerOptionsCreator> _hhoCreator;
private Mock<IHeaderFindAndReplaceCreator> _hfarCreator;
private Mock<IDownstreamAddressesCreator> _daCreator;
private Mock<ILoadBalancerOptionsCreator> _lboCreator;
private Mock<IReRouteKeyCreator> _rrkCreator;
private FileConfiguration _fileConfig;
private ReRouteOptions _rro;
private string _requestId;
private string _rrk;
private UpstreamPathTemplate _upt;
private AuthenticationOptions _ao;
private List<ClaimToThing> _ctt;
private QoSOptions _qoso;
private RateLimitOptions _rlo;
private string _region;
private HttpHandlerOptions _hho;
private HeaderTransformations _ht;
private List<DownstreamHostAndPort> _dhp;
private LoadBalancerOptions _lbo;
private List<ReRoute> _result;
public ReRoutesCreatorTests()
{
_cthCreator = new Mock<IClaimsToThingCreator>();
_aoCreator = new Mock<IAuthenticationOptionsCreator>();
_utpCreator = new Mock<IUpstreamTemplatePatternCreator>();
_ridkCreator = new Mock<IRequestIdKeyCreator>();
_qosoCreator = new Mock<IQoSOptionsCreator>();
_rroCreator = new Mock<IReRouteOptionsCreator>();
_rloCreator = new Mock<IRateLimitOptionsCreator>();
_rCreator = new Mock<IRegionCreator>();
_hhoCreator = new Mock<IHttpHandlerOptionsCreator>();
_hfarCreator = new Mock<IHeaderFindAndReplaceCreator>();
_daCreator = new Mock<IDownstreamAddressesCreator>();
_lboCreator = new Mock<ILoadBalancerOptionsCreator>();
_rrkCreator = new Mock<IReRouteKeyCreator>();
_creator = new ReRoutesCreator(
_cthCreator.Object,
_aoCreator.Object,
_utpCreator.Object,
_ridkCreator.Object,
_qosoCreator.Object,
_rroCreator.Object,
_rloCreator.Object,
_rCreator.Object,
_hhoCreator.Object,
_hfarCreator.Object,
_daCreator.Object,
_lboCreator.Object,
_rrkCreator.Object
);
}
[Fact]
public void should_return_nothing()
{
var fileConfig = new FileConfiguration();
this.Given(_ => GivenThe(fileConfig))
.When(_ => WhenICreate())
.Then(_ => ThenNoReRoutesAreReturned())
.BDDfy();
}
[Fact]
public void should_return_re_routes()
{
var fileConfig = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
ServiceName = "dave",
DangerousAcceptAnyServerCertificateValidator = true,
AddClaimsToRequest = new Dictionary<string, string>
{
{ "a","b" }
},
AddHeadersToRequest = new Dictionary<string, string>
{
{ "c","d" }
},
AddQueriesToRequest = new Dictionary<string, string>
{
{ "e","f" }
},
UpstreamHttpMethod = new List<string> { "GET", "POST" }
},
new FileReRoute
{
ServiceName = "wave",
DangerousAcceptAnyServerCertificateValidator = false,
AddClaimsToRequest = new Dictionary<string, string>
{
{ "g","h" }
},
AddHeadersToRequest = new Dictionary<string, string>
{
{ "i","j" }
},
AddQueriesToRequest = new Dictionary<string, string>
{
{ "k","l" }
},
UpstreamHttpMethod = new List<string> { "PUT", "DELETE" }
}
}
};
this.Given(_ => GivenThe(fileConfig))
.And(_ => GivenTheDependenciesAreSetUpCorrectly())
.When(_ => WhenICreate())
.Then(_ => ThenTheDependenciesAreCalledCorrectly())
.And(_ => ThenTheReRoutesAreCreated())
.BDDfy();
}
private void ThenTheDependenciesAreCalledCorrectly()
{
ThenTheDepsAreCalledFor(_fileConfig.ReRoutes[0], _fileConfig.GlobalConfiguration);
ThenTheDepsAreCalledFor(_fileConfig.ReRoutes[1], _fileConfig.GlobalConfiguration);
}
private void GivenTheDependenciesAreSetUpCorrectly()
{
_rro = new ReRouteOptions(false, false, false, false, false);
_requestId = "testy";
_rrk = "besty";
_upt = new UpstreamPathTemplateBuilder().Build();
_ao = new AuthenticationOptionsBuilder().Build();
_ctt = new List<ClaimToThing>();
_qoso = new QoSOptionsBuilder().Build();
_rlo = new RateLimitOptionsBuilder().Build();
_region = "vesty";
_hho = new HttpHandlerOptionsBuilder().Build();
_ht = new HeaderTransformations(new List<HeaderFindAndReplace>(), new List<HeaderFindAndReplace>(), new List<AddHeader>(), new List<AddHeader>());
_dhp = new List<DownstreamHostAndPort>();
_lbo = new LoadBalancerOptionsBuilder().Build();
_rroCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_rro);
_ridkCreator.Setup(x => x.Create(It.IsAny<FileReRoute>(), It.IsAny<FileGlobalConfiguration>())).Returns(_requestId);
_rrkCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_rrk);
_utpCreator.Setup(x => x.Create(It.IsAny<IReRoute>())).Returns(_upt);
_aoCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_ao);
_cthCreator.Setup(x => x.Create(It.IsAny<Dictionary<string, string>>())).Returns(_ctt);
_qosoCreator.Setup(x => x.Create(It.IsAny<FileQoSOptions>(), It.IsAny<string>(), It.IsAny<List<string>>())).Returns(_qoso);
_rloCreator.Setup(x => x.Create(It.IsAny<FileRateLimitRule>(), It.IsAny<FileGlobalConfiguration>())).Returns(_rlo);
_rCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(_region);
_hhoCreator.Setup(x => x.Create(It.IsAny<FileHttpHandlerOptions>())).Returns(_hho);
_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);
}
private void ThenTheReRoutesAreCreated()
{
_result.Count.ShouldBe(2);
ThenTheReRouteIsSet(_fileConfig.ReRoutes[0], 0);
ThenTheReRouteIsSet(_fileConfig.ReRoutes[1], 1);
}
private void ThenNoReRoutesAreReturned()
{
_result.ShouldBeEmpty();
}
private void GivenThe(FileConfiguration fileConfig)
{
_fileConfig = fileConfig;
}
private void WhenICreate()
{
_result = _creator.Create(_fileConfig);
}
private void ThenTheReRouteIsSet(FileReRoute expected, int reRouteIndex)
{
_result[reRouteIndex].DownstreamReRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated);
_result[reRouteIndex].DownstreamReRoute[0].IsAuthorised.ShouldBe(_rro.IsAuthorised);
_result[reRouteIndex].DownstreamReRoute[0].IsCached.ShouldBe(_rro.IsCached);
_result[reRouteIndex].DownstreamReRoute[0].EnableEndpointEndpointRateLimiting.ShouldBe(_rro.EnableRateLimiting);
_result[reRouteIndex].DownstreamReRoute[0].RequestIdKey.ShouldBe(_requestId);
_result[reRouteIndex].DownstreamReRoute[0].LoadBalancerKey.ShouldBe(_rrk);
_result[reRouteIndex].DownstreamReRoute[0].UpstreamPathTemplate.ShouldBe(_upt);
_result[reRouteIndex].DownstreamReRoute[0].AuthenticationOptions.ShouldBe(_ao);
_result[reRouteIndex].DownstreamReRoute[0].ClaimsToHeaders.ShouldBe(_ctt);
_result[reRouteIndex].DownstreamReRoute[0].ClaimsToQueries.ShouldBe(_ctt);
_result[reRouteIndex].DownstreamReRoute[0].ClaimsToClaims.ShouldBe(_ctt);
_result[reRouteIndex].DownstreamReRoute[0].QosOptions.ShouldBe(_qoso);
_result[reRouteIndex].DownstreamReRoute[0].RateLimitOptions.ShouldBe(_rlo);
_result[reRouteIndex].DownstreamReRoute[0].CacheOptions.Region.ShouldBe(_region);
_result[reRouteIndex].DownstreamReRoute[0].CacheOptions.TtlSeconds.ShouldBe(expected.FileCacheOptions.TtlSeconds);
_result[reRouteIndex].DownstreamReRoute[0].HttpHandlerOptions.ShouldBe(_hho);
_result[reRouteIndex].DownstreamReRoute[0].UpstreamHeadersFindAndReplace.ShouldBe(_ht.Upstream);
_result[reRouteIndex].DownstreamReRoute[0].DownstreamHeadersFindAndReplace.ShouldBe(_ht.Downstream);
_result[reRouteIndex].DownstreamReRoute[0].AddHeadersToUpstream.ShouldBe(_ht.AddHeadersToUpstream);
_result[reRouteIndex].DownstreamReRoute[0].AddHeadersToDownstream.ShouldBe(_ht.AddHeadersToDownstream);
_result[reRouteIndex].DownstreamReRoute[0].DownstreamAddresses.ShouldBe(_dhp);
_result[reRouteIndex].DownstreamReRoute[0].LoadBalancerOptions.ShouldBe(_lbo);
_result[reRouteIndex].DownstreamReRoute[0].UseServiceDiscovery.ShouldBe(_rro.UseServiceDiscovery);
_result[reRouteIndex].DownstreamReRoute[0].DangerousAcceptAnyServerCertificateValidator.ShouldBe(expected.DangerousAcceptAnyServerCertificateValidator);
_result[reRouteIndex].DownstreamReRoute[0].DelegatingHandlers.ShouldBe(expected.DelegatingHandlers);
_result[reRouteIndex].DownstreamReRoute[0].ServiceName.ShouldBe(expected.ServiceName);
_result[reRouteIndex].DownstreamReRoute[0].DownstreamScheme.ShouldBe(expected.DownstreamScheme);
_result[reRouteIndex].DownstreamReRoute[0].RouteClaimsRequirement.ShouldBe(expected.RouteClaimsRequirement);
_result[reRouteIndex].DownstreamReRoute[0].DownstreamPathTemplate.Value.ShouldBe(expected.DownstreamPathTemplate);
_result[reRouteIndex].DownstreamReRoute[0].Key.ShouldBe(expected.Key);
_result[reRouteIndex].UpstreamHttpMethod
.Select(x => x.Method)
.ToList()
.ShouldContain(x => x == expected.UpstreamHttpMethod[0]);
_result[reRouteIndex].UpstreamHttpMethod
.Select(x => x.Method)
.ToList()
.ShouldContain(x => x == expected.UpstreamHttpMethod[1]);
_result[reRouteIndex].UpstreamHost.ShouldBe(expected.UpstreamHost);
_result[reRouteIndex].DownstreamReRoute.Count.ShouldBe(1);
_result[reRouteIndex].UpstreamTemplatePattern.ShouldBe(_upt);
}
private void ThenTheDepsAreCalledFor(FileReRoute fileReRoute, FileGlobalConfiguration globalConfig)
{
_rroCreator.Verify(x => x.Create(fileReRoute), Times.Once);
_ridkCreator.Verify(x => x.Create(fileReRoute, globalConfig), Times.Once);
_rrkCreator.Verify(x => x.Create(fileReRoute), Times.Once);
_utpCreator.Verify(x => x.Create(fileReRoute), Times.Exactly(2));
_aoCreator.Verify(x => x.Create(fileReRoute), Times.Once);
_cthCreator.Verify(x => x.Create(fileReRoute.AddHeadersToRequest), Times.Once);
_cthCreator.Verify(x => x.Create(fileReRoute.AddClaimsToRequest), Times.Once);
_cthCreator.Verify(x => x.Create(fileReRoute.AddQueriesToRequest), Times.Once);
_qosoCreator.Verify(x => x.Create(fileReRoute.QoSOptions, fileReRoute.UpstreamPathTemplate, fileReRoute.UpstreamHttpMethod));
_rloCreator.Verify(x => x.Create(fileReRoute.RateLimitOptions, globalConfig), Times.Once);
_rCreator.Verify(x => x.Create(fileReRoute), Times.Once);
_hhoCreator.Verify(x => x.Create(fileReRoute.HttpHandlerOptions), Times.Once);
_hfarCreator.Verify(x => x.Create(fileReRoute), Times.Once);
_daCreator.Verify(x => x.Create(fileReRoute), Times.Once);
_lboCreator.Verify(x => x.Create(fileReRoute.LoadBalancerOptions), Times.Once);
}
}
}

View File

@ -37,7 +37,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_handlerOptions = new HttpHandlerOptionsBuilder().Build();
_loadBalancerOptions = new LoadBalancerOptionsBuilder().WithType(nameof(NoLoadBalancer)).Build();
_qosOptionsCreator
.Setup(x => x.Create(It.IsAny<QoSOptions>(), It.IsAny<string>(), It.IsAny<string[]>()))
.Setup(x => x.Create(It.IsAny<QoSOptions>(), It.IsAny<string>(), It.IsAny<List<string>>()))
.Returns(_qoSOptions);
_creator = new DownstreamRouteCreator(_qosOptionsCreator.Object);
}
@ -198,7 +198,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
private void GivenTheQosCreatorReturns(QoSOptions options)
{
_qosOptionsCreator
.Setup(x => x.Create(It.IsAny<QoSOptions>(), It.IsAny<string>(), It.IsAny<string[]>()))
.Setup(x => x.Create(It.IsAny<QoSOptions>(), It.IsAny<string>(), It.IsAny<List<string>>()))
.Returns(options);
}
@ -260,7 +260,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_result.Data.ReRoute.DownstreamReRoute[0].QosOptions.ShouldBe(expected);
_result.Data.ReRoute.DownstreamReRoute[0].QosOptions.UseQos.ShouldBeTrue();
_qosOptionsCreator
.Verify(x => x.Create(expected, _upstreamUrlPath, It.IsAny<string[]>()), Times.Once);
.Verify(x => x.Create(expected, _upstreamUrlPath, It.IsAny<List<string>>()), Times.Once);
}
private void GivenTheConfiguration(IInternalConfiguration config)

View File

@ -459,7 +459,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.WithDownstreamPathTemplate("someDownstreamPath")
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
.WithUpstreamHost("MATCH")
.Build())
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
@ -545,7 +544,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.WithDownstreamPathTemplate("someDownstreamPath")
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
.WithUpstreamHost("MATCH")
.Build())
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
@ -556,7 +554,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.WithDownstreamPathTemplate("someDownstreamPath")
.WithUpstreamHttpMethod(new List<string> { }) // empty list of methods
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
.WithUpstreamHost("MATCH")
.Build())
.WithUpstreamHttpMethod(new List<string> { }) // empty list of methods
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
@ -587,7 +584,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.WithDownstreamPathTemplate("someDownstreamPath")
.WithUpstreamHttpMethod(new List<string>())
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
.WithUpstreamHost("MATCH")
.Build())
.WithUpstreamHttpMethod(new List<string>())
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
@ -618,7 +614,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.WithDownstreamPathTemplate("someDownstreamPath")
.WithUpstreamHttpMethod(new List<string>())
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
.WithUpstreamHost("MATCH")
.Build())
.WithUpstreamHttpMethod(new List<string>())
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
@ -659,7 +654,6 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.WithDownstreamPathTemplate("someDownstreamPath")
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))
.WithUpstreamHost("MATCH")
.Build())
.WithUpstreamHttpMethod(new List<string> { "Get" })
.WithUpstreamPathTemplate(new UpstreamPathTemplate("someUpstreamPath", 1, false, "someUpstreamPath"))

View File

@ -1,63 +0,0 @@
namespace Ocelot.UnitTests.TestData
{
using System.Collections.Generic;
using Ocelot.Configuration.File;
public class AuthenticationConfigTestData
{
public static IEnumerable<object[]> GetAuthenticationData()
{
yield return new object[]
{
new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
UpstreamPathTemplate = "/api/products/{productId}",
DownstreamPathTemplate = "/products/{productId}",
UpstreamHttpMethod = new List<string> { "Get" },
ReRouteIsCaseSensitive = true,
AuthenticationOptions = new FileAuthenticationOptions
{
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>(),
},
AddHeadersToRequest =
{
{ "CustomerId", "Claims[CustomerId] > value" },
}
}
}
}
};
yield return new object[]
{
new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
UpstreamPathTemplate = "/api/products/{productId}",
DownstreamPathTemplate = "/products/{productId}",
UpstreamHttpMethod = new List<string> { "Get" },
ReRouteIsCaseSensitive = true,
AuthenticationOptions = new FileAuthenticationOptions
{
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>(),
},
AddHeadersToRequest =
{
{ "CustomerId", "Claims[CustomerId] > value" },
}
}
}
}
};
}
}
}