mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 15:28:16 +08:00
Feature/use any id server for admin area (#232)
* initial commits around using any id servers * add your own id server for admin area * lots of refactoring, now instead of injecting IWebHostBuilder we just set the Ocelot base url as a configuration extension method..this means people can pass it in on the command line aswell as hardcode which is OK I guess, also can now use your own IdentityServer to authenticate admin area * updated docs for #231 * some tests that hopefully bump up coverage
This commit is contained in:
@ -0,0 +1,41 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DependencyInjection
|
||||
{
|
||||
public class ConfigurationBuilderExtensionsTests
|
||||
{
|
||||
private IConfigurationRoot _configuration;
|
||||
private string _result;
|
||||
|
||||
[Fact]
|
||||
public void should_add_base_url_to_config()
|
||||
{
|
||||
this.Given(x => GivenTheBaseUrl("test"))
|
||||
.When(x => WhenIGet("BaseUrl"))
|
||||
.Then(x => ThenTheResultIs("test"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheBaseUrl(string baseUrl)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.AddOcelotBaseUrl(baseUrl);
|
||||
|
||||
_configuration = builder.Build();
|
||||
}
|
||||
|
||||
private void WhenIGet(string key)
|
||||
{
|
||||
_result = _configuration.GetValue("BaseUrl", "");
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(string expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ using Ocelot.Requester;
|
||||
using Ocelot.UnitTests.Requester;
|
||||
using Shouldly;
|
||||
using System;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
@ -31,13 +32,11 @@ namespace Ocelot.UnitTests.DependencyInjection
|
||||
|
||||
public OcelotBuilderTests()
|
||||
{
|
||||
IWebHostBuilder builder = new WebHostBuilder();
|
||||
_configRoot = new ConfigurationRoot(new List<IConfigurationProvider>());
|
||||
_services = new ServiceCollection();
|
||||
_services.AddSingleton(builder);
|
||||
_services.AddSingleton<IHostingEnvironment, HostingEnvironment>();
|
||||
_services.AddSingleton<IConfiguration>(_configRoot);
|
||||
_maxRetries = 100;
|
||||
_configRoot = new ConfigurationRoot(new List<IConfigurationProvider>());
|
||||
_services = new ServiceCollection();
|
||||
_services.AddSingleton<IHostingEnvironment, HostingEnvironment>();
|
||||
_services.AddSingleton<IConfiguration>(_configRoot);
|
||||
_maxRetries = 100;
|
||||
}
|
||||
private Exception _ex;
|
||||
|
||||
@ -100,6 +99,40 @@ namespace Ocelot.UnitTests.DependencyInjection
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_administration_with_identity_server_options()
|
||||
{
|
||||
Action<IdentityServerAuthenticationOptions> options = o => {
|
||||
|
||||
};
|
||||
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpAdministration(options))
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.Then(x => ThenTheCorrectAdminPathIsRegitered())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_administration()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpAdministration())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.Then(x => ThenTheCorrectAdminPathIsRegitered())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenISetUpAdministration()
|
||||
{
|
||||
_ocelotBuilder.AddAdministration("/administration", "secret");
|
||||
}
|
||||
|
||||
private void WhenISetUpAdministration(Action<IdentityServerAuthenticationOptions> options)
|
||||
{
|
||||
_ocelotBuilder.AddAdministration("/administration", options);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_logger_factory()
|
||||
{
|
||||
@ -255,6 +288,7 @@ namespace Ocelot.UnitTests.DependencyInjection
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider();
|
||||
var logger = _serviceProvider.GetService<IFileConfigurationSetter>();
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -15,6 +15,7 @@ namespace Ocelot.UnitTests.Errors
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Rafty.Concensus;
|
||||
using Ocelot.Errors;
|
||||
|
||||
public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
@ -40,11 +41,6 @@ namespace Ocelot.UnitTests.Errors
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void TheRequestIdIsNotSet()
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DownstreamException()
|
||||
{
|
||||
@ -83,6 +79,55 @@ namespace Ocelot.UnitTests.Errors
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_throw_exception_if_config_provider_returns_error()
|
||||
{
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigReturnsError())
|
||||
.When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
|
||||
.Then(_ => ThenAnExceptionIsThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_throw_exception_if_config_provider_throws()
|
||||
{
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigThrows())
|
||||
.When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
|
||||
.Then(_ => ThenAnExceptionIsThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheConfigThrows()
|
||||
{
|
||||
var ex = new Exception("outer", new Exception("inner"));
|
||||
_provider
|
||||
.Setup(x => x.Get()).ThrowsAsync(ex);
|
||||
}
|
||||
|
||||
private void ThenAnExceptionIsThrown()
|
||||
{
|
||||
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
private void GivenTheConfigReturnsError()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
var response = new Ocelot.Responses.ErrorResponse<IOcelotConfiguration>(new FakeError());
|
||||
_provider
|
||||
.Setup(x => x.Get()).ReturnsAsync(response);
|
||||
}
|
||||
|
||||
public class FakeError : Error
|
||||
{
|
||||
public FakeError()
|
||||
: base("meh", OcelotErrorCode.CannotAddDataError)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private void TheRequestIdIsSet(string key, string value)
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(key, value), Times.Once);
|
||||
@ -140,5 +185,10 @@ namespace Ocelot.UnitTests.Errors
|
||||
{
|
||||
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
private void TheRequestIdIsNotSet()
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Memory;
|
||||
using Moq;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
@ -14,38 +16,41 @@ namespace Ocelot.UnitTests.Middleware
|
||||
public class BaseUrlFinderTests
|
||||
{
|
||||
private readonly BaseUrlFinder _baseUrlFinder;
|
||||
private readonly Mock<IWebHostBuilder> _webHostBuilder;
|
||||
private readonly Mock<IConfiguration> _config;
|
||||
|
||||
private string _result;
|
||||
|
||||
public BaseUrlFinderTests()
|
||||
{
|
||||
_webHostBuilder = new Mock<IWebHostBuilder>();
|
||||
_baseUrlFinder = new BaseUrlFinder(_webHostBuilder.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_find_base_url_based_on_webhostbuilder()
|
||||
{
|
||||
this.Given(x => GivenTheWebHostBuilderReturns("http://localhost:7000"))
|
||||
.When(x => WhenIFindTheUrl())
|
||||
.Then(x => ThenTheUrlIs("http://localhost:7000"))
|
||||
.BDDfy();
|
||||
_config = new Mock<IConfiguration>();
|
||||
_baseUrlFinder = new BaseUrlFinder(_config.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_default_base_url()
|
||||
{
|
||||
this.Given(x => GivenTheWebHostBuilderReturns(""))
|
||||
this.Given(x => GivenTheConfigBaseUrlIs(""))
|
||||
.And(x => GivenTheConfigBaseUrlIs(""))
|
||||
.When(x => WhenIFindTheUrl())
|
||||
.Then(x => ThenTheUrlIs("http://localhost:5000"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheWebHostBuilderReturns(string url)
|
||||
[Fact]
|
||||
public void should_use_file_config_base_url()
|
||||
{
|
||||
_webHostBuilder
|
||||
.Setup(x => x.GetSetting(WebHostDefaults.ServerUrlsKey))
|
||||
.Returns(url);
|
||||
this.Given(x => GivenTheConfigBaseUrlIs("http://localhost:7000"))
|
||||
.And(x => GivenTheConfigBaseUrlIs("http://baseurlfromconfig.com:5181"))
|
||||
.When(x => WhenIFindTheUrl())
|
||||
.Then(x => ThenTheUrlIs("http://baseurlfromconfig.com:5181"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheConfigBaseUrlIs(string configValue)
|
||||
{
|
||||
var configSection = new ConfigurationSection(new ConfigurationRoot(new List<IConfigurationProvider>{new MemoryConfigurationProvider(new MemoryConfigurationSource())}), "");
|
||||
configSection.Value = configValue;
|
||||
_config.Setup(x => x.GetSection(It.IsAny<string>())).Returns(configSection);
|
||||
}
|
||||
|
||||
private void WhenIFindTheUrl()
|
||||
|
Reference in New Issue
Block a user