PageRenderTime 3401ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/MvcCodeRouting.Web.Mvc.Tests/Routing/RoutingFacts.cs

http://mvccoderouting.codeplex.com
C# | 310 lines | 208 code | 96 blank | 6 comment | 0 complexity | 0d204fb55a15776695d35301aac8190c MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.Web.Routing;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Moq;
  10. namespace MvcCodeRouting.Web.Mvc.Tests.Routing {
  11. using UrlHelper = System.Web.Mvc.UrlHelper;
  12. //[TestClass]
  13. public class RoutingFacts {
  14. readonly RouteCollection routes;
  15. readonly UrlHelper Url;
  16. public RoutingFacts() {
  17. this.routes = new RouteCollection();
  18. var httpContextMock = new Mock<HttpContextBase>();
  19. httpContextMock.Setup(c => c.Request.ApplicationPath).Returns("");
  20. httpContextMock.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s);
  21. var routeData = new RouteData();
  22. var requestContext = new RequestContext(httpContextMock.Object, routeData);
  23. this.Url = new UrlHelper(requestContext, routes);
  24. }
  25. [TestMethod]
  26. public void NameIsOptional() {
  27. routes.Clear();
  28. routes.MapRoute(null, "");
  29. }
  30. [TestMethod]
  31. public void ParametersAreRequiredByDefault() {
  32. routes.Clear();
  33. routes.MapRoute(null, "{a}");
  34. Assert.IsNull(Url.RouteUrl(new { }));
  35. Assert.AreEqual("/b", Url.RouteUrl(new { a = "b" }));
  36. var httpContextMock = new Mock<HttpContextBase>();
  37. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
  38. Assert.IsNull(routes.GetRouteData(httpContextMock.Object));
  39. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/b");
  40. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  41. }
  42. [TestMethod]
  43. public void DefaultValueMakesParameterOptional() {
  44. routes.Clear();
  45. routes.MapRoute(null, "{a}", new { a = "b" });
  46. Assert.AreEqual("/", Url.RouteUrl(new { }));
  47. var httpContextMock = new Mock<HttpContextBase>();
  48. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
  49. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  50. }
  51. [TestMethod]
  52. public void CanUseNullOrEmptyStringForParameterWithDefaultValue() {
  53. routes.Clear();
  54. routes.MapRoute(null, "{a}", new { a = "b" });
  55. Assert.AreEqual("/", Url.RouteUrl(new { a = (string)null }));
  56. Assert.AreEqual("/", Url.RouteUrl(new { a = "" }));
  57. }
  58. [TestMethod]
  59. public void CannotUseNullOrEmptyStringForDefaultValueWithoutParameter() {
  60. routes.Clear();
  61. routes.MapRoute(null, "a", new { b = "c" });
  62. Assert.IsNull(Url.RouteUrl(new { b = (string)null }));
  63. Assert.IsNull(Url.RouteUrl(new { b = "" }));
  64. }
  65. [TestMethod]
  66. public void ValueMustMatchDefaultValueWithoutParameterOrBeOmitted() {
  67. routes.Clear();
  68. routes.MapRoute(null, "a", new { b = "c" });
  69. Assert.AreEqual("/a", Url.RouteUrl(new { b = "c" }));
  70. Assert.AreEqual("/a", Url.RouteUrl(new { }));
  71. }
  72. [TestMethod]
  73. public void ConstraintLimitsTheValueSpaceOfParameter() {
  74. routes.Clear();
  75. routes.MapRoute(null, "{a}", new { }, new { a = "b|c" });
  76. Assert.AreEqual("/b", Url.RouteUrl(new { a = "b" }));
  77. Assert.AreEqual("/c", Url.RouteUrl(new { a = "c" }));
  78. Assert.IsNull(Url.RouteUrl(new { a = "d" }));
  79. var httpContextMock = new Mock<HttpContextBase>();
  80. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/b");
  81. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  82. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/c");
  83. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  84. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/d");
  85. Assert.IsNull(routes.GetRouteData(httpContextMock.Object));
  86. }
  87. [TestMethod]
  88. public void ConstraintTestsTheWholeValue() {
  89. routes.Clear();
  90. routes.MapRoute(null, "{a}", new { }, new { a = "b" });
  91. Assert.AreEqual("/b", Url.RouteUrl(new { a = "b" }));
  92. Assert.IsNull(Url.RouteUrl(new { a = "b2" }));
  93. var httpContextMock = new Mock<HttpContextBase>();
  94. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/b");
  95. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  96. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/b2");
  97. Assert.IsNull(routes.GetRouteData(httpContextMock.Object));
  98. }
  99. [TestMethod]
  100. public void ConstraintForOptionalParameterShouldMatchAnEmptyStringIfDefaultValueIsEmptyString() {
  101. routes.Clear();
  102. routes.MapRoute(null, "{a}", new { a = "" }, new { a = "b" });
  103. Assert.IsNull(Url.RouteUrl(new { a = "" }));
  104. var httpContextMock = new Mock<HttpContextBase>();
  105. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
  106. Assert.IsNull(routes.GetRouteData(httpContextMock.Object));
  107. //--------------------------------------
  108. routes.Clear();
  109. routes.MapRoute(null, "{a}", new { a = "" }, new { a = "(b)?" });
  110. Assert.AreEqual("/", Url.RouteUrl(new { a = "" }));
  111. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
  112. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  113. //--------------------------------------
  114. routes.Clear();
  115. routes.MapRoute(null, "{a}", new { a = "b" }, new { a = "b" });
  116. Assert.AreEqual("/", Url.RouteUrl(new { a = "" }));
  117. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/");
  118. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  119. }
  120. [TestMethod]
  121. public void UrlHelperActionUsesTheCurrentRequestControllerAndActionValuesAsDefaults() {
  122. var routeData = new RouteData {
  123. Values = {
  124. { "controller", "b" },
  125. { "action", "c" }
  126. }
  127. };
  128. var requestContext = new RequestContext(this.Url.RequestContext.HttpContext, routeData);
  129. var Url = new UrlHelper(requestContext, routes);
  130. routes.Clear();
  131. routes.MapRoute(null, "a", new { controller = "b", action = "c" });
  132. Assert.IsNull(Url.RouteUrl(new { controller = (string)null, action = (string)null }));
  133. Assert.AreEqual("/a", Url.Action(null));
  134. Assert.AreEqual("/a", Url.Action(null, (string)null));
  135. }
  136. [TestMethod]
  137. public void CurrentRequestValuesAreUsedAsDefaults() {
  138. routes.Clear();
  139. routes.MapRoute(null, "{a}");
  140. var routeData = new RouteData {
  141. Values = {
  142. { "a", "x" },
  143. }
  144. };
  145. var requestContext = new RequestContext(this.Url.RequestContext.HttpContext, routeData);
  146. var Url = new UrlHelper(requestContext, routes);
  147. Assert.AreEqual("/x", Url.RouteUrl(new { }));
  148. }
  149. [TestMethod]
  150. public void RoutingIsCaseInsensitive() {
  151. routes.Clear();
  152. routes.MapRoute(null, "a/{b}", new { a = "A" }, new { b = "xyz" });
  153. Assert.IsNotNull(Url.RouteUrl(new { a = "a", b = "XYZ" }));
  154. var httpContextMock = new Mock<HttpContextBase>();
  155. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/A/XYZ");
  156. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  157. }
  158. [TestMethod]
  159. public void RoutesAreEvaluatedInOrder() {
  160. routes.Clear();
  161. routes.MapRoute(null, "{a}/{b}", new { b = "c" });
  162. routes.MapRoute(null, "{a}");
  163. var httpContextMock = new Mock<HttpContextBase>();
  164. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/x");
  165. Assert.IsTrue(
  166. Object.ReferenceEquals(
  167. routes.GetVirtualPath(Url.RequestContext, new RouteValueDictionary(new { a = "x" })).Route,
  168. routes.First()
  169. )
  170. );
  171. Assert.IsTrue(
  172. Object.ReferenceEquals(
  173. routes.GetRouteData(httpContextMock.Object).Route,
  174. routes.First()
  175. )
  176. );
  177. var lastRoute = routes.Last();
  178. routes.Remove(lastRoute);
  179. routes.Insert(0, lastRoute);
  180. Assert.IsTrue(
  181. Object.ReferenceEquals(
  182. routes.GetVirtualPath(Url.RequestContext, new RouteValueDictionary(new { a = "x" })).Route,
  183. routes.First()
  184. )
  185. );
  186. Assert.IsTrue(
  187. Object.ReferenceEquals(
  188. routes.GetRouteData(httpContextMock.Object).Route,
  189. routes.First()
  190. )
  191. );
  192. }
  193. [TestMethod]
  194. public void LiteralSubsegmentBug() {
  195. // http://stackoverflow.com/questions/4318373
  196. routes.Clear();
  197. routes.MapRoute(null, "_{a}");
  198. var httpContextMock = new Mock<HttpContextBase>();
  199. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/_b");
  200. Assert.IsNotNull(routes.GetRouteData(httpContextMock.Object));
  201. httpContextMock.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/__b");
  202. Assert.IsNull(routes.GetRouteData(httpContextMock.Object));
  203. }
  204. //[TestMethod] // Appears to be fixed in .NET 4.5
  205. public void TwoConsecutiveOptionalParametersBug() {
  206. // http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx
  207. routes.Clear();
  208. routes.MapRoute(null, "a/{b}/{c}", new { b = "", c = "" });
  209. Assert.IsNotNull(Url.RouteUrl(new { b = "1", c = "2" }));
  210. Assert.IsNotNull(Url.RouteUrl(new { b = "1" }));
  211. Assert.IsNull(Url.RouteUrl(new { }));
  212. }
  213. }
  214. }