Permalink
Browse files

Added property and method based explicit route support

  • Loading branch information...
1 parent 3f01a54 commit 0e21dad59b639d5315ab7bedcfb84288900561c8 @mattburton mattburton committed Aug 7, 2011
@@ -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]
@@ -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));
}
}
}
@@ -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]
@@ -1,6 +1,6 @@
namespace tinyweb.framework.tests
{
- public class ExplicitRootHandler
+ public class ExplicitFieldRouteRootHandler
{
Route route = new Route("/");
@@ -0,0 +1,15 @@
+namespace tinyweb.framework.tests
+{
+ public class ExplicitMethodRouteRootHandler
+ {
+ public Route Route()
+ {
+ return new Route("/");
+ }
+
+ public IResult Get()
+ {
+ return new StringResult("Get");
+ }
+ }
+}
@@ -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");
+ }
+ }
+}
@@ -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" />
@@ -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;
@@ -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");

0 comments on commit 0e21dad

Please sign in to comment.