2 // System.Web.VirtualPathUtilityTest.cs - Unit tests for System.Web.VirtualPathUtility
5 // Chris Toshok <toshok@novell.com>
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
34 using VPU = System.Web.VirtualPathUtility;
36 using NUnit.Framework;
38 namespace MonoTests.System.Web {
41 public class VirtualPathUtilityTest {
43 public void AppendTrailingSlash ()
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");
53 public void Combine ()
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");
61 [ExpectedException (typeof (ArgumentException))]
62 public void Combine_ArgException1 ()
64 Assert.AreEqual ("hi/there/you", VPU.Combine ("hi/there", "you"), "A1");
68 [ExpectedException (typeof (ArgumentNullException))]
69 public void Combine_ArgException2 ()
71 Assert.AreEqual ("hi/there", VPU.Combine ("hi/there", null), "A1");
75 [ExpectedException (typeof (ArgumentNullException))]
76 public void Combine_ArgException3 ()
78 Assert.AreEqual ("hi/there", VPU.Combine (null, "there"), "A1");
82 [ExpectedException (typeof (ArgumentNullException))]
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()
88 public void Combine_ArgException4 ()
90 Assert.AreEqual ("/you", VPU.Combine ("", "you"), "A1");
94 [ExpectedException (typeof (ArgumentNullException))]
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()
100 public void Combine_ArgException5 ()
102 Assert.AreEqual ("/hi", VPU.Combine ("/hi", ""), "A1");
106 public void GetDirectory ()
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");
114 [ExpectedException (typeof (ArgumentNullException))]
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()
120 public void GetDirectory_ArgException1 ()
122 Assert.AreEqual ("", VPU.GetDirectory (""), "A1");
126 public void GetExtension ()
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");
134 [ExpectedException (typeof (ArgumentNullException))]
135 public void GetExtension_ArgException1 ()
137 Assert.AreEqual (null, VPU.GetExtension (null), "A1");
141 [ExpectedException (typeof (ArgumentNullException))]
142 public void GetExtension_ArgException2 ()
144 Assert.AreEqual ("", VPU.GetExtension (""), "A1");
148 public void GetFileName ()
150 Assert.AreEqual ("index.aspx", VPU.GetFileName ("/hi/index.aspx"), "A1");
151 Assert.AreEqual ("hi", VPU.GetFileName ("/hi/"), "A2");
155 [ExpectedException (typeof (ArgumentNullException))]
156 public void GetFileName_ArgException1 ()
158 Assert.AreEqual (null, VPU.GetFileName (null), "A1");
162 [ExpectedException (typeof (ArgumentNullException))]
163 public void GetFileName_ArgException2 ()
165 Assert.AreEqual ("", VPU.GetFileName (""), "A1");
169 [ExpectedException (typeof (ArgumentException))]
170 public void GetFileName_ArgException3 ()
172 Assert.AreEqual ("index.aspx", VPU.GetFileName ("index.aspx"), "A1");
176 public void IsAbsolute ()
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");
185 [ExpectedException (typeof (ArgumentNullException))]
186 public void IsAbsolute_ArgException1 ()
188 Assert.IsFalse (VPU.IsAbsolute (""), "A1");
192 [ExpectedException (typeof (ArgumentNullException))]
193 public void IsAbsolute_ArgException2 ()
195 Assert.IsFalse (VPU.IsAbsolute (null), "A1");
199 public void IsAppRelative ()
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");
208 [ExpectedException (typeof (ArgumentNullException))]
209 public void IsAppRelative_ArgException1 ()
211 Assert.IsFalse (VPU.IsAppRelative (""), "A1");
215 [ExpectedException (typeof (ArgumentNullException))]
216 public void IsAppRelative_ArgException2 ()
218 Assert.IsFalse (VPU.IsAppRelative (null), "A1");
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()
229 public void MakeRelative ()
231 Assert.AreEqual ("../bar", VPU.MakeRelative ("~/foo/hi", "~/foo/bar"), "A1");
236 public void RemoveTrailingSlash ()
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");
246 [ExpectedException (typeof (ArgumentNullException))]
247 public void Combine1 ()
249 VPU.Combine (null, "something");
253 [ExpectedException (typeof (ArgumentNullException))]
254 public void Combine2 ()
256 VPU.Combine ("something", null);
260 [ExpectedException (typeof (ArgumentNullException))]
261 public void GetDirectory1 ()
263 VPU.GetDirectory (null);
267 [ExpectedException (typeof (ArgumentException))]
268 public void GetDirectory2 ()
270 VPU.GetDirectory ("");
274 [ExpectedException (typeof (ArgumentNullException))]
275 public void GetDirectory3 ()
277 VPU.GetDirectory ("hola");
281 public void GetDirectory4 ()
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"));
289 [ExpectedException (typeof (ArgumentNullException))]
290 public void GetExtension1 ()
292 VPU.GetExtension (null);
296 [ExpectedException (typeof (ArgumentNullException))]
297 public void GetExtension2 ()
300 VPU.GetExtension ("");
304 public void GetExtension3 ()
306 Assert.AreEqual ("", VPU.GetExtension ("/direc/somefilenoextension"));
307 Assert.AreEqual ("", VPU.GetExtension ("/"));
308 Assert.AreEqual (".aspx", VPU.GetDirectory ("/////direc///somefile.aspx"));
312 public void GetFileName1 ()
314 Assert.AreEqual ("", VPU.GetFileName ("/"));
315 Assert.AreEqual ("hola", VPU.GetFileName ("/hola"));
319 [ExpectedException (typeof (ArgumentNullException))]
320 public void GetFileName2 ()
322 VPU.GetFileName (null);
326 [ExpectedException (typeof (ArgumentNullException))]
327 public void GetFileName3 ()
329 VPU.GetFileName ("");
333 [ExpectedException (typeof (NullReferenceException))]
334 public void MakeRelative1 ()
336 VPU.MakeRelative (null, "");
340 [ExpectedException (typeof (NullReferenceException))]
341 public void MakeRelative2 ()
343 VPU.MakeRelative ("", null);
347 [ExpectedException (typeof (ArgumentOutOfRangeException))]
348 public void MakeRelative3 ()
350 VPU.MakeRelative ("/", "i");
354 [ExpectedException (typeof (ArgumentOutOfRangeException))]
355 public void MakeRelative4 ()
357 VPU.MakeRelative ("aa", "/i");
361 public void MakeRelative5 ()
363 Assert.AreEqual ("", VPU.MakeRelative ("", ""));
364 Assert.AreEqual ("", VPU.MakeRelative ("/something", ""));
365 Assert.AreEqual ("./", VPU.MakeRelative ("/", "/"));
369 public void RemoveTrailingSlash2 ()
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/"));
380 [ExpectedException (typeof (ArgumentNullException))]
381 public void ToAbsolute1 ()
383 VPU.ToAbsolute (null);
387 [ExpectedException (typeof (ArgumentNullException))]
388 public void ToAbsolute2 ()
394 [ExpectedException (typeof (ArgumentException))]
395 public void ToAbsolute3 ()
397 VPU.ToAbsolute ("..");
401 [ExpectedException (typeof (ArgumentException))]
402 public void ToAbsolute4 ()
404 VPU.ToAbsolute ("...");
408 [ExpectedException (typeof (ArgumentException))]
409 public void ToAbsolute5 ()
411 VPU.ToAbsolute ("../blah");
415 [ExpectedException (typeof (HttpException))]
416 public void ToAbsolute6 ()
418 VPU.ToAbsolute ("~/");
422 [ExpectedException (typeof (HttpException))]
423 public void ToAbsolute7 ()
425 Assert.AreEqual ("/", VPU.ToAbsolute ("/"));