do not check order sequence if option /order was not used
[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                 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
542                 public void GetVirtualPathToExistingFile ()
543                 {
544                         var c = new RouteCollection ();
545                         c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
546                         var hc = new HttpContextStub2 ("~/Test/test.html", String.Empty, ".");
547                         // it tries to get HttpContextBase.Response, so set it.
548                         hc.SetResponse (new HttpResponseStub (3));
549                         var rd = c.GetRouteData (hc);
550                         var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
551                         Assert.AreEqual ("./Test/test.html", vpd.VirtualPath, "#1");
552                         Assert.AreEqual (0, vpd.DataTokens.Count, "#2");
553                 }
554
555                 [Test (Description="Routes from NerdDinner")]
556                 public void GetRouteDataNerdDinner ()
557                 {
558                         var c = new RouteCollection ();
559
560                         c.Add ("UpcomingDiners",
561                                new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
562                         );
563
564                         c.Add ("Default",
565                                new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
566                         );
567
568                         var hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
569                         hc.SetResponse (new HttpResponseStub (3));
570                         var rd = c.GetRouteData (hc);
571                         
572                         Assert.IsNotNull (rd, "#A1");
573                 }
574
575                 [Test (Description="Routes from NerdDinner")]
576                 public void GetRouteDataNerdDinner2 ()
577                 {
578                         var c = new RouteCollection ();
579
580                         c.Add ("UpcomingDiners",
581                                new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
582                         );
583
584                         c.Add ("Default",
585                                new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
586                         );
587
588                         var hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
589                         hc.SetResponse (new HttpResponseStub (3));
590                         var rd = c.GetRouteData (hc);
591                         
592                         Assert.IsNotNull (rd, "#A1");
593                 }
594 #if NET_4_0
595                 [Test]
596                 public void Ignore_String ()
597                 {
598                         var c = new RouteCollection ();
599
600                         AssertExtensions.Throws<ArgumentNullException> (() => {
601                                 c.Ignore (null);
602                         }, "#A1");
603
604                         c.Ignore ("{resource}.axd/{*pathInfo}");
605                         var hc = new HttpContextStub2 ("~/something.axd/pathinfo", String.Empty, String.Empty);
606                         hc.SetResponse (new HttpResponseStub (3));
607                         var rd = c.GetRouteData (hc);
608
609                         Assert.IsNotNull (rd, "#A1-1");
610                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
611                         Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
612                         Assert.IsTrue (rd.Route is Route, "#A1-4");
613                         Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
614                         Assert.AreEqual (0, ((Route) rd.Route).Constraints.Count, "#A1-6");
615                 }
616
617                 [Test]
618                 public void Ignore_String_Object ()
619                 {
620                         var c = new RouteCollection ();
621
622                         AssertExtensions.Throws<ArgumentNullException> (() => {
623                                 c.Ignore (null, new { allaspx = @".*\.aspx(/.*)?" });
624                         }, "#A1");
625
626                         c.Ignore ("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
627                         var hc = new HttpContextStub2 ("~/page.aspx", String.Empty, String.Empty);
628                         hc.SetResponse (new HttpResponseStub (3));
629                         var rd = c.GetRouteData (hc);
630
631                         Assert.IsNotNull (rd, "#A1-1");
632                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
633                         Assert.AreEqual (typeof (StopRoutingHandler), rd.RouteHandler.GetType (), "#A1-3");
634                         Assert.IsTrue (rd.Route is Route, "#A1-4");
635                         Assert.IsNotNull (((Route) rd.Route).Constraints, "#A1-5");
636                         Assert.AreEqual (1, ((Route) rd.Route).Constraints.Count, "#A1-6");
637                         Assert.AreEqual (@".*\.aspx(/.*)?", ((Route) rd.Route).Constraints ["allaspx"], "#A1-7");
638
639                         c = new RouteCollection ();
640                         c.Ignore ("{*allaspx}", "something invalid");
641
642                         AssertExtensions.Throws<InvalidOperationException> (() => {
643                                 rd = c.GetRouteData (hc);
644                         }, "#A2");
645                 }
646
647                 [Test]
648                 public void MapPageRoute_String_String_String ()
649                 {
650                         var c = new RouteCollection ();
651
652                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url");
653                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
654                         hc.SetResponse (new HttpResponseStub (3));
655                         var rd = c.GetRouteData (hc);
656
657                         Assert.IsNotNull (rd, "#A1-1");
658                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
659                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
660
661                         c = new RouteCollection ();
662                         AssertExtensions.Throws<ArgumentNullException> (() => {
663                                 c.MapPageRoute ("RouteName", null, "~/some-url");
664                         }, "#A2");
665
666                         c = new RouteCollection ();
667                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url");
668                         rd = c.GetRouteData (hc);
669
670                         Assert.IsNull (rd, "#A2");
671
672                         c = new RouteCollection ();
673                         // thrown by PageRouteHandler's constructor
674                         AssertExtensions.Throws<ArgumentException> (() => {
675                                 c.MapPageRoute ("RouteName", "~/some-url", null);
676                         }, "#A3");
677                 }
678
679                 [Test]
680                 public void MapPageRoute_String_String_String_Bool ()
681                 {
682                         var c = new RouteCollection ();
683
684                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true);
685                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
686                         hc.SetResponse (new HttpResponseStub (3));
687                         var rd = c.GetRouteData (hc);
688
689                         Assert.IsNotNull (rd, "#A1-1");
690                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
691                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
692                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
693
694                         c = new RouteCollection ();
695                         AssertExtensions.Throws<ArgumentNullException> (() => {
696                                 c.MapPageRoute ("RouteName", null, "~/some-url", true);
697                         }, "#A2");
698
699                         c = new RouteCollection ();
700                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true);
701                         rd = c.GetRouteData (hc);
702
703                         Assert.IsNull (rd, "#A2");
704
705                         c = new RouteCollection ();
706                         // thrown by PageRouteHandler's constructor
707                         AssertExtensions.Throws<ArgumentException> (() => {
708                                 c.MapPageRoute ("RouteName", "~/some-url", null, true);
709                         }, "#A3");
710
711                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false);
712                         rd = c.GetRouteData (hc);
713
714                         Assert.IsNotNull (rd, "#A4-1");
715                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
716                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
717                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
718                 }
719
720                 [Test]
721                 public void MapPageRoute_String_String_String_Bool_RVD ()
722                 {
723                         var c = new RouteCollection ();
724                         var defaults = new RouteValueDictionary ();
725
726                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults);
727                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
728                         hc.SetResponse (new HttpResponseStub (3));
729                         var rd = c.GetRouteData (hc);
730
731                         Assert.IsNotNull (rd, "#A1-1");
732                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
733                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
734                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
735
736                         c = new RouteCollection ();
737                         AssertExtensions.Throws<ArgumentNullException> (() => {
738                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults);
739                         }, "#A2");
740
741                         c = new RouteCollection ();
742                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults);
743                         rd = c.GetRouteData (hc);
744
745                         Assert.IsNull (rd, "#A2");
746
747                         c = new RouteCollection ();
748                         // thrown by PageRouteHandler's constructor
749                         AssertExtensions.Throws<ArgumentException> (() => {
750                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults);
751                         }, "#A3");
752
753                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults);
754                         rd = c.GetRouteData (hc);
755
756                         Assert.IsNotNull (rd, "#A4-1");
757                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
758                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
759                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
760
761                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null);
762                         rd = c.GetRouteData (hc);
763
764                         Assert.IsNotNull (rd, "#A4-1");
765                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
766                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
767                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4"); 
768                 }
769
770                 [Test]
771                 public void MapPageRoute_String_String_String_Bool_RVD_RVD ()
772                 {
773                         var c = new RouteCollection ();
774                         var defaults = new RouteValueDictionary ();
775                         var constraints = new RouteValueDictionary ();
776
777                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints);
778                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
779                         hc.SetResponse (new HttpResponseStub (3));
780                         var rd = c.GetRouteData (hc);
781
782                         Assert.IsNotNull (rd, "#A1-1");
783                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
784                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
785                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
786
787                         c = new RouteCollection ();
788                         AssertExtensions.Throws<ArgumentNullException> (() => {
789                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints);
790                         }, "#A2");
791
792                         c = new RouteCollection ();
793                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints);
794                         rd = c.GetRouteData (hc);
795
796                         Assert.IsNull (rd, "#A2");
797
798                         c = new RouteCollection ();
799                         // thrown by PageRouteHandler's constructor
800                         AssertExtensions.Throws<ArgumentException> (() => {
801                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints);
802                         }, "#A3");
803
804                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints);
805                         rd = c.GetRouteData (hc);
806
807                         Assert.IsNotNull (rd, "#A4-1");
808                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
809                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
810                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
811
812                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints);
813                         rd = c.GetRouteData (hc);
814
815                         Assert.IsNotNull (rd, "#A4-1");
816                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
817                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
818                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
819                 }
820
821                 [Test]
822                 public void MapPageRoute_String_String_String_Bool_RVD_RVD_RVD ()
823                 {
824                         var c = new RouteCollection ();
825                         var defaults = new RouteValueDictionary ();
826                         var constraints = new RouteValueDictionary ();
827                         var dataTokens = new RouteValueDictionary ();
828
829                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", true, defaults, constraints, dataTokens);
830                         var hc = new HttpContextStub2 ("~/some-url", String.Empty, String.Empty);
831                         hc.SetResponse (new HttpResponseStub (3));
832                         var rd = c.GetRouteData (hc);
833
834                         Assert.IsNotNull (rd, "#A1-1");
835                         Assert.IsNotNull (rd.RouteHandler, "#A1-2");
836                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A1-3");
837                         Assert.IsTrue (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A1-4");
838
839                         c = new RouteCollection ();
840                         AssertExtensions.Throws<ArgumentNullException> (() => {
841                                 c.MapPageRoute ("RouteName", null, "~/some-url", true, defaults, constraints, dataTokens);
842                         }, "#A2");
843
844                         c = new RouteCollection ();
845                         c.MapPageRoute ("RouteName", String.Empty, "~/some-url", true, defaults, constraints, dataTokens);
846                         rd = c.GetRouteData (hc);
847
848                         Assert.IsNull (rd, "#A2");
849
850                         c = new RouteCollection ();
851                         // thrown by PageRouteHandler's constructor
852                         AssertExtensions.Throws<ArgumentException> (() => {
853                                 c.MapPageRoute ("RouteName", "~/some-url", null, true, defaults, constraints, dataTokens);
854                         }, "#A3");
855
856                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, defaults, constraints, dataTokens);
857                         rd = c.GetRouteData (hc);
858
859                         Assert.IsNotNull (rd, "#A4-1");
860                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
861                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
862                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
863
864                         c.MapPageRoute (null, "{foo}-{bar}", "~/some-url", false, null, constraints, dataTokens);
865                         rd = c.GetRouteData (hc);
866
867                         Assert.IsNotNull (rd, "#A4-1");
868                         Assert.IsNotNull (rd.RouteHandler, "#A4-2");
869                         Assert.AreEqual (typeof (PageRouteHandler), rd.RouteHandler.GetType (), "#A4-3");
870                         Assert.IsFalse (((PageRouteHandler) rd.RouteHandler).CheckPhysicalUrlAccess, "#A4-4");
871                 }
872 #endif
873         }
874 }