Permalink
Browse files

#60 - Added route attribute override of global config settings for Us…

…eLowercaseRoutes and PreserveCaseForUrlParameters. Refactored massive and dangerous duplication of code in the two implentations of IAttributeRoute.
  • Loading branch information...
1 parent cac69ff commit b7dce87364d743db06ef36427467fe2599924aed @mccalltd committed Apr 19, 2012
@@ -1,16 +1,26 @@
-using System.Web.Mvc;
-using AttributeRouting.Web;
-using AttributeRouting.Web.Mvc;
-
-namespace AttributeRouting.Specs.Subjects
-{
- [RoutePrefix("LowercaseUrl")]
- public class LowercaseUrlController : Controller
- {
- [GET("Hello/{userName}/Goodbye")]
- public ActionResult Index(string userName)
- {
- return Content("How ya doing today?");
- }
- }
-}
+using System.Web.Mvc;
+
+namespace AttributeRouting.Specs.Subjects
+{
+ [RoutePrefix("LowercaseUrl")]
+ public class LowercaseUrlController : Controller
+ {
+ [GET("Hello/{userName}/Goodbye")]
+ public ActionResult Index(string userName)
+ {
+ return Content("How ya doing today?");
+ }
+
+ [GET("Lowercase-Override/{routeParam}", UseLowercaseRoute = true)]
+ public ActionResult LowercaseOverride()
+ {
+ return Content("");
+ }
+
+ [GET("Lowercase-Preserve-Url-Param-Case-Override/{routeParam}", UseLowercaseRoute = true, PreserveCaseForUrlParameters = true)]
+ public ActionResult LowercasePreserveUrlParamCaseOverride()
+ {
+ return Content("");
+ }
+ }
+}
@@ -1,69 +1,113 @@
-using System.Linq;
-using System.Web.Routing;
-using AttributeRouting.Framework;
-using AttributeRouting.Specs.Subjects;
-using AttributeRouting.Web.Mvc;
-using AttributeRouting.Web.Mvc.Framework;
-using NUnit.Framework;
-
-namespace AttributeRouting.Specs.Tests.LowercaseUrls
-{
- public class AttributeRouteTests
- {
- [Test]
- public void It_returns_routes_with_everything_lowered_except_params_and_query_strings_when_so_configured()
- {
- var routes = RouteTable.Routes;
- routes.Clear();
- routes.MapAttributeRoutes(c =>
- {
- c.AddRoutesFromController<LowercaseUrlController>();
- c.UseLowercaseRoutes = true;
- c.PreserveCaseForUrlParameters = true;
- });
-
- var route = routes.Cast<Route>().FirstOrDefault();
-
- Assert.That(route, Is.Not.Null);
-
- var requestContext = MockBuilder.BuildRequestContext();
-
- var pathData = route.GetVirtualPath(requestContext, new RouteValueDictionary
- {
- { "userName", "CharlieChan" },
- { "queryString", "WhatTimeIsIt" }
- });
-
- Assert.That(pathData, Is.Not.Null);
- Assert.That(pathData.VirtualPath, Is.EqualTo("lowercaseurl/hello/CharlieChan/goodbye?queryString=WhatTimeIsIt"));
- }
-
- [Test]
- public void It_returns_routes_with_everything_lowered_including_params_and_query_strings_when_so_configured()
- {
- var routes = RouteTable.Routes;
- routes.Clear();
- routes.MapAttributeRoutes(c =>
- {
- c.AddRoutesFromController<LowercaseUrlController>();
- c.UseLowercaseRoutes = true;
- c.PreserveCaseForUrlParameters = false;
- });
-
- var route = routes.Cast<Route>().FirstOrDefault();
-
- Assert.That(route, Is.Not.Null);
-
- var requestContext = MockBuilder.BuildRequestContext();
-
- var pathData = route.GetVirtualPath(requestContext, new RouteValueDictionary
- {
- { "userName", "CharlieChan" },
- { "queryString", "WhatTimeIsIt" }
- });
-
- Assert.That(pathData, Is.Not.Null);
- Assert.That(pathData.VirtualPath, Is.EqualTo("lowercaseurl/hello/charliechan/goodbye?queryString=WhatTimeIsIt"));
- }
- }
+using System.Linq;
+using System.Web.Routing;
+using AttributeRouting.Specs.Subjects;
+using AttributeRouting.Web.Mvc;
+using NUnit.Framework;
+
+namespace AttributeRouting.Specs.Tests.LowercaseUrls
+{
+ public class AttributeRouteTests
+ {
+ [Test]
+ public void It_returns_routes_with_everything_lowered_including_params_when_configured_globally()
+ {
+ var routes = RouteTable.Routes;
+ routes.Clear();
+ routes.MapAttributeRoutes(c =>
+ {
+ c.AddRoutesFromController<LowercaseUrlController>();
+ c.UseLowercaseRoutes = true;
+ c.PreserveCaseForUrlParameters = false;
+ });
+
+ var route = routes.Cast<Route>().FirstOrDefault();
+
+ Assert.That(route, Is.Not.Null);
+
+ var requestContext = MockBuilder.BuildRequestContext();
+
+ var pathData = route.GetVirtualPath(requestContext, new RouteValueDictionary
+ {
+ { "userName", "CharlieChan" },
+ { "queryString", "WhatTimeIsIt" }
+ });
+
+ Assert.That(pathData, Is.Not.Null);
+ Assert.That(pathData.VirtualPath, Is.EqualTo("lowercaseurl/hello/charliechan/goodbye?queryString=WhatTimeIsIt"));
+ }
+
+ [Test]
+ public void It_returns_routes_with_everything_lowered_including_params_when_configured_via_route_attribute()
+ {
+ var routes = RouteTable.Routes;
+ routes.Clear();
+ routes.MapAttributeRoutes(c => c.AddRoutesFromController<LowercaseUrlController>());
+
+ var route = routes.Cast<Route>().ElementAt(1);
+
+ Assert.That(route, Is.Not.Null);
+
+ var requestContext = MockBuilder.BuildRequestContext();
+
+ var pathData = route.GetVirtualPath(requestContext, new RouteValueDictionary
+ {
+ { "routeParam", "CharlieChan" },
+ { "queryString", "WhatTimeIsIt" }
+ });
+
+ Assert.That(pathData, Is.Not.Null);
+ Assert.That(pathData.VirtualPath, Is.EqualTo("lowercaseurl/lowercase-override/charliechan?queryString=WhatTimeIsIt"));
+ }
+
+ [Test]
+ public void It_returns_routes_with_everything_lowered_except_params_and_query_strings_when_configured_globally()
+ {
+ var routes = RouteTable.Routes;
+ routes.Clear();
+ routes.MapAttributeRoutes(c =>
+ {
+ c.AddRoutesFromController<LowercaseUrlController>();
+ c.UseLowercaseRoutes = true;
+ c.PreserveCaseForUrlParameters = true;
+ });
+
+ var route = routes.Cast<Route>().FirstOrDefault();
+
+ Assert.That(route, Is.Not.Null);
+
+ var requestContext = MockBuilder.BuildRequestContext();
+
+ var pathData = route.GetVirtualPath(requestContext, new RouteValueDictionary
+ {
+ { "userName", "CharlieChan" },
+ { "queryString", "WhatTimeIsIt" }
+ });
+
+ Assert.That(pathData, Is.Not.Null);
+ Assert.That(pathData.VirtualPath, Is.EqualTo("lowercaseurl/hello/CharlieChan/goodbye?queryString=WhatTimeIsIt"));
+ }
+
+ [Test]
+ public void It_returns_routes_with_everything_lowered_except_params_and_query_strings_when_configured_via_route_attribute()
+ {
+ var routes = RouteTable.Routes;
+ routes.Clear();
+ routes.MapAttributeRoutes(c => c.AddRoutesFromController<LowercaseUrlController>());
+
+ var route = routes.Cast<Route>().ElementAt(2);
+
+ Assert.That(route, Is.Not.Null);
+
+ var requestContext = MockBuilder.BuildRequestContext();
+
+ var pathData = route.GetVirtualPath(requestContext, new RouteValueDictionary
+ {
+ { "routeParam", "CharlieChan" },
+ { "queryString", "WhatTimeIsIt" }
+ });
+
+ Assert.That(pathData, Is.Not.Null);
+ Assert.That(pathData.VirtualPath, Is.EqualTo("lowercaseurl/lowercase-preserve-url-param-case-override/CharlieChan?queryString=WhatTimeIsIt"));
+ }
+ }
}
@@ -1,19 +1,17 @@
using System;
using System.Linq;
-using System.Web.Routing;
-using AttributeRouting.Framework;
+using System.Web.Routing;
using AttributeRouting.Specs.Subjects;
using AttributeRouting.Web.Logging;
-using AttributeRouting.Web.Mvc;
-using AttributeRouting.Web.Mvc.Framework;
+using AttributeRouting.Web.Mvc;
using NUnit.Framework;
namespace AttributeRouting.Specs.Tests.LowercaseUrls
{
public class RouteBuilderTests
{
[Test]
- public void It_builds_routes_with_all_but_url_params_lowercased_when_so_configured()
+ public void It_builds_routes_with_all_but_url_params_lowercased_when_configured_globally()
{
var routes = RouteTable.Routes;
routes.Clear();
@@ -31,6 +29,20 @@ public void It_builds_routes_with_all_but_url_params_lowercased_when_so_configur
}
[Test]
+ public void It_builds_routes_with_all_but_url_params_lowercased_when_configured_via_route_attribute()
+ {
+ var routes = RouteTable.Routes;
+ routes.Clear();
+ routes.MapAttributeRoutes(c => c.AddRoutesFromController<LowercaseUrlController>());
+
+ routes.Cast<Route>().LogTo(Console.Out);
+
+ var route = routes.Cast<Route>().ElementAt(1);
+ Assert.That(route, Is.Not.Null);
+ Assert.That(route.Url, Is.EqualTo("lowercaseurl/lowercase-override/{routeParam}"));
+ }
+
+ [Test]
public void It_builds_routes_with_urls_in_specified_case_when_not_configured_to_generate_lowercase_routes()
{
var routes = RouteTable.Routes;
Oops, something went wrong.

0 comments on commit b7dce87

Please sign in to comment.