Improved RouteMatcher and added more unit tests to cover some edge cases. Cleaned up code. Bumped package version to 1.7.0
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
|
||||
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<Version>1.6.9</Version>
|
||||
<Version>1.7.0</Version>
|
||||
<PackageReleaseNotes></PackageReleaseNotes>
|
||||
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
|
||||
</PropertyGroup>
|
||||
|
@@ -49,9 +49,9 @@ namespace MontoyaTech.Rest.Net
|
||||
if (target == null)
|
||||
throw new ArgumentNullException(nameof(target));
|
||||
else if (string.IsNullOrWhiteSpace(method))
|
||||
throw new ArgumentException("Cannot be null or empty", nameof(method));
|
||||
throw new ArgumentException("Method cannot be null or empty", nameof(method));
|
||||
else if (string.IsNullOrWhiteSpace(syntax))
|
||||
throw new ArgumentException("Cannot be null or empty", nameof(syntax));
|
||||
throw new ArgumentException("Syntax cannot be null or empty", nameof(syntax));
|
||||
|
||||
this.Method = method;
|
||||
this.Syntax = syntax;
|
||||
|
@@ -34,6 +34,10 @@ namespace MontoyaTech.Rest.Net
|
||||
//Set the arguments to null, initially.
|
||||
arguments = null;
|
||||
|
||||
//If the input url is invalid, default to false.
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
return false;
|
||||
|
||||
//Trim the url and the syntax.
|
||||
url = url.Trim();
|
||||
syntax = syntax.Trim();
|
||||
@@ -42,17 +46,27 @@ namespace MontoyaTech.Rest.Net
|
||||
if (url.StartsWith("http://"))
|
||||
{
|
||||
url = url.Substring(7);
|
||||
url = url.Substring(url.IndexOf('/'));
|
||||
|
||||
//If there is a slash, skip to the first one, or default to empty path.
|
||||
if (url.Contains('/'))
|
||||
url = url.Substring(url.IndexOf('/'));
|
||||
else
|
||||
url = "/";
|
||||
}
|
||||
else if (url.StartsWith("https://"))
|
||||
{
|
||||
url = url.Substring(8);
|
||||
url = url.Substring(url.IndexOf('/'));
|
||||
|
||||
//If there is a slash, skip to the first one, or default to empty path.
|
||||
if (url.Contains('/'))
|
||||
url = url.Substring(url.IndexOf('/'));
|
||||
else
|
||||
url = "/";
|
||||
}
|
||||
|
||||
//Split the url and the syntax into path segments
|
||||
var urlSegments = url.Split('/').Where(segment => segment.Length > 0).Select(segment => segment.Trim()).ToArray();
|
||||
var syntaxSegments = syntax.Split('/').Where(segment => segment.Length > 0).Select(segment => segment.Trim()).ToArray();
|
||||
var urlSegments = url.Split('/').Select(segment => segment.Trim()).ToArray();
|
||||
var syntaxSegments = syntax.Split('/').Select(segment => segment.Trim()).ToArray();
|
||||
|
||||
//If we have no url segments, and we have no syntax segments, this is a root match which is fine.
|
||||
if (urlSegments.Length == 0 && syntaxSegments.Length == 0)
|
||||
@@ -85,20 +99,25 @@ namespace MontoyaTech.Rest.Net
|
||||
return false;
|
||||
}
|
||||
//If the segments syntax is a double wild card then everything after this is a match.
|
||||
else if (syntaxSegment == "**")
|
||||
else if (syntaxSegment == "**" && urlSegment != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
//If we ran out of url segments, and this syntax segment is a wild card, this is okay.
|
||||
else if (urlSegment == null && syntaxSegment == "*")
|
||||
//If we ran out of url segments, and this syntax segment is a wild card, this is not a match.
|
||||
else if (syntaxSegment == "*" && urlSegment != null)
|
||||
{
|
||||
return true;
|
||||
//Allowed
|
||||
}
|
||||
//If we have a syntaxSegment but no urlSegment, this can't be a match.
|
||||
else if (urlSegment == null && syntaxSegment != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//If both the url segment and syntax segment are empty then this is allowed
|
||||
else if (urlSegment != null && urlSegment.Length == 0 && syntaxSegment.Length == 0)
|
||||
{
|
||||
//Allowed
|
||||
}
|
||||
//If we have a url segment see if it matches against the syntax segment.
|
||||
else if (urlSegment != null)
|
||||
{
|
||||
|
Reference in New Issue
Block a user