mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-26 09:52:51 +08:00

* feat: update to asp.net core 3.0 preview 9 * fix : AspDotNetLogger unittest * feat: update generic host and useMvc 1、Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing https://github.com/aspnet/AspNetCore/issues/9542 2、 use IHost and IHostBuilder * feat : update .net core 3.0 rc1 * eureka extension * fixed logger formatter error * fixed synchronous operations are disallowed of ReadToEnd method * fix log tests * Flush method of FakeStream should do nothing * Update ContentTests.cs * Fixed ws tests * feat: delelte comment code * feat: update .net core 3.0 RTM * Update OcelotBuilderTests.cs * Update .travis.yml mono 6.0.0 and dotnet 3.0.100 * Update Ocelot.IntegrationTests.csproj update Microsoft.Data.SQLite 3.0.0 * Update .travis.yml * feat: remove FrameworkReference 1、 remove FrameworkReference 2、 update package * add appveyor configuration to use version of VS2019 with dotnet core 3 sdk support * update obsoleted SetCollectionValidator method * Swap out OpenCover for Coverlet * Bump Cake to 0.35.0 * Downgrade coveralls.net to 0.7.0 Fix disposing of PollConsul instance * Remove environment specific path separator * Do not return ReportGenerator on Mac/Linux * Remove direct dependency on IInternalConfiguration * Fix ordering of variable assignment * Fix broken tests * Fix acceptance tests for Consul
289 lines
11 KiB
C#
289 lines
11 KiB
C#
namespace Ocelot.UnitTests.Configuration
|
|
{
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Moq;
|
|
using Newtonsoft.Json;
|
|
using Ocelot.Configuration.File;
|
|
using Ocelot.Configuration.Repository;
|
|
using Shouldly;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
public class DiskFileConfigurationRepositoryTests : IDisposable
|
|
{
|
|
private readonly Mock<IWebHostEnvironment> _hostingEnvironment;
|
|
private IFileConfigurationRepository _repo;
|
|
private string _environmentSpecificPath;
|
|
private string _ocelotJsonPath;
|
|
private FileConfiguration _result;
|
|
private FileConfiguration _fileConfiguration;
|
|
|
|
// This is a bit dirty and it is dev.dev so that the ConfigurationBuilderExtensionsTests
|
|
// cant pick it up if they run in parralel..and the semaphore stops them running at the same time...sigh
|
|
// these are not really unit tests but whatever...
|
|
private string _environmentName = "DEV.DEV";
|
|
|
|
private static SemaphoreSlim _semaphore;
|
|
|
|
public DiskFileConfigurationRepositoryTests()
|
|
{
|
|
_semaphore = new SemaphoreSlim(1, 1);
|
|
_semaphore.Wait();
|
|
_hostingEnvironment = new Mock<IWebHostEnvironment>();
|
|
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
|
_repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_file_configuration()
|
|
{
|
|
var config = FakeFileConfigurationForGet();
|
|
|
|
this.Given(_ => GivenTheConfigurationIs(config))
|
|
.When(_ => WhenIGetTheReRoutes())
|
|
.Then(_ => ThenTheFollowingIsReturned(config))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_return_file_configuration_if_environment_name_is_unavailable()
|
|
{
|
|
var config = FakeFileConfigurationForGet();
|
|
|
|
this.Given(_ => GivenTheEnvironmentNameIsUnavailable())
|
|
.And(_ => GivenTheConfigurationIs(config))
|
|
.When(_ => WhenIGetTheReRoutes())
|
|
.Then(_ => ThenTheFollowingIsReturned(config))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_set_file_configuration()
|
|
{
|
|
var config = FakeFileConfigurationForSet();
|
|
|
|
this.Given(_ => GivenIHaveAConfiguration(config))
|
|
.When(_ => WhenISetTheConfiguration())
|
|
.Then(_ => ThenTheConfigurationIsStoredAs(config))
|
|
.And(_ => ThenTheConfigurationJsonIsIndented(config))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_set_file_configuration_if_environment_name_is_unavailable()
|
|
{
|
|
var config = FakeFileConfigurationForSet();
|
|
|
|
this.Given(_ => GivenIHaveAConfiguration(config))
|
|
.And(_ => GivenTheEnvironmentNameIsUnavailable())
|
|
.When(_ => WhenISetTheConfiguration())
|
|
.Then(_ => ThenTheConfigurationIsStoredAs(config))
|
|
.And(_ => ThenTheConfigurationJsonIsIndented(config))
|
|
.BDDfy();
|
|
}
|
|
|
|
[Fact]
|
|
public void should_set_environment_file_configuration_and_ocelot_file_configuration()
|
|
{
|
|
var config = FakeFileConfigurationForSet();
|
|
|
|
this.Given(_ => GivenIHaveAConfiguration(config))
|
|
.And(_ => GivenTheConfigurationIs(config))
|
|
.And(_ => GivenTheUserAddedOcelotJson())
|
|
.When(_ => WhenISetTheConfiguration())
|
|
.Then(_ => ThenTheConfigurationIsStoredAs(config))
|
|
.And(_ => ThenTheConfigurationJsonIsIndented(config))
|
|
.Then(_ => ThenTheOcelotJsonIsStoredAs(config))
|
|
.BDDfy();
|
|
}
|
|
|
|
private void GivenTheUserAddedOcelotJson()
|
|
{
|
|
_ocelotJsonPath = $"{AppContext.BaseDirectory}/ocelot.json";
|
|
|
|
if (File.Exists(_ocelotJsonPath))
|
|
{
|
|
File.Delete(_ocelotJsonPath);
|
|
}
|
|
|
|
File.WriteAllText(_ocelotJsonPath, "Doesnt matter");
|
|
}
|
|
|
|
private void GivenTheEnvironmentNameIsUnavailable()
|
|
{
|
|
_environmentName = null;
|
|
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
|
_repo = new DiskFileConfigurationRepository(_hostingEnvironment.Object);
|
|
}
|
|
|
|
private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
|
|
{
|
|
_fileConfiguration = fileConfiguration;
|
|
}
|
|
|
|
private void WhenISetTheConfiguration()
|
|
{
|
|
_repo.Set(_fileConfiguration);
|
|
_result = _repo.Get().Result.Data;
|
|
}
|
|
|
|
private void ThenTheConfigurationIsStoredAs(FileConfiguration expecteds)
|
|
{
|
|
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey);
|
|
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
|
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
|
|
|
for (var i = 0; i < _result.ReRoutes.Count; i++)
|
|
{
|
|
for (int j = 0; j < _result.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
|
|
{
|
|
var result = _result.ReRoutes[i].DownstreamHostAndPorts[j];
|
|
var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j];
|
|
|
|
result.Host.ShouldBe(expected.Host);
|
|
result.Port.ShouldBe(expected.Port);
|
|
}
|
|
|
|
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate);
|
|
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme);
|
|
}
|
|
}
|
|
|
|
private void ThenTheOcelotJsonIsStoredAs(FileConfiguration expecteds)
|
|
{
|
|
var resultText = File.ReadAllText(_ocelotJsonPath);
|
|
var expectedText = JsonConvert.SerializeObject(expecteds, Formatting.Indented);
|
|
resultText.ShouldBe(expectedText);
|
|
}
|
|
|
|
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
|
{
|
|
_environmentSpecificPath = $"{AppContext.BaseDirectory}/ocelot{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json";
|
|
|
|
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration, Formatting.Indented);
|
|
|
|
if (File.Exists(_environmentSpecificPath))
|
|
{
|
|
File.Delete(_environmentSpecificPath);
|
|
}
|
|
|
|
File.WriteAllText(_environmentSpecificPath, jsonConfiguration);
|
|
}
|
|
|
|
private void ThenTheConfigurationJsonIsIndented(FileConfiguration expecteds)
|
|
{
|
|
var path = !string.IsNullOrEmpty(_environmentSpecificPath) ? _environmentSpecificPath : _environmentSpecificPath = $"{AppContext.BaseDirectory}/ocelot{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json";
|
|
|
|
var resultText = File.ReadAllText(path);
|
|
var expectedText = JsonConvert.SerializeObject(expecteds, Formatting.Indented);
|
|
resultText.ShouldBe(expectedText);
|
|
}
|
|
|
|
private void WhenIGetTheReRoutes()
|
|
{
|
|
_result = _repo.Get().Result.Data;
|
|
}
|
|
|
|
private void ThenTheFollowingIsReturned(FileConfiguration expecteds)
|
|
{
|
|
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey);
|
|
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
|
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
|
|
|
for (var i = 0; i < _result.ReRoutes.Count; i++)
|
|
{
|
|
for (int j = 0; j < _result.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
|
|
{
|
|
var result = _result.ReRoutes[i].DownstreamHostAndPorts[j];
|
|
var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j];
|
|
|
|
result.Host.ShouldBe(expected.Host);
|
|
result.Port.ShouldBe(expected.Port);
|
|
}
|
|
|
|
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate);
|
|
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme);
|
|
}
|
|
}
|
|
|
|
private FileConfiguration FakeFileConfigurationForSet()
|
|
{
|
|
var reRoutes = new List<FileReRoute>
|
|
{
|
|
new FileReRoute
|
|
{
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "123.12.12.12",
|
|
Port = 80,
|
|
}
|
|
},
|
|
DownstreamScheme = "https",
|
|
DownstreamPathTemplate = "/asdfs/test/{test}"
|
|
}
|
|
};
|
|
|
|
var globalConfiguration = new FileGlobalConfiguration
|
|
{
|
|
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
|
{
|
|
Port = 198,
|
|
Host = "blah"
|
|
}
|
|
};
|
|
|
|
return new FileConfiguration
|
|
{
|
|
GlobalConfiguration = globalConfiguration,
|
|
ReRoutes = reRoutes
|
|
};
|
|
}
|
|
|
|
private FileConfiguration FakeFileConfigurationForGet()
|
|
{
|
|
var reRoutes = new List<FileReRoute>
|
|
{
|
|
new FileReRoute
|
|
{
|
|
DownstreamHostAndPorts = new List<FileHostAndPort>
|
|
{
|
|
new FileHostAndPort
|
|
{
|
|
Host = "localhost",
|
|
Port = 80,
|
|
}
|
|
},
|
|
DownstreamScheme = "https",
|
|
DownstreamPathTemplate = "/test/test/{test}"
|
|
}
|
|
};
|
|
|
|
var globalConfiguration = new FileGlobalConfiguration
|
|
{
|
|
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
|
{
|
|
Port = 198,
|
|
Host = "blah"
|
|
}
|
|
};
|
|
|
|
return new FileConfiguration
|
|
{
|
|
GlobalConfiguration = globalConfiguration,
|
|
ReRoutes = reRoutes
|
|
};
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_semaphore.Release();
|
|
}
|
|
}
|
|
}
|