Added unit test project. Improved route matcher and fixed bugs.
This commit is contained in:
parent
16c42c1ef7
commit
da8184f152
25
Rest.Net.Tests/Rest.Net.Tests.sln
Normal file
25
Rest.Net.Tests/Rest.Net.Tests.sln
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.32112.339
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rest.Net.Tests", "Rest.Net.Tests\Rest.Net.Tests.csproj", "{729431C7-DA13-4668-9D6A-C785C5AB4064}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{729431C7-DA13-4668-9D6A-C785C5AB4064}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{729431C7-DA13-4668-9D6A-C785C5AB4064}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{729431C7-DA13-4668-9D6A-C785C5AB4064}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{729431C7-DA13-4668-9D6A-C785C5AB4064}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {0DB589F0-19C2-46AC-9CAB-903F66E22B52}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
30
Rest.Net.Tests/Rest.Net.Tests/Rest.Net.Tests.csproj
Normal file
30
Rest.Net.Tests/Rest.Net.Tests/Rest.Net.Tests.csproj
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
|
||||||
|
<AssemblyName>MontoyaTech.Rest.Net.Tests</AssemblyName>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentAssertions" Version="6.5.1" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.1.0">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Rest.Net\Rest.Net\Rest.Net.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
104
Rest.Net.Tests/Rest.Net.Tests/RouteMatcherTests.cs
Normal file
104
Rest.Net.Tests/Rest.Net.Tests/RouteMatcherTests.cs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace MontoyaTech.Rest.Net.Tests
|
||||||
|
{
|
||||||
|
public class RouteMatcherTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithRootShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/", "/", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithRootCatchAllShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test1/test2", "/**", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithRootWildcardShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test1", "/*", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithRootWildcardShouldNotMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test1/test2", "/*", out _).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithRootNotShouldNotMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test1", "/!test1", out _).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithRootNotShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test2", "/!test1", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithArgumentShouldNotMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test1/test2", "/{a}", out _).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithArgumentShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/test1", "/{a}", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithWildcardShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/1/2/3", "/1/*/3", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithOrShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/a/b", "/a/b|c", out _).Should().BeTrue();
|
||||||
|
RouteMatcher.Matches("http://localhost/a/c", "/a/b|c", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithOrShouldNotMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/a/d", "/a/b|c", out _).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithNotAndShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/a/d", "/a/!b&!c", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithNotAndShouldNotMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/a/b", "/a/!b&!c", out _).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithSegmentShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/a", "/a", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void SyntaxWithMultipleSegmentsShouldMatch()
|
||||||
|
{
|
||||||
|
RouteMatcher.Matches("http://localhost/a/b/c", "/a/b/c", out _).Should().BeTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rest.Net", "Rest.Net\Rest.N
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rest.Net.Example", "Rest.Net.Example\Rest.Net.Example\Rest.Net.Example.csproj", "{D476199D-526A-4831-866F-790676F8BC37}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rest.Net.Example", "Rest.Net.Example\Rest.Net.Example\Rest.Net.Example.csproj", "{D476199D-526A-4831-866F-790676F8BC37}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rest.Net.Tests", "Rest.Net.Tests\Rest.Net.Tests\Rest.Net.Tests.csproj", "{CBA3A43C-3987-433D-9924-3486E9782E2F}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -21,6 +23,10 @@ Global
|
|||||||
{D476199D-526A-4831-866F-790676F8BC37}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{D476199D-526A-4831-866F-790676F8BC37}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{D476199D-526A-4831-866F-790676F8BC37}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{D476199D-526A-4831-866F-790676F8BC37}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{D476199D-526A-4831-866F-790676F8BC37}.Release|Any CPU.Build.0 = Release|Any CPU
|
{D476199D-526A-4831-866F-790676F8BC37}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{CBA3A43C-3987-433D-9924-3486E9782E2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{CBA3A43C-3987-433D-9924-3486E9782E2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{CBA3A43C-3987-433D-9924-3486E9782E2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{CBA3A43C-3987-433D-9924-3486E9782E2F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -17,8 +17,8 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// ** = anything from this point forward is accepted
|
/// ** = anything from this point forward is accepted
|
||||||
/// {} = route parameter
|
/// {} = route parameter
|
||||||
/// ! = must not match
|
/// ! = must not match
|
||||||
/// || = logical or
|
/// | = logical or
|
||||||
/// && = logical and
|
/// & = logical and
|
||||||
///
|
///
|
||||||
/// Note:
|
/// Note:
|
||||||
/// If route parameter doesn't end with /, then the parameter will be the rest of the url.
|
/// If route parameter doesn't end with /, then the parameter will be the rest of the url.
|
||||||
@ -37,6 +37,18 @@ namespace MontoyaTech.Rest.Net
|
|||||||
url = url.Trim();
|
url = url.Trim();
|
||||||
syntax = syntax.Trim();
|
syntax = syntax.Trim();
|
||||||
|
|
||||||
|
//If the url starts with http or https remove that so we just have the path left.
|
||||||
|
if (url.StartsWith("http://"))
|
||||||
|
{
|
||||||
|
url = url.Substring(7);
|
||||||
|
url = url.Substring(url.IndexOf('/'));
|
||||||
|
}
|
||||||
|
else if (url.StartsWith("https://"))
|
||||||
|
{
|
||||||
|
url = url.Substring(8);
|
||||||
|
url = url.Substring(url.IndexOf('/'));
|
||||||
|
}
|
||||||
|
|
||||||
//Split the url and the syntax into path segments
|
//Split the url and the syntax into path segments
|
||||||
var urlSegments = url.Split('/').Where(segment => segment.Length > 0).Select(segment => segment.Trim()).ToArray();
|
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 syntaxSegments = syntax.Split('/').Where(segment => segment.Length > 0).Select(segment => segment.Trim()).ToArray();
|
||||||
@ -47,10 +59,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
//If we have no syntax segments this is not a match.
|
//If we have no syntax segments this is not a match.
|
||||||
else if (syntaxSegments.Length == 0)
|
else if (syntaxSegments.Length == 0)
|
||||||
return false;
|
return false;
|
||||||
|
//If the url has no segments but the syntax is a double wild card then this is a match.
|
||||||
//If we have segments and the url does not then this may not be a match.
|
else if (urlSegments.Length == 0 && syntaxSegments[0] == "**")
|
||||||
if (urlSegments.Length == 0 && syntaxSegments[0] == "**")
|
|
||||||
return true;
|
return true;
|
||||||
|
//If the url has no segments but the syntax is a wildcard then this is a match.
|
||||||
else if (urlSegments.Length == 0 && syntaxSegments.Length == 1 && syntaxSegments[0] == "*")
|
else if (urlSegments.Length == 0 && syntaxSegments.Length == 1 && syntaxSegments[0] == "*")
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -64,26 +76,71 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
var syntaxSegment = syntaxSegments[i];
|
var syntaxSegment = syntaxSegments[i];
|
||||||
|
|
||||||
|
var urlSegment = urlSegments[i];
|
||||||
|
|
||||||
|
//If the segments syntax is a double wild card then everything after this is a match.
|
||||||
if (syntaxSegment == "**")
|
if (syntaxSegment == "**")
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (syntaxSegment.StartsWith("{") && syntaxSegment.EndsWith("}") && i + 1 >= syntaxSegments.Length && syntax.EndsWith("/") == false)
|
else
|
||||||
{
|
{
|
||||||
//Special case, syntax ends with a parameter, so recombine the rest of the url and add it as a parameter.
|
var conditions = new List<string>();
|
||||||
var key = syntaxSegment.Substring(1, syntaxSegment.Length - 2);
|
|
||||||
var builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
|
for (int c = 0; c < syntaxSegment.Length; c++)
|
||||||
|
{
|
||||||
|
if (syntaxSegment[c] == '|' || syntaxSegment[c] == '&')
|
||||||
|
{
|
||||||
|
conditions.Add(builder.ToString());
|
||||||
|
conditions.Add(syntaxSegment[c].ToString());
|
||||||
|
builder.Clear();
|
||||||
|
}
|
||||||
|
else if (syntaxSegment[c] != ' ')
|
||||||
|
{
|
||||||
|
builder.Append(syntaxSegment[c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (int i2 = i; i2 < urlSegments.Length; i2++)
|
if (builder.Length > 0)
|
||||||
builder.Append(urlSegments[i2]).Append(i2 + 1 < urlSegments.Length ? "/" : "");
|
conditions.Add(builder.ToString());
|
||||||
|
|
||||||
arguments[argumentIndex++] = builder.ToString();
|
bool match = false;
|
||||||
|
foreach (var condition in conditions)
|
||||||
|
{
|
||||||
|
if (condition == "*")
|
||||||
|
{
|
||||||
|
match = true;
|
||||||
|
}
|
||||||
|
else if (condition == "**")
|
||||||
|
{
|
||||||
|
match = true;
|
||||||
|
}
|
||||||
|
else if (condition.StartsWith("!") && condition.Substring(1) != urlSegment)
|
||||||
|
{
|
||||||
|
match = true;
|
||||||
|
}
|
||||||
|
else if (condition.StartsWith("{") && condition.EndsWith("}"))
|
||||||
|
{
|
||||||
|
arguments[argumentIndex++] = urlSegment;
|
||||||
|
|
||||||
return true;
|
match = true;
|
||||||
}
|
}
|
||||||
else if (SegmentMatches(urlSegments[i], syntaxSegment, arguments, ref argumentIndex) == false)
|
else if (condition == "&" && !match)
|
||||||
{
|
{
|
||||||
return false;
|
break;
|
||||||
|
}
|
||||||
|
else if (condition == "|" && match)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (condition == urlSegment)
|
||||||
|
{
|
||||||
|
match = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!match)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,58 +151,5 @@ namespace MontoyaTech.Rest.Net
|
|||||||
else
|
else
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool SegmentMatches(string segment, string syntax, string[] arguments, ref int argumentIndex)
|
|
||||||
{
|
|
||||||
//Split the syntax into conditions
|
|
||||||
string[] conditions = syntax.Split(' ').Where(condition => condition.Length > 0).ToArray();
|
|
||||||
|
|
||||||
//Based off the matches, see if the segment matches.
|
|
||||||
bool match = false;
|
|
||||||
for (int i = 0; i < conditions.Length; i++)
|
|
||||||
{
|
|
||||||
var condition = conditions[i];
|
|
||||||
|
|
||||||
if (condition == "*")
|
|
||||||
{
|
|
||||||
match = true;
|
|
||||||
}
|
|
||||||
else if (condition == "**")
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (condition.StartsWith("!"))
|
|
||||||
{
|
|
||||||
if (condition.Substring(1) == segment)
|
|
||||||
match = false;
|
|
||||||
else
|
|
||||||
match = true;
|
|
||||||
}
|
|
||||||
else if (condition.StartsWith("{") && condition.EndsWith("}"))
|
|
||||||
{
|
|
||||||
var key = condition.Substring(1, condition.Length - 2);
|
|
||||||
|
|
||||||
arguments[argumentIndex++] = segment;
|
|
||||||
|
|
||||||
match = true;
|
|
||||||
}
|
|
||||||
else if (condition == "&&")
|
|
||||||
{
|
|
||||||
if (match == false)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else if (condition == "||")
|
|
||||||
{
|
|
||||||
if (match)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (condition == segment)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return match;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user