Added base url finder for when nothing set in Program.cs

This commit is contained in:
TomPallister 2017-02-25 17:02:36 +00:00
parent 07c334cc98
commit be24f9a9ca
5 changed files with 97 additions and 6 deletions

View File

@ -5,8 +5,6 @@ using System.Net.Http;
using CacheManager.Core; using CacheManager.Core;
using IdentityServer4.AccessTokenValidation; using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Models; using IdentityServer4.Models;
using IdentityServer4.Test;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -32,12 +30,14 @@ using Ocelot.Infrastructure.Claims.Parser;
using Ocelot.Infrastructure.RequestData; using Ocelot.Infrastructure.RequestData;
using Ocelot.LoadBalancer.LoadBalancers; using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Logging; using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.QueryStrings; using Ocelot.QueryStrings;
using Ocelot.Request.Builder; using Ocelot.Request.Builder;
using Ocelot.Requester; using Ocelot.Requester;
using Ocelot.Requester.QoS; using Ocelot.Requester.QoS;
using Ocelot.Responder; using Ocelot.Responder;
using Ocelot.ServiceDiscovery; using Ocelot.ServiceDiscovery;
using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider;
namespace Ocelot.DependencyInjection namespace Ocelot.DependencyInjection
{ {
@ -59,6 +59,7 @@ namespace Ocelot.DependencyInjection
services.AddSingleton<IOcelotConfigurationCreator, FileOcelotConfigurationCreator>(); services.AddSingleton<IOcelotConfigurationCreator, FileOcelotConfigurationCreator>();
services.AddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>(); services.AddSingleton<IOcelotConfigurationRepository, InMemoryOcelotConfigurationRepository>();
services.AddSingleton<IConfigurationValidator, FileConfigurationValidator>(); services.AddSingleton<IConfigurationValidator, FileConfigurationValidator>();
services.AddSingleton<IBaseUrlFinder, BaseUrlFinder>();
var identityServerConfiguration = GetIdentityServerConfiguration(); var identityServerConfiguration = GetIdentityServerConfiguration();
@ -107,7 +108,7 @@ namespace Ocelot.DependencyInjection
services.AddLogging(); services.AddLogging();
services.AddSingleton<IFileConfigurationRepository, FileConfigurationRepository>(); services.AddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
services.AddSingleton<IFileConfigurationSetter, FileConfigurationSetter>(); services.AddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();
services.AddSingleton<Configuration.Provider.IFileConfigurationProvider, Configuration.Provider.FileConfigurationProvider>(); services.AddSingleton<IFileConfigurationProvider, FileConfigurationProvider>();
services.AddSingleton<IQosProviderHouse, QosProviderHouse>(); services.AddSingleton<IQosProviderHouse, QosProviderHouse>();
services.AddSingleton<IQoSProviderFactory, QoSProviderFactory>(); services.AddSingleton<IQoSProviderFactory, QoSProviderFactory>();
services.AddSingleton<IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>(); services.AddSingleton<IServiceDiscoveryProviderFactory, ServiceDiscoveryProviderFactory>();

View File

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Hosting;
namespace Ocelot.Middleware
{
public class BaseUrlFinder : IBaseUrlFinder
{
private readonly IWebHostBuilder _webHostBuilder;
public BaseUrlFinder(IWebHostBuilder webHostBuilder)
{
_webHostBuilder = webHostBuilder;
}
public string Find()
{
var baseSchemeUrlAndPort = _webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey);
return string.IsNullOrEmpty(baseSchemeUrlAndPort) ? "http://localhost:5000" : baseSchemeUrlAndPort;
}
}
}

View File

@ -0,0 +1,7 @@
namespace Ocelot.Middleware
{
public interface IBaseUrlFinder
{
string Find();
}
}

View File

@ -162,9 +162,9 @@ namespace Ocelot.Middleware
if(!string.IsNullOrEmpty(configuration.AdministrationPath) && identityServerConfiguration != null) if(!string.IsNullOrEmpty(configuration.AdministrationPath) && identityServerConfiguration != null)
{ {
var webHostBuilder = (IWebHostBuilder)builder.ApplicationServices.GetService(typeof(IWebHostBuilder)); var urlFinder = (IBaseUrlFinder)builder.ApplicationServices.GetService(typeof(IBaseUrlFinder));
var baseSchemeUrlAndPort = webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey); var baseSchemeUrlAndPort = urlFinder.Find();
builder.Map(configuration.AdministrationPath, app => builder.Map(configuration.AdministrationPath, app =>
{ {
@ -186,6 +186,7 @@ namespace Ocelot.Middleware
}); });
} }
} }
private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware) private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
{ {
if (middleware != null) if (middleware != null)

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Moq;
using Ocelot.Middleware;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Middleware
{
public class BaseUrlFinderTests
{
private readonly BaseUrlFinder _baseUrlFinder;
private readonly Mock<IWebHostBuilder> _webHostBuilder;
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();
}
[Fact]
public void should_use_default_base_url()
{
this.Given(x => GivenTheWebHostBuilderReturns(""))
.When(x => WhenIFindTheUrl())
.Then(x => ThenTheUrlIs("http://localhost:5000"))
.BDDfy();
}
private void GivenTheWebHostBuilderReturns(string url)
{
_webHostBuilder
.Setup(x => x.GetSetting(WebHostDefaults.ServerUrlsKey))
.Returns(url);
}
private void WhenIFindTheUrl()
{
_result = _baseUrlFinder.Find();
}
private void ThenTheUrlIs(string expected)
{
_result.ShouldBe(expected);
}
}
}