working state

This commit is contained in:
Tom Gardham-Pallister 2016-07-10 15:51:13 +01:00
parent 4f2d94ceba
commit 4ca1dc8846
4 changed files with 67 additions and 56 deletions

View File

@ -0,0 +1,7 @@
namespace Ocelot.Library.Infrastructure.Router.UrlPathMatcher
{
public interface IUrlPathToUrlPathTemplateMatcher
{
bool Match(string urlPath, string urlPathTemplate);
}
}

View File

@ -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 == '{';
}
}
}

View File

@ -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 =>

View File

@ -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 == '{';
}
}
}