1 // 2 // UriTest3.cs - Even more (2.0 specific) unit tests for System.Uri 3 // 4 // Author: 5 // Sebastien Pouliot <sebastien@ximian.com> 6 // 7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining 10 // a copy of this software and associated documentation files (the 11 // "Software"), to deal in the Software without restriction, including 12 // without limitation the rights to use, copy, modify, merge, publish, 13 // distribute, sublicense, and/or sell copies of the Software, and to 14 // permit persons to whom the Software is furnished to do so, subject to 15 // the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be 18 // included in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 // 28 29 using NUnit.Framework; 30 31 using System; 32 33 namespace MonoTests.System 34 { 35 [TestFixture] 36 public class UriTest3 37 { 38 private const string absolute = "http://www.mono-project.com/CAS"; 39 private const string relative = "server.com/directory/"; 40 41 [Test] // .ctor (String, UriKind) Constructor4_UriKind_Invalid()42 public void Constructor4_UriKind_Invalid () 43 { 44 try { 45 new Uri ("http://www.contoso.com", (UriKind) 666); 46 Assert.Fail ("#1"); 47 } catch (ArgumentException ex) { 48 // The value '666' passed for the UriKind parameter 49 // is invalid 50 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 51 Assert.IsNull (ex.InnerException, "#3"); 52 Assert.IsNotNull (ex.Message, "#4"); 53 Assert.IsNotNull (ex.Message.IndexOf ("'666'") != -1, "#5:" + ex.Message); 54 Assert.IsNotNull (ex.Message.IndexOf ("UriKind") != -1, "#6:" + ex.Message); 55 Assert.IsNull (ex.ParamName, "#7"); 56 } 57 } 58 59 [Test] // .ctor (String, UriKind) Constructor4_UriString_Null()60 public void Constructor4_UriString_Null () 61 { 62 try { 63 new Uri ((string) null, (UriKind) 666); 64 Assert.Fail ("#1"); 65 } catch (ArgumentNullException ex) { 66 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 67 Assert.IsNull (ex.InnerException, "#3"); 68 Assert.IsNotNull (ex.Message, "#4"); 69 Assert.AreEqual ("uriString", ex.ParamName, "#5"); 70 } 71 } 72 73 [Test] AbsoluteUri_RelativeUri()74 public void AbsoluteUri_RelativeUri () 75 { 76 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2"); 77 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment"); 78 Uri relativeUri = uri1.MakeRelativeUri (uri2); 79 80 try { 81 string result = relativeUri.AbsoluteUri; 82 Assert.Fail ("#1: " + result); 83 } catch (InvalidOperationException ex) { 84 // This operation is not supported for a relative URI 85 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2"); 86 Assert.IsNull (ex.InnerException, "#3"); 87 Assert.IsNotNull (ex.Message, "#4"); 88 } 89 } 90 91 [Test] Absolute_UriKind_Absolute()92 public void Absolute_UriKind_Absolute () 93 { 94 Uri uri = new Uri (absolute, UriKind.Absolute); 95 Assert.AreEqual ("www.mono-project.com", uri.DnsSafeHost, "#1"); 96 Assert.IsTrue (uri.IsAbsoluteUri, "#2"); 97 Assert.AreEqual (absolute, uri.OriginalString, "#3"); 98 Assert.AreEqual (absolute, uri.ToString (), "#4"); 99 Assert.IsFalse (uri.UserEscaped, "#5"); 100 } 101 102 [Test] Relative_UriKind_Relative()103 public void Relative_UriKind_Relative () 104 { 105 Uri uri = new Uri (relative, UriKind.Relative); 106 Assert.IsFalse (uri.IsAbsoluteUri, "#A1"); 107 Assert.AreEqual (relative, uri.OriginalString, "#A2"); 108 Assert.AreEqual (relative, uri.ToString (), "#A3"); 109 Assert.IsFalse (uri.UserEscaped, "#A4"); 110 111 uri = new Uri (string.Empty, UriKind.Relative); 112 Assert.IsFalse (uri.IsAbsoluteUri, "#B1"); 113 Assert.AreEqual (string.Empty, uri.OriginalString, "#B2"); 114 Assert.AreEqual (string.Empty, uri.ToString (), "#B3"); 115 Assert.IsFalse (uri.UserEscaped, "#B4"); 116 117 uri = new Uri ("foo/bar", UriKind.Relative); 118 Assert.IsFalse (uri.IsAbsoluteUri, "#C1"); 119 Assert.AreEqual ("foo/bar", uri.OriginalString, "#C2"); 120 Assert.AreEqual ("foo/bar", uri.ToString (), "#C3"); 121 Assert.IsFalse (uri.UserEscaped, "#C4"); 122 123 uri = new Uri ("/test.aspx", UriKind.Relative); 124 Assert.IsFalse (uri.IsAbsoluteUri, "#D1"); 125 Assert.AreEqual ("/test.aspx", uri.OriginalString, "#D2"); 126 Assert.AreEqual ("/test.aspx", uri.ToString (), "#D3"); 127 Assert.IsFalse (uri.UserEscaped, "#D4"); 128 129 uri = new Uri ("", UriKind.Relative); 130 Assert.IsFalse (uri.IsAbsoluteUri, "#E1"); 131 Assert.AreEqual ("", uri.OriginalString, "#E2"); 132 Assert.AreEqual ("", uri.ToString (), "#E3"); 133 Assert.IsFalse (uri.UserEscaped, "#E4"); 134 135 uri = new Uri ("a", UriKind.Relative); 136 Assert.IsFalse (uri.IsAbsoluteUri, "#F1"); 137 Assert.AreEqual ("a", uri.OriginalString, "#F2"); 138 Assert.AreEqual ("a", uri.ToString (), "#F3"); 139 Assert.IsFalse (uri.UserEscaped, "#F4"); 140 } 141 142 [Test] 143 [ExpectedException (typeof (UriFormatException))] Relative_UriKind_Absolute()144 public void Relative_UriKind_Absolute () 145 { 146 new Uri (relative, UriKind.Absolute); 147 } 148 149 [Test] 150 [ExpectedException (typeof (UriFormatException))] Absolute_UriKind_Relative()151 public void Absolute_UriKind_Relative () 152 { 153 new Uri (absolute, UriKind.Relative); 154 } 155 156 [Test] // TryCreate (String, UriKind, Uri) TryCreate1()157 public void TryCreate1 () 158 { 159 Uri uri; 160 161 uri = new Uri ("http://dummy.com"); 162 Assert.IsTrue (Uri.TryCreate (absolute, UriKind.Absolute, out uri), "absolute-Absolute"); 163 Assert.AreEqual (absolute, uri.AbsoluteUri, "absolute-Absolute-AbsoluteUri"); 164 165 uri = new Uri ("http://dummy.com"); 166 Assert.IsTrue (Uri.TryCreate (absolute, UriKind.RelativeOrAbsolute, out uri), "absolute-RelativeOrAbsolute"); 167 Assert.AreEqual (absolute, uri.AbsoluteUri, "absolute-RelativeOrAbsolute-AbsoluteUri"); 168 169 uri = new Uri ("http://dummy.com"); 170 Assert.IsFalse (Uri.TryCreate (absolute, UriKind.Relative, out uri), "absolute-Relative"); 171 Assert.IsNull (uri, "absolute-Relative-uri"); 172 173 uri = new Uri ("http://dummy.com"); 174 Assert.IsFalse (Uri.TryCreate (relative, UriKind.Absolute, out uri), "relative-Absolute"); 175 Assert.IsNull (uri, "relative-Relative-uri"); 176 177 uri = new Uri ("http://dummy.com"); 178 Assert.IsTrue (Uri.TryCreate (relative, UriKind.RelativeOrAbsolute, out uri), "relative-RelativeOrAbsolute"); 179 Assert.AreEqual (relative, uri.OriginalString, "relative-RelativeOrAbsolute-OriginalString"); 180 181 uri = new Uri ("http://dummy.com"); 182 Assert.IsTrue (Uri.TryCreate (relative, UriKind.Relative, out uri), "relative-Relative"); 183 Assert.AreEqual (relative, uri.OriginalString, "relative-RelativeOrAbsolute-OriginalString"); 184 185 Assert.IsTrue (Uri.TryCreate ("http://mono-project.com/☕", UriKind.Absolute, out uri), "highunicode-Absolute"); 186 Assert.AreEqual("http://mono-project.com/%E2%98%95", uri.AbsoluteUri, "highunicode-Absolute-AbsoluteUri"); 187 188 string mixedCaseUri = "http://mOnO-proJECT.com"; 189 uri = new Uri (mixedCaseUri); 190 Uri uri2; 191 Assert.IsTrue (Uri.TryCreate (mixedCaseUri, UriKind.Absolute, out uri2), "mixedcasehost-absolute"); 192 Assert.AreEqual (uri.AbsoluteUri, uri2.AbsoluteUri, "mixedcasehost-absoluteuri-absoluteuri"); 193 } 194 195 [Test] // TryCreate (String, UriKind, Uri) TryCreate1_UriKind_Invalid()196 public void TryCreate1_UriKind_Invalid () 197 { 198 Uri relativeUri = new Uri (relative, UriKind.Relative); 199 Uri uri = relativeUri; 200 201 try { 202 Uri.TryCreate (absolute, (UriKind) 666, out uri); 203 Assert.Fail ("#A1"); 204 } catch (ArgumentException ex) { 205 // The value '666' passed for the UriKind parameter 206 // is invalid 207 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2"); 208 Assert.IsNull (ex.InnerException, "#A3"); 209 Assert.IsNotNull (ex.Message, "#A4"); 210 Assert.IsNotNull (ex.Message.IndexOf ("'666'") != -1, "#A5:" + ex.Message); 211 Assert.IsNotNull (ex.Message.IndexOf ("UriKind") != -1, "#A6:" + ex.Message); 212 Assert.IsNull (ex.ParamName, "#A7"); 213 214 Assert.IsNotNull (uri, "#A8"); 215 Assert.AreSame (relativeUri, uri, "#A9"); 216 } 217 218 Assert.IsFalse (Uri.TryCreate ((string) null, (UriKind) 666, out uri), "#B1"); 219 Assert.IsNull (uri, "#B2"); 220 } 221 222 [Test] // TryCreate (Uri, String, Uri) TryCreate2()223 public void TryCreate2 () 224 { 225 Uri baseUri = new Uri (absolute); 226 Uri uri; 227 228 uri = new Uri ("http://dummy.com"); 229 Assert.IsTrue (Uri.TryCreate (baseUri, relative, out uri), "baseUri+relative"); 230 Assert.AreEqual ("http://www.mono-project.com/server.com/directory/", uri.AbsoluteUri, "baseUri+relative+AbsoluteUri"); 231 Assert.AreEqual ("http://www.mono-project.com/server.com/directory/", uri.OriginalString, "baseUri+relative+OriginalString"); 232 233 uri = new Uri ("http://dummy.com"); 234 Assert.IsTrue (Uri.TryCreate (baseUri, absolute, out uri), "baseUri+absolute"); 235 Assert.AreEqual (absolute, uri.AbsoluteUri, "baseUri+absolute+AbsoluteUri"); 236 Assert.AreEqual (absolute, uri.OriginalString, "baseUri+absolute+OriginalString"); 237 238 uri = new Uri ("http://dummy.com"); 239 Uri relativeUri = new Uri (relative, UriKind.Relative); 240 Assert.IsFalse (Uri.TryCreate (relativeUri, relative, out uri), "relativeUri+relative"); 241 Assert.IsNull (uri, "relativeUri+relative+Uri"); 242 243 uri = new Uri ("http://dummy.com"); 244 Assert.IsTrue (Uri.TryCreate (relativeUri, absolute, out uri), "relativeUri+absolute"); 245 Assert.AreEqual (absolute, uri.OriginalString, "relativeUri+absolute+OriginalString"); 246 247 uri = new Uri ("http://dummy.com"); 248 string n = null; 249 Assert.IsFalse (Uri.TryCreate (baseUri, n, out uri), "baseUri+null"); 250 Assert.IsNull (uri, "baseUri+null+Uri"); 251 Assert.IsFalse (Uri.TryCreate (relativeUri, n, out uri), "relativeUri+null"); 252 Assert.IsNull (uri, "relativeUri+null+Uri"); 253 Assert.IsFalse (Uri.TryCreate (null, relative, out uri), "null+relative"); 254 Assert.IsNull (uri, "null+relative+Uri"); 255 256 uri = new Uri ("http://dummy.com"); 257 Assert.IsTrue (Uri.TryCreate (null, absolute, out uri), "null+absolute"); 258 Assert.AreEqual (absolute, uri.OriginalString, "null+absolute+OriginalString"); 259 } 260 261 [Test] // TryCreate (Uri, Uri, Uri) TryCreate3()262 public void TryCreate3 () 263 { 264 Uri baseUri = new Uri (absolute); 265 Uri relativeUri = new Uri (relative, UriKind.Relative); 266 Uri uri; 267 268 uri = new Uri ("http://dummy.com"); 269 Assert.IsTrue (Uri.TryCreate (baseUri, relativeUri, out uri), "baseUri+relativeUri"); 270 Assert.AreEqual ("http://www.mono-project.com/server.com/directory/", uri.AbsoluteUri, "baseUri+relativeUri+AbsoluteUri"); 271 Assert.AreEqual ("http://www.mono-project.com/server.com/directory/", uri.OriginalString, "baseUri+relativeUri+OriginalString"); 272 273 uri = new Uri ("http://dummy.com"); 274 Assert.IsTrue (Uri.TryCreate (baseUri, baseUri, out uri), "baseUri+baseUri"); 275 Assert.AreEqual (absolute, uri.AbsoluteUri, "baseUri+baseUri+AbsoluteUri"); 276 Assert.AreEqual (absolute, uri.OriginalString, "baseUri+baseUri+OriginalString"); 277 278 uri = new Uri ("http://dummy.com"); 279 Assert.IsFalse (Uri.TryCreate (relativeUri, relativeUri, out uri), "relativeUri+relativeUri"); 280 Assert.IsNull (uri, "relativeUri+relativeUri+Uri"); 281 282 uri = new Uri ("http://dummy.com"); 283 Assert.IsFalse (Uri.TryCreate (relativeUri, baseUri, out uri), "relativeUri+baseUri"); 284 Assert.IsNull (uri, "relativeUri+baseUri+Uri"); 285 286 uri = new Uri ("http://dummy.com"); 287 Assert.IsFalse (Uri.TryCreate (null, relativeUri, out uri), "null+relativeUri"); 288 Assert.IsNull (uri, "null+relativeUri+Uri"); 289 Assert.IsFalse (Uri.TryCreate (null, baseUri, out uri), "null+baseUri"); 290 Assert.IsNull (uri, "null+baseUri+Uri"); 291 } 292 293 [Test] // TryCreate (Uri, Uri, out Uri) TryCreate3_RelativeUri_Null()294 public void TryCreate3_RelativeUri_Null () 295 { 296 Uri uri = null; 297 Uri baseUri = new Uri (absolute); 298 try { 299 Uri.TryCreate (baseUri, (Uri) null, out uri); 300 Assert.IsNull (uri); 301 } 302 catch (NullReferenceException) { 303 Assert.Fail ("does not throw NRE under FX 4.0"); 304 } 305 } 306 307 [Test] IsWellFormedUriString()308 public void IsWellFormedUriString () 309 { 310 Assert.IsFalse (Uri.IsWellFormedUriString ("http://www.go-mono.com/Main Page", UriKind.Absolute), "http/space"); 311 Assert.IsTrue (Uri.IsWellFormedUriString ("http://www.go-mono.com/Main%20Page", UriKind.Absolute), "http/%20"); 312 Assert.IsFalse (Uri.IsWellFormedUriString (null, UriKind.Absolute), "null"); 313 Assert.IsFalse (Uri.IsWellFormedUriString ("data", UriKind.Absolute), "data"); 314 Assert.IsTrue (Uri.IsWellFormedUriString ("http://www.go-mono.com/Main_Page#1", UriKind.Absolute), "http/hex"); 315 Assert.IsTrue (Uri.IsWellFormedUriString ("test", UriKind.RelativeOrAbsolute), "rel1"); 316 Assert.IsTrue (Uri.IsWellFormedUriString ("/test", UriKind.RelativeOrAbsolute), "rel2"); 317 } 318 319 [Test] IsWellFormedUriString_UriKind_Invalid()320 public void IsWellFormedUriString_UriKind_Invalid () 321 { 322 try { 323 Uri.IsWellFormedUriString ("http://www.go-mono.com/Main Page", 324 (UriKind) 666); 325 Assert.Fail ("#1"); 326 } catch (ArgumentException ex) { 327 // The value '666' passed for the UriKind parameter 328 // is invalid 329 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 330 Assert.IsNull (ex.InnerException, "#3"); 331 Assert.IsNotNull (ex.Message, "#4"); 332 Assert.IsNotNull (ex.Message.IndexOf ("'666'") != -1, "#5:" + ex.Message); 333 Assert.IsNotNull (ex.Message.IndexOf ("UriKind") != -1, "#6:" + ex.Message); 334 Assert.IsNull (ex.ParamName, "#7"); 335 } 336 } 337 338 [Test] Compare()339 public void Compare () 340 { 341 Uri u1 = null; 342 Uri u2 = null; 343 Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "null-null"); 344 345 u1 = new Uri ("http://www.go-mono.com"); 346 Assert.AreEqual (1, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "non-null - null"); 347 348 u1 = null; 349 u2 = new Uri ("http://www.go-mono.com"); 350 Assert.AreEqual (-1, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "null - non-null"); 351 352 u1 = new Uri ("http://www.go-mono.com/Main Page"); 353 u2 = new Uri ("http://www.go-mono.com/Main%20Page"); 354 Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.CurrentCulture), "http/space-http/%20-unescaped"); 355 Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "http/space-http/%20-escaped"); 356 Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.CurrentCulture), "http/space-http/%20-safe"); 357 } 358 359 [Test] IsBaseOf()360 public void IsBaseOf () 361 { 362 Uri http = new Uri ("http://www.mono-project.com/Main_Page#FAQ?Edit"); 363 Assert.IsTrue (http.IsBaseOf (http), "http-http"); 364 365 Uri u = new Uri ("http://www.mono-project.com/Main_Page#FAQ"); 366 Assert.IsTrue (u.IsBaseOf (http), "http-1a"); 367 Assert.IsTrue (http.IsBaseOf (u), "http-1b"); 368 369 u = new Uri ("http://www.mono-project.com/Main_Page"); 370 Assert.IsTrue (u.IsBaseOf (http), "http-2a"); 371 Assert.IsTrue (http.IsBaseOf (u), "http-2b"); 372 373 u = new Uri ("http://www.mono-project.com/"); 374 Assert.IsTrue (u.IsBaseOf (http), "http-3a"); 375 Assert.IsTrue (http.IsBaseOf (u), "http-3b"); 376 377 u = new Uri ("http://www.mono-project.com/Main_Page/"); 378 Assert.IsFalse (u.IsBaseOf (http), "http-4a"); 379 Assert.IsTrue (http.IsBaseOf (u), "http-4b"); 380 381 // docs says the UserInfo isn't evaluated, but... 382 u = new Uri ("http://username:password@www.mono-project.com/Main_Page"); 383 Assert.IsFalse (u.IsBaseOf (http), "http-5a"); 384 Assert.IsFalse (http.IsBaseOf (u), "http-5b"); 385 386 // scheme case sensitive ? no 387 u = new Uri ("HTTP://www.mono-project.com/Main_Page"); 388 Assert.IsTrue (u.IsBaseOf (http), "http-6a"); 389 Assert.IsTrue (http.IsBaseOf (u), "http-6b"); 390 391 // host case sensitive ? no 392 u = new Uri ("http://www.Mono-Project.com/Main_Page"); 393 Assert.IsTrue (u.IsBaseOf (http), "http-7a"); 394 Assert.IsTrue (http.IsBaseOf (u), "http-7b"); 395 396 // path case sensitive ? no 397 u = new Uri ("http://www.Mono-Project.com/MAIN_Page"); 398 Assert.IsTrue (u.IsBaseOf (http), "http-8a"); 399 Assert.IsTrue (http.IsBaseOf (u), "http-8b"); 400 401 // different scheme 402 u = new Uri ("ftp://www.mono-project.com/Main_Page"); 403 Assert.IsFalse (u.IsBaseOf (http), "http-9a"); 404 Assert.IsFalse (http.IsBaseOf (u), "http-9b"); 405 406 // different host 407 u = new Uri ("http://www.go-mono.com/Main_Page"); 408 Assert.IsFalse (u.IsBaseOf (http), "http-10a"); 409 Assert.IsFalse (http.IsBaseOf (u), "http-10b"); 410 411 // different port 412 u = new Uri ("http://www.mono-project.com:8080/"); 413 Assert.IsFalse (u.IsBaseOf (http), "http-11a"); 414 Assert.IsFalse (http.IsBaseOf (u), "http-11b"); 415 416 // specify default port 417 u = new Uri ("http://www.mono-project.com:80/"); 418 Assert.IsTrue (u.IsBaseOf (http), "http-12a"); 419 Assert.IsTrue (http.IsBaseOf (u), "http-12b"); 420 } 421 422 [Test] IsBaseOf_Null()423 public void IsBaseOf_Null () 424 { 425 Uri http = new Uri ("http://www.mono-project.com/Main_Page#FAQ?Edit"); 426 try { 427 http.IsBaseOf (null); 428 Assert.Fail (); 429 } 430 catch (ArgumentNullException) { 431 } 432 } 433 434 [Test] MakeRelativeUri()435 public void MakeRelativeUri () 436 { 437 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2"); 438 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment"); 439 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1"); 440 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0"); 441 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1"); 442 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0"); 443 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5"); 444 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9); 445 Uri uri10 = new Uri ("mailto:xxx@xxx.com"); 446 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola"); 447 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola"); 448 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar"); 449 Uri uri14 = new Uri ("http://www.contoso.com/test1/"); 450 Uri uri15 = new Uri ("http://www.contoso.com/"); 451 Uri uri16 = new Uri ("http://www.contoso.com/test"); 452 453 AssertRelativeUri ("foo/bar/index.htm#fragment", uri1, uri2, "#A"); 454 AssertRelativeUri ("../../index.htm?x=2", uri2, uri1, "#B"); 455 AssertRelativeUri ("../../bar/foo/index.htm?y=1", uri2, uri3, "#C"); 456 AssertRelativeUri ("../../foo/bar/index.htm#fragment", uri3, uri2, "#D"); 457 AssertRelativeUri ("../foo2/index.htm?x=0", uri3, uri4, "#E"); 458 AssertRelativeUri ("../foo/index.htm?y=1", uri4, uri3, "#F"); 459 AssertRelativeUri ("?x=0", uri6, uri6, "#G"); 460 AssertRelativeUri ("foobar.htm?z=0&y=5", uri6, uri7, "#H"); 461 AssertRelativeUri ("?subject=hola", uri10, uri11, "#I"); 462 AssertRelativeUri ("/foo/bar", uri10, uri13, "#J"); 463 464 Uri relativeUri = uri1.MakeRelativeUri (uri8); 465 Assert.IsTrue (relativeUri.IsAbsoluteUri, "#K1"); 466 Assert.AreEqual (uri8.ToString (), relativeUri.ToString (), "#K2"); 467 Assert.AreEqual (uri8.OriginalString, relativeUri.OriginalString, "#K3"); 468 469 relativeUri = uri10.MakeRelativeUri (uri12); 470 Assert.IsTrue (relativeUri.IsAbsoluteUri, "#L1"); 471 Assert.AreEqual (uri12.ToString (), relativeUri.ToString (), "#L2"); 472 Assert.AreEqual (uri12.OriginalString, relativeUri.OriginalString, "#L3"); 473 474 relativeUri = uri4.MakeRelativeUri (uri6); 475 Assert.IsTrue (relativeUri.IsAbsoluteUri, "#M1"); 476 Assert.AreEqual (uri6.ToString (), relativeUri.ToString (), "#M2"); 477 Assert.AreEqual (uri6.OriginalString, relativeUri.OriginalString, "#M3"); 478 479 relativeUri = uri4.MakeRelativeUri (uri5); 480 Assert.IsTrue (relativeUri.IsAbsoluteUri, "#N1"); 481 Assert.AreEqual (uri5.ToString (), relativeUri.ToString (), "#N2"); 482 Assert.AreEqual (uri5.OriginalString, relativeUri.OriginalString, "#N3"); 483 484 AssertRelativeUri ("../", uri14, uri15, "#O"); 485 AssertRelativeUri ("./", uri16, uri15, "#P"); 486 487 Uri a1 = new Uri ("http://something/something2/test/"); 488 Uri a2 = new Uri ("http://something/something2/"); 489 Uri a3 = new Uri ("http://something/something2/test"); 490 Uri a4 = new Uri ("http://something/something2"); 491 492 AssertRelativeUri ("../", a1, a2, "Q1"); 493 AssertRelativeUri ("../../something2", a1, a4, "Q2"); 494 AssertRelativeUri ("./", a3, a2, "Q3"); 495 AssertRelativeUri ("../something2", a3, a4, "Q4"); 496 497 Uri b1 = new Uri ("http://something/test/"); 498 Uri b2 = new Uri ("http://something/"); 499 Uri b3 = new Uri ("http://something/test"); 500 Uri b4 = new Uri ("http://something"); 501 502 AssertRelativeUri ("../", b1, b2, "R1"); 503 AssertRelativeUri ("../", b1, b4, "R2"); 504 AssertRelativeUri ("./", b3, b2, "R3"); 505 AssertRelativeUri ("./", b3, b4, "R4"); 506 507 Uri c1 = new Uri ("C:\\something\\something2\\test\\"); 508 Uri c2 = new Uri ("C:\\something\\something2\\"); 509 Uri c3 = new Uri ("C:\\something\\something2\\test"); 510 Uri c4 = new Uri ("C:\\something\\something2"); 511 512 AssertRelativeUri ("../", c1, c2, "S1"); 513 AssertRelativeUri ("../../something2", c1, c4, "S2"); 514 AssertRelativeUri ("./", c3, c2, "S3"); 515 AssertRelativeUri ("../something2", c3, c4, "S4"); 516 517 Uri d1 = new Uri ("C:\\something\\test\\"); 518 Uri d2 = new Uri ("C:\\something\\"); 519 Uri d3 = new Uri ("C:\\something\\test"); 520 Uri d4 = new Uri ("C:\\something"); 521 522 AssertRelativeUri ("../", d1, d2, "T1"); 523 AssertRelativeUri ("../../something", d1, d4, "T2"); 524 AssertRelativeUri ("./", d3, d2, "T3"); 525 AssertRelativeUri ("../something", d3, d4, "T4"); 526 527 Uri e1 = new Uri ("C:\\something\\"); 528 Uri e2 = new Uri ("C:\\"); 529 Uri e3 = new Uri ("C:\\something"); 530 531 AssertRelativeUri ("../", e1, e2, "U1"); 532 AssertRelativeUri ("./", e3, e2, "U2"); 533 AssertRelativeUri ("", e1, e1, "U3"); 534 AssertRelativeUri ("", e3, e3, "U4"); 535 AssertRelativeUri ("../something", e1, e3, "U5"); 536 AssertRelativeUri ("something/", e3, e1, "U6"); 537 AssertRelativeUri ("something", e2, e3, "U7"); 538 } 539 540 [Test] MakeRelativeUri_Uri_Null()541 public void MakeRelativeUri_Uri_Null () 542 { 543 Uri uri = new Uri ("http://test.com"); 544 try { 545 uri.MakeRelativeUri ((Uri) null); 546 Assert.Fail ("#1"); 547 } 548 catch (ArgumentNullException ex) { 549 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 550 Assert.IsNull (ex.InnerException, "#3"); 551 Assert.IsNotNull (ex.Message, "#4"); 552 Assert.IsNotNull (ex.ParamName, "#5"); 553 Assert.AreEqual ("uri", ex.ParamName, "#6"); 554 } 555 } 556 557 [Test] // LAMESPEC: see bug #321113 OriginalStringRelative()558 public void OriginalStringRelative () 559 { 560 Uri k1 = new Uri ("http://www.mono-project.com"); 561 Uri k2 = new Uri (k1, "docs"); 562 Assert.AreEqual ("http://www.mono-project.com/docs", k2.OriginalString, "#1"); 563 564 Uri a = new Uri ("http://www.mono-project.com:808/foo"); 565 Uri b = new Uri (a, "../docs?queryyy#% %20%23%25bar"); 566 Assert.AreEqual ("http://www.mono-project.com:808/docs?queryyy#% %20%23%25bar", b.OriginalString, "#2"); 567 568 Uri c = new Uri ("http://www.mono-project.com:808/foo"); 569 Uri d = new Uri (a, "../docs?queryyy#%20%23%25bar"); 570 Assert.AreEqual ("http://www.mono-project.com:808/docs?queryyy#%20%23%25bar", d.OriginalString, "#3"); 571 572 Uri e = new Uri ("http://www.mono-project.com:909"); 573 Uri f = new Uri (e, "http://www.mono-project.com:606/docs"); 574 Assert.AreEqual ("http://www.mono-project.com:606/docs", f.OriginalString, "#4"); 575 576 Uri g = new Uri ("http://www.mono-project.com:303/foo"); 577 Uri h = new Uri (g, "?query"); 578 // it doesn't work. MS.NET also returns incorrect URI: ..303/?query 579 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=412604 580 //Assert.AreEqual ("http://www.mono-project.com:303/foo?query", h.OriginalString, "#5"); 581 } 582 583 [Test] PathAndQuery_RelativeUri()584 public void PathAndQuery_RelativeUri () 585 { 586 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2"); 587 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment"); 588 Uri relativeUri = uri1.MakeRelativeUri (uri2); 589 590 try { 591 string result = relativeUri.PathAndQuery; 592 Assert.Fail ("#1: " + result); 593 } catch (InvalidOperationException ex) { 594 // This operation is not supported for a relative URI 595 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2"); 596 Assert.IsNull (ex.InnerException, "#3"); 597 Assert.IsNotNull (ex.Message, "#4"); 598 } 599 } 600 601 [Test] Query_RelativeUri()602 public void Query_RelativeUri () 603 { 604 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2"); 605 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment"); 606 Uri relativeUri = uri1.MakeRelativeUri (uri2); 607 608 try { 609 string result = relativeUri.Query; 610 Assert.Fail ("#1: " + result); 611 } catch (InvalidOperationException ex) { 612 // This operation is not supported for a relative URI 613 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2"); 614 Assert.IsNull (ex.InnerException, "#3"); 615 Assert.IsNotNull (ex.Message, "#4"); 616 } 617 } 618 619 [Test] Scheme_RelativeUri()620 public void Scheme_RelativeUri () 621 { 622 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2"); 623 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment"); 624 Uri relativeUri = uri1.MakeRelativeUri (uri2); 625 626 try { 627 string result = relativeUri.Scheme; 628 Assert.Fail ("#1: " + result); 629 } catch (InvalidOperationException ex) { 630 // This operation is not supported for a relative URI 631 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2"); 632 Assert.IsNull (ex.InnerException, "#3"); 633 Assert.IsNotNull (ex.Message, "#4"); 634 } 635 } 636 637 [Test] UnescapeDataString()638 public void UnescapeDataString () 639 { 640 Assert.AreEqual ("/new folder/", Uri.UnescapeDataString ("/new%20folder/")); 641 Assert.AreEqual ("/new folder/", Uri.UnescapeDataString ("/new%20%66older/")); 642 Assert.AreEqual ("/new+folder/", Uri.UnescapeDataString ("/new+folder/")); 643 } 644 AssertRelativeUri(string expected, Uri uri1, Uri uri2, string msg)645 void AssertRelativeUri (string expected, Uri uri1, Uri uri2, string msg) 646 { 647 Uri relativeUri; 648 relativeUri = uri1.MakeRelativeUri (uri2); 649 650 Assert.IsFalse (relativeUri.IsAbsoluteUri, msg + "1"); 651 Assert.AreEqual (expected, relativeUri.ToString (), msg + "2"); 652 Assert.AreEqual (expected, relativeUri.OriginalString, msg + "3"); 653 Assert.IsFalse (relativeUri.UserEscaped, msg + "4"); 654 } 655 656 [Test] DontCheckHostWithCustomParsers()657 public void DontCheckHostWithCustomParsers () 658 { 659 UriParser.Register (new TolerantUriParser (), "assembly", 0); 660 try { 661 new Uri ("assembly://Spring.Core, Version=1.2.0.20001, Culture=neutral, " 662 + "PublicKeyToken=null/Spring.Objects.Factory.Xml/spring-objects-1.1.xsd"); 663 } catch (UriFormatException) { 664 Assert.Fail ("Spring Uri is expected to work."); 665 } 666 } 667 668 [Test] ParseShortNameAsRelativeOrAbsolute()669 public void ParseShortNameAsRelativeOrAbsolute () 670 { 671 new Uri ("x", UriKind.RelativeOrAbsolute); 672 } 673 674 private class TolerantUriParser : GenericUriParser 675 { 676 private const GenericUriParserOptions DefaultOptions 677 = GenericUriParserOptions.Default 678 | GenericUriParserOptions.GenericAuthority 679 | GenericUriParserOptions.AllowEmptyAuthority; 680 TolerantUriParser()681 public TolerantUriParser() 682 : base(DefaultOptions) 683 { 684 } 685 } 686 687 [Test] DomainLabelLength()688 public void DomainLabelLength () 689 { 690 UriHostNameType type = Uri.CheckHostName ("3.141592653589793238462643383279502884197169399375105820974944592.com"); 691 Assert.AreEqual (UriHostNameType.Dns, type, "DomainLabelLength#1"); 692 type = Uri.CheckHostName ("3141592653589793238462643383279502884197169399375105820974944592.com"); 693 Assert.AreEqual (UriHostNameType.Unknown, type, "DomainLabelLength#2"); 694 type = Uri.CheckHostName ("3.1415926535897932384626433832795028841971693993751058209749445923.com"); 695 Assert.AreEqual (UriHostNameType.Unknown, type, "DomainLabelLength#2"); 696 type = Uri.CheckHostName ("3.141592653589793238462643383279502884197169399375105820974944592._om"); 697 Assert.AreEqual (UriHostNameType.Unknown, type, "DomainLabelLength#3"); 698 } 699 700 // https://bugzilla.xamarin.com/show_bug.cgi?id=58400 701 [Test] Test_LocalPath_Bug58400()702 public static void Test_LocalPath_Bug58400() 703 { 704 var uriAndExpected = new [] 705 { 706 ("file://host/directory/filename", @"\\host\directory\filename"), 707 ("file://host/directory/", @"\\host\directory\"), 708 ("file://host/filename", @"\\host\filename"), 709 ("file://host/", @"\\host\"), 710 ("file://host", @"\\host"), 711 ("file:///directory/filename", "/directory/filename"), 712 ("file:///directory/", "/directory/"), 713 ("file:///filename", "/filename"), 714 ("file:///", "/"), 715 // This is an invalid URI, technically 716 ("file://", "/"), 717 ("file://////hello/world", @"\\hello\world"), 718 ("file://hello/////world", @"\\hello\\\\\world"), 719 }; 720 721 foreach (var tup in uriAndExpected) 722 { 723 var originalUri = tup.Item1; 724 var expectedLocalPath = tup.Item2; 725 var uri = new Uri (originalUri); 726 var localPath = uri.LocalPath; 727 728 Assert.AreEqual (expectedLocalPath, localPath, originalUri); 729 } 730 } 731 } 732 } 733 734