diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs b/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs new file mode 100644 index 00000000..5fb89c15 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/IUrlPathToUrlPathTemplateMatcher.cs @@ -0,0 +1,7 @@ +namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher +{ + public interface IUrlPathToUrlPathTemplateMatcher + { + bool Match(string urlPath, string urlPathTemplate); + } +} \ No newline at end of file diff --git a/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs b/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs new file mode 100644 index 00000000..fb543fb3 --- /dev/null +++ b/src/Ocelot.Library/Infrastructure/Router/UrlPathMatcher/UrlPathToUrlPathTemplateMatcher.cs @@ -0,0 +1,54 @@ +namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher +{ + public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher + { + public bool Match(string urlPath, string urlPathTemplate) + { + urlPath = urlPath.ToLower(); + + urlPathTemplate = urlPathTemplate.ToLower(); + + int counterForUrl = 0; + + for (int counterForTemplate = 0; counterForTemplate < urlPathTemplate.Length; counterForTemplate++) + { + if (CharactersDontMatch(urlPathTemplate[counterForTemplate], urlPath[counterForUrl]) && ContinueScanningUrl(counterForUrl,urlPath.Length)) + { + if (IsPlaceholder(urlPathTemplate[counterForTemplate])) + { + counterForTemplate = GetNextCounterPosition(urlPathTemplate, counterForTemplate, '}'); + counterForUrl = GetNextCounterPosition(urlPath, counterForUrl, '/'); + continue; + } + else + { + return false; + } + } + counterForUrl++; + } + return true; + } + + private int GetNextCounterPosition(string urlTemplate, int counterForTemplate, char delimiter) + { + var closingPlaceHolderPositionOnTemplate = urlTemplate.IndexOf(delimiter, counterForTemplate); + return closingPlaceHolderPositionOnTemplate + 1; + } + + private bool CharactersDontMatch(char characterOne, char characterTwo) + { + return characterOne != characterTwo; + } + + private bool ContinueScanningUrl(int counterForUrl, int urlLength) + { + return counterForUrl < urlLength; + } + + private bool IsPlaceholder(char character) + { + return character == '{'; + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Startup.cs b/src/Ocelot/Startup.cs index 990e0cd5..8d284c04 100644 --- a/src/Ocelot/Startup.cs +++ b/src/Ocelot/Startup.cs @@ -32,6 +32,7 @@ namespace Ocelot public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); + loggerFactory.AddDebug(); app.Run(async context => diff --git a/test/Ocelot.UnitTests/UrlMapperTests.cs b/test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs similarity index 61% rename from test/Ocelot.UnitTests/UrlMapperTests.cs rename to test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs index b35d9933..b81bed68 100644 --- a/test/Ocelot.UnitTests/UrlMapperTests.cs +++ b/test/Ocelot.UnitTests/UrlPathToUrlPathTemplateMatcherTests.cs @@ -1,18 +1,19 @@ using System; using Ocelot.Library.Infrastructure.Responses; using Ocelot.Library.Infrastructure.Router.UpstreamRouter; +using Ocelot.Library.Infrastructure.Router.UrlPathMatcher; using Shouldly; using Xunit; namespace Ocelot.UnitTests { - public class UrlMapperTests + public class UrlPathToUrlPathTemplateMatcherTests { - private UrlToUrlTemplateMatcher _urlMapper; + private IUrlPathToUrlPathTemplateMatcher _urlMapper; - public UrlMapperTests() + public UrlPathToUrlPathTemplateMatcherTests() { - _urlMapper = new UrlToUrlTemplateMatcher(); + _urlMapper = new UrlPathToUrlPathTemplateMatcher(); } [Fact] @@ -78,56 +79,4 @@ namespace Ocelot.UnitTests result.ShouldBeTrue(); } } - - public class UrlToUrlTemplateMatcher - { - public bool Match(string url, string urlTemplate) - { - url = url.ToLower(); - - urlTemplate = urlTemplate.ToLower(); - - int counterForUrl = 0; - - for (int counterForTemplate = 0; counterForTemplate < urlTemplate.Length; counterForTemplate++) - { - if (CharactersDontMatch(urlTemplate[counterForTemplate], url[counterForUrl]) && ContinueScanningUrl(counterForUrl,url.Length)) - { - if (IsPlaceholder(urlTemplate[counterForTemplate])) - { - counterForTemplate = GetNextCounterPosition(urlTemplate, counterForTemplate, '}'); - counterForUrl = GetNextCounterPosition(url, counterForUrl, '/'); - continue; - } - else - { - return false; - } - } - counterForUrl++; - } - return true; - } - - private int GetNextCounterPosition(string urlTemplate, int counterForTemplate, char delimiter) - { - var closingPlaceHolderPositionOnTemplate = urlTemplate.IndexOf(delimiter, counterForTemplate); - return closingPlaceHolderPositionOnTemplate + 1; - } - - private bool CharactersDontMatch(char characterOne, char characterTwo) - { - return characterOne != characterTwo; - } - - private bool ContinueScanningUrl(int counterForUrl, int urlLength) - { - return counterForUrl < urlLength; - } - - private bool IsPlaceholder(char character) - { - return character == '{'; - } - } } \ No newline at end of file