RouteCollection.RouteExistingFiles support.
[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 namespace MonoTests.System.Web.Routing
36 {
37         [TestFixture]
38         public class RouteCollectionTest
39         {
40                 [Test]
41                 public void ConstructorNullArgs ()
42                 {
43                         // allowed
44                         new RouteCollection (null);
45                 }
46
47                 [Test]
48                 public void RouteExistingFiles ()
49                 {
50                         var c = new RouteCollection ();
51                         Assert.IsFalse (c.RouteExistingFiles);
52                 }
53
54                 [Test]
55                 public void AddNullMame ()
56                 {
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));
61                 }
62
63                 [Test]
64                 public void AddDuplicateEmpty ()
65                 {
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));
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentException))]
74                 public void AddDuplicateName ()
75                 {
76                         var c = new RouteCollection ();
77                         c.Add ("x", new Route (null, null));
78                         c.Add ("x", new Route (null, null));
79                 }
80
81                 [Test]
82                 public void IndexForNonExistent ()
83                 {
84                         Assert.IsNull (new RouteCollection () [null]);
85                 }
86
87                 [Test]
88                 public void IndexForExistent ()
89                 {
90                         var c = new RouteCollection ();
91                         var r = new Route (null, null);
92                         c.Add ("x", r);
93                         Assert.AreEqual (r, c ["x"]);
94                 }
95
96                 [Test]
97                 public void IndexForNonExistentAfterRemoval ()
98                 {
99                         var c = new RouteCollection ();
100                         var r = new Route (null, null);
101                         c.Add ("x", r);
102                         c.Remove (r);
103                         Assert.IsNull(c ["x"]);
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentNullException))]
108                 public void GetRouteDataNullArg ()
109                 {
110                         new RouteCollection ().GetRouteData (null);
111                 }
112
113                 [Test]
114                 public void GetRouteDataForNonExistent ()
115                 {
116                         var rd = new RouteCollection ().GetRouteData (new HttpContextStub ("~/foo"));
117                         Assert.IsNull (rd);
118                 }
119
120                 [Test]
121                 public void GetRouteDataForNonExistent2 ()
122                 {
123                         var rd = new RouteCollection () { RouteExistingFiles = true }.GetRouteData (new HttpContextStub2 (null, null, null));
124                         Assert.IsNull (rd);
125                         try {
126                                 new RouteCollection ().GetRouteData (new HttpContextStub2 (null, null, null));
127                                 Assert.Fail ("#1");
128                         } catch (NotImplementedException) {
129                                 // it should fail due to the NIE on AppRelativeCurrentExecutionFilePath.
130                         }
131                 }
132
133                 [Test]
134                 public void GetRouteDataWrongPathNoRoute ()
135                 {
136                         new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
137                 }
138
139                 /*
140                 comment out those tests; I cannot explain those tests.
141
142                 [Test]
143                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
144                 public void GetRouteDataWrongPathOneRoute ()
145                 {
146                         var c = new RouteCollection ();
147                         var r = new Route ("foo", null);
148                         c.Add (null, r);
149                         // it somehow causes ArgumentOutOfRangeException for 
150                         // Request.AppRelativeCurrentExecutionFilePath.
151                         c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
152                 }
153
154                 [Test]
155                 public void GetRouteDataWrongPathOneRoute2 ()
156                 {
157                         var c = new RouteCollection ();
158                         var r = new Route ("foo", null);
159                         c.Add (null, r);
160                         c.GetRouteData (new HttpContextStub ("/~", String.Empty));
161                 }
162                 */
163
164                 [Test]
165                 [ExpectedException (typeof (NotImplementedException))]
166                 public void GetRouteDataForPathInfoNIE ()
167                 {
168                         var c = new RouteCollection ();
169                         var r = new Route ("foo", null);
170                         c.Add (null, r);
171                         // it retrieves PathInfo and then dies.
172                         var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
173                 }
174
175                 [Test]
176                 public void GetRouteDataForNullHandler ()
177                 {
178                         var c = new RouteCollection ();
179                         var r = new Route ("foo", null); // allowed
180                         c.Add (null, r);
181                         var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
182                         Assert.IsNotNull (rd, "#1");
183                         Assert.AreEqual (r, rd.Route, "#2");
184                 }
185
186                 // below tests in RouteCollection, unlike Route, do some additional checks than Route.GetVirtualPath().
187
188                 [Test]
189                 [ExpectedException (typeof (NotImplementedException))]
190                 public void GetVirtualPathNoApplicationPath ()
191                 {
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);
198                 }
199
200                 [Test]
201                 [ExpectedException (typeof (NotImplementedException))]
202                 public void GetVirtualPathNoApplyAppPathModifier ()
203                 {
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);
211                 }
212
213                 [Test]
214                 public void GetVirtualPathCheckVirtualPathToModify ()
215                 {
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);
222                         try {
223                                 var vpd = c.GetVirtualPath (new RequestContext (hc, rd), rd.Values);
224                                 Assert.Fail ("#1");
225                         } catch (ApplicationException ex) {
226                                 Assert.AreEqual ("apppath/x/y", ex.Message, "#2");
227                         }
228                 }
229
230                 [Test]
231                 public void GetVirtualPath ()
232                 {
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");
242                 }
243
244                 [Test]
245                 [Ignore ("looks like RouteExistingFiles ( = false) does not affect... so this test needs more investigation")]
246                 public void GetVirtualPathToExistingFile ()
247                 {
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");
257                 }
258         }
259 }