2 // RouteCollectionTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell Inc. http://novell.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Routing;
33 using NUnit.Framework;
35 using MonoTests.Common;
37 namespace MonoTests.System.Web.Routing
40 public class RouteCollectionTest
43 public void ConstructorNullArgs ()
46 new RouteCollection (null);
50 public void RouteExistingFiles ()
52 var c = new RouteCollection ();
53 Assert.IsFalse (c.RouteExistingFiles);
57 public void AddNullMame ()
59 var c = new RouteCollection ();
60 // when name is null, no duplicate check is done.
61 c.Add (null, new Route (null, null));
62 c.Add (null, new Route (null, null));
66 public void AddDuplicateEmpty ()
68 var c = new RouteCollection ();
69 // when name is "", no duplicate check is done.
70 c.Add ("", new Route (null, null));
71 c.Add ("", new Route (null, null));
75 [ExpectedException (typeof (ArgumentException))]
76 public void AddDuplicateName ()
78 var c = new RouteCollection ();
79 c.Add ("x", new Route (null, null));
80 c.Add ("x", new Route (null, null));
84 public void IndexForNonExistent ()
86 Assert.IsNull (new RouteCollection () [null]);
90 public void IndexForExistent ()
92 var c = new RouteCollection ();
93 var r = new Route (null, null);
95 Assert.AreEqual (r, c ["x"]);
99 public void IndexForNonExistentAfterRemoval ()
101 var c = new RouteCollection ();
102 var r = new Route (null, null);
105 Assert.IsNull(c ["x"]);
109 [ExpectedException (typeof (ArgumentNullException))]
110 public void GetRouteDataNullArg ()
112 new RouteCollection ().GetRouteData (null);
116 public void GetRouteDataForNonExistent ()
118 var rd = new RouteCollection ().GetRouteData (new HttpContextStub ("~/foo"));
123 public void GetRouteDataForNonExistent2 ()
125 var rd = new RouteCollection () { RouteExistingFiles = true }.GetRouteData (new HttpContextStub2 (null, null, null));
128 new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
130 } catch (NotImplementedException) {
131 // it should fail due to the NIE on AppRelativeCurrentExecutionFilePath.
136 public void GetRouteDataWrongPathNoRoute ()
138 new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
142 comment out those tests; I cannot explain those tests.
145 [ExpectedException (typeof (ArgumentOutOfRangeException))]
146 public void GetRouteDataWrongPathOneRoute ()
148 var c = new RouteCollection ();
149 var r = new Route ("foo", null);
151 // it somehow causes ArgumentOutOfRangeException for
152 // Request.AppRelativeCurrentExecutionFilePath.
153 c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
157 public void GetRouteDataWrongPathOneRoute2 ()
159 var c = new RouteCollection ();
160 var r = new Route ("foo", null);
162 c.GetRouteData (new HttpContextStub ("/~", String.Empty));
167 [ExpectedException (typeof (NotImplementedException))]
168 public void GetRouteDataForPathInfoNIE ()
170 var c = new RouteCollection ();
171 var r = new Route ("foo", null);
173 // it retrieves PathInfo and then dies.
174 var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
178 public void GetRouteDataForNullHandler ()
180 var c = new RouteCollection ();
181 var r = new Route ("foo", null); // allowed
183 var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
184 Assert.IsNotNull (rd, "#1");
185 Assert.AreEqual (r, rd.Route, "#2");
188 // below tests in RouteCollection, unlike Route, do some additional checks than Route.GetVirtualPath().
191 [ExpectedException (typeof (NotImplementedException))]
192 public void GetVirtualPathNoApplicationPath ()
194 var c = new RouteCollection ();
195 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
196 var hc = new HttpContextStub2 ("~/x/y", String.Empty);
197 var rd = c.GetRouteData (hc);
198 // it tries to get HttpContextBase.Request.ApplicationPath and then throws NIE.
199 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
203 [ExpectedException (typeof (NotImplementedException))]
204 public void GetVirtualPathNoApplyAppPathModifier ()
206 var c = new RouteCollection ();
207 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
208 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
209 // it tries to call HttpContextBase.Response.ApplyAppPathModifier() and then causes NIE.
210 hc.SetResponse (new HttpResponseStub ());
211 var rd = c.GetRouteData (hc);
212 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
216 public void GetVirtualPathCheckVirtualPathToModify ()
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 get HttpContextBase.Response, so set it.
222 hc.SetResponse (new HttpResponseStub (1));
223 var rd = c.GetRouteData (hc);
225 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
227 } catch (ApplicationException ex) {
228 Assert.AreEqual ("apppath/x/y", ex.Message, "#2");
233 public void GetVirtualPath ()
235 var c = new RouteCollection ();
236 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
237 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
238 // it tries to get HttpContextBase.Response, so set it.
239 hc.SetResponse (new HttpResponseStub (2));
240 var rd = c.GetRouteData (hc);
241 Assert.IsNotNull (rd, "#1");
243 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
244 Assert.IsNotNull (vpd, "#2");
245 Assert.AreEqual ("apppath/x/y_modified", vpd.VirtualPath, "#3");
246 Assert.AreEqual (0, vpd.DataTokens.Count, "#4");
249 [Test (Description = "Bug #502555")]
250 public void GetVirtualPath2 ()
252 var c = new RouteCollection ();
255 new MyRoute ("summary/{action}-{type}/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Summary", action = "Index", page = 1}) }
259 new MyRoute ("apis/{apiname}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Apis", action = "Index" }) }
262 c.Add ("Single Report",
263 new MyRoute ("report/{guid}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "SingleReport" }) }
267 new MyRoute ("reports/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "Index", page = 1 }) }
271 new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index"}) }
274 var hc = new HttpContextStub2 ("~/Home/About", String.Empty, String.Empty);
275 hc.SetResponse (new HttpResponseStub (2));
276 var rd = c.GetRouteData (hc);
277 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
278 Assert.IsNotNull (vpd, "#A1");
279 Assert.AreEqual ("/Home/About_modified", vpd.VirtualPath, "#A2");
280 Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
282 hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
283 hc.SetResponse (new HttpResponseStub (2));
284 rd = c.GetRouteData (hc);
285 vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
286 Assert.IsNotNull (vpd, "#B1");
287 Assert.AreEqual ("/_modified", vpd.VirtualPath, "#B2");
288 Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
290 hc = new HttpContextStub2 ("~/Account/LogOn", String.Empty, String.Empty);
291 hc.SetResponse (new HttpResponseStub (2));
292 rd = c.GetRouteData (hc);
293 vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
294 Assert.IsNotNull (vpd, "#C1");
295 Assert.AreEqual ("/Account/LogOn_modified", vpd.VirtualPath, "#C2");
296 Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
298 hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
299 hc.SetResponse (new HttpResponseStub (3));
300 rd = c.GetRouteData (hc);
301 vpd = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }) );
302 Assert.IsNotNull (vpd, "#D1");
303 Assert.AreEqual ("/", vpd.VirtualPath, "#D2");
304 Assert.AreEqual (0, vpd.DataTokens.Count, "#D3");
306 hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
307 hc.SetResponse (new HttpResponseStub (3));
308 rd = c.GetRouteData (hc);
309 vpd = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "Home" }) );
310 Assert.IsNotNull (vpd, "#E1");
311 Assert.AreEqual ("/", vpd.VirtualPath, "#E2");
312 Assert.AreEqual (0, vpd.DataTokens.Count, "#E3");
316 public void GetVirtualPath3 ()
318 var c = new RouteCollection ();
321 new MyRoute ("todo/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "todo", action="list", page=0}) }
324 c.Add ("another-route",
325 new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "home", action="list", page=0}) }
328 var hc = new HttpContextStub2 ("~/home/list", String.Empty, String.Empty);
329 hc.SetResponse (new HttpResponseStub (3));
330 var rd = c.GetRouteData (hc);
331 Assert.IsNotNull (rd, "#1");
332 Assert.AreEqual (3, rd.Values.Count, "#1-1");
333 Assert.AreEqual ("home", rd.Values["controller"], "#1-2");
334 Assert.AreEqual ("list", rd.Values["action"], "#1-3");
335 Assert.AreEqual (0, rd.Values["page"], "#1-4");
337 var vp = c.GetVirtualPath (new RequestContext (hc, rd), "todo-route", new RouteValueDictionary ());
338 Assert.IsNotNull (vp, "#2");
339 Assert.AreEqual ("/todo", vp.VirtualPath, "#2-1");
341 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary ());
342 Assert.IsNotNull (vp, "#3");
343 Assert.AreEqual ("/todo", vp.VirtualPath, "#3-1");
345 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }));
346 Assert.IsNotNull (vp, "#4");
347 Assert.AreEqual ("/", vp.VirtualPath, "#4-1");
349 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home", extra="stuff" }));
350 Assert.IsNotNull (vp, "#5");
351 Assert.AreEqual ("/?extra=stuff", vp.VirtualPath, "#5-1");
355 public void GetVirtualPath4 ()
357 var c = new RouteCollection ();
359 c.Add (new MyRoute ("blog/{user}/{action}", new MyRouteHandler ()) {
360 Defaults = new RouteValueDictionary {
361 { "controller", "blog" },
367 c.Add (new MyRoute ("forum/{user}/{action}", new MyRouteHandler ()) {
368 Defaults = new RouteValueDictionary {
369 { "controller", "forum" },
375 var hc = new HttpContextStub2 ("~/forum/admin/Index", String.Empty, String.Empty);
376 hc.SetResponse (new HttpResponseStub (3));
377 var rd = c.GetRouteData (hc);
378 Assert.IsNotNull (rd, "#1");
380 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="forum"}));
381 Assert.IsNotNull (vp, "#2");
382 Assert.AreEqual ("/forum/admin/Index", vp.VirtualPath, "#2-1");
384 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="blah"}));
385 Assert.IsNull (vp, "#3");
389 public void GetVirtualPath5 ()
391 var c = new RouteCollection ();
393 c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
394 Defaults = new RouteValueDictionary {
400 var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, String.Empty);
401 hc.SetResponse (new HttpResponseStub (3));
402 var rd = c.GetRouteData (hc);
403 Assert.IsNotNull (rd, "#1");
405 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
411 Assert.IsNotNull (vp, "#2");
412 Assert.AreEqual ("/reports/2007/1/12", vp.VirtualPath, "#2-1");
414 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
419 Assert.IsNotNull (vp, "#3");
420 Assert.AreEqual ("/reports/2007/1", vp.VirtualPath, "#3-1");
422 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
429 Assert.IsNotNull (vp, "#4");
430 Assert.AreEqual ("/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
432 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
436 Assert.IsNull (vp, "#5");
440 public void GetVirtualPath6 ()
442 var c = new RouteCollection ();
444 c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
445 Defaults = new RouteValueDictionary {
451 var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, "/myapp");
452 hc.SetResponse (new HttpResponseStub (3));
453 var rd = c.GetRouteData (hc);
454 Assert.IsNotNull (rd, "#1");
456 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
462 Assert.IsNotNull (vp, "#2");
463 Assert.AreEqual ("/myapp/reports/2007/1/12", vp.VirtualPath, "#2-1");
465 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
470 Assert.IsNotNull (vp, "#3");
471 Assert.AreEqual ("/myapp/reports/2007/1", vp.VirtualPath, "#3-1");
473 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
480 Assert.IsNotNull (vp, "#4");
481 Assert.AreEqual ("/myapp/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
483 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
487 Assert.IsNull (vp, "#5");
491 public void GetVirtualPath7 ()
493 var c = new RouteCollection ();
495 c.Add (new MyRoute ("{table}/{action}.aspx", new MyRouteHandler ()) {
496 Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }),
499 var req = new FakeHttpWorkerRequest ();
500 var ctx = new HttpContext (req);
501 HttpContext.Current = ctx;
502 var rd = new RouteData ();
503 var hc = new HttpContextWrapper (ctx);
505 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
506 {"Table", "FooTable"},
507 {"Action", "Details"}
510 Assert.IsNotNull (vp, "#A1");
511 Assert.AreEqual ("/FooTable/Details.aspx", vp.VirtualPath, "#A1-1");
513 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
514 {"Table", "FooTable"},
515 {"Action", String.Empty}
518 Assert.IsNull (vp, "#B1");
520 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
521 {"Table", "FooTable"},
525 Assert.IsNull (vp, "#C1");
529 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
530 public void GetVirtualPathToExistingFile ()
532 var c = new RouteCollection ();
533 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
534 var hc = new HttpContextStub2 ("~/Test/test.html", String.Empty, ".");
535 // it tries to get HttpContextBase.Response, so set it.
536 hc.SetResponse (new HttpResponseStub (3));
537 var rd = c.GetRouteData (hc);
538 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
539 Assert.AreEqual ("./Test/test.html", vpd.VirtualPath, "#1");
540 Assert.AreEqual (0, vpd.DataTokens.Count, "#2");
543 [Test (Description="Routes from NerdDinner")]
544 public void GetRouteDataNerdDinner ()
546 var c = new RouteCollection ();
548 c.Add ("UpcomingDiners",
549 new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
553 new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
556 var hc = new HttpContextStub2 ("~/", String.Empty, String.Empty);
557 hc.SetResponse (new HttpResponseStub (3));
558 var rd = c.GetRouteData (hc);
560 Assert.IsNotNull (rd, "#A1");
563 [Test (Description="Routes from NerdDinner")]
564 public void GetRouteDataNerdDinner2 ()
566 var c = new RouteCollection ();
568 c.Add ("UpcomingDiners",
569 new MyRoute ("Dinners/Page/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Dinners", action = "Index" }) }
573 new MyRoute ("{controller}/{action}/{id}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index", id = "" })}
576 var hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
577 hc.SetResponse (new HttpResponseStub (3));
578 var rd = c.GetRouteData (hc);
580 Assert.IsNotNull (rd, "#A1");