-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from maxnatamo/feat/custom-route-parsing
feat/custom-route-parsing
- Loading branch information
Showing
29 changed files
with
1,272 additions
and
108 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
Paracord.Core.UnitTests/Controller/ControllerRouteMatch/MatchTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Paracord.Core.Controller; | ||
using Paracord.Core.Http; | ||
|
||
using HttpMethod = Paracord.Shared.Models.Http.HttpMethod; | ||
|
||
namespace Paracord.Core.UnitTests.Controller.ControllerRouteTests | ||
{ | ||
public class MatchTests | ||
{ | ||
private HttpRequest MakeRequest(string path, HttpMethod method = HttpMethod.GET) | ||
=> new HttpRequest | ||
{ | ||
Target = new HttpTarget | ||
{ | ||
PathSegments = new string[] { path } | ||
}, | ||
Method = method | ||
}; | ||
|
||
[Fact] | ||
public void MatchReturnsNonSuccessfulMatchGivenEmptyStringWithConstantRoute() | ||
{ | ||
// Arrange | ||
HttpRequest request = this.MakeRequest(string.Empty); | ||
ControllerRoute route = ControllerRoute.Parse("/", "index"); | ||
|
||
// Act | ||
ControllerRouteMatch match = route.Match(request); | ||
|
||
// Assert | ||
match.Success.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void MatchReturnsSuccessfulMatchGivenUppercaseStringWithConstantRoute() | ||
{ | ||
// Arrange | ||
HttpRequest request = this.MakeRequest("index"); | ||
ControllerRoute route = ControllerRoute.Parse("index", ""); | ||
|
||
// Act | ||
ControllerRouteMatch match = route.Match(request); | ||
|
||
// Assert | ||
match.Success.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void MatchReturnsNonSuccessfulMatchGivenEmptyStringWithVariableRoute() | ||
{ | ||
// Arrange | ||
HttpRequest request = this.MakeRequest(string.Empty); | ||
ControllerRoute route = ControllerRoute.Parse("{controller}", ""); | ||
|
||
// Act | ||
ControllerRouteMatch match = route.Match(request); | ||
|
||
// Assert | ||
match.Success.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void MatchReturnsSuccessfulMatchGivenStringWithVariableRoute() | ||
{ | ||
// Arrange | ||
HttpRequest request = this.MakeRequest("index"); | ||
ControllerRoute route = ControllerRoute.Parse("{controller}", ""); | ||
|
||
// Act | ||
ControllerRouteMatch match = route.Match(request); | ||
|
||
// Assert | ||
match.Success.Should().BeTrue(); | ||
match.Parameters.Should().HaveCount(1); | ||
match.Parameters["controller"].Should().Be("index"); | ||
} | ||
|
||
[Fact] | ||
public void MatchReturnsSuccessfulMatchGivenEmptyStringWithVariableRouteWithDefault() | ||
{ | ||
// Arrange | ||
HttpRequest request = this.MakeRequest(string.Empty); | ||
ControllerRoute route = ControllerRoute.Parse("{controller=index}", ""); | ||
|
||
// Act | ||
ControllerRouteMatch match = route.Match(request); | ||
|
||
// Assert | ||
match.Success.Should().BeTrue(); | ||
match.Parameters.Should().HaveCount(1); | ||
match.Parameters["controller"].Should().Be("index"); | ||
} | ||
|
||
[Fact] | ||
public void MatchReturnsSuccessfulMatchGivenStringWithVariableRouteWithDefault() | ||
{ | ||
// Arrange | ||
HttpRequest request = this.MakeRequest("dashboard"); | ||
ControllerRoute route = ControllerRoute.Parse("{controller=index}", ""); | ||
|
||
// Act | ||
ControllerRouteMatch match = route.Match(request); | ||
|
||
// Assert | ||
match.Success.Should().BeTrue(); | ||
match.Parameters.Should().HaveCount(1); | ||
match.Parameters["controller"].Should().Be("dashboard"); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...s/Http/HttpParser/DeserializeBodyTests.cs → ...g/Http/HttpParser/DeserializeBodyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ttp/HttpParser/DeserializeHeadersTests.cs → ...ttp/HttpParser/DeserializeHeadersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ttp/HttpParser/DeserializeRequestTests.cs → ...ttp/HttpParser/DeserializeRequestTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...tp/HttpParser/DeserializeResponseTests.cs → ...tp/HttpParser/DeserializeResponseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
.../HttpParser/DeserializeStatusLineTests.cs → .../HttpParser/DeserializeStatusLineTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
Paracord.Core.UnitTests/Parsing/Routing/RouteParser/ParseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
using Paracord.Core.Controller; | ||
using Paracord.Core.Parsing.Routing; | ||
using Paracord.Shared.Exceptions; | ||
|
||
namespace Paracord.Core.UnitTests.Parsing.Routing.RouteParserTests | ||
{ | ||
public class ParseTests | ||
{ | ||
[Fact] | ||
public void ParseReturnsNoSegmentsGivenEmptyString() | ||
{ | ||
// Arrange | ||
string route = string.Empty; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsConstantRouteSegmentGivenAlphabeticalWord() | ||
{ | ||
// Arrange | ||
string route = "Controller"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(1); | ||
segments[0].Name.Should().Be("Controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Constant); | ||
segments[0].Default.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsConstantRouteSegmentGivenAlphanumericWord() | ||
{ | ||
// Arrange | ||
string route = "Co123ler"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(1); | ||
segments[0].Name.Should().Be("Co123ler"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Constant); | ||
segments[0].Default.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseThrowsUnexpectedTokenExceptionGivenEnclosedAlphanumericWord() | ||
{ | ||
// Arrange | ||
string route = "{co123ler}"; | ||
|
||
// Act | ||
Action act = () => new RouteParser().Parse(route); | ||
|
||
// Assert | ||
act.Should().Throw<UnexpectedTokenException>(); | ||
} | ||
|
||
[Fact] | ||
public void ParseThrowsUnexpectedTokenExceptionGivenAlphabeticalWordWithEqualSign() | ||
{ | ||
// Arrange | ||
string route = "Controller="; | ||
|
||
// Act | ||
Action act = () => new RouteParser().Parse(route); | ||
|
||
// Assert | ||
act.Should().Throw<UnexpectedTokenException>(); | ||
} | ||
|
||
[Fact] | ||
public void ParseThrowsUnexpectedTokenExceptionGivenBracesWithoutContent() | ||
{ | ||
// Arrange | ||
string route = "{}"; | ||
|
||
// Act | ||
Action act = () => new RouteParser().Parse(route); | ||
|
||
// Assert | ||
act.Should().Throw<UnexpectedTokenException>(); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsVariableRouteSegmentGivenEnclosedValue() | ||
{ | ||
// Arrange | ||
string route = "{controller}"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(1); | ||
segments[0].Name.Should().Be("controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[0].Default.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsVariableRouteSegmentGivenEnclosedValueWithDefaultValue() | ||
{ | ||
// Arrange | ||
string route = "{controller=index}"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(1); | ||
segments[0].Name.Should().Be("controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[0].Default.Should().Be("index"); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsVariableRouteSegmentGivenEnclosedValueWithAlphanumericDefaultValue() | ||
{ | ||
// Arrange | ||
string route = "{controller=index1}"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(1); | ||
segments[0].Name.Should().Be("controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[0].Default.Should().Be("index1"); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsConstantRouteSegmentsGivenSeparatedWords() | ||
{ | ||
// Arrange | ||
string route = "controller/action"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(2); | ||
segments[0].Name.Should().Be("controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Constant); | ||
segments[0].Default.Should().BeNull(); | ||
segments[1].Name.Should().Be("action"); | ||
segments[1].Type.Should().Be(ControllerRouteSegmentType.Constant); | ||
segments[1].Default.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsVariableRouteSegmentsGivenSeparatedEnclosedWords() | ||
{ | ||
// Arrange | ||
string route = "{controller}/{action}"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(2); | ||
segments[0].Name.Should().Be("controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[0].Default.Should().BeNull(); | ||
segments[1].Name.Should().Be("action"); | ||
segments[1].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[1].Default.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ParseReturnsVariableRouteSegmentsGivenSeparatedEnclosedWordsWithDefaultValue() | ||
{ | ||
// Arrange | ||
string route = "{controller}/{action=Index}"; | ||
|
||
// Act | ||
List<ControllerRouteSegment> segments = new RouteParser().Parse(route); | ||
|
||
// Assert | ||
segments.Should().HaveCount(2); | ||
segments[0].Name.Should().Be("controller"); | ||
segments[0].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[0].Default.Should().BeNull(); | ||
segments[1].Name.Should().Be("action"); | ||
segments[1].Type.Should().Be(ControllerRouteSegmentType.Variable); | ||
segments[1].Default.Should().Be("Index"); | ||
} | ||
} | ||
} |
Oops, something went wrong.