Skip to content

Commit

Permalink
Added property and method based explicit route support
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburton committed Aug 7, 2011
1 parent 3f01a54 commit 0e21dad
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Setup()
public void FindAll_WhenCalled_FindsAllHandlers()
{
var handlers = defaultHandlerScanner.FindAll();
Assert.That(handlers.Count(), Is.EqualTo(9));
Assert.That(handlers.Count(), Is.EqualTo(11));
}

[Test]
Expand Down Expand Up @@ -100,10 +100,24 @@ public void FindAll_WhenCalled_RootHandlerHasEmptyUri()
}

[Test]
public void FindAll_WhenCalled_ExplicitRootHandlerHasEmptyUri()
public void FindAll_WhenCalled_ExplicitFieldRouteRootHandlerHasEmptyUri()
{
var handlers = defaultHandlerScanner.FindAll();
Assert.That(handlers.Single(h => h.Type == new ExplicitRootHandler().GetType()).Uri, Is.EqualTo(String.Empty));
Assert.That(handlers.Single(h => h.Type == new ExplicitFieldRouteRootHandler().GetType()).Uri, Is.EqualTo(String.Empty));
}

[Test]
public void FindAll_WhenCalled_ExplicitPropertyRouteRootHandlerHasEmptyUri()
{
var handlers = defaultHandlerScanner.FindAll();
Assert.That(handlers.Single(h => h.Type == new ExplicitPropertyRouteRootHandler().GetType()).Uri, Is.EqualTo(String.Empty));
}

[Test]
public void FindAll_WhenCalled_ExplicitMethodRouteRootHandlerHasEmptyUri()
{
var handlers = defaultHandlerScanner.FindAll();
Assert.That(handlers.Single(h => h.Type == new ExplicitMethodRouteRootHandler().GetType()).Uri, Is.EqualTo(String.Empty));
}
}
}
2 changes: 1 addition & 1 deletion src/tinyweb.framework.tests/Init/InitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Setup()
[Test]
public void Initialise_WithSpecificNumberOfHandlers_ReturnsCorrectNumberOfHandlers()
{
Assert.That(Tinyweb.Handlers.Count(), Is.EqualTo(9));
Assert.That(Tinyweb.Handlers.Count(), Is.EqualTo(11));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace tinyweb.framework.tests
{
public class ExplicitRootHandler
public class ExplicitFieldRouteRootHandler
{
Route route = new Route("/");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace tinyweb.framework.tests
{
public class ExplicitMethodRouteRootHandler
{
public Route Route()
{
return new Route("/");
}

public IResult Get()
{
return new StringResult("Get");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace tinyweb.framework.tests
{
public class ExplicitPropertyRouteRootHandler
{
public Route Route
{
get { return new Route("/"); }
}

public IResult Get()
{
return new StringResult("Get");
}
}
}
4 changes: 3 additions & 1 deletion src/tinyweb.framework.tests/tinyweb.framework.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
<Compile Include="Test Data\Filters\NonPirorityFilter.cs" />
<Compile Include="Test Data\Filters\InvalidFilter.cs" />
<Compile Include="Test Data\Handlers\BeforeAfterHandler.cs" />
<Compile Include="Test Data\Handlers\ExplicitRootHandler.cs" />
<Compile Include="Test Data\Handlers\ExplicitMethodRouteRootHandler.cs" />
<Compile Include="Test Data\Handlers\ExplicitPropertyRouteRootHandler.cs" />
<Compile Include="Test Data\Handlers\ExplicitFieldRouteRootHandler.cs" />
<Compile Include="Test Data\Handlers\RootHandler.cs" />
<Compile Include="TestHelpers\ContextHelpers.cs" />
<Compile Include="TestHelpers\ObjectHelper.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ private bool handlerIsValid(Type type)
private Route getRoute(Type type)
{
var handler = HandlerFactory.Current.Create(new HandlerData { Type = type });
var routeProperty = handler.GetType().GetField("route", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var route = getExplicitRouteFromHandler(handler);

if (routeProperty == null)
if (route == null)
{
return new Route(getRouteUriByConvention(type));
}

var route = routeProperty.GetValue(handler) as Route;

if (route.RouteUri == "/")
{
route.RouteUri = String.Empty;
Expand All @@ -63,6 +61,35 @@ private Route getRoute(Type type)
return route;
}

private Route getExplicitRouteFromHandler(object handler)
{
var routeMember = handler.GetType().GetMember("Route", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase).FirstOrDefault();

if (routeMember == null)
{
return null;
}

Route route = null;

if (routeMember is MethodInfo)
{
route = ((MethodInfo)routeMember).Invoke(handler, null) as Route;
}

if (routeMember is FieldInfo)
{
route = ((FieldInfo)routeMember).GetValue(handler) as Route;
}

if (routeMember is PropertyInfo)
{
route = ((PropertyInfo)routeMember).GetValue(handler, null) as Route;
}

return route;
}

private string getRouteUriByConvention(Type type)
{
var handlerStart = type.Name.ToLower().IndexOf("handler");
Expand Down

0 comments on commit 0e21dad

Please sign in to comment.