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 namespace MonoTests.System.Web.Routing
38 public class RouteCollectionTest
41 public void ConstructorNullArgs ()
44 new RouteCollection (null);
48 public void RouteExistingFiles ()
50 var c = new RouteCollection ();
51 Assert.IsFalse (c.RouteExistingFiles);
55 public void AddNullMame ()
57 var c = new RouteCollection ();
58 // when name is null, no duplicate check is done.
59 c.Add (null, new Route (null, null));
60 c.Add (null, new Route (null, null));
64 public void AddDuplicateEmpty ()
66 var c = new RouteCollection ();
67 // when name is "", no duplicate check is done.
68 c.Add ("", new Route (null, null));
69 c.Add ("", new Route (null, null));
73 [ExpectedException (typeof (ArgumentException))]
74 public void AddDuplicateName ()
76 var c = new RouteCollection ();
77 c.Add ("x", new Route (null, null));
78 c.Add ("x", new Route (null, null));
82 public void IndexForNonExistent ()
84 Assert.IsNull (new RouteCollection () [null]);
88 public void IndexForExistent ()
90 var c = new RouteCollection ();
91 var r = new Route (null, null);
93 Assert.AreEqual (r, c ["x"]);
97 public void IndexForNonExistentAfterRemoval ()
99 var c = new RouteCollection ();
100 var r = new Route (null, null);
103 Assert.IsNull(c ["x"]);
107 [ExpectedException (typeof (ArgumentNullException))]
108 public void GetRouteDataNullArg ()
110 new RouteCollection ().GetRouteData (null);
114 public void GetRouteDataForNonExistent ()
116 var rd = new RouteCollection ().GetRouteData (new HttpContextStub ("~/foo"));
121 public void GetRouteDataForNonExistent2 ()
123 var rd = new RouteCollection () { RouteExistingFiles = true }.GetRouteData (new HttpContextStub2 (null, null, null));
126 new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
128 } catch (NotImplementedException) {
129 // it should fail due to the NIE on AppRelativeCurrentExecutionFilePath.
134 public void GetRouteDataWrongPathNoRoute ()
136 new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
140 comment out those tests; I cannot explain those tests.
143 [ExpectedException (typeof (ArgumentOutOfRangeException))]
144 public void GetRouteDataWrongPathOneRoute ()
146 var c = new RouteCollection ();
147 var r = new Route ("foo", null);
149 // it somehow causes ArgumentOutOfRangeException for
150 // Request.AppRelativeCurrentExecutionFilePath.
151 c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
155 public void GetRouteDataWrongPathOneRoute2 ()
157 var c = new RouteCollection ();
158 var r = new Route ("foo", null);
160 c.GetRouteData (new HttpContextStub ("/~", String.Empty));
165 [ExpectedException (typeof (NotImplementedException))]
166 public void GetRouteDataForPathInfoNIE ()
168 var c = new RouteCollection ();
169 var r = new Route ("foo", null);
171 // it retrieves PathInfo and then dies.
172 var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
176 public void GetRouteDataForNullHandler ()
178 var c = new RouteCollection ();
179 var r = new Route ("foo", null); // allowed
181 var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
182 Assert.IsNotNull (rd, "#1");
183 Assert.AreEqual (r, rd.Route, "#2");
186 // below tests in RouteCollection, unlike Route, do some additional checks than Route.GetVirtualPath().
189 [ExpectedException (typeof (NotImplementedException))]
190 public void GetVirtualPathNoApplicationPath ()
192 var c = new RouteCollection ();
193 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
194 var hc = new HttpContextStub2 ("~/x/y", String.Empty);
195 var rd = c.GetRouteData (hc);
196 // it tries to get HttpContextBase.Request.ApplicationPath and then throws NIE.
197 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
201 [ExpectedException (typeof (NotImplementedException))]
202 public void GetVirtualPathNoApplyAppPathModifier ()
204 var c = new RouteCollection ();
205 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
206 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
207 // it tries to call HttpContextBase.Response.ApplyAppPathModifier() and then causes NIE.
208 hc.SetResponse (new HttpResponseStub ());
209 var rd = c.GetRouteData (hc);
210 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
214 public void GetVirtualPathCheckVirtualPathToModify ()
216 var c = new RouteCollection ();
217 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
218 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
219 // it tries to get HttpContextBase.Response, so set it.
220 hc.SetResponse (new HttpResponseStub (1));
221 var rd = c.GetRouteData (hc);
223 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
225 } catch (ApplicationException ex) {
226 Assert.AreEqual ("apppath/x/y", ex.Message, "#2");
231 public void GetVirtualPath ()
233 var c = new RouteCollection ();
234 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
235 var hc = new HttpContextStub2 ("~/x/y", String.Empty, "apppath");
236 // it tries to get HttpContextBase.Response, so set it.
237 hc.SetResponse (new HttpResponseStub (2));
238 var rd = c.GetRouteData (hc);
239 Assert.IsNotNull (rd, "#1");
241 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
242 Assert.IsNotNull (vpd, "#2");
243 Assert.AreEqual ("apppath/x/y_modified", vpd.VirtualPath, "#3");
244 Assert.AreEqual (0, vpd.DataTokens.Count, "#4");
247 [Test (Description = "Bug #502555")]
248 public void GetVirtualPath2 ()
250 var c = new RouteCollection ();
253 new MyRoute ("summary/{action}-{type}/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Summary", action = "Index", page = 1}) }
257 new MyRoute ("apis/{apiname}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Apis", action = "Index" }) }
260 c.Add ("Single Report",
261 new MyRoute ("report/{guid}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "SingleReport" }) }
265 new MyRoute ("reports/{page}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Reports", action = "Index", page = 1 }) }
269 new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new { controller = "Home", action = "Index"}) }
272 var hc = new HttpContextStub2 ("~/Home/About", String.Empty, String.Empty);
273 hc.SetResponse (new HttpResponseStub (2));
274 var rd = c.GetRouteData (hc);
275 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
276 Assert.IsNotNull (vpd, "#A1");
277 Assert.AreEqual ("/Home/About_modified", vpd.VirtualPath, "#A2");
278 Assert.AreEqual (0, vpd.DataTokens.Count, "#A3");
280 hc = new HttpContextStub2 ("~/Home/Index", String.Empty, String.Empty);
281 hc.SetResponse (new HttpResponseStub (2));
282 rd = c.GetRouteData (hc);
283 vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
284 Assert.IsNotNull (vpd, "#B1");
285 Assert.AreEqual ("/_modified", vpd.VirtualPath, "#B2");
286 Assert.AreEqual (0, vpd.DataTokens.Count, "#B3");
288 hc = new HttpContextStub2 ("~/Account/LogOn", String.Empty, String.Empty);
289 hc.SetResponse (new HttpResponseStub (2));
290 rd = c.GetRouteData (hc);
291 vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
292 Assert.IsNotNull (vpd, "#C1");
293 Assert.AreEqual ("/Account/LogOn_modified", vpd.VirtualPath, "#C2");
294 Assert.AreEqual (0, vpd.DataTokens.Count, "#C3");
298 public void GetVirtualPath3 ()
300 var c = new RouteCollection ();
303 new MyRoute ("todo/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "todo", action="list", page=0}) }
306 c.Add ("another-route",
307 new MyRoute ("{controller}/{action}", new MyRouteHandler ()) { Defaults = new RouteValueDictionary (new {controller = "home", action="list", page=0}) }
310 var hc = new HttpContextStub2 ("~/home/list", String.Empty, String.Empty);
311 hc.SetResponse (new HttpResponseStub (3));
312 var rd = c.GetRouteData (hc);
313 Assert.IsNotNull (rd, "#1");
314 Assert.AreEqual (3, rd.Values.Count, "#1-1");
315 Assert.AreEqual ("home", rd.Values["controller"], "#1-2");
316 Assert.AreEqual ("list", rd.Values["action"], "#1-3");
317 Assert.AreEqual (0, rd.Values["page"], "#1-4");
319 var vp = c.GetVirtualPath (new RequestContext (hc, rd), "todo-route", new RouteValueDictionary ());
320 Assert.IsNotNull (vp, "#2");
321 Assert.AreEqual ("/todo", vp.VirtualPath, "#2-1");
323 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary ());
324 Assert.IsNotNull (vp, "#3");
325 Assert.AreEqual ("/todo", vp.VirtualPath, "#3-1");
327 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home" }));
328 Assert.IsNotNull (vp, "#4");
329 Assert.AreEqual ("/", vp.VirtualPath, "#4-1");
331 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { controller = "home", extra="stuff" }));
332 Assert.IsNotNull (vp, "#5");
333 Assert.AreEqual ("/?extra=stuff", vp.VirtualPath, "#5-1");
337 public void GetVirtualPath4 ()
339 var c = new RouteCollection ();
341 c.Add (new MyRoute ("blog/{user}/{action}", new MyRouteHandler ()) {
342 Defaults = new RouteValueDictionary {
343 { "controller", "blog" },
349 c.Add (new MyRoute ("forum/{user}/{action}", new MyRouteHandler ()) {
350 Defaults = new RouteValueDictionary {
351 { "controller", "forum" },
357 var hc = new HttpContextStub2 ("~/forum/admin/Index", String.Empty, String.Empty);
358 hc.SetResponse (new HttpResponseStub (3));
359 var rd = c.GetRouteData (hc);
360 Assert.IsNotNull (rd, "#1");
362 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="forum"}));
363 Assert.IsNotNull (vp, "#2");
364 Assert.AreEqual ("/forum/admin/Index", vp.VirtualPath, "#2-1");
366 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { action="Index", controller="blah"}));
367 Assert.IsNull (vp, "#3");
371 public void GetVirtualPath5 ()
373 var c = new RouteCollection ();
375 c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
376 Defaults = new RouteValueDictionary {
382 var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, String.Empty);
383 hc.SetResponse (new HttpResponseStub (3));
384 var rd = c.GetRouteData (hc);
385 Assert.IsNotNull (rd, "#1");
387 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
393 Assert.IsNotNull (vp, "#2");
394 Assert.AreEqual ("/reports/2007/1/12", vp.VirtualPath, "#2-1");
396 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
401 Assert.IsNotNull (vp, "#3");
402 Assert.AreEqual ("/reports/2007/1", vp.VirtualPath, "#3-1");
404 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
411 Assert.IsNotNull (vp, "#4");
412 Assert.AreEqual ("/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
414 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
418 Assert.IsNull (vp, "#5");
422 public void GetVirtualPath6 ()
424 var c = new RouteCollection ();
426 c.Add (new MyRoute ("reports/{year}/{month}/{day}", new MyRouteHandler ()) {
427 Defaults = new RouteValueDictionary {
433 var hc = new HttpContextStub2 ("~/reports/2009/05", String.Empty, "/myapp");
434 hc.SetResponse (new HttpResponseStub (3));
435 var rd = c.GetRouteData (hc);
436 Assert.IsNotNull (rd, "#1");
438 var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
444 Assert.IsNotNull (vp, "#2");
445 Assert.AreEqual ("/myapp/reports/2007/1/12", vp.VirtualPath, "#2-1");
447 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
452 Assert.IsNotNull (vp, "#3");
453 Assert.AreEqual ("/myapp/reports/2007/1", vp.VirtualPath, "#3-1");
455 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
462 Assert.IsNotNull (vp, "#4");
463 Assert.AreEqual ("/myapp/reports/2007/1/12?category=123", vp.VirtualPath, "#4-1");
465 vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary {
469 Assert.IsNull (vp, "#5");
473 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
474 public void GetVirtualPathToExistingFile ()
476 var c = new RouteCollection ();
477 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
478 var hc = new HttpContextStub2 ("~/Test/test.html", String.Empty, ".");
479 // it tries to get HttpContextBase.Response, so set it.
480 hc.SetResponse (new HttpResponseStub (3));
481 var rd = c.GetRouteData (hc);
482 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
483 Assert.AreEqual ("./Test/test.html", vpd.VirtualPath, "#1");
484 Assert.AreEqual (0, vpd.DataTokens.Count, "#2");