extracted thing that creates request id key

This commit is contained in:
Tom Gardham-Pallister
2017-03-01 08:11:39 +00:00
parent c85ea41951
commit d4119ab33d
6 changed files with 146 additions and 16 deletions

View File

@ -34,6 +34,7 @@ namespace Ocelot.UnitTests.Configuration
private Mock<IClaimsToThingCreator> _claimsToThingCreator;
private Mock<IAuthenticationOptionsCreator> _authOptionsCreator;
private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
private Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
public FileConfigurationCreatorTests()
{
@ -49,12 +50,13 @@ namespace Ocelot.UnitTests.Configuration
_claimsToThingCreator = new Mock<IClaimsToThingCreator>();
_authOptionsCreator = new Mock<IAuthenticationOptionsCreator>();
_upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
_requestIdKeyCreator = new Mock<IRequestIdKeyCreator>();
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
_fileConfig.Object, _validator.Object, _logger.Object,
_loadBalancerFactory.Object, _loadBalancerHouse.Object,
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object);
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object);
}
[Fact]
@ -279,7 +281,7 @@ namespace Ocelot.UnitTests.Configuration
}
[Fact]
public void should_set_global_request_id_key()
public void should_call_request_id_creator()
{
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
{
@ -298,7 +300,8 @@ namespace Ocelot.UnitTests.Configuration
RequestIdKey = "blahhhh"
}
}))
.And(x => x.GivenTheConfigIsValid())
.And(x => x.GivenTheConfigIsValid())
.And(x => x.GivenTheRequestIdCreatorReturns("blahhhh"))
.When(x => x.WhenICreateTheConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
@ -309,6 +312,7 @@ namespace Ocelot.UnitTests.Configuration
.WithRequestIdKey("blahhhh")
.Build()
}))
.And(x => x.ThenTheRequestIdKeyCreatorIsCalledCorrectly())
.BDDfy();
}
@ -465,6 +469,7 @@ namespace Ocelot.UnitTests.Configuration
result.ClaimsToClaims.Count.ShouldBe(expected.ClaimsToClaims.Count);
result.ClaimsToHeaders.Count.ShouldBe(expected.ClaimsToHeaders.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>()))
.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);
}
}
}

View File

@ -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);
}
}
}