2 // RouteCollectionTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Routing;
33 using NUnit.Framework;
35 using MonoTests.Common;
37 namespace MonoTests.System.Web.Routing
40 public class RouteCollectionTest
43 public void ConstructorNullArgs ()
46 new RouteCollection (null);
50 public void RouteExistingFiles ()
52 var c = new RouteCollection ();
53 Assert.IsFalse (c.RouteExistingFiles);
57 public void AddNullMame ()
59 var c = new RouteCollection ();
60 // when name is null, no duplicate check is done.
61 c.Add (null, new Route (null, null));
62 c.Add (null, new Route (null, null));
66 public void AddDuplicateEmpty ()
68 var c = new RouteCollection ();
69 // when name is "", no duplicate check is done.
70 c.Add ("", new Route (null, null));
71 c.Add ("", new Route (null, null));
75 [ExpectedException (typeof (ArgumentException))]
76 public void AddDuplicateName ()
78 var c = new RouteCollection ();
79 c.Add ("x", new Route (null, null));
80 c.Add ("x", new Route (null, null));
84 public void IndexForNonExistent ()
86 Assert.IsNull (new RouteCollection () [null]);
90 public void IndexForExistent ()
92 var c = new RouteCollection ();
93 var r = new Route (null, null);
95 Assert.AreEqual (r, c ["x"]);
99 public void IndexForNonExistentAfterRemoval ()
101 var c = new RouteCollection ();
102 var r = new Route (null, null);
105 Assert.IsNull(c ["x"]);
109 [ExpectedException (typeof (ArgumentException))]
110 public void GetRouteDataNoRequest ()
112 new RouteCollection ().GetRouteData (new HttpContextStub (true));
116 [ExpectedException (typeof (ArgumentNullException))]
117 public void GetRouteDataNullArg ()
119 new RouteCollection ().GetRouteData (null);
123 public void GetRouteDataForNonExistent ()
125 var rd = new RouteCollection ().GetRouteData (new HttpContextStub ("~/foo"));
130 public void GetRouteDataForNonExistent2 ()
132 var rd = new RouteCollection () { RouteExistingFiles = true }.GetRouteData (new HttpContextStub2 (null, null, null));
133 Assert.IsNull (rd, "#A1");
134 rd = new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
135 Assert.IsNull (rd, "#A2");
139 public void GetRouteDataWrongPathNoRoute ()
141 new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
145 comment out those tests; I cannot explain those tests.
148 [ExpectedException (typeof (ArgumentOutOfRangeException))]
149 public void GetRouteDataWrongPathOneRoute ()
151 var c = new RouteCollection ();
152 var r = new Route ("foo", null);
154 // it somehow causes ArgumentOutOfRangeException for
155 // Request.AppRelativeCurrentExecutionFilePath.
156 c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
160 public void GetRouteDataWrongPathOneRoute2 ()
162 var c = new RouteCollection ();
163 var r = new Route ("foo", null);
165 c.GetRouteData (new HttpContextStub ("/~", String.Empty));
170 [ExpectedException (typeof (NotImplementedException))]
171 public void GetRouteDataForPathInfoNIE ()
173 var c = new RouteCollection ();
174 var r = new Route ("foo", null);
176 // it retrieves PathInfo and then dies.
177 var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
181 public void GetRouteDataForNullHandler ()
183 var c = new RouteCollection ();
184 var r = new Route ("foo", null); // allowed
186 var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
187 Assert.IsNotNull (rd, "#1");
188 Assert.AreEqual (r, rd.Route, "#2");
191 // below tests in RouteCollection, unlike Route, do some additional checks than Route.GetVirtualPath().
194 [ExpectedException (typeof (NotImplementedException))]
195 public void GetVirtualPathNoApplicationPath ()
197 var c = new RouteCollection ();
198 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
199 var hc = new HttpContextStub2 ("~/x/y", String.Empty);
200 var rd = c.GetRouteData (hc);
201 // it tries to get HttpContextBase.Request.ApplicationPath and then throws NIE.
202 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
206 [ExpectedException (typeof (NotImplementedException))]
207 public void GetVirtualPathNoApplyAppPathModifier ()
209 var c = new RouteCollection ();
210 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
211 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
212 // it tries to call HttpContextBase.Response.ApplyAppPathModifier() and then causes NIE.
213 hc.SetResponse (new HttpResponseStub ());
214 var rd = c.GetRouteData (hc);
215 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
219 public void GetVirtualPathCheckVirtualPathToModify ()
221 var c = new RouteCollection ();
222 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
223 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
224 // it tries to get HttpContextBase.Response, so set it.
225 hc.SetResponse (new HttpResponseStub (1));
226 var rd = c.GetRouteData (hc);
228 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
230 } catch (ApplicationException ex) {
231 Assert.AreEqual ("apppath/x/y", ex.Message, "#2");
236 public void GetVirtualPath ()
238 var c = new RouteCollection ();
239 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
240 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
241 // it tries to get HttpContextBase.Response, so set it.
242 hc.SetResponse (new HttpResponseStub (2));
243 var rd = c.GetRouteData (hc);
244 Assert.IsNotNull (rd, "#1");
246 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
247 Assert.IsNotNull (vpd, "#2");
248 Assert.AreEqual ("apppath/x/y_modified", vpd.VirtualPath, "#3");
249 Assert.AreEqual (0, vpd.DataTokens.Count, "#4");
252 [Test (Description = "Bug #502555")]
253 public void GetVirtualPath2 ()
255 var c = new RouteCollection ();
258 new MyRoute ("summary/{action}-{type}/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Summary", action = "Index", page = 1}) }
262 new MyRoute ("apis/{apiname}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Apis", action = "Index" }) }
265 c.Add ("Single Report",
266 new MyRoute ("report/{guid}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "SingleReport" }) }
270 new MyRoute ("reports/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "Index", page = 1 }) }
274 new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index"}) }
277 var hc = new HttpContextStub2 ("~/Home/About", String.Empty, String.Empty);
278 hc.SetResponse (new HttpResponseStub (2));
279 var rd = c.GetRouteData (hc);
280 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
281 Assert.IsNotNull (vpd, "#A1");
282 Assert.AreEqual ("/Home/About_modified", vpd.VirtualPath, "#A2");
283 Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
285 hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
286 hc.SetResponse (new HttpResponseStub (2));
287 rd = c.GetRouteData (hc);
288 vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
289 Assert.IsNotNull (vpd, "#B1");
290 Assert.AreEqual ("/_modified", vpd.VirtualPath, "#B2");
291 Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
293 hc = new HttpContextStub2 ("~/Account/LogOn", String.Empty, String.Empty);
294 hc.SetResponse (new HttpResponseStub (2));
295 rd = c.GetRouteData (hc);
296 vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
297 Assert.IsNotNull (vpd, "#C1");
298 Assert.AreEqual ("/Account/LogOn_modified", vpd.VirtualPath, "#C2");
299 Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
301 hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
302 hc.SetResponse (new HttpResponseStub (3));
303 rd = c.GetRouteData (hc);
304 vpd = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }) );
305 Assert.IsNotNull (vpd, "#D1");
306 Assert.AreEqual ("/", vpd.VirtualPath, "#D2");
307 Assert.AreEqual (0, vpd.DataTokens.Count, "#D3");
309 hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
310 hc.SetResponse (new HttpResponseStub (3));
311 rd = c.GetRouteData (hc);
312 vpd = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "Home" }) );
313 Assert.IsNotNull (vpd, "#E1");
314 Assert.AreEqual ("/", vpd.VirtualPath, "#E2");
315 Assert.AreEqual (0, vpd.DataTokens.Count, "#E3");
319 public void GetVirtualPath3 ()
321 var c = new RouteCollection ();
324 new MyRoute ("todo/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "todo", action="list", page=0}) }
327 c.Add ("another-route",
328 new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "home", action="list", page=0}) }
331 var hc = new HttpContextStub2 ("~/home/list", String.Empty, String.Empty);
332 hc.SetResponse (new HttpResponseStub (3));
333 var rd = c.GetRouteData (hc);
334 Assert.IsNotNull (rd, "#1");
335 Assert.AreEqual (3, rd.Values.Count, "#1-1");
336 Assert.AreEqual ("home", rd.Values["controller"], "#1-2");
337 Assert.AreEqual ("list", rd.Values["action"], "#1-3");
338 Assert.AreEqual (0, rd.Values["page"], "#1-4");
340 var vp = c.GetVirtualPath (new RequestContext (hc, rd), "todo-route", new RouteValueDictionary ());
341 Assert.IsNotNull (vp, "#2");
342 Assert.AreEqual ("/todo", vp.VirtualPath, "#2-1");
344 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary ());
345 Assert.IsNotNull (vp, "#3");
346 Assert.AreEqual ("/todo", vp.VirtualPath, "#3-1");
348 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }));
349 Assert.IsNotNull (vp, "#4");
350 Assert.AreEqual ("/", vp.VirtualPath, "#4-1");
352 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home", extra="stuff" }));
353 Assert.IsNotNull (vp, "#5");
354 Assert.AreEqual ("/?extra=stuff", vp.VirtualPath, "#5-1");
358 public void GetVirtualPath4 ()
360 var c = new RouteCollection ();
362 c.Add (new MyRoute ("blog/{user}/{action}", new MyRouteHandler ()) {
363 Defaults = new RouteValueDictionary {
364 { "controller", "blog" },
370 c.Add (new MyRoute ("forum/{user}/{action}", new MyRouteHandler ()) {
371 Defaults = new RouteValueDictionary {
372 { "controller", "forum" },
378 var hc = new HttpContextStub2 ("~/forum/admin/Index", String.Empty, String.Empty);
379 hc.SetResponse (new HttpResponseStub (3));
380 var rd = c.GetRouteData (hc);
381 Assert.IsNotNull (rd, "#1");
383 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="forum"}));
384 Assert.IsNotNull (vp, "#2");
385 Assert.AreEqual ("/forum/admin/Index", vp.VirtualPath, "#2-1");
387 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="blah"}));
388 Assert.IsNull (vp, "#3");
392 public void GetVirtualPath5 ()
394 var c = new RouteCollection ();
396 c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
397 Defaults = new RouteValueDictionary {
403 var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, String.Empty);
404 hc.SetResponse (new HttpResponseStub (3));
405 var rd = c.GetRouteData (hc);
406 Assert.IsNotNull (rd, "#1");
408 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
414 Assert.IsNotNull (vp, "#2");
415 Assert.AreEqual ("/reports/2007/1/12", vp.VirtualPath, "#2-1");
417 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
422 Assert.IsNotNull (vp, "#3");
423 Assert.AreEqual ("/reports/2007/1", vp.VirtualPath, "#3-1");
425 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
432 Assert.IsNotNull (vp, "#4");
433 Assert.AreEqual ("/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
435 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
439 Assert.IsNull (vp, "#5");
443 public void GetVirtualPath6 ()
445 var c = new RouteCollection ();
447 c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
448 Defaults = new RouteValueDictionary {
454 var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, "/myapp");
455 hc.SetResponse (new HttpResponseStub (3));
456 var rd = c.GetRouteData (hc);
457 Assert.IsNotNull (rd, "#1");
459 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
465 Assert.IsNotNull (vp, "#2");
466 Assert.AreEqual ("/myapp/reports/2007/1/12", vp.VirtualPath, "#2-1");
468 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
473 Assert.IsNotNull (vp, "#3");
474 Assert.AreEqual ("/myapp/reports/2007/1", vp.VirtualPath, "#3-1");
476 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
483 Assert.IsNotNull (vp, "#4");
484 Assert.AreEqual ("/myapp/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
486 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
490 Assert.IsNull (vp, "#5");
494 public void GetVirtualPath7 ()
496 var c = new RouteCollection ();
498 c.Add (new MyRoute ("{table}/{action}.aspx", new MyRouteHandler ()) {
499 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
502 var req = new FakeHttpWorkerRequest ();
503 var ctx = new HttpContext (req);
504 HttpContext.Current = ctx;
505 var rd = new RouteData ();
506 var hc = new HttpContextWrapper (ctx);
508 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
509 {"Table", "FooTable"},
510 {"Action", "Details"}
513 Assert.IsNotNull (vp, "#A1");
514 Assert.AreEqual ("/FooTable/Details.aspx", vp.VirtualPath, "#A1-1");
516 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
517 {"Table", "FooTable"},
518 {"Action", String.Empty}
521 Assert.IsNull (vp, "#B1");
523 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
524 {"Table", "FooTable"},
528 Assert.IsNull (vp, "#C1");
532 public void GetVirtualPath8 ()
534 var routes = new RouteCollection();
536 routes.Add (new MyRoute ("login", new MyRouteHandler ()) {
537 Defaults = new RouteValueDictionary (new { controller = "Home", action = "LogOn" })
540 routes.Add (new MyRoute ("{site}/{controller}/{action}", new MyRouteHandler ()) {
541 Defaults = new RouteValueDictionary (new { site = "_", controller = "Home", action = "Index" }),
542 Constraints = new RouteValueDictionary ( new { site = "_?[0-9A-Za-z-]*" })
545 routes.Add (new MyRoute ("{*path}", new MyRouteHandler ()) {
546 Defaults = new RouteValueDictionary (new { controller = "Error", action = "NotFound" }),
549 var hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
550 hc.SetResponse (new HttpResponseStub (3));
551 var rd = routes.GetRouteData (hc);
552 var rvs = new RouteValueDictionary () {
553 { "controller", "Home" },
554 { "action" , "Index" }
556 var vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
557 Assert.IsNotNull (vpd, "#A1");
558 Assert.AreEqual ("/", vpd.VirtualPath, "#A2");
559 Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
561 hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
562 hc.SetResponse (new HttpResponseStub (3));
563 rd = routes.GetRouteData (hc);
564 rvs = new RouteValueDictionary () {
565 { "controller", "Home" }
567 vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
568 Assert.IsNotNull (vpd, "#B1");
569 Assert.AreEqual ("/login", vpd.VirtualPath, "#B2");
570 Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
572 hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
573 hc.SetResponse (new HttpResponseStub (3));
574 rd = routes.GetRouteData (hc);
575 rvs = new RouteValueDictionary () {
576 { "action" , "Index" }
578 vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
579 Assert.IsNotNull (vpd, "#C1");
580 Assert.AreEqual ("/", vpd.VirtualPath, "#C2");
581 Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
583 hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
584 rd = routes.GetRouteData (hc);
585 Assert.IsNotNull (rd, "#D1");
589 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
590 public void GetVirtualPathToExistingFile ()
592 var c = new RouteCollection ();
593 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
594 var hc = new HttpContextStub2 ("~/Test/test.html", String.Empty, ".");
595 // it tries to get HttpContextBase.Response, so set it.
596 hc.SetResponse (new HttpResponseStub (3));
597 var rd = c.GetRouteData (hc);
598 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
599 Assert.AreEqual ("./Test/test.html", vpd.VirtualPath, "#1");
600 Assert.AreEqual (0, vpd.DataTokens.Count, "#2");
603 [Test (Description="Routes from NerdDinner")]
604 public void GetRouteDataNerdDinner ()
606 var c = new RouteCollection ();
608 c.Add ("UpcomingDiners",
609 new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
613 new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
616 var hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
617 hc.SetResponse (new HttpResponseStub (3));
618 var rd = c.GetRouteData (hc);
620 Assert.IsNotNull (rd, "#A1");
623 [Test (Description="Routes from NerdDinner")]
624 public void GetRouteDataNerdDinner2 ()
626 var c = new RouteCollection ();
628 c.Add ("UpcomingDiners",
629 new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
633 new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
636 var hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
637 hc.SetResponse (new HttpResponseStub (3));
638 var rd = c.GetRouteData (hc);
640 Assert.IsNotNull (rd, "#A1");
643 public void Ignore_String ()
645 var c = new RouteCollection ();
647 AssertExtensions.Throws<ArgumentNullException> (() => {
651 c.Ignore ("{resource}.axd/{*pathInfo}");
652 var hc = new HttpContextStub2 ("~/something.axd/pathinfo", String.Empty, String.Empty);
653 hc.SetResponse (new HttpResponseStub (3));
654 var rd = c.GetRouteData (hc);
656 Assert.IsNotNull (rd, "#A1-1");
657 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
658 Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
659 Assert.IsTrue (rd.Route is Route, "#A1-4");
660 Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
661 Assert.AreEqual (0, ((Route) rd.Route).Constraints.Count, "#A1-6");
665 public void Ignore_String_Object ()
667 var c = new RouteCollection ();
669 AssertExtensions.Throws<ArgumentNullException> (() => {
670 c.Ignore (null, new { allaspx = @".*\.aspx(/.*)?" });
673 c.Ignore ("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
674 var hc = new HttpContextStub2 ("~/page.aspx", String.Empty, String.Empty);
675 hc.SetResponse (new HttpResponseStub (3));
676 var rd = c.GetRouteData (hc);
678 Assert.IsNotNull (rd, "#A1-1");
679 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
680 Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
681 Assert.IsTrue (rd.Route is Route, "#A1-4");
682 Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
683 Assert.AreEqual (1, ((Route) rd.Route).Constraints.Count, "#A1-6");
684 Assert.AreEqual (@".*\.aspx(/.*)?", ((Route) rd.Route).Constraints ["allaspx"], "#A1-7");
686 c = new RouteCollection ();
687 c.Ignore ("{*allaspx}", "something invalid");
689 AssertExtensions.Throws<InvalidOperationException> (() => {
690 rd = c.GetRouteData (hc);
695 public void MapPageRoute_String_String_String ()
697 var c = new RouteCollection ();
699 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url");
700 var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
701 hc.SetResponse (new HttpResponseStub (3));
702 var rd = c.GetRouteData (hc);
704 Assert.IsNotNull (rd, "#A1-1");
705 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
706 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
708 c = new RouteCollection ();
709 AssertExtensions.Throws<ArgumentNullException> (() => {
710 c.MapPageRoute ("RouteName", null, "~/some-url");
713 c = new RouteCollection ();
714 c.MapPageRoute ("RouteName", String.Empty, "~/some-url");
715 rd = c.GetRouteData (hc);
717 Assert.IsNull (rd, "#A2");
719 c = new RouteCollection ();
720 // thrown by PageRouteHandler's constructor
721 AssertExtensions.Throws<ArgumentException> (() => {
722 c.MapPageRoute ("RouteName", "~/some-url", null);
727 public void MapPageRoute_String_String_String_Bool ()
729 var c = new RouteCollection ();
731 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true);
732 var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
733 hc.SetResponse (new HttpResponseStub (3));
734 var rd = c.GetRouteData (hc);
736 Assert.IsNotNull (rd, "#A1-1");
737 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
738 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
739 Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
741 c = new RouteCollection ();
742 AssertExtensions.Throws<ArgumentNullException> (() => {
743 c.MapPageRoute ("RouteName", null, "~/some-url", true);
746 c = new RouteCollection ();
747 c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true);
748 rd = c.GetRouteData (hc);
750 Assert.IsNull (rd, "#A2");
752 c = new RouteCollection ();
753 // thrown by PageRouteHandler's constructor
754 AssertExtensions.Throws<ArgumentException> (() => {
755 c.MapPageRoute ("RouteName", "~/some-url", null, true);
758 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false);
759 rd = c.GetRouteData (hc);
761 Assert.IsNotNull (rd, "#A4-1");
762 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
763 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
764 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
768 public void MapPageRoute_String_String_String_Bool_RVD ()
770 var c = new RouteCollection ();
771 var defaults = new RouteValueDictionary ();
773 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults);
774 var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
775 hc.SetResponse (new HttpResponseStub (3));
776 var rd = c.GetRouteData (hc);
778 Assert.IsNotNull (rd, "#A1-1");
779 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
780 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
781 Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
783 c = new RouteCollection ();
784 AssertExtensions.Throws<ArgumentNullException> (() => {
785 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults);
788 c = new RouteCollection ();
789 c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults);
790 rd = c.GetRouteData (hc);
792 Assert.IsNull (rd, "#A2");
794 c = new RouteCollection ();
795 // thrown by PageRouteHandler's constructor
796 AssertExtensions.Throws<ArgumentException> (() => {
797 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults);
800 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults);
801 rd = c.GetRouteData (hc);
803 Assert.IsNotNull (rd, "#A4-1");
804 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
805 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
806 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
808 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null);
809 rd = c.GetRouteData (hc);
811 Assert.IsNotNull (rd, "#A4-1");
812 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
813 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
814 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
818 public void MapPageRoute_String_String_String_Bool_RVD_RVD ()
820 var c = new RouteCollection ();
821 var defaults = new RouteValueDictionary ();
822 var constraints = new RouteValueDictionary ();
824 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints);
825 var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
826 hc.SetResponse (new HttpResponseStub (3));
827 var rd = c.GetRouteData (hc);
829 Assert.IsNotNull (rd, "#A1-1");
830 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
831 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
832 Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
834 c = new RouteCollection ();
835 AssertExtensions.Throws<ArgumentNullException> (() => {
836 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints);
839 c = new RouteCollection ();
840 c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints);
841 rd = c.GetRouteData (hc);
843 Assert.IsNull (rd, "#A2");
845 c = new RouteCollection ();
846 // thrown by PageRouteHandler's constructor
847 AssertExtensions.Throws<ArgumentException> (() => {
848 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints);
851 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints);
852 rd = c.GetRouteData (hc);
854 Assert.IsNotNull (rd, "#A4-1");
855 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
856 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
857 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
859 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints);
860 rd = c.GetRouteData (hc);
862 Assert.IsNotNull (rd, "#A4-1");
863 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
864 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
865 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
869 public void MapPageRoute_String_String_String_Bool_RVD_RVD_RVD ()
871 var c = new RouteCollection ();
872 var defaults = new RouteValueDictionary ();
873 var constraints = new RouteValueDictionary ();
874 var dataTokens = new RouteValueDictionary ();
876 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints, dataTokens);
877 var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
878 hc.SetResponse (new HttpResponseStub (3));
879 var rd = c.GetRouteData (hc);
881 Assert.IsNotNull (rd, "#A1-1");
882 Assert.IsNotNull (rd.RouteHandler, "#A1-2");
883 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
884 Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
886 c = new RouteCollection ();
887 AssertExtensions.Throws<ArgumentNullException> (() => {
888 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints, dataTokens);
891 c = new RouteCollection ();
892 c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints, dataTokens);
893 rd = c.GetRouteData (hc);
895 Assert.IsNull (rd, "#A2");
897 c = new RouteCollection ();
898 // thrown by PageRouteHandler's constructor
899 AssertExtensions.Throws<ArgumentException> (() => {
900 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints, dataTokens);
903 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints, dataTokens);
904 rd = c.GetRouteData (hc);
906 Assert.IsNotNull (rd, "#A4-1");
907 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
908 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
909 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
911 c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints, dataTokens);
912 rd = c.GetRouteData (hc);
914 Assert.IsNotNull (rd, "#A4-1");
915 Assert.IsNotNull (rd.RouteHandler, "#A4-2");
916 Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
917 Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
920 [Test] // https://bugzilla.xamarin.com/show_bug.cgi?id=13909
921 public void MapPageRoute_Bug13909 ()
923 var c = new RouteCollection ();
925 c.MapPageRoute("test", "test", "~/test.aspx");
927 c.MapPageRoute("test", "test", "~/test.aspx");