Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mcs / class / System.Web.Routing / Test / System.Web.Routing / RouteCollectionTest.cs
1 //
2 // RouteCollectionTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System;
31 using System.Web;
32 using System.Web.Routing;
33 using NUnit.Framework;
34
35 using MonoTests.Common;
36
37 namespace MonoTests.System.Web.Routing
38 {
39         [TestFixture]
40         public class RouteCollectionTest
41         {
42                 [Test]
43                 public void ConstructorNullArgs ()
44                 {
45                         // allowed
46                         new RouteCollection (null);
47                 }
48
49                 [Test]
50                 public void RouteExistingFiles ()
51                 {
52                         var c = new RouteCollection ();
53                         Assert.IsFalse (c.RouteExistingFiles);
54                 }
55
56                 [Test]
57                 public void AddNullMame ()
58                 {
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));
63                 }
64
65                 [Test]
66                 public void AddDuplicateEmpty ()
67                 {
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));
72                 }
73
74                 [Test]
75                 [ExpectedException (typeof (ArgumentException))]
76                 public void AddDuplicateName ()
77                 {
78                         var c = new RouteCollection ();
79                         c.Add ("x", new Route (null, null));
80                         c.Add ("x", new Route (null, null));
81                 }
82
83                 [Test]
84                 public void IndexForNonExistent ()
85                 {
86                         Assert.IsNull (new RouteCollection () [null]);
87                 }
88
89                 [Test]
90                 public void IndexForExistent ()
91                 {
92                         var c = new RouteCollection ();
93                         var r = new Route (null, null);
94                         c.Add ("x", r);
95                         Assert.AreEqual (r, c ["x"]);
96                 }
97
98                 [Test]
99                 public void IndexForNonExistentAfterRemoval ()
100                 {
101                         var c = new RouteCollection ();
102                         var r = new Route (null, null);
103                         c.Add ("x", r);
104                         c.Remove (r);
105                         Assert.IsNull(c ["x"]);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void GetRouteDataNoRequest ()
111                 {
112                         new RouteCollection ().GetRouteData (new HttpContextStub (true));
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentNullException))]
117                 public void GetRouteDataNullArg ()
118                 {
119                         new RouteCollection ().GetRouteData (null);
120                 }
121
122                 [Test]
123                 public void GetRouteDataForNonExistent ()
124                 {
125                         var rd = new RouteCollection ().GetRouteData (new HttpContextStub ("~/foo"));
126                         Assert.IsNull (rd);
127                 }
128
129                 [Test]
130                 public void GetRouteDataForNonExistent2 ()
131                 {
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");
136                 }
137
138                 [Test]
139                 public void GetRouteDataWrongPathNoRoute ()
140                 {
141                         new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
142                 }
143
144                 /*
145                 comment out those tests; I cannot explain those tests.
146
147                 [Test]
148                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
149                 public void GetRouteDataWrongPathOneRoute ()
150                 {
151                         var c = new RouteCollection ();
152                         var r = new Route ("foo", null);
153                         c.Add (null, r);
154                         // it somehow causes ArgumentOutOfRangeException for 
155                         // Request.AppRelativeCurrentExecutionFilePath.
156                         c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
157                 }
158
159                 [Test]
160                 public void GetRouteDataWrongPathOneRoute2 ()
161                 {
162                         var c = new RouteCollection ();
163                         var r = new Route ("foo", null);
164                         c.Add (null, r);
165                         c.GetRouteData (new HttpContextStub ("/~", String.Empty));
166                 }
167                 */
168
169                 [Test]
170                 [ExpectedException (typeof (NotImplementedException))]
171                 public void GetRouteDataForPathInfoNIE ()
172                 {
173                         var c = new RouteCollection ();
174                         var r = new Route ("foo", null);
175                         c.Add (null, r);
176                         // it retrieves PathInfo and then dies.
177                         var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
178                 }
179
180                 [Test]
181                 public void GetRouteDataForNullHandler ()
182                 {
183                         var c = new RouteCollection ();
184                         var r = new Route ("foo", null); // allowed
185                         c.Add (null, r);
186                         var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
187                         Assert.IsNotNull (rd, "#1");
188                         Assert.AreEqual (r, rd.Route, "#2");
189                 }
190
191                 // below tests in RouteCollection, unlike Route, do some additional checks than Route.GetVirtualPath().
192
193                 [Test]
194                 [ExpectedException (typeof (NotImplementedException))]
195                 public void GetVirtualPathNoApplicationPath ()
196                 {
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);
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (NotImplementedException))]
207                 public void GetVirtualPathNoApplyAppPathModifier ()
208                 {
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);
216                 }
217
218                 [Test]
219                 public void GetVirtualPathCheckVirtualPathToModify ()
220                 {
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);
227                         try {
228                                 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
229                                 Assert.Fail ("#1");
230                         } catch (ApplicationException ex) {
231                                 Assert.AreEqual ("apppath/x/y", ex.Message, "#2");
232                         }
233                 }
234
235                 [Test]
236                 public void GetVirtualPath ()
237                 {
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");
245                         
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");
250                 }
251
252                 [Test (Description = "Bug #502555")]
253                 public void GetVirtualPath2 ()
254                 {
255                         var c = new RouteCollection ();
256                         
257                         c.Add ("Summary",
258                                new MyRoute ("summary/{action}-{type}/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Summary", action = "Index", page = 1}) }
259                         );
260                                
261                         c.Add ("Apis",
262                                new MyRoute ("apis/{apiname}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Apis", action = "Index" }) }
263                         );
264                                                             
265                         c.Add ("Single Report",
266                                new MyRoute ("report/{guid}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "SingleReport" }) }
267                         );
268                         
269                         c.Add ("Reports",
270                                new MyRoute ("reports/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "Index", page = 1 }) }
271                         );
272
273                         c.Add ("Default",
274                                new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index"}) }
275                         );
276
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");
284
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");
292
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");
300
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");
308
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");
316                 }
317
318                 [Test]
319                 public void GetVirtualPath3 ()
320                 {
321                         var c = new RouteCollection ();
322
323                         c.Add ("todo-route",
324                                new MyRoute ("todo/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "todo", action="list", page=0}) }
325                         );
326
327                         c.Add ("another-route",
328                                new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "home", action="list", page=0}) }
329                         );
330
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");
339                         
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");
343
344                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary ());
345                         Assert.IsNotNull (vp, "#3");
346                         Assert.AreEqual ("/todo", vp.VirtualPath, "#3-1");
347
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");
351
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");
355                 }
356
357                 [Test]
358                 public void GetVirtualPath4 ()
359                 {
360                         var c = new RouteCollection ();
361
362                         c.Add (new MyRoute ("blog/{user}/{action}", new MyRouteHandler ()) {
363                                         Defaults = new RouteValueDictionary {
364                                                         { "controller", "blog" },
365                                                         { "user", "admin" }
366                                                 }
367                                 }
368                         );
369
370                         c.Add (new MyRoute ("forum/{user}/{action}", new MyRouteHandler ()) {
371                                         Defaults = new RouteValueDictionary {
372                                                         { "controller", "forum" },
373                                                         { "user", "admin" }
374                                                 }
375                                 }
376                         );
377
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");
382
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");
386                         
387                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="blah"}));
388                         Assert.IsNull (vp, "#3");
389                 }
390
391                 [Test]
392                 public void GetVirtualPath5 ()
393                 {
394                         var c = new RouteCollection ();
395
396                         c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
397                                         Defaults = new RouteValueDictionary {
398                                                         { "day", 1 }
399                                                 }
400                                 }
401                         );
402
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");
407
408                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
409                                         { "year", 2007 },
410                                         { "month", 1 },
411                                         { "day", 12 },
412                                 }
413                         );
414                         Assert.IsNotNull (vp, "#2");
415                         Assert.AreEqual ("/reports/2007/1/12", vp.VirtualPath, "#2-1");
416                         
417                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
418                                         { "year", 2007 },
419                                         { "month", 1 }
420                                 }
421                         );
422                         Assert.IsNotNull (vp, "#3");
423                         Assert.AreEqual ("/reports/2007/1", vp.VirtualPath, "#3-1");
424
425                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
426                                         { "year", 2007 },
427                                         { "month", 1 },
428                                         { "day", 12 },
429                                         { "category", 123 }
430                                 }
431                         );
432                         Assert.IsNotNull (vp, "#4");
433                         Assert.AreEqual ("/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
434
435                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
436                                         { "year", 2007 },
437                                 }
438                         );
439                         Assert.IsNull (vp, "#5");
440                 }
441
442                 [Test]
443                 public void GetVirtualPath6 ()
444                 {
445                         var c = new RouteCollection ();
446
447                         c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
448                                         Defaults = new RouteValueDictionary {
449                                                         { "day", 1 }
450                                                 }
451                                 }
452                         );
453
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");
458
459                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
460                                         { "year", 2007 },
461                                         { "month", 1 },
462                                         { "day", 12 },
463                                 }
464                         );
465                         Assert.IsNotNull (vp, "#2");
466                         Assert.AreEqual ("/myapp/reports/2007/1/12", vp.VirtualPath, "#2-1");
467                         
468                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
469                                         { "year", 2007 },
470                                         { "month", 1 }
471                                 }
472                         );
473                         Assert.IsNotNull (vp, "#3");
474                         Assert.AreEqual ("/myapp/reports/2007/1", vp.VirtualPath, "#3-1");
475
476                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
477                                         { "year", 2007 },
478                                         { "month", 1 },
479                                         { "day", 12 },
480                                         { "category", 123 }
481                                 }
482                         );
483                         Assert.IsNotNull (vp, "#4");
484                         Assert.AreEqual ("/myapp/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
485                         
486                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
487                                         { "year", 2007 },
488                                 }
489                         );
490                         Assert.IsNull (vp, "#5");
491                 }
492
493                 [Test]
494                 public void GetVirtualPath7 ()
495                 {
496                         var c = new RouteCollection ();
497
498                         c.Add (new MyRoute ("{table}/{action}.aspx", new MyRouteHandler ()) {
499                                 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
500                         });
501
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);
507
508                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
509                                 {"Table", "FooTable"},
510                                 {"Action", "Details"}
511                         });
512
513                         Assert.IsNotNull (vp, "#A1");
514                         Assert.AreEqual ("/FooTable/Details.aspx", vp.VirtualPath, "#A1-1");
515
516                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
517                                 {"Table", "FooTable"},
518                                 {"Action", String.Empty}
519                         });
520
521                         Assert.IsNull (vp, "#B1");
522
523                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
524                                 {"Table", "FooTable"},
525                                 {"Action", null}
526                         });
527
528                         Assert.IsNull (vp, "#C1");
529                 }
530
531                 [Test]
532                 public void GetVirtualPath8 ()
533                 {
534                         var routes = new RouteCollection();
535
536                         routes.Add (new MyRoute ("login", new MyRouteHandler ()) {
537                                 Defaults = new RouteValueDictionary (new { controller = "Home", action = "LogOn" })
538                         });
539
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-]*" })
543                         });
544
545                         routes.Add (new MyRoute ("{*path}", new MyRouteHandler ()) {
546                                 Defaults = new RouteValueDictionary (new { controller = "Error", action = "NotFound" }),
547                         });
548
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" }
555                         };
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");
560
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" }
566                         };
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");
571
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" }
577                         };
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");
582
583                         hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
584                         rd = routes.GetRouteData (hc);
585                         Assert.IsNotNull (rd, "#D1");
586                 }
587
588                 [Test]
589                 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
590                 public void GetVirtualPathToExistingFile ()
591                 {
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");
601                 }
602
603                 [Test (Description="Routes from NerdDinner")]
604                 public void GetRouteDataNerdDinner ()
605                 {
606                         var c = new RouteCollection ();
607
608                         c.Add ("UpcomingDiners",
609                                new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
610                         );
611
612                         c.Add ("Default",
613                                new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
614                         );
615
616                         var hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
617                         hc.SetResponse (new HttpResponseStub (3));
618                         var rd = c.GetRouteData (hc);
619                         
620                         Assert.IsNotNull (rd, "#A1");
621                 }
622
623                 [Test (Description="Routes from NerdDinner")]
624                 public void GetRouteDataNerdDinner2 ()
625                 {
626                         var c = new RouteCollection ();
627
628                         c.Add ("UpcomingDiners",
629                                new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
630                         );
631
632                         c.Add ("Default",
633                                new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
634                         );
635
636                         var hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
637                         hc.SetResponse (new HttpResponseStub (3));
638                         var rd = c.GetRouteData (hc);
639                         
640                         Assert.IsNotNull (rd, "#A1");
641                 }
642                 [Test]
643                 public void Ignore_String ()
644                 {
645                         var c = new RouteCollection ();
646
647                         AssertExtensions.Throws<ArgumentNullException> (() => {
648                                 c.Ignore (null);
649                         }, "#A1");
650
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);
655
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");
662                 }
663
664                 [Test]
665                 public void Ignore_String_Object ()
666                 {
667                         var c = new RouteCollection ();
668
669                         AssertExtensions.Throws<ArgumentNullException> (() => {
670                                 c.Ignore (null, new { allaspx = @".*\.aspx(/.*)?" });
671                         }, "#A1");
672
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);
677
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");
685
686                         c = new RouteCollection ();
687                         c.Ignore ("{*allaspx}", "something invalid");
688
689                         AssertExtensions.Throws<InvalidOperationException> (() => {
690                                 rd = c.GetRouteData (hc);
691                         }, "#A2");
692                 }
693
694                 [Test]
695                 public void MapPageRoute_String_String_String ()
696                 {
697                         var c = new RouteCollection ();
698
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);
703
704                         Assert.IsNotNull (rd, "#A1-1");
705                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
706                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
707
708                         c = new RouteCollection ();
709                         AssertExtensions.Throws<ArgumentNullException> (() => {
710                                 c.MapPageRoute ("RouteName", null, "~/some-url");
711                         }, "#A2");
712
713                         c = new RouteCollection ();
714                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url");
715                         rd = c.GetRouteData (hc);
716
717                         Assert.IsNull (rd, "#A2");
718
719                         c = new RouteCollection ();
720                         // thrown by PageRouteHandler's constructor
721                         AssertExtensions.Throws<ArgumentException> (() => {
722                                 c.MapPageRoute ("RouteName", "~/some-url", null);
723                         }, "#A3");
724                 }
725
726                 [Test]
727                 public void MapPageRoute_String_String_String_Bool ()
728                 {
729                         var c = new RouteCollection ();
730
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);
735
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");
740
741                         c = new RouteCollection ();
742                         AssertExtensions.Throws<ArgumentNullException> (() => {
743                                 c.MapPageRoute ("RouteName", null, "~/some-url", true);
744                         }, "#A2");
745
746                         c = new RouteCollection ();
747                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true);
748                         rd = c.GetRouteData (hc);
749
750                         Assert.IsNull (rd, "#A2");
751
752                         c = new RouteCollection ();
753                         // thrown by PageRouteHandler's constructor
754                         AssertExtensions.Throws<ArgumentException> (() => {
755                                 c.MapPageRoute ("RouteName", "~/some-url", null, true);
756                         }, "#A3");
757
758                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false);
759                         rd = c.GetRouteData (hc);
760
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");
765                 }
766
767                 [Test]
768                 public void MapPageRoute_String_String_String_Bool_RVD ()
769                 {
770                         var c = new RouteCollection ();
771                         var defaults = new RouteValueDictionary ();
772
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);
777
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");
782
783                         c = new RouteCollection ();
784                         AssertExtensions.Throws<ArgumentNullException> (() => {
785                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults);
786                         }, "#A2");
787
788                         c = new RouteCollection ();
789                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults);
790                         rd = c.GetRouteData (hc);
791
792                         Assert.IsNull (rd, "#A2");
793
794                         c = new RouteCollection ();
795                         // thrown by PageRouteHandler's constructor
796                         AssertExtensions.Throws<ArgumentException> (() => {
797                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults);
798                         }, "#A3");
799
800                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults);
801                         rd = c.GetRouteData (hc);
802
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");
807
808                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null);
809                         rd = c.GetRouteData (hc);
810
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"); 
815                 }
816
817                 [Test]
818                 public void MapPageRoute_String_String_String_Bool_RVD_RVD ()
819                 {
820                         var c = new RouteCollection ();
821                         var defaults = new RouteValueDictionary ();
822                         var constraints = new RouteValueDictionary ();
823
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);
828
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");
833
834                         c = new RouteCollection ();
835                         AssertExtensions.Throws<ArgumentNullException> (() => {
836                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints);
837                         }, "#A2");
838
839                         c = new RouteCollection ();
840                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints);
841                         rd = c.GetRouteData (hc);
842
843                         Assert.IsNull (rd, "#A2");
844
845                         c = new RouteCollection ();
846                         // thrown by PageRouteHandler's constructor
847                         AssertExtensions.Throws<ArgumentException> (() => {
848                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints);
849                         }, "#A3");
850
851                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints);
852                         rd = c.GetRouteData (hc);
853
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");
858
859                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints);
860                         rd = c.GetRouteData (hc);
861
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");
866                 }
867
868                 [Test]
869                 public void MapPageRoute_String_String_String_Bool_RVD_RVD_RVD ()
870                 {
871                         var c = new RouteCollection ();
872                         var defaults = new RouteValueDictionary ();
873                         var constraints = new RouteValueDictionary ();
874                         var dataTokens = new RouteValueDictionary ();
875
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);
880
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");
885
886                         c = new RouteCollection ();
887                         AssertExtensions.Throws<ArgumentNullException> (() => {
888                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints, dataTokens);
889                         }, "#A2");
890
891                         c = new RouteCollection ();
892                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints, dataTokens);
893                         rd = c.GetRouteData (hc);
894
895                         Assert.IsNull (rd, "#A2");
896
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);
901                         }, "#A3");
902
903                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints, dataTokens);
904                         rd = c.GetRouteData (hc);
905
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");
910
911                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints, dataTokens);
912                         rd = c.GetRouteData (hc);
913
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");
918                 }
919                 
920                 [Test] // https://bugzilla.xamarin.com/show_bug.cgi?id=13909
921                 public void MapPageRoute_Bug13909 ()
922                 {
923                         var c = new RouteCollection ();
924
925                         c.MapPageRoute("test", "test", "~/test.aspx");
926                         c.Clear();
927                         c.MapPageRoute("test", "test", "~/test.aspx");
928                 }
929         }
930 }