Skip to content

Commit

Permalink
#340 working towards supporting dynamic routing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Gardham-Pallister committed May 10, 2018
1 parent ed695e3 commit f564917
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Ocelot/DependencyInjection/OcelotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
_services.TryAddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
_services.TryAddSingleton<IPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
_services.TryAddSingleton<IDownstreamPathPlaceholderReplacer, DownstreamTemplatePathPlaceholderReplacer>();
_services.TryAddSingleton<IDownstreamRouteProvider, DownstreamRouteProvider>();
_services.TryAddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
_services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteFinder>();
_services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
_services.TryAddSingleton<IDownstreamRouteProviderFactory, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProviderFactory>();
_services.TryAddSingleton<IHttpRequester, HttpClientHttpRequester>();
_services.TryAddSingleton<IHttpResponder, HttpContextResponder>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace Ocelot.DownstreamRouteFinder.Finder
{
public class DownstreamRouteProvider : IDownstreamRouteProvider
public class DownstreamRouteFinder : IDownstreamRouteProvider
{
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
private readonly IPlaceholderNameAndValueFinder _placeholderNameAndValueFinder;

public DownstreamRouteProvider(IUrlPathToUrlTemplateMatcher urlMatcher, IPlaceholderNameAndValueFinder urlPathPlaceholderNameAndValueFinder)
public DownstreamRouteFinder(IUrlPathToUrlTemplateMatcher urlMatcher, IPlaceholderNameAndValueFinder urlPathPlaceholderNameAndValueFinder)
{
_urlMatcher = urlMatcher;
_placeholderNameAndValueFinder = urlPathPlaceholderNameAndValueFinder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
using Ocelot.Configuration.Builder;
using Ocelot.DownstreamRouteFinder.UrlMatcher;

namespace Ocelot.DownstreamRouteFinder.Finder
{
Expand All @@ -17,7 +19,41 @@ public class DownstreamRouteCreator : IDownstreamRouteProvider
{
public Response<DownstreamRoute> Get(string upstreamUrlPath, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost)
{
throw new NotImplementedException();
var serviceName = upstreamUrlPath
.Substring(1, upstreamUrlPath.IndexOf('/', 1))
.TrimEnd('/');

var downstreamPath = upstreamUrlPath
.Substring(upstreamUrlPath.IndexOf('/', 1));

if(downstreamPath.Contains("?"))
{
downstreamPath = downstreamPath
.Substring(0, downstreamPath.IndexOf('?'));
}

var key = CreateReRouteKey(upstreamUrlPath, upstreamHttpMethod);

var downstreamReRoute = new DownstreamReRouteBuilder()
.WithServiceName(serviceName)
.WithReRouteKey(key)
.WithDownstreamPathTemplate(downstreamPath)
.Build();

var reRoute = new ReRouteBuilder()
.WithDownstreamReRoute(downstreamReRoute)
.WithUpstreamHttpMethod(new List<string>(){ upstreamHttpMethod })
.Build();

return new OkResponse<DownstreamRoute>(new DownstreamRoute(new List<PlaceholderNameAndValue>(), reRoute));

}

private string CreateReRouteKey(string downstreamTemplatePath, string httpMethod)
{
//note - not sure if this is the correct key, but this is probably the only unique key i can think of given my poor brain
var loadBalancerKey = $"{downstreamTemplatePath}|{httpMethod}";
return loadBalancerKey;
}
}

Expand All @@ -42,7 +78,7 @@ public IDownstreamRouteProvider Get(IInternalConfiguration config)
return _providers[nameof(DownstreamRouteCreator)];
}

return _providers[nameof(DownstreamRouteProvider)];
return _providers[nameof(DownstreamRouteFinder)];
}

private bool IsServiceDiscovery(ServiceProviderConfiguration config)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Ocelot.DownstreamRouteFinder.Finder;
using Xunit;
using Shouldly;
using Ocelot.Configuration;
using System.Net.Http;

namespace Ocelot.UnitTests.DownstreamRouteFinder
{
public class DownstreamRouteCreatorTests
{
private DownstreamRouteCreator _creator;

public DownstreamRouteCreatorTests()
{
_creator = new DownstreamRouteCreator();
}

[Fact]
public void should_create_downstream_route()
{
var upstreamUrlPath = "/auth/test";
var upstreamHttpMethod = "GET";
IInternalConfiguration configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter");
var upstreamHost = "doesnt matter";
var result = _creator.Get(upstreamUrlPath, upstreamHttpMethod, configuration, upstreamHost);
result.Data.ReRoute.DownstreamReRoute[0].DownstreamPathTemplate.Value.ShouldBe("/test");
result.Data.ReRoute.UpstreamHttpMethod[0].ShouldBe(HttpMethod.Get);
result.Data.ReRoute.DownstreamReRoute[0].ServiceName.ShouldBe("auth");
result.Data.ReRoute.DownstreamReRoute[0].LoadBalancerKey.ShouldBe("/auth/test|GET");
}

[Fact]
public void should_create_downstream_route_and_remove_query_string()
{
var upstreamUrlPath = "/auth/test?test=1&best=2";
var upstreamHttpMethod = "GET";
IInternalConfiguration configuration = new InternalConfiguration(null, "doesnt matter", null, "doesnt matter");
var upstreamHost = "doesnt matter";
var result = _creator.Get(upstreamUrlPath, upstreamHttpMethod, configuration, upstreamHost);
result.Data.ReRoute.DownstreamReRoute[0].DownstreamPathTemplate.Value.ShouldBe("/test");
result.Data.ReRoute.UpstreamHttpMethod[0].ShouldBe(HttpMethod.Get);
result.Data.ReRoute.DownstreamReRoute[0].ServiceName.ShouldBe("auth");
result.Data.ReRoute.DownstreamReRoute[0].LoadBalancerKey.ShouldBe("/auth/test?test=1&best=2|GET");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Ocelot.UnitTests.DownstreamRouteFinder
{
public class DownstreamRouteProviderTests
public class DownstreamRouteFinderTests
{
private readonly IDownstreamRouteProvider _downstreamRouteFinder;
private readonly Mock<IUrlPathToUrlTemplateMatcher> _mockMatcher;
Expand All @@ -26,11 +26,11 @@ public class DownstreamRouteProviderTests
private string _upstreamHttpMethod;
private string _upstreamHost;

public DownstreamRouteProviderTests()
public DownstreamRouteFinderTests()
{
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
_finder = new Mock<IPlaceholderNameAndValueFinder>();
_downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider(_mockMatcher.Object, _finder.Object);
_downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder(_mockMatcher.Object, _finder.Object);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public DownstreamRouteProviderFactoryTests()
var services = new ServiceCollection();
services.AddSingleton<IPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider>();
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
services.AddSingleton<IDownstreamRouteProvider, Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteCreator>();
var provider = services.BuildServiceProvider();
_factory = new DownstreamRouteProviderFactory(provider);
Expand All @@ -40,7 +40,7 @@ public void should_return_downstream_route_finder()
};
IInternalConfiguration config = new InternalConfiguration(reRoutes, "", null, "");
var result = _factory.Get(config);
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider>();
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
}

[Fact]
Expand All @@ -52,7 +52,7 @@ public void should_return_downstream_route_finder_as_no_service_discovery()
};
IInternalConfiguration config = new InternalConfiguration(reRoutes, "", spConfig, "");
var result = _factory.Get(config);
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider>();
result.ShouldBeOfType<Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
}

[Fact]
Expand Down

0 comments on commit f564917

Please sign in to comment.