mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 18:22:49 +08:00
Initial draft for the issue 147 fix (#155)
* Initial draft for the issue 147 fix * Added unit tests for scenarios for getting and setting file configuration if the environment name is unavailable.
This commit is contained in:
parent
9ba57f8ba6
commit
6c5c6495c0
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Ocelot.Configuration.File;
|
using Ocelot.Configuration.File;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
@ -8,33 +9,41 @@ namespace Ocelot.Configuration.Repository
|
|||||||
{
|
{
|
||||||
public class FileConfigurationRepository : IFileConfigurationRepository
|
public class FileConfigurationRepository : IFileConfigurationRepository
|
||||||
{
|
{
|
||||||
|
private readonly string _configFilePath;
|
||||||
|
|
||||||
private static readonly object _lock = new object();
|
private static readonly object _lock = new object();
|
||||||
|
|
||||||
|
public FileConfigurationRepository(IHostingEnvironment hostingEnvironment)
|
||||||
|
{
|
||||||
|
_configFilePath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(hostingEnvironment.EnvironmentName) ? string.Empty : ".")}{hostingEnvironment.EnvironmentName}.json";
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<Response<FileConfiguration>> Get()
|
public async Task<Response<FileConfiguration>> Get()
|
||||||
{
|
{
|
||||||
var configFilePath = $"{AppContext.BaseDirectory}/configuration.json";
|
string jsonConfiguration;
|
||||||
string json = string.Empty;
|
|
||||||
lock(_lock)
|
lock(_lock)
|
||||||
{
|
{
|
||||||
json = System.IO.File.ReadAllText(configFilePath);
|
jsonConfiguration = System.IO.File.ReadAllText(_configFilePath);
|
||||||
}
|
}
|
||||||
var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(json);
|
|
||||||
|
var fileConfiguration = JsonConvert.DeserializeObject<FileConfiguration>(jsonConfiguration);
|
||||||
|
|
||||||
return new OkResponse<FileConfiguration>(fileConfiguration);
|
return new OkResponse<FileConfiguration>(fileConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Response> Set(FileConfiguration fileConfiguration)
|
public async Task<Response> Set(FileConfiguration fileConfiguration)
|
||||||
{
|
{
|
||||||
var configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
string jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||||
|
|
||||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
|
||||||
|
|
||||||
lock(_lock)
|
lock(_lock)
|
||||||
{
|
{
|
||||||
if (System.IO.File.Exists(configurationPath))
|
if (System.IO.File.Exists(_configFilePath))
|
||||||
{
|
{
|
||||||
System.IO.File.Delete(configurationPath);
|
System.IO.File.Delete(_configFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
System.IO.File.WriteAllText(configurationPath, jsonConfiguration);
|
System.IO.File.WriteAllText(_configFilePath, jsonConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OkResponse();
|
return new OkResponse();
|
||||||
|
@ -1,56 +1,35 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Configuration;
|
|
||||||
using Ocelot.Configuration.File;
|
using Ocelot.Configuration.File;
|
||||||
using Ocelot.Responses;
|
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Ocelot.Configuration.Repository;
|
using Ocelot.Configuration.Repository;
|
||||||
|
|
||||||
namespace Ocelot.UnitTests.Configuration
|
namespace Ocelot.UnitTests.Configuration
|
||||||
{
|
{
|
||||||
public class FileConfigurationRepositoryTests
|
public class FileConfigurationRepositoryTests
|
||||||
{
|
{
|
||||||
private readonly IFileConfigurationRepository _repo;
|
private readonly Mock<IHostingEnvironment> _hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||||
|
private IFileConfigurationRepository _repo;
|
||||||
private FileConfiguration _result;
|
private FileConfiguration _result;
|
||||||
private FileConfiguration _fileConfiguration;
|
private FileConfiguration _fileConfiguration;
|
||||||
|
private string _environmentName = "DEV";
|
||||||
|
|
||||||
public FileConfigurationRepositoryTests()
|
public FileConfigurationRepositoryTests()
|
||||||
{
|
{
|
||||||
_repo = new FileConfigurationRepository();
|
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
||||||
|
_repo = new FileConfigurationRepository(_hostingEnvironment.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_file_configuration()
|
public void should_return_file_configuration()
|
||||||
{
|
{
|
||||||
var reRoutes = new List<FileReRoute>
|
var config = FakeFileConfigurationForGet();
|
||||||
{
|
|
||||||
new FileReRoute
|
|
||||||
{
|
|
||||||
DownstreamHost = "localhost",
|
|
||||||
DownstreamPort = 80,
|
|
||||||
DownstreamScheme = "https",
|
|
||||||
DownstreamPathTemplate = "/test/test/{test}"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var globalConfiguration = new FileGlobalConfiguration
|
|
||||||
{
|
|
||||||
AdministrationPath = "testy",
|
|
||||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
|
||||||
{
|
|
||||||
Port = 198,
|
|
||||||
Host = "blah"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var config = new FileConfiguration();
|
|
||||||
config.GlobalConfiguration = globalConfiguration;
|
|
||||||
config.ReRoutes.AddRange(reRoutes);
|
|
||||||
|
|
||||||
this.Given(x => x.GivenTheConfigurationIs(config))
|
this.Given(x => x.GivenTheConfigurationIs(config))
|
||||||
.When(x => x.WhenIGetTheReRoutes())
|
.When(x => x.WhenIGetTheReRoutes())
|
||||||
@ -58,33 +37,22 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_file_configuration_if_environment_name_is_unavailable()
|
||||||
|
{
|
||||||
|
var config = FakeFileConfigurationForGet();
|
||||||
|
|
||||||
|
this.Given(x => x.GivenTheEnvironmentNameIsUnavailable())
|
||||||
|
.And(x => x.GivenTheConfigurationIs(config))
|
||||||
|
.When(x => x.WhenIGetTheReRoutes())
|
||||||
|
.Then(x => x.ThenTheFollowingIsReturned(config))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_set_file_configuration()
|
public void should_set_file_configuration()
|
||||||
{
|
{
|
||||||
var reRoutes = new List<FileReRoute>
|
var config = FakeFileConfigurationForSet();
|
||||||
{
|
|
||||||
new FileReRoute
|
|
||||||
{
|
|
||||||
DownstreamHost = "123.12.12.12",
|
|
||||||
DownstreamPort = 80,
|
|
||||||
DownstreamScheme = "https",
|
|
||||||
DownstreamPathTemplate = "/asdfs/test/{test}"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var globalConfiguration = new FileGlobalConfiguration
|
|
||||||
{
|
|
||||||
AdministrationPath = "asdas",
|
|
||||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
|
||||||
{
|
|
||||||
Port = 198,
|
|
||||||
Host = "blah"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var config = new FileConfiguration();
|
|
||||||
config.GlobalConfiguration = globalConfiguration;
|
|
||||||
config.ReRoutes.AddRange(reRoutes);
|
|
||||||
|
|
||||||
this.Given(x => GivenIHaveAConfiguration(config))
|
this.Given(x => GivenIHaveAConfiguration(config))
|
||||||
.When(x => WhenISetTheConfiguration())
|
.When(x => WhenISetTheConfiguration())
|
||||||
@ -92,6 +60,24 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_set_file_configuration_if_environment_name_is_unavailable()
|
||||||
|
{
|
||||||
|
var config = FakeFileConfigurationForSet();
|
||||||
|
this.Given(x => GivenIHaveAConfiguration(config))
|
||||||
|
.And(x => GivenTheEnvironmentNameIsUnavailable())
|
||||||
|
.When(x => WhenISetTheConfiguration())
|
||||||
|
.Then(x => ThenTheConfigurationIsStoredAs(config))
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheEnvironmentNameIsUnavailable()
|
||||||
|
{
|
||||||
|
_environmentName = null;
|
||||||
|
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
||||||
|
_repo = new FileConfigurationRepository(_hostingEnvironment.Object);
|
||||||
|
}
|
||||||
|
|
||||||
private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
|
private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
|
||||||
{
|
{
|
||||||
_fileConfiguration = fileConfiguration;
|
_fileConfiguration = fileConfiguration;
|
||||||
@ -121,7 +107,7 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
|
|
||||||
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
||||||
{
|
{
|
||||||
var configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
var configurationPath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json";
|
||||||
|
|
||||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||||
|
|
||||||
@ -153,5 +139,65 @@ namespace Ocelot.UnitTests.Configuration
|
|||||||
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private FileConfiguration FakeFileConfigurationForSet()
|
||||||
|
{
|
||||||
|
var reRoutes = new List<FileReRoute>
|
||||||
|
{
|
||||||
|
new FileReRoute
|
||||||
|
{
|
||||||
|
DownstreamHost = "123.12.12.12",
|
||||||
|
DownstreamPort = 80,
|
||||||
|
DownstreamScheme = "https",
|
||||||
|
DownstreamPathTemplate = "/asdfs/test/{test}"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var globalConfiguration = new FileGlobalConfiguration
|
||||||
|
{
|
||||||
|
AdministrationPath = "asdas",
|
||||||
|
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||||
|
{
|
||||||
|
Port = 198,
|
||||||
|
Host = "blah"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return new FileConfiguration
|
||||||
|
{
|
||||||
|
GlobalConfiguration = globalConfiguration,
|
||||||
|
ReRoutes = reRoutes
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileConfiguration FakeFileConfigurationForGet()
|
||||||
|
{
|
||||||
|
var reRoutes = new List<FileReRoute>
|
||||||
|
{
|
||||||
|
new FileReRoute
|
||||||
|
{
|
||||||
|
DownstreamHost = "localhost",
|
||||||
|
DownstreamPort = 80,
|
||||||
|
DownstreamScheme = "https",
|
||||||
|
DownstreamPathTemplate = "/test/test/{test}"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var globalConfiguration = new FileGlobalConfiguration
|
||||||
|
{
|
||||||
|
AdministrationPath = "testy",
|
||||||
|
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||||
|
{
|
||||||
|
Port = 198,
|
||||||
|
Host = "blah"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return new FileConfiguration
|
||||||
|
{
|
||||||
|
GlobalConfiguration = globalConfiguration,
|
||||||
|
ReRoutes = reRoutes
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user