Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Added VersionRouteSegmentConstraint to Nancy.Routing.Constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
hlesesne committed Mar 24, 2015
1 parent 50e3610 commit 5bf3e23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/Nancy.Tests.Functional/Tests/RouteConstraintTests.cs
Expand Up @@ -25,7 +25,7 @@ public RouteConstraintTests()
{
configuration.ApplicationStartup((c, p) => { });
configuration.Modules(new Type[] { typeof(RouteConstraintsModule) });
configuration.RouteSegmentConstraints(new[] { typeof(VersionRouteConstraint) });
configuration.RouteSegmentConstraints(new[] { typeof(UltimateRouteSegmentConstraint), typeof(VersionRouteSegmentConstraint) });
});

this.browser = new Browser(this.bootstrapper);
Expand All @@ -35,7 +35,7 @@ public RouteConstraintTests()
public void multiple_parameters_per_segment_should_support_constraints()
{
// Given
const string url = @"/4.3...5.3";
const string url = @"/42...42";

// When
var response = this.browser.Get(
Expand All @@ -50,18 +50,42 @@ public void multiple_parameters_per_segment_should_support_constraints()
Assert.True(Invoked);
}

[Fact]
public void versionsegmentrouteconstraint_should_match_on_version_numbers()
{
// Given
const string url = @"/4.1.2...4.1.5";

// When
var response = this.browser.Get(
url,
with =>
{
with.HttpRequest();
with.Accept("application/json");
});

public class VersionRouteConstraint : RouteSegmentConstraintBase<Version>
// Then
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(response.Body.AsString(), "{\"left\":\"4.1.2\",\"right\":\"4.1.5\"}");
}


public class UltimateRouteSegmentConstraint : RouteSegmentConstraintBase<int>
{
public override string Name
{
get { return "version"; }
get { return "correctanswer"; }
}

protected override bool TryMatch(string constraint, string segment, out Version matchedValue)
protected override bool TryMatch(string constraint, string segment, out int matchedValue)
{
Invoked = true;
return Version.TryParse(segment, out matchedValue);
if (int.TryParse(segment, out matchedValue))
{
return matchedValue == 42;
}
return false;
}
}
}
Expand All @@ -70,11 +94,17 @@ public class RouteConstraintsModule : NancyModule
{
public RouteConstraintsModule()
{
this.Get["/{left:version}...{right:version}"] = _ =>
this.Get["/{left:correctanswer}...{right:correctanswer}"] = _ =>
{
return HttpStatusCode.OK;
};

// For testing VersionSegmentRouteConstraint
this.Get["/{left:version}...{right:version}"] = _ =>
{
return new {_.left, _.right};
};


}
}
Expand Down
1 change: 1 addition & 0 deletions src/Nancy/Nancy.csproj
Expand Up @@ -251,6 +251,7 @@
<Compile Include="Routing\Constraints\RangeRouteSegmentConstraint.cs" />
<Compile Include="Routing\Constraints\RouteSegmentConstraintBase.cs" />
<Compile Include="Routing\Constraints\ParameterizedRouteSegmentConstraintBase.cs" />
<Compile Include="Routing\Constraints\VersionRouteSegmentConstraint.cs" />
<Compile Include="Routing\DefaultRequestDispatcher.cs" />
<Compile Include="Routing\DefaultRouteDescriptionProvider.cs" />
<Compile Include="Routing\DefaultRouteInvoker.cs" />
Expand Down
20 changes: 20 additions & 0 deletions src/Nancy/Routing/Constraints/VersionRouteSegmentConstraint.cs
@@ -0,0 +1,20 @@
namespace Nancy.Routing.Constraints
{
using System;

/// <summary>
/// Constraint for version route segments.
/// </summary>
public class VersionRouteSegmentConstraint : RouteSegmentConstraintBase<Version>
{
public override string Name
{
get { return "version"; }
}

protected override bool TryMatch(string constraint, string segment, out Version matchedValue)
{
return Version.TryParse(segment, out matchedValue);
}
}
}

0 comments on commit 5bf3e23

Please sign in to comment.