Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mcs / class / System.Web.Routing / Test / System.Web.Routing / RouteCollectionTest.cs
index 7631d86d29b4413f5f9c9abfa962b07a76f7f831..c2d2d51a28d30b629d9e2d72d8ff3620f0881bb2 100644 (file)
@@ -105,6 +105,13 @@ namespace MonoTests.System.Web.Routing
                        Assert.IsNull(c ["x"]);
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void GetRouteDataNoRequest ()
+               {
+                       new RouteCollection ().GetRouteData (new HttpContextStub (true));
+               }
+
                [Test]
                [ExpectedException (typeof (ArgumentNullException))]
                public void GetRouteDataNullArg ()
@@ -123,13 +130,9 @@ namespace MonoTests.System.Web.Routing
                public void GetRouteDataForNonExistent2 ()
                {
                        var rd = new RouteCollection () { RouteExistingFiles = true }.GetRouteData (new HttpContextStub2 (null, null, null));
-                       Assert.IsNull (rd);
-                       try {
-                               new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
-                               Assert.Fail ("#1");
-                       } catch (NotImplementedException) {
-                               // it should fail due to the NIE on AppRelativeCurrentExecutionFilePath.
-                       }
+                       Assert.IsNull (rd, "#A1");
+                       rd = new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
+                       Assert.IsNull (rd, "#A2");
                }
 
                [Test]
@@ -525,6 +528,63 @@ namespace MonoTests.System.Web.Routing
                        Assert.IsNull (vp, "#C1");
                }
 
+               [Test]
+               public void GetVirtualPath8 ()
+               {
+                       var routes = new RouteCollection();
+
+                       routes.Add (new MyRoute ("login", new MyRouteHandler ()) {
+                               Defaults = new RouteValueDictionary (new { controller = "Home", action = "LogOn" })
+                       });
+
+                       routes.Add (new MyRoute ("{site}/{controller}/{action}", new MyRouteHandler ()) {
+                               Defaults = new RouteValueDictionary (new { site = "_", controller = "Home", action = "Index" }),
+                               Constraints = new RouteValueDictionary ( new { site = "_?[0-9A-Za-z-]*" })
+                       });
+
+                       routes.Add (new MyRoute ("{*path}", new MyRouteHandler ()) {
+                               Defaults = new RouteValueDictionary (new { controller = "Error", action = "NotFound" }),
+                       });
+
+                       var hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = routes.GetRouteData (hc);
+                       var rvs = new RouteValueDictionary () {
+                               { "controller", "Home" },
+                               { "action" , "Index" }
+                       };
+                       var vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
+                       Assert.IsNotNull (vpd, "#A1");
+                       Assert.AreEqual ("/", vpd.VirtualPath, "#A2");
+                       Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
+
+                       hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       rd = routes.GetRouteData (hc);
+                       rvs = new RouteValueDictionary () {
+                               { "controller", "Home" }
+                       };
+                       vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
+                       Assert.IsNotNull (vpd, "#B1");
+                       Assert.AreEqual ("/login", vpd.VirtualPath, "#B2");
+                       Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
+
+                       hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       rd = routes.GetRouteData (hc);
+                       rvs = new RouteValueDictionary () {
+                               { "action" , "Index" }
+                       };
+                       vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
+                       Assert.IsNotNull (vpd, "#C1");
+                       Assert.AreEqual ("/", vpd.VirtualPath, "#C2");
+                       Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
+
+                       hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
+                       rd = routes.GetRouteData (hc);
+                       Assert.IsNotNull (rd, "#D1");
+               }
+
                [Test]
                [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
                public void GetVirtualPathToExistingFile ()
@@ -579,5 +639,292 @@ namespace MonoTests.System.Web.Routing
                        
                        Assert.IsNotNull (rd, "#A1");
                }
