Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Ocelot/DependencyInjection/OcelotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
Services.TryAddSingleton<IDownstreamPathPlaceholderReplacer, DownstreamPathPlaceholderReplacer>();
Services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteFinder.Finder.DownstreamRouteFinder>();
Services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteCreator>();
Services.TryAddSingleton<IDownstreamServiceFinder, DownstreamServiceFinder>();
Services.TryAddSingleton<IDownstreamRouteProviderFactory, DownstreamRouteProviderFactory>();
Services.TryAddSingleton<IHttpResponder, HttpContextResponder>();
Services.TryAddSingleton<IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
Expand Down
21 changes: 5 additions & 16 deletions src/Ocelot/DownstreamRouteFinder/Finder/DownstreamRouteCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
using Ocelot.DownstreamRouteFinder.UrlMatcher;
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Responses;

namespace Ocelot.DownstreamRouteFinder.Finder
{
public class DownstreamRouteCreator : IDownstreamRouteProvider
{
private readonly IQoSOptionsCreator _qoSOptionsCreator;
private readonly IDownstreamServiceFinder _serviceFinder;
private readonly ConcurrentDictionary<string, OkResponse<DownstreamRouteHolder>> _cache;

public DownstreamRouteCreator(IQoSOptionsCreator qoSOptionsCreator)
public DownstreamRouteCreator(IQoSOptionsCreator qoSOptionsCreator, IDownstreamServiceFinder serviceFinder)
{
_qoSOptionsCreator = qoSOptionsCreator;
_serviceFinder = serviceFinder;
_cache = new ConcurrentDictionary<string, OkResponse<DownstreamRouteHolder>>();
}

public Response<DownstreamRouteHolder> Get(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod,
IInternalConfiguration configuration, string upstreamHost, IDictionary<string, string> upstreamHeaders)
{
var serviceName = GetServiceName(upstreamUrlPath);
var serviceName = _serviceFinder.GetServiceName(upstreamUrlPath, upstreamQueryString, upstreamHttpMethod, upstreamHost, configuration);

var downstreamPath = GetDownstreamPath(upstreamUrlPath);

Expand Down Expand Up @@ -102,19 +104,6 @@ private static string GetDownstreamPath(string upstreamUrlPath)
.Substring(upstreamUrlPath.IndexOf('/', 1));
}

private static string GetServiceName(string upstreamUrlPath)
{
if (upstreamUrlPath.IndexOf('/', 1) == -1)
{
return upstreamUrlPath
.Substring(1);
}

return upstreamUrlPath
.Substring(1, upstreamUrlPath.IndexOf('/', 1))
.TrimEnd('/');
}

private static string CreateLoadBalancerKey(string downstreamTemplatePath, string httpMethod, LoadBalancerOptions loadBalancerOptions)
{
if (!string.IsNullOrEmpty(loadBalancerOptions.Type) && !string.IsNullOrEmpty(loadBalancerOptions.Key) && loadBalancerOptions.Type == nameof(CookieStickySessions))
Expand Down
20 changes: 20 additions & 0 deletions src/Ocelot/DownstreamRouteFinder/Finder/DownstreamServiceFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Ocelot.Configuration;

namespace Ocelot.DownstreamRouteFinder.Finder
{
public class DownstreamServiceFinder: IDownstreamServiceFinder
{
public string GetServiceName(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, string upstreamHost, IInternalConfiguration configuration)
{
if (upstreamUrlPath.IndexOf('/', 1) == -1)
{
return upstreamUrlPath
.Substring(1);
}

return upstreamUrlPath
.Substring(1, upstreamUrlPath.IndexOf('/', 1))
.TrimEnd('/');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Ocelot.Configuration;

namespace Ocelot.DownstreamRouteFinder.Finder
{
public interface IDownstreamServiceFinder
{
string GetServiceName(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, string upstreamHost, IInternalConfiguration configuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DownstreamRouteCreatorTests()
_qosOptionsCreator
.Setup(x => x.Create(It.IsAny<QoSOptions>(), It.IsAny<string>(), It.IsAny<List<string>>()))
.Returns(_qoSOptions);
_creator = new DownstreamRouteCreator(_qosOptionsCreator.Object);
_creator = new DownstreamRouteCreator(_qosOptionsCreator.Object, new DownstreamServiceFinder());
_upstreamQuery = string.Empty;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public DownstreamRouteProviderFactoryTests()
services.AddSingleton<IQoSOptionsCreator, QoSOptionsCreator>();
services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteFinder>();
services.AddSingleton<IDownstreamRouteProvider, DownstreamRouteCreator>();
services.AddSingleton<IDownstreamServiceFinder, DownstreamServiceFinder>();
var provider = services.BuildServiceProvider(true);
_logger = new Mock<IOcelotLogger>();
_loggerFactory = new Mock<IOcelotLoggerFactory>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Ocelot.DownstreamRouteFinder.Finder;
using Xunit;

namespace Ocelot.UnitTests.DownstreamRouteFinder
{
public class DownstreamServiceFinderTests
{
private readonly DownstreamServiceFinder _serviceFinder;

public DownstreamServiceFinderTests()
{
_serviceFinder = new DownstreamServiceFinder();
}

[Fact]
public void should_return_empty_when_without_path()
{
var serviceName = _serviceFinder.GetServiceName("/", null, null, null, null);
Assert.Equal("", serviceName);
}

[Fact]
public void should_return_node_name_when_single_node()
{
var serviceName = _serviceFinder.GetServiceName("/service-name", null, null, null, null);
Assert.Equal("service-name", serviceName);
}

[Fact]
public void should_return_first_node_name_when_multiple_nodes()
{
var serviceName = _serviceFinder.GetServiceName("/service-name/some/longer/path", null, null, null, null);
Assert.Equal("service-name", serviceName);
}
}
}