From 85889973c820ad405a6713dc850e4510d26e57a2 Mon Sep 17 00:00:00 2001 From: MattMo Date: Wed, 8 Feb 2023 15:14:17 -0800 Subject: [PATCH] Fixed some bugs within the RouteMatcher to better support wild card and catch all matching. Added unit tests. Bumped package version to 1.2.9 --- Rest.Net.Tests/RouteMatcherTests.cs | 12 ++++++++++++ Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/RouteMatcher.cs | 30 +++++++++++++++++------------ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Rest.Net.Tests/RouteMatcherTests.cs b/Rest.Net.Tests/RouteMatcherTests.cs index caf91e4..f99455a 100644 --- a/Rest.Net.Tests/RouteMatcherTests.cs +++ b/Rest.Net.Tests/RouteMatcherTests.cs @@ -22,12 +22,24 @@ namespace MontoyaTech.Rest.Net.Tests RouteMatcher.Matches("http://localhost/test1/test2", "/**", out _).Should().BeTrue(); } + [Fact] + public void SyntaxCatchAllEmptyShouldMatch() + { + RouteMatcher.Matches("http://localhost/test1", "/test1/**", out _).Should().BeTrue(); + } + [Fact] public void SyntaxWithRootWildcardShouldMatch() { RouteMatcher.Matches("http://localhost/test1", "/*", out _).Should().BeTrue(); } + [Fact] + public void SyntaxWildCardEmptyShouldMatch() + { + RouteMatcher.Matches("http://localhost/test1", "/test1/*", out _).Should().BeTrue(); + } + [Fact] public void SyntaxWithRootWildcardShouldNotMatch() { diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 2d6824b..56590f6 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.2.8 + 1.2.9 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RouteMatcher.cs b/Rest.Net/RouteMatcher.cs index 9f5ab98..bda6dce 100644 --- a/Rest.Net/RouteMatcher.cs +++ b/Rest.Net/RouteMatcher.cs @@ -72,19 +72,30 @@ namespace MontoyaTech.Rest.Net int argumentIndex = 0; //Check each segment against the url. - var max = Math.Min(urlSegments.Length, syntaxSegments.Length); + int max = Math.Max(urlSegments.Length, syntaxSegments.Length); for (int i = 0; i < max; i++) { - var syntaxSegment = syntaxSegments[i]; + var syntaxSegment = i >= syntaxSegments.Length ? null : syntaxSegments[i]; - var urlSegment = urlSegments[i]; + var urlSegment = i >= urlSegments.Length ? null : urlSegments[i]; + //If the syntax segment is null, this is not a match. + if (syntaxSegment == null) + { + return false; + } //If the segments syntax is a double wild card then everything after this is a match. - if (syntaxSegment == "**") + else if (syntaxSegment == "**") { return true; } - else + //If we ran out of url segments, and this syntax segment is a wild card, this is okay. + else if (urlSegment == null && syntaxSegment == "*") + { + return true; + } + //If we have a url segment see if it matches against the syntax segment. + else if (urlSegment != null) { var conditions = new List(); var builder = new StringBuilder(); @@ -99,7 +110,7 @@ namespace MontoyaTech.Rest.Net else if (syntaxSegment[c] != ' ') { builder.Append(syntaxSegment[c]); - } + } } if (builder.Length > 0) @@ -145,12 +156,7 @@ namespace MontoyaTech.Rest.Net } } - if (urlSegments.Length > syntaxSegments.Length) - return false; - else if (syntaxSegments.Length > urlSegments.Length) - return false; - else - return true; + return true; } } }