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 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
240 Assert.AreEqual ("apppath/x/y_modified", vpd.VirtualPath, "#1");
241 Assert.AreEqual (0, vpd.DataTokens.Count, "#2");
245 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
246 public void GetVirtualPathToExistingFile ()
248 var c = new RouteCollection ();
249 c.Add (new MyRoute ("{foo}/{bar}", new MyRouteHandler ()));
250 var hc = new HttpContextStub2 ("~/Test/test.html", String.Empty, ".");
251 // it tries to get HttpContextBase.Response, so set it.
252 hc.SetResponse (new HttpResponseStub (3));
253 var rd = c.GetRouteData (hc);
254 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
255 Assert.AreEqual ("./Test/test.html", vpd.VirtualPath, "#1");
256 Assert.AreEqual (0, vpd.DataTokens.Count, "#2");