[System] UriKind.RelativeOrAbsolute workaround.
[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 #if NET_4_0
135                         rd = new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
136                         Assert.IsNull (rd, "#A2");
137 #else
138                         try {
139                                 new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
140                                 Assert.Fail ("#A3");
141                         } catch (NotImplementedException) {
142                                 // it should fail due to the NIE on AppRelativeCurrentExecutionFilePath.
143                         }
144 #endif
145                 }
146
147                 [Test]
148                 public void GetRouteDataWrongPathNoRoute ()
149                 {
150                         new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
151                 }
152
153                 /*
154                 comment out those tests; I cannot explain those tests.
155
156                 [Test]
157                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
158                 public void GetRouteDataWrongPathOneRoute ()
159                 {
160                         var c = new RouteCollection ();
161                         var r = new Route ("foo", null);
162                         c.Add (null, r);
163                         // it somehow causes ArgumentOutOfRangeException for 
164                         // Request.AppRelativeCurrentExecutionFilePath.
165                         c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
166                 }
167
168                 [Test]
169                 public void GetRouteDataWrongPathOneRoute2 ()
170                 {
171                         var c = new RouteCollection ();
172                         var r = new Route ("foo", null);
173                         c.Add (null, r);
174                         c.GetRouteData (new HttpContextStub ("/~", String.Empty));
175                 }
176                 */
177
178                 [Test]
179                 [ExpectedException (typeof (NotImplementedException))]
180                 public void GetRouteDataForPathInfoNIE ()
181                 {
182                         var c = new RouteCollection ();
183                         var r = new Route ("foo", null);
184                         c.Add (null, r);
185                         // it retrieves PathInfo and then dies.
186                         var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
187                 }
188
189                 [Test]
190                 public void GetRouteDataForNullHandler ()
191                 {
192                         var c = new RouteCollection ();
193                         var r = new Route ("foo", null); // allowed
194                         c.Add (null, r);
195                         var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
196                         Assert.IsNotNull (rd, "#1");
197                         Assert.AreEqual (r, rd.Route, "#2");
198                 }
199
200                 // below tests in RouteCollection, unlike Route, do some additional checks than Route.GetVirtualPath().
201
202                 [Test]
203                 [ExpectedException (typeof (NotImplementedException))]
204                 public void GetVirtualPathNoApplicationPath ()
205                 {
206                         var c = new RouteCollection ();
207                         c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
208                         var hc = new HttpContextStub2 ("~/x/y", String.Empty);
209                         var rd = c.GetRouteData (hc);
210                         // it tries to get HttpContextBase.Request.ApplicationPath and then throws NIE.
211                         var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (NotImplementedException))]
216                 public void GetVirtualPathNoApplyAppPathModifier ()
217                 {
218                         var c = new RouteCollection ();
219                         c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
220                         var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
221                         // it tries to call HttpContextBase.Response.ApplyAppPathModifier() and then causes NIE.
222                         hc.SetResponse (new HttpResponseStub ());
223                         var rd = c.GetRouteData (hc);
224                         var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
225                 }
226
227                 [Test]
228                 public void GetVirtualPathCheckVirtualPathToModify ()
229                 {
230                         var c = new RouteCollection ();
231                         c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
232                         var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
233                         // it tries to get HttpContextBase.Response, so set it.
234                         hc.SetResponse (new HttpResponseStub (1));
235                         var rd = c.GetRouteData (hc);
236                         try {
237                                 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
238                                 Assert.Fail ("#1");
239                         } catch (ApplicationException ex) {
240                                 Assert.AreEqual ("apppath/x/y", ex.Message, "#2");
241                         }
242                 }
243
244                 [Test]
245                 public void GetVirtualPath ()
246                 {
247                         var c = new RouteCollection ();
248                         c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
249                         var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
250                         // it tries to get HttpContextBase.Response, so set it.
251                         hc.SetResponse (new HttpResponseStub (2));
252                         var rd = c.GetRouteData (hc);
253                         Assert.IsNotNull (rd, "#1");
254                         
255                         var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
256                         Assert.IsNotNull (vpd, "#2");
257                         Assert.AreEqual ("apppath/x/y_modified", vpd.VirtualPath, "#3");
258                         Assert.AreEqual (0, vpd.DataTokens.Count, "#4");
259                 }
260
261                 [Test (Description = "Bug #502555")]
262                 public void GetVirtualPath2 ()
263                 {
264                         var c = new RouteCollection ();
265                         
266                         c.Add ("Summary",
267                                new MyRoute ("summary/{action}-{type}/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Summary", action = "Index", page = 1}) }
268                         );
269                                
270                         c.Add ("Apis",
271                                new MyRoute ("apis/{apiname}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Apis", action = "Index" }) }
272                         );
273                                                             
274                         c.Add ("Single Report",
275                                new MyRoute ("report/{guid}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "SingleReport" }) }
276                         );
277                         
278                         c.Add ("Reports",
279                                new MyRoute ("reports/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "Index", page = 1 }) }
280                         );
281
282                         c.Add ("Default",
283                                new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index"}) }
284                         );
285
286                         var hc = new HttpContextStub2 ("~/Home/About", String.Empty, String.Empty);
287                         hc.SetResponse (new HttpResponseStub (2));
288                         var rd = c.GetRouteData (hc);
289                         var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
290                         Assert.IsNotNull (vpd, "#A1");
291                         Assert.AreEqual ("/Home/About_modified", vpd.VirtualPath, "#A2");
292                         Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
293
294                         hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
295                         hc.SetResponse (new HttpResponseStub (2));
296                         rd = c.GetRouteData (hc);
297                         vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
298                         Assert.IsNotNull (vpd, "#B1");
299                         Assert.AreEqual ("/_modified", vpd.VirtualPath, "#B2");
300                         Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
301
302                         hc = new HttpContextStub2 ("~/Account/LogOn", String.Empty, String.Empty);
303                         hc.SetResponse (new HttpResponseStub (2));
304                         rd = c.GetRouteData (hc);
305                         vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
306                         Assert.IsNotNull (vpd, "#C1");
307                         Assert.AreEqual ("/Account/LogOn_modified", vpd.VirtualPath, "#C2");
308                         Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
309
310                         hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
311                         hc.SetResponse (new HttpResponseStub (3));
312                         rd = c.GetRouteData (hc);
313                         vpd = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }) );
314                         Assert.IsNotNull (vpd, "#D1");
315                         Assert.AreEqual ("/", vpd.VirtualPath, "#D2");
316                         Assert.AreEqual (0, vpd.DataTokens.Count, "#D3");
317
318                         hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
319                         hc.SetResponse (new HttpResponseStub (3));
320                         rd = c.GetRouteData (hc);
321                         vpd = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "Home" }) );
322                         Assert.IsNotNull (vpd, "#E1");
323                         Assert.AreEqual ("/", vpd.VirtualPath, "#E2");
324                         Assert.AreEqual (0, vpd.DataTokens.Count, "#E3");
325                 }
326
327                 [Test]
328                 public void GetVirtualPath3 ()
329                 {
330                         var c = new RouteCollection ();
331
332                         c.Add ("todo-route",
333                                new MyRoute ("todo/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "todo", action="list", page=0}) }
334                         );
335
336                         c.Add ("another-route",
337                                new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "home", action="list", page=0}) }
338                         );
339
340                         var hc = new HttpContextStub2 ("~/home/list", String.Empty, String.Empty);
341                         hc.SetResponse (new HttpResponseStub (3));
342                         var rd = c.GetRouteData (hc);
343                         Assert.IsNotNull (rd, "#1");
344                         Assert.AreEqual (3, rd.Values.Count, "#1-1");
345                         Assert.AreEqual ("home", rd.Values["controller"], "#1-2");
346                         Assert.AreEqual ("list", rd.Values["action"], "#1-3");
347                         Assert.AreEqual (0, rd.Values["page"], "#1-4");
348                         
349                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), "todo-route", new RouteValueDictionary ());
350                         Assert.IsNotNull (vp, "#2");
351                         Assert.AreEqual ("/todo", vp.VirtualPath, "#2-1");
352
353                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary ());
354                         Assert.IsNotNull (vp, "#3");
355                         Assert.AreEqual ("/todo", vp.VirtualPath, "#3-1");
356
357                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }));
358                         Assert.IsNotNull (vp, "#4");
359                         Assert.AreEqual ("/", vp.VirtualPath, "#4-1");
360
361                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home", extra="stuff" }));
362                         Assert.IsNotNull (vp, "#5");
363                         Assert.AreEqual ("/?extra=stuff", vp.VirtualPath, "#5-1");
364                 }
365
366                 [Test]
367                 public void GetVirtualPath4 ()
368                 {
369                         var c = new RouteCollection ();
370
371                         c.Add (new MyRoute ("blog/{user}/{action}", new MyRouteHandler ()) {
372                                         Defaults = new RouteValueDictionary {
373                                                         { "controller", "blog" },
374                                                         { "user", "admin" }
375                                                 }
376                                 }
377                         );
378
379                         c.Add (new MyRoute ("forum/{user}/{action}", new MyRouteHandler ()) {
380                                         Defaults = new RouteValueDictionary {
381                                                         { "controller", "forum" },
382                                                         { "user", "admin" }
383                                                 }
384                                 }
385                         );
386
387                         var hc = new HttpContextStub2 ("~/forum/admin/Index", String.Empty, String.Empty);
388                         hc.SetResponse (new HttpResponseStub (3));
389                         var rd = c.GetRouteData (hc);
390                         Assert.IsNotNull (rd, "#1");
391
392                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="forum"}));
393                         Assert.IsNotNull (vp, "#2");
394                         Assert.AreEqual ("/forum/admin/Index", vp.VirtualPath, "#2-1");
395                         
396                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="blah"}));
397                         Assert.IsNull (vp, "#3");
398                 }
399
400                 [Test]
401                 public void GetVirtualPath5 ()
402                 {
403                         var c = new RouteCollection ();
404
405                         c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
406                                         Defaults = new RouteValueDictionary {
407                                                         { "day", 1 }
408                                                 }
409                                 }
410                         );
411
412                         var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, String.Empty);
413                         hc.SetResponse (new HttpResponseStub (3));
414                         var rd = c.GetRouteData (hc);
415                         Assert.IsNotNull (rd, "#1");
416
417                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
418                                         { "year", 2007 },
419                                         { "month", 1 },
420                                         { "day", 12 },
421                                 }
422                         );
423                         Assert.IsNotNull (vp, "#2");
424                         Assert.AreEqual ("/reports/2007/1/12", vp.VirtualPath, "#2-1");
425                         
426                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
427                                         { "year", 2007 },
428                                         { "month", 1 }
429                                 }
430                         );
431                         Assert.IsNotNull (vp, "#3");
432                         Assert.AreEqual ("/reports/2007/1", vp.VirtualPath, "#3-1");
433
434                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
435                                         { "year", 2007 },
436                                         { "month", 1 },
437                                         { "day", 12 },
438                                         { "category", 123 }
439                                 }
440                         );
441                         Assert.IsNotNull (vp, "#4");
442                         Assert.AreEqual ("/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
443
444                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
445                                         { "year", 2007 },
446                                 }
447                         );
448                         Assert.IsNull (vp, "#5");
449                 }
450
451                 [Test]
452                 public void GetVirtualPath6 ()
453                 {
454                         var c = new RouteCollection ();
455
456                         c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
457                                         Defaults = new RouteValueDictionary {
458                                                         { "day", 1 }
459                                                 }
460                                 }
461                         );
462
463                         var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, "/myapp");
464                         hc.SetResponse (new HttpResponseStub (3));
465                         var rd = c.GetRouteData (hc);
466                         Assert.IsNotNull (rd, "#1");
467
468                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
469                                         { "year", 2007 },
470                                         { "month", 1 },
471                                         { "day", 12 },
472                                 }
473                         );
474                         Assert.IsNotNull (vp, "#2");
475                         Assert.AreEqual ("/myapp/reports/2007/1/12", vp.VirtualPath, "#2-1");
476                         
477                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
478                                         { "year", 2007 },
479                                         { "month", 1 }
480                                 }
481                         );
482                         Assert.IsNotNull (vp, "#3");
483                         Assert.AreEqual ("/myapp/reports/2007/1", vp.VirtualPath, "#3-1");
484
485                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
486                                         { "year", 2007 },
487                                         { "month", 1 },
488                                         { "day", 12 },
489                                         { "category", 123 }
490                                 }
491                         );
492                         Assert.IsNotNull (vp, "#4");
493                         Assert.AreEqual ("/myapp/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
494                         
495                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
496                                         { "year", 2007 },
497                                 }
498                         );
499                         Assert.IsNull (vp, "#5");
500                 }
501
502                 [Test]
503                 public void GetVirtualPath7 ()
504                 {
505                         var c = new RouteCollection ();
506
507                         c.Add (new MyRoute ("{table}/{action}.aspx", new MyRouteHandler ()) {
508                                 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
509                         });
510
511                         var req = new FakeHttpWorkerRequest ();
512                         var ctx = new HttpContext (req);
513                         HttpContext.Current = ctx;
514                         var rd = new RouteData ();
515                         var hc = new HttpContextWrapper (ctx);
516
517                         var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
518                                 {"Table", "FooTable"},
519                                 {"Action", "Details"}
520                         });
521
522                         Assert.IsNotNull (vp, "#A1");
523                         Assert.AreEqual ("/FooTable/Details.aspx", vp.VirtualPath, "#A1-1");
524
525                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
526                                 {"Table", "FooTable"},
527                                 {"Action", String.Empty}
528                         });
529
530                         Assert.IsNull (vp, "#B1");
531
532                         vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
533                                 {"Table", "FooTable"},
534                                 {"Action", null}
535                         });
536
537                         Assert.IsNull (vp, "#C1");
538                 }
539
540                 [Test]
541                 public void GetVirtualPath8 ()
542                 {
543                         var routes = new RouteCollection();
544
545                         routes.Add (new MyRoute ("login", new MyRouteHandler ()) {
546                                 Defaults = new RouteValueDictionary (new { controller = "Home", action = "LogOn" })
547                         });
548
549                         routes.Add (new MyRoute ("{site}/{controller}/{action}", new MyRouteHandler ()) {
550                                 Defaults = new RouteValueDictionary (new { site = "_", controller = "Home", action = "Index" }),
551                                 Constraints = new RouteValueDictionary ( new { site = "_?[0-9A-Za-z-]*" })
552                         });
553
554                         routes.Add (new MyRoute ("{*path}", new MyRouteHandler ()) {
555                                 Defaults = new RouteValueDictionary (new { controller = "Error", action = "NotFound" }),
556                         });
557
558                         var hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
559                         hc.SetResponse (new HttpResponseStub (3));
560                         var rd = routes.GetRouteData (hc);
561                         var rvs = new RouteValueDictionary () {
562                                 { "controller", "Home" },
563                                 { "action" , "Index" }
564                         };
565                         var vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
566                         Assert.IsNotNull (vpd, "#A1");
567                         Assert.AreEqual ("/", vpd.VirtualPath, "#A2");
568                         Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
569
570                         hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
571                         hc.SetResponse (new HttpResponseStub (3));
572                         rd = routes.GetRouteData (hc);
573                         rvs = new RouteValueDictionary () {
574                                 { "controller", "Home" }
575                         };
576                         vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
577                         Assert.IsNotNull (vpd, "#B1");
578                         Assert.AreEqual ("/login", vpd.VirtualPath, "#B2");
579                         Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
580
581                         hc = new HttpContextStub2 ("~/login", String.Empty, String.Empty);
582                         hc.SetResponse (new HttpResponseStub (3));
583                         rd = routes.GetRouteData (hc);
584                         rvs = new RouteValueDictionary () {
585                                 { "action" , "Index" }
586                         };
587                         vpd = routes.GetVirtualPath (new RequestContext (hc, rd), rvs);
588                         Assert.IsNotNull (vpd, "#C1");
589                         Assert.AreEqual ("/", vpd.VirtualPath, "#C2");
590                         Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
591
592                         hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
593                         rd = routes.GetRouteData (hc);
594                         Assert.IsNotNull (rd, "#D1");
595                 }
596
597                 [Test]
598                 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
599                 public void GetVirtualPathToExistingFile ()
600                 {
601                         var c = new RouteCollection ();
602                         c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
603                         var hc = new HttpContextStub2 ("~/Test/test.html", String.Empty, ".");
604                         // it tries to get HttpContextBase.Response, so set it.
605                         hc.SetResponse (new HttpResponseStub (3));
606                         var rd = c.GetRouteData (hc);
607                         var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
608                         Assert.AreEqual ("./Test/test.html", vpd.VirtualPath, "#1");
609                         Assert.AreEqual (0, vpd.DataTokens.Count, "#2");
610                 }
611
612                 [Test (Description="Routes from NerdDinner")]
613                 public void GetRouteDataNerdDinner ()
614                 {
615                         var c = new RouteCollection ();
616
617                         c.Add ("UpcomingDiners",
618                                new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
619                         );
620
621                         c.Add ("Default",
622                                new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
623                         );
624
625                         var hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
626                         hc.SetResponse (new HttpResponseStub (3));
627                         var rd = c.GetRouteData (hc);
628                         
629                         Assert.IsNotNull (rd, "#A1");
630                 }
631
632                 [Test (Description="Routes from NerdDinner")]
633                 public void GetRouteDataNerdDinner2 ()
634                 {
635                         var c = new RouteCollection ();
636
637                         c.Add ("UpcomingDiners",
638                                new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
639                         );
640
641                         c.Add ("Default",
642                                new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
643                         );
644
645                         var hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
646                         hc.SetResponse (new HttpResponseStub (3));
647                         var rd = c.GetRouteData (hc);
648                         
649                         Assert.IsNotNull (rd, "#A1");
650                 }
651 #if NET_4_0
652                 [Test]
653                 public void Ignore_String ()
654                 {
655                         var c = new RouteCollection ();
656
657                         AssertExtensions.Throws<ArgumentNullException> (() => {
658                                 c.Ignore (null);
659                         }, "#A1");
660
661                         c.Ignore ("{resource}.axd/{*pathInfo}");
662                         var hc = new HttpContextStub2 ("~/something.axd/pathinfo", String.Empty, String.Empty);
663                         hc.SetResponse (new HttpResponseStub (3));
664                         var rd = c.GetRouteData (hc);
665
666                         Assert.IsNotNull (rd, "#A1-1");
667                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
668                         Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
669                         Assert.IsTrue (rd.Route is Route, "#A1-4");
670                         Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
671                         Assert.AreEqual (0, ((Route) rd.Route).Constraints.Count, "#A1-6");
672                 }
673
674                 [Test]
675                 public void Ignore_String_Object ()
676                 {
677                         var c = new RouteCollection ();
678
679                         AssertExtensions.Throws<ArgumentNullException> (() => {
680                                 c.Ignore (null, new { allaspx = @".*\.aspx(/.*)?" });
681                         }, "#A1");
682
683                         c.Ignore ("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
684                         var hc = new HttpContextStub2 ("~/page.aspx", String.Empty, String.Empty);
685                         hc.SetResponse (new HttpResponseStub (3));
686                         var rd = c.GetRouteData (hc);
687
688                         Assert.IsNotNull (rd, "#A1-1");
689                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
690                         Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
691                         Assert.IsTrue (rd.Route is Route, "#A1-4");
692                         Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
693                         Assert.AreEqual (1, ((Route) rd.Route).Constraints.Count, "#A1-6");
694                         Assert.AreEqual (@".*\.aspx(/.*)?", ((Route) rd.Route).Constraints ["allaspx"], "#A1-7");
695
696                         c = new RouteCollection ();
697                         c.Ignore ("{*allaspx}", "something invalid");
698
699                         AssertExtensions.Throws<InvalidOperationException> (() => {
700                                 rd = c.GetRouteData (hc);
701                         }, "#A2");
702                 }
703
704                 [Test]
705                 public void MapPageRoute_String_String_String ()
706                 {
707                         var c = new RouteCollection ();
708
709                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url");
710                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
711                         hc.SetResponse (new HttpResponseStub (3));
712                         var rd = c.GetRouteData (hc);
713
714                         Assert.IsNotNull (rd, "#A1-1");
715                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
716                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
717
718                         c = new RouteCollection ();
719                         AssertExtensions.Throws<ArgumentNullException> (() => {
720                                 c.MapPageRoute ("RouteName", null, "~/some-url");
721                         }, "#A2");
722
723                         c = new RouteCollection ();
724                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url");
725                         rd = c.GetRouteData (hc);
726
727                         Assert.IsNull (rd, "#A2");
728
729                         c = new RouteCollection ();
730                         // thrown by PageRouteHandler's constructor
731                         AssertExtensions.Throws<ArgumentException> (() => {
732                                 c.MapPageRoute ("RouteName", "~/some-url", null);
733                         }, "#A3");
734                 }
735
736                 [Test]
737                 public void MapPageRoute_String_String_String_Bool ()
738                 {
739                         var c = new RouteCollection ();
740
741                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true);
742                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
743                         hc.SetResponse (new HttpResponseStub (3));
744                         var rd = c.GetRouteData (hc);
745
746                         Assert.IsNotNull (rd, "#A1-1");
747                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
748                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
749                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
750
751                         c = new RouteCollection ();
752                         AssertExtensions.Throws<ArgumentNullException> (() => {
753                                 c.MapPageRoute ("RouteName", null, "~/some-url", true);
754                         }, "#A2");
755
756                         c = new RouteCollection ();
757                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true);
758                         rd = c.GetRouteData (hc);
759
760                         Assert.IsNull (rd, "#A2");
761
762                         c = new RouteCollection ();
763                         // thrown by PageRouteHandler's constructor
764                         AssertExtensions.Throws<ArgumentException> (() => {
765                                 c.MapPageRoute ("RouteName", "~/some-url", null, true);
766                         }, "#A3");
767
768                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false);
769                         rd = c.GetRouteData (hc);
770
771                         Assert.IsNotNull (rd, "#A4-1");
772                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
773                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
774                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
775                 }
776
777                 [Test]
778                 public void MapPageRoute_String_String_String_Bool_RVD ()
779                 {
780                         var c = new RouteCollection ();
781                         var defaults = new RouteValueDictionary ();
782
783                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults);
784                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
785                         hc.SetResponse (new HttpResponseStub (3));
786                         var rd = c.GetRouteData (hc);
787
788                         Assert.IsNotNull (rd, "#A1-1");
789                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
790                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
791                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
792
793                         c = new RouteCollection ();
794                         AssertExtensions.Throws<ArgumentNullException> (() => {
795                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults);
796                         }, "#A2");
797
798                         c = new RouteCollection ();
799                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults);
800                         rd = c.GetRouteData (hc);
801
802                         Assert.IsNull (rd, "#A2");
803
804                         c = new RouteCollection ();
805                         // thrown by PageRouteHandler's constructor
806                         AssertExtensions.Throws<ArgumentException> (() => {
807                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults);
808                         }, "#A3");
809
810                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults);
811                         rd = c.GetRouteData (hc);
812
813                         Assert.IsNotNull (rd, "#A4-1");
814                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
815                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
816                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
817
818                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null);
819                         rd = c.GetRouteData (hc);
820
821                         Assert.IsNotNull (rd, "#A4-1");
822                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
823                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
824                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4"); 
825                 }
826
827                 [Test]
828                 public void MapPageRoute_String_String_String_Bool_RVD_RVD ()
829                 {
830                         var c = new RouteCollection ();
831                         var defaults = new RouteValueDictionary ();
832                         var constraints = new RouteValueDictionary ();
833
834                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints);
835                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
836                         hc.SetResponse (new HttpResponseStub (3));
837                         var rd = c.GetRouteData (hc);
838
839                         Assert.IsNotNull (rd, "#A1-1");
840                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
841                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
842                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
843
844                         c = new RouteCollection ();
845                         AssertExtensions.Throws<ArgumentNullException> (() => {
846                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints);
847                         }, "#A2");
848
849                         c = new RouteCollection ();
850                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints);
851                         rd = c.GetRouteData (hc);
852
853                         Assert.IsNull (rd, "#A2");
854
855                         c = new RouteCollection ();
856                         // thrown by PageRouteHandler's constructor
857                         AssertExtensions.Throws<ArgumentException> (() => {
858                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints);
859                         }, "#A3");
860
861                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints);
862                         rd = c.GetRouteData (hc);
863
864                         Assert.IsNotNull (rd, "#A4-1");
865                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
866                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
867                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
868
869                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints);
870                         rd = c.GetRouteData (hc);
871
872                         Assert.IsNotNull (rd, "#A4-1");
873                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
874                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
875                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
876                 }
877
878                 [Test]
879                 public void MapPageRoute_String_String_String_Bool_RVD_RVD_RVD ()
880                 {
881                         var c = new RouteCollection ();
882                         var defaults = new RouteValueDictionary ();
883                         var constraints = new RouteValueDictionary ();
884                         var dataTokens = new RouteValueDictionary ();
885
886                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints, dataTokens);
887                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
888                         hc.SetResponse (new HttpResponseStub (3));
889                         var rd = c.GetRouteData (hc);
890
891                         Assert.IsNotNull (rd, "#A1-1");
892                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
893                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
894                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
895
896                         c = new RouteCollection ();
897                         AssertExtensions.Throws<ArgumentNullException> (() => {
898                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints, dataTokens);
899                         }, "#A2");
900
901                         c = new RouteCollection ();
902                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints, dataTokens);
903                         rd = c.GetRouteData (hc);
904
905                         Assert.IsNull (rd, "#A2");
906
907                         c = new RouteCollection ();
908                         // thrown by PageRouteHandler's constructor
909                         AssertExtensions.Throws<ArgumentException> (() => {
910                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints, dataTokens);
911                         }, "#A3");
912
913                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints, dataTokens);
914                         rd = c.GetRouteData (hc);
915
916                         Assert.IsNotNull (rd, "#A4-1");
917                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
918                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
919                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
920
921                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints, dataTokens);
922                         rd = c.GetRouteData (hc);
923
924                         Assert.IsNotNull (rd, "#A4-1");
925                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
926                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
927                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
928                 }
929 #endif
930         }
931 }