+               [Test]
+               public void Ignore_String ()
+               {
+                       var c = new RouteCollection ();
+
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.Ignore (null);
+                       }, "#A1");
+
+                       c.Ignore ("{resource}.axd/{*pathInfo}");
+                       var hc = new HttpContextStub2 ("~/something.axd/pathinfo", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
+                       Assert.IsTrue (rd.Route is Route, "#A1-4");
+                       Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
+                       Assert.AreEqual (0, ((Route) rd.Route).Constraints.Count, "#A1-6");
+               }
+
+               [Test]
+               public void Ignore_String_Object ()
+               {
+                       var c = new RouteCollection ();
+
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.Ignore (null, new { allaspx = @".*\.aspx(/.*)?" });
+                       }, "#A1");
+
+                       c.Ignore ("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
+                       var hc = new HttpContextStub2 ("~/page.aspx", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
+                       Assert.IsTrue (rd.Route is Route, "#A1-4");
+                       Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
+                       Assert.AreEqual (1, ((Route) rd.Route).Constraints.Count, "#A1-6");
+                       Assert.AreEqual (@".*\.aspx(/.*)?", ((Route) rd.Route).Constraints ["allaspx"], "#A1-7");
+
+                       c = new RouteCollection ();
+                       c.Ignore ("{*allaspx}", "something invalid");
+
+                       AssertExtensions.Throws<InvalidOperationException> (() => {
+                               rd = c.GetRouteData (hc);
+                       }, "#A2");
+               }
+
+               [Test]
+               public void MapPageRoute_String_String_String ()
+               {
+                       var c = new RouteCollection ();
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url");
+                       var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
+
+                       c = new RouteCollection ();
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.MapPageRoute ("RouteName", null, "~/some-url");
+                       }, "#A2");
+
+                       c = new RouteCollection ();
+                       c.MapPageRoute ("RouteName", String.Empty, "~/some-url");
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNull (rd, "#A2");
+
+                       c = new RouteCollection ();
+                       // thrown by PageRouteHandler's constructor
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               c.MapPageRoute ("RouteName", "~/some-url", null);
+                       }, "#A3");
+               }
+
+               [Test]
+               public void MapPageRoute_String_String_String_Bool ()
+               {
+                       var c = new RouteCollection ();
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true);
+                       var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
+                       Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
+
+                       c = new RouteCollection ();
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.MapPageRoute ("RouteName", null, "~/some-url", true);
+                       }, "#A2");
+
+                       c = new RouteCollection ();
+                       c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNull (rd, "#A2");
+
+                       c = new RouteCollection ();
+                       // thrown by PageRouteHandler's constructor
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               c.MapPageRoute ("RouteName", "~/some-url", null, true);
+                       }, "#A3");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
+               }
+
+               [Test]
+               public void MapPageRoute_String_String_String_Bool_RVD ()
+               {
+                       var c = new RouteCollection ();
+                       var defaults = new RouteValueDictionary ();
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults);
+                       var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
+                       Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
+
+                       c = new RouteCollection ();
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults);
+                       }, "#A2");
+
+                       c = new RouteCollection ();
+                       c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNull (rd, "#A2");
+
+                       c = new RouteCollection ();
+                       // thrown by PageRouteHandler's constructor
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults);
+                       }, "#A3");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4"); 
+               }
+
+               [Test]
+               public void MapPageRoute_String_String_String_Bool_RVD_RVD ()
+               {
+                       var c = new RouteCollection ();
+                       var defaults = new RouteValueDictionary ();
+                       var constraints = new RouteValueDictionary ();
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints);
+                       var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
+                       Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
+
+                       c = new RouteCollection ();
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints);
+                       }, "#A2");
+
+                       c = new RouteCollection ();
+                       c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNull (rd, "#A2");
+
+                       c = new RouteCollection ();
+                       // thrown by PageRouteHandler's constructor
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints);
+                       }, "#A3");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
+               }
+
+               [Test]
+               public void MapPageRoute_String_String_String_Bool_RVD_RVD_RVD ()
+               {
+                       var c = new RouteCollection ();
+                       var defaults = new RouteValueDictionary ();
+                       var constraints = new RouteValueDictionary ();
+                       var dataTokens = new RouteValueDictionary ();
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints, dataTokens);
+                       var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
+                       hc.SetResponse (new HttpResponseStub (3));
+                       var rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A1-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A1-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
+                       Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
+
+                       c = new RouteCollection ();
+                       AssertExtensions.Throws<ArgumentNullException> (() => {
+                               c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints, dataTokens);
+                       }, "#A2");
+
+                       c = new RouteCollection ();
+                       c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints, dataTokens);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNull (rd, "#A2");
+
+                       c = new RouteCollection ();
+                       // thrown by PageRouteHandler's constructor
+                       AssertExtensions.Throws<ArgumentException> (() => {
+                               c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints, dataTokens);
+                       }, "#A3");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints, dataTokens);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
+
+                       c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints, dataTokens);
+                       rd = c.GetRouteData (hc);
+
+                       Assert.IsNotNull (rd, "#A4-1");
+                       Assert.IsNotNull (rd.RouteHandler, "#A4-2");
+                       Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
+                       Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
+               }
+               
+               [Test] // https://bugzilla.xamarin.com/show_bug.cgi?id=13909
+               public void MapPageRoute_Bug13909 ()
+               {
+                       var c = new RouteCollection ();
+
+                       c.MapPageRoute("test", "test", "~/test.aspx");
+                       c.Clear();
+                       c.MapPageRoute("test", "test", "~/test.aspx");
+               }
        }
 }