Merge branch 'feature/config_grow_when_merged' of https://github.com/snb83/Ocelot into snb83-feature/config_grow_when_merged

This commit is contained in:
Tom Gardham-Pallister
2018-09-20 18:19:46 +01:00
2 changed files with 97 additions and 22 deletions

View File

@ -9,6 +9,8 @@
using Shouldly;
using TestStack.BDDfy;
using Xunit;
using Moq;
using Microsoft.AspNetCore.Hosting;
public class ConfigurationBuilderExtensionsTests
{
@ -19,6 +21,18 @@
private FileConfiguration _reRouteA;
private FileConfiguration _reRouteB;
private FileConfiguration _aggregate;
private FileConfiguration _envSpecific;
public ConfigurationBuilderExtensionsTests()
{
// Clean up config files before each test
var subConfigFiles = new DirectoryInfo(".").GetFiles("ocelot.*.json");
foreach(var config in subConfigFiles)
{
config.Delete();
}
}
[Fact]
public void should_add_base_url_to_config()
@ -32,23 +46,33 @@
[Fact]
public void should_merge_files()
{
this.Given(_ => GivenMultipleConfigurationFiles(""))
.When(_ => WhenIAddOcelotConfiguration())
this.Given(_ => GivenMultipleConfigurationFiles("", false))
.When(_ => WhenIAddOcelotConfiguration(false))
.Then(_ => ThenTheConfigsAreMerged())
.BDDfy();
}
[Fact]
public void should_merge_files_except_env()
{
this.Given(_ => GivenMultipleConfigurationFiles("", true))
.When(_ => WhenIAddOcelotConfiguration(true))
.Then(_ => ThenTheConfigsAreMerged())
.And(_ => NotContainsEnvSpecificConfig())
.BDDfy();
}
[Fact]
public void should_merge_files_in_specific_folder()
{
string configFolder = "ConfigFiles";
this.Given(_ => GivenMultipleConfigurationFiles(configFolder))
this.Given(_ => GivenMultipleConfigurationFiles(configFolder, false))
.When(_ => WhenIAddOcelotConfigurationWithSpecificFolder(configFolder))
.Then(_ => ThenTheConfigsAreMerged())
.BDDfy();
}
private void GivenMultipleConfigurationFiles(string folder)
private void GivenMultipleConfigurationFiles(string folder, bool addEnvSpecificConfig)
{
if (!string.IsNullOrEmpty(folder))
{
@ -155,7 +179,7 @@
{
new FileAggregateReRoute
{
ReRouteKeys = new List<string>
ReRouteKeys = new List<string>
{
"KeyB",
"KeyBB"
@ -164,7 +188,7 @@
},
new FileAggregateReRoute
{
ReRouteKeys = new List<string>
ReRouteKeys = new List<string>
{
"KeyB",
"KeyBB"
@ -174,6 +198,32 @@
}
};
_envSpecific = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamScheme = "DownstreamSchemeSpec",
DownstreamPathTemplate = "DownstreamPathTemplateSpec",
Key = "KeySpec",
UpstreamHost = "UpstreamHostSpec",
UpstreamHttpMethod = new List<string>
{
"UpstreamHttpMethodSpec"
},
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort
{
Host = "HostSpec",
Port = 80
}
}
}
}
};
string globalFilename = Path.Combine(folder, "ocelot.global.json");
string reroutesAFilename = Path.Combine(folder, "ocelot.reRoutesA.json");
string reroutesBFilename = Path.Combine(folder, "ocelot.reRoutesB.json");
@ -183,12 +233,23 @@
File.WriteAllText(reroutesAFilename, JsonConvert.SerializeObject(_reRouteA));
File.WriteAllText(reroutesBFilename, JsonConvert.SerializeObject(_reRouteB));
File.WriteAllText(aggregatesFilename, JsonConvert.SerializeObject(_aggregate));
if (addEnvSpecificConfig)
{
string envSpecificFilename = Path.Combine(folder, "ocelot.Env.json");
File.WriteAllText(envSpecificFilename, JsonConvert.SerializeObject(_envSpecific));
}
}
private void WhenIAddOcelotConfiguration()
private void WhenIAddOcelotConfiguration(bool addEnv)
{
IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddOcelot();
var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupGet(x => x.EnvironmentName).Returns(addEnv ? "Env" : null);
builder.AddOcelot(hostingEnvironment.Object);
_configRoot = builder.Build();
}
@ -235,6 +296,14 @@
fc.Aggregates.Count.ShouldBe(_aggregate.Aggregates.Count);
}
private void NotContainsEnvSpecificConfig()
{
var fc = (FileConfiguration)_configRoot.Get(typeof(FileConfiguration));
fc.ReRoutes.ShouldNotContain(x => x.DownstreamScheme == _envSpecific.ReRoutes[0].DownstreamScheme);
fc.ReRoutes.ShouldNotContain(x => x.DownstreamPathTemplate == _envSpecific.ReRoutes[0].DownstreamPathTemplate);
fc.ReRoutes.ShouldNotContain(x => x.Key == _envSpecific.ReRoutes[0].Key);
}
private void GivenTheBaseUrl(string baseUrl)
{
#pragma warning disable CS0618