Skip to content

Commit

Permalink
Merge pull request #11 from maxnatamo/feat/custom-route-parsing
Browse files Browse the repository at this point in the history
feat/custom-route-parsing
  • Loading branch information
maxnatamo authored Apr 18, 2023
2 parents 5df7c4f + 6583dd0 commit a702c54
Show file tree
Hide file tree
Showing 29 changed files with 1,272 additions and 108 deletions.
110 changes: 110 additions & 0 deletions Paracord.Core.UnitTests/Controller/ControllerRouteMatch/MatchTests.cs
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");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;

using Paracord.Core.Http;
using Paracord.Core.Parsing.Http;

namespace Paracord.Core.UnitTests.Http.HttpParserTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Paracord.Core.Http;
using Paracord.Core.Parsing.Http;

namespace Paracord.Core.UnitTests.Http.HttpParserTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;

using Paracord.Core.Http;
using Paracord.Core.Parsing.Http;

namespace Paracord.Core.UnitTests.Http.HttpParserTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;

using Paracord.Core.Http;
using Paracord.Core.Parsing.Http;

namespace Paracord.Core.UnitTests.Http.HttpParserTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Paracord.Core.Http;
using Paracord.Core.Parsing.Http;

namespace Paracord.Core.UnitTests.Http.HttpParserTests
{
Expand Down
198 changes: 198 additions & 0 deletions Paracord.Core.UnitTests/Parsing/Routing/RouteParser/ParseTests.cs
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");
}
}
}
Loading

0 comments on commit a702c54

Please sign in to comment.