2006-01-30 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / Test / System.Web / VirtualPathUtilityTest.cs
1 //
2 // System.Web.VirtualPathUtilityTest.cs - Unit tests for System.Web.VirtualPathUtility
3 //
4 // Author:
5 //      Chris Toshok  <toshok@novell.com>
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Web;
34 using VPU = System.Web.VirtualPathUtility;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Web {
39
40         [TestFixture]
41         public class VirtualPathUtilityTest {
42                 [Test]
43                 public void AppendTrailingSlash ()
44                 {
45                         Assert.AreEqual ("/hithere/", VPU.AppendTrailingSlash ("/hithere"), "A1");
46                         Assert.AreEqual ("/hithere/", VPU.AppendTrailingSlash ("/hithere/"), "A2");
47                         Assert.AreEqual ("/", VPU.AppendTrailingSlash ("/"), "A3");
48                         Assert.AreEqual ("", VPU.AppendTrailingSlash (""), "A4");
49                         Assert.AreEqual (null, VPU.AppendTrailingSlash (null), "A5");
50                 }
51
52                 [Test]
53                 public void Combine ()
54                 {
55                         Assert.AreEqual ("/there", VPU.Combine ("/hi", "there"), "A1");
56                         Assert.AreEqual ("/hi/you", VPU.Combine ("/hi/there", "you"), "A2");
57                         Assert.AreEqual ("/hi/there/you", VPU.Combine ("/hi/there/", "you"), "A3");
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (ArgumentException))]
62                 public void Combine_ArgException1 ()
63                 {
64                         Assert.AreEqual ("hi/there/you", VPU.Combine ("hi/there", "you"), "A1");
65                 }
66
67                 [Test]
68                 [ExpectedException (typeof (ArgumentNullException))]
69                 public void Combine_ArgException2 ()
70                 {
71                         Assert.AreEqual ("hi/there", VPU.Combine ("hi/there", null), "A1");
72                 }
73
74                 [Test]
75                 [ExpectedException (typeof (ArgumentNullException))]
76                 public void Combine_ArgException3 ()
77                 {
78                         Assert.AreEqual ("hi/there", VPU.Combine (null, "there"), "A1");
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (ArgumentNullException))]
83                 /* stack trace is:
84                    at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
85                    at System.Web.VirtualPathUtility.Combine(String basePath, String relativePath)
86                    at MonoTests.System.Web.VirtualPathUtilityTest.Combine()
87                 */
88                 public void Combine_ArgException4 ()
89                 {
90                         Assert.AreEqual ("/you", VPU.Combine ("", "you"), "A1");
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (ArgumentNullException))]
95                 /* stack trace is:
96                    at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
97                    at System.Web.VirtualPathUtility.Combine(String basePath, String relativePath)
98                    at MonoTests.System.Web.VirtualPathUtilityTest.Combine()
99                 */
100                 public void Combine_ArgException5 ()
101                 {
102                         Assert.AreEqual ("/hi", VPU.Combine ("/hi", ""), "A1");
103                 }
104
105                 [Test]
106                 public void GetDirectory ()
107                 {
108                         Assert.AreEqual ("/hi/", VPU.GetDirectory ("/hi/there"), "A1");
109                         Assert.AreEqual ("/hi/", VPU.GetDirectory ("/hi/there/"), "A2");
110                         Assert.AreEqual (null, VPU.GetDirectory ("/"), "A3");
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentNullException))]
115                 /* stack trace is:
116                    at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
117                    at System.Web.VirtualPathUtility.GetDirectory(String virtualPath)
118                    at MonoTests.System.Web.VirtualPathUtilityTest.GetDirectory()
119                  */
120                 public void GetDirectory_ArgException1 ()
121                 {
122                         Assert.AreEqual ("", VPU.GetDirectory (""), "A1");
123                 }
124
125                 [Test]
126                 public void GetExtension ()
127                 {
128                         Assert.AreEqual (".aspx", VPU.GetExtension ("/hi/index.aspx"), "A1");
129                         Assert.AreEqual (".aspx", VPU.GetExtension ("index.aspx"), "A2");
130                         Assert.AreEqual ("", VPU.GetExtension ("/hi/index"), "A3");
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (ArgumentNullException))]
135                 public void GetExtension_ArgException1 ()
136                 {
137                         Assert.AreEqual (null, VPU.GetExtension (null), "A1");
138                 }
139
140                 [Test]
141                 [ExpectedException (typeof (ArgumentNullException))]
142                 public void GetExtension_ArgException2 ()
143                 {
144                         Assert.AreEqual ("", VPU.GetExtension (""), "A1");
145                 }
146
147                 [Test]
148                 public void GetFileName ()
149                 {
150                         Assert.AreEqual ("index.aspx", VPU.GetFileName ("/hi/index.aspx"), "A1");
151                         Assert.AreEqual ("hi", VPU.GetFileName ("/hi/"), "A2");
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (ArgumentNullException))]
156                 public void GetFileName_ArgException1 ()
157                 {
158                         Assert.AreEqual (null, VPU.GetFileName (null), "A1");
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentNullException))]
163                 public void GetFileName_ArgException2 ()
164                 {
165                         Assert.AreEqual ("", VPU.GetFileName (""), "A1");
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (ArgumentException))]
170                 public void GetFileName_ArgException3 ()
171                 {
172                         Assert.AreEqual ("index.aspx", VPU.GetFileName ("index.aspx"), "A1");
173                 }
174
175                 [Test]
176                 public void IsAbsolute ()
177                 {
178                         Assert.IsTrue (VPU.IsAbsolute ("/"), "A1");
179                         Assert.IsTrue (VPU.IsAbsolute ("/hi/there"), "A2");
180                         Assert.IsFalse (VPU.IsAbsolute ("hi/there"), "A3");
181                         Assert.IsFalse (VPU.IsAbsolute ("./hi"), "A4");
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (ArgumentNullException))]
186                 public void IsAbsolute_ArgException1 ()
187                 {
188                         Assert.IsFalse (VPU.IsAbsolute (""), "A1");
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (ArgumentNullException))]
193                 public void IsAbsolute_ArgException2 ()
194                 {
195                         Assert.IsFalse (VPU.IsAbsolute (null), "A1");
196                 }
197
198                 [Test]
199                 public void IsAppRelative ()
200                 {
201                         Assert.IsTrue (VPU.IsAppRelative ("~/Stuff"), "A1");
202                         Assert.IsFalse (VPU.IsAppRelative ("./Stuff"), "A2");
203                         Assert.IsFalse (VPU.IsAppRelative ("/Stuff"), "A3");
204                         Assert.IsFalse (VPU.IsAppRelative ("/"), "A4");
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (ArgumentNullException))]
209                 public void IsAppRelative_ArgException1 ()
210                 {
211                         Assert.IsFalse (VPU.IsAppRelative (""), "A1");
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (ArgumentNullException))]
216                 public void IsAppRelative_ArgException2 ()
217                 {
218                         Assert.IsFalse (VPU.IsAppRelative (null), "A1");
219                 }
220
221 #if false
222                 [Test]
223                 /* this test when run on MS generates the following stack trace (NRE):
224                    at System.Web.Util.UrlPath.MakeVirtualPathAppAbsolute(String virtualPath, String applicationPath)
225                    at System.Web.Util.UrlPath.MakeRelative(String from, String to)
226                    at System.Web.VirtualPathUtility.MakeRelative(String fromPath, String toPath)
227                    at MonoTests.System.Web.VirtualPathUtilityTest.MakeRelative()
228                 */
229                 public void MakeRelative ()
230                 {
231                         Assert.AreEqual ("../bar", VPU.MakeRelative ("~/foo/hi", "~/foo/bar"), "A1");
232                 }
233 #endif
234
235                 [Test]
236                 public void RemoveTrailingSlash ()
237                 {
238                         Assert.AreEqual ("/hi/there", VPU.RemoveTrailingSlash ("/hi/there/"), "A1");
239                         Assert.AreEqual ("/hi/there", VPU.RemoveTrailingSlash ("/hi/there"), "A2");
240                         Assert.AreEqual ("/", VPU.RemoveTrailingSlash ("/"), "A3");
241                         Assert.AreEqual (null, VPU.RemoveTrailingSlash (""), "A4");
242                         Assert.AreEqual (null, VPU.RemoveTrailingSlash (null), "A5");
243                 }
244
245                 [Test]
246                 [ExpectedException (typeof (ArgumentNullException))]
247                 public void Combine1 ()
248                 {
249                         VPU.Combine (null, "something");
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (ArgumentNullException))]
254                 public void Combine2 ()
255                 {
256                         VPU.Combine ("something", null);
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (ArgumentNullException))]
261                 public void GetDirectory1 ()
262                 {
263                         VPU.GetDirectory (null);
264                 }
265                 
266                 [Test]
267                 [ExpectedException (typeof (ArgumentException))]
268                 public void GetDirectory2 ()
269                 {
270                         VPU.GetDirectory ("");
271                 }
272
273                 [Test]
274                 [ExpectedException (typeof (ArgumentNullException))]
275                 public void GetDirectory3 ()
276                 {
277                         VPU.GetDirectory ("hola");
278                 }
279
280                 [Test]
281                 public void GetDirectory4 ()
282                 {
283                         Assert.AreEqual ("/direc/", VPU.GetDirectory ("/direc/somefilenoextension"));
284                         Assert.AreEqual ("/direc/", VPU.GetDirectory ("/direc/somefile.aspx"));
285                         Assert.AreEqual ("/direc/", VPU.GetDirectory ("/////direc///somefile.aspx"));
286                 }
287
288                 [Test]
289                 [ExpectedException (typeof (ArgumentNullException))]
290                 public void GetExtension1 ()
291                 {
292                         VPU.GetExtension (null);
293                 }
294
295                 [Test]
296                 [ExpectedException (typeof (ArgumentNullException))]
297                 public void GetExtension2 ()
298                 {
299                         // Amazing.
300                         VPU.GetExtension ("");
301                 }
302
303                 [Test]
304                 public void GetExtension3 ()
305                 {
306                         Assert.AreEqual ("", VPU.GetExtension ("/direc/somefilenoextension"));
307                         Assert.AreEqual ("", VPU.GetExtension ("/"));
308                         Assert.AreEqual (".aspx", VPU.GetDirectory ("/////direc///somefile.aspx"));
309                 }
310
311                 [Test]
312                 public void GetFileName1 ()
313                 {
314                         Assert.AreEqual ("", VPU.GetFileName ("/"));
315                         Assert.AreEqual ("hola", VPU.GetFileName ("/hola"));
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentNullException))]
320                 public void GetFileName2 ()
321                 {
322                         VPU.GetFileName (null);
323                 }
324
325                 [Test]
326                 [ExpectedException (typeof (ArgumentNullException))]
327                 public void GetFileName3 ()
328                 {
329                         VPU.GetFileName ("");
330                 }
331
332                 [Test]
333                 [ExpectedException (typeof (NullReferenceException))]
334                 public void MakeRelative1 ()
335                 {
336                         VPU.MakeRelative (null, "");
337                 }
338
339                 [Test]
340                 [ExpectedException (typeof (NullReferenceException))]
341                 public void MakeRelative2 ()
342                 {
343                         VPU.MakeRelative ("", null);
344                 }
345
346                 [Test]
347                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
348                 public void MakeRelative3 ()
349                 {
350                         VPU.MakeRelative ("/", "i");
351                 }
352
353                 [Test]
354                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
355                 public void MakeRelative4 ()
356                 {
357                         VPU.MakeRelative ("aa", "/i");
358                 }
359
360                 [Test]
361                 public void MakeRelative5 ()
362                 {
363                         Assert.AreEqual ("", VPU.MakeRelative ("", ""));
364                         Assert.AreEqual ("", VPU.MakeRelative ("/something", ""));
365                         Assert.AreEqual ("./", VPU.MakeRelative ("/", "/"));
366                 }
367
368                 [Test]
369                 public void RemoveTrailingSlash2 ()
370                 {
371                         Assert.AreEqual (null, VPU.RemoveTrailingSlash (null));
372                         Assert.AreEqual (null, VPU.RemoveTrailingSlash (""));
373                         Assert.AreEqual ("/", VPU.RemoveTrailingSlash ("/"));
374                         Assert.AreEqual ("////", VPU.RemoveTrailingSlash ("/////"));
375                         Assert.AreEqual ("/pepe", VPU.RemoveTrailingSlash ("/pepe"));
376                         Assert.AreEqual ("/pepe", VPU.RemoveTrailingSlash ("/pepe/"));
377                 }
378
379                 [Test]
380                 [ExpectedException (typeof (ArgumentNullException))]
381                 public void ToAbsolute1 ()
382                 {
383                         VPU.ToAbsolute (null);
384                 }
385
386                 [Test]
387                 [ExpectedException (typeof (ArgumentNullException))]
388                 public void ToAbsolute2 ()
389                 {
390                         VPU.ToAbsolute ("");
391                 }
392
393                 [Test]
394                 [ExpectedException (typeof (ArgumentException))]
395                 public void ToAbsolute3 ()
396                 {
397                         VPU.ToAbsolute ("..");
398                 }
399
400                 [Test]
401                 [ExpectedException (typeof (ArgumentException))]
402                 public void ToAbsolute4 ()
403                 {
404                         VPU.ToAbsolute ("...");
405                 }
406
407                 [Test]
408                 [ExpectedException (typeof (ArgumentException))]
409                 public void ToAbsolute5 ()
410                 {
411                         VPU.ToAbsolute ("../blah");
412                 }
413
414                 [Test]
415                 [ExpectedException (typeof (HttpException))]
416                 public void ToAbsolute6 ()
417                 {
418                         VPU.ToAbsolute ("~/");
419                 }
420
421                 [Test]
422                 [ExpectedException (typeof (HttpException))]
423                 public void ToAbsolute7 ()
424                 {
425                         Assert.AreEqual ("/", VPU.ToAbsolute ("/"));
426                 }
427         }
428 }
429
430 #endif
431