diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 74da40e54..3ff2defcf 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -103,8 +103,8 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); - _services.TryAddSingleton(); - _services.TryAddSingleton(); + _services.AddSingleton(); + _services.AddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); _services.TryAddSingleton(); diff --git a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteProvider.cs b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs similarity index 92% rename from src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteProvider.cs rename to src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs index a1cd50fc3..d7713aeee 100644 --- a/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteProvider.cs +++ b/src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteFinder.cs @@ -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; diff --git a/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteProvider.cs b/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteProvider.cs index b32a2d9c0..7ba519af5 100644 --- a/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteProvider.cs +++ b/src/Ocelot/DownstreamRouteFinder/Finder/IDownstreamRouteProvider.cs @@ -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 { @@ -17,7 +19,41 @@ public class DownstreamRouteCreator : IDownstreamRouteProvider { public Response 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(){ upstreamHttpMethod }) + .Build(); + + return new OkResponse(new DownstreamRoute(new List(), 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; } } @@ -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) diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs new file mode 100644 index 000000000..b5d28ea7e --- /dev/null +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteCreatorTests.cs @@ -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"); + } + } +} diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs similarity index 99% rename from test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderTests.cs rename to test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs index b6ee0628e..65324003e 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderTests.cs @@ -13,7 +13,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder { - public class DownstreamRouteProviderTests + public class DownstreamRouteFinderTests { private readonly IDownstreamRouteProvider _downstreamRouteFinder; private readonly Mock _mockMatcher; @@ -26,11 +26,11 @@ public class DownstreamRouteProviderTests private string _upstreamHttpMethod; private string _upstreamHost; - public DownstreamRouteProviderTests() + public DownstreamRouteFinderTests() { _mockMatcher = new Mock(); _finder = new Mock(); - _downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteProvider(_mockMatcher.Object, _finder.Object); + _downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder(_mockMatcher.Object, _finder.Object); } [Fact] diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs index bbd6e8cda..1efa70ec5 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteProviderFactoryTests.cs @@ -23,7 +23,7 @@ public DownstreamRouteProviderFactoryTests() var services = new ServiceCollection(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); var provider = services.BuildServiceProvider(); _factory = new DownstreamRouteProviderFactory(provider); @@ -40,7 +40,7 @@ public void should_return_downstream_route_finder() }; IInternalConfiguration config = new InternalConfiguration(reRoutes, "", null, ""); var result = _factory.Get(config); - result.ShouldBeOfType(); + result.ShouldBeOfType(); } [Fact] @@ -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(); + result.ShouldBeOfType(); } [Fact]