2 // UriTest.cs - NUnit Test Cases for System.Uri
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Ben Maurer (bmaurer@users.sourceforge.net)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Ben Maurer
13 using NUnit.Framework;
17 namespace MonoTests.System
20 public class UriTest : Assertion
22 protected bool isWin32 = false;
25 public void GetReady ()
27 isWin32 = (Path.DirectorySeparatorChar == '\\');
31 public void Constructors ()
36 uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
39 uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
42 uri = new Uri("http://11.22.33.588:9090");
45 uri = new Uri("http://[11:22:33::88]:9090");
48 uri = new Uri("http://[::127.11.22.33]:8080");
51 uri = new Uri("http://[abcde::127.11.22.33]:8080");
56 uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
59 uri = new Uri ("http://www.contoso.com:1234/foo/bar");
62 uri = new Uri ("http://www.contoso.com:1234/");
65 uri = new Uri ("http://www.contoso.com:1234");
69 uri = new Uri ("http://contoso.com?subject=uri");
70 AssertEquals ("#k1", "/", uri.AbsolutePath);
71 AssertEquals ("#k2", "http://contoso.com/?subject=uri", uri.AbsoluteUri);
72 AssertEquals ("#k3", "contoso.com", uri.Authority);
73 AssertEquals ("#k4", "", uri.Fragment);
74 AssertEquals ("#k5", "contoso.com", uri.Host);
75 AssertEquals ("#k6", UriHostNameType.Dns, uri.HostNameType);
76 AssertEquals ("#k7", true, uri.IsDefaultPort);
77 AssertEquals ("#k8", false, uri.IsFile);
78 AssertEquals ("#k9", false, uri.IsLoopback);
79 AssertEquals ("#k10", false, uri.IsUnc);
80 AssertEquals ("#k11", "/", uri.LocalPath);
81 AssertEquals ("#k12", "/?subject=uri", uri.PathAndQuery);
82 AssertEquals ("#k13", 80, uri.Port);
83 AssertEquals ("#k14", "?subject=uri", uri.Query);
84 AssertEquals ("#k15", "http", uri.Scheme);
85 AssertEquals ("#k16", false, uri.UserEscaped);
86 AssertEquals ("#k17", "", uri.UserInfo);
88 uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
89 AssertEquals ("#m1", "", uri.AbsolutePath);
90 AssertEquals ("#m2", "mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri);
91 AssertEquals ("#m3", "contoso.com", uri.Authority);
92 AssertEquals ("#m4", "", uri.Fragment);
93 AssertEquals ("#m5", "contoso.com", uri.Host);
94 AssertEquals ("#m6", UriHostNameType.Dns, uri.HostNameType);
95 AssertEquals ("#m7", true, uri.IsDefaultPort);
96 AssertEquals ("#m8", false, uri.IsFile);
97 AssertEquals ("#m9", false, uri.IsLoopback);
98 AssertEquals ("#m10", false, uri.IsUnc);
99 AssertEquals ("#m11", "", uri.LocalPath);
100 AssertEquals ("#m12", "?subject=uri", uri.PathAndQuery);
101 AssertEquals ("#m13", 25, uri.Port);
102 AssertEquals ("#m14", "?subject=uri", uri.Query);
103 AssertEquals ("#m15", "mailto", uri.Scheme);
104 AssertEquals ("#m16", false, uri.UserEscaped);
105 AssertEquals ("#m17", "user:pwd", uri.UserInfo);
107 uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
108 AssertEquals ("#n1", "/mydir/mysubdir/myfile.ext", uri.AbsolutePath);
109 AssertEquals ("#n2", "file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri);
110 AssertEquals ("#n3", "myserver", uri.Authority);
111 AssertEquals ("#n4", "", uri.Fragment);
112 AssertEquals ("#n5", "myserver", uri.Host);
113 AssertEquals ("#n6", UriHostNameType.Dns, uri.HostNameType);
114 AssertEquals ("#n7", true, uri.IsDefaultPort);
115 AssertEquals ("#n8", true, uri.IsFile);
116 AssertEquals ("#n9", false, uri.IsLoopback);
117 AssertEquals ("#n10", true, uri.IsUnc);
120 AssertEquals ("#n11", @"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath);
122 // myserver never could be the part of Unix path.
123 AssertEquals ("#n11", "/mydir/mysubdir/myfile.ext", uri.LocalPath);
125 AssertEquals ("#n12", "/mydir/mysubdir/myfile.ext", uri.PathAndQuery);
126 AssertEquals ("#n13", -1, uri.Port);
127 AssertEquals ("#n14", "", uri.Query);
128 AssertEquals ("#n15", "file", uri.Scheme);
129 AssertEquals ("#n16", false, uri.UserEscaped);
130 AssertEquals ("#n17", "", uri.UserInfo);
132 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
133 AssertEquals ("#rel1a", "http://www.contoso.com/Hello World.htm", uri.AbsoluteUri);
134 AssertEquals ("#rel1b", true, uri.UserEscaped);
135 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
136 AssertEquals ("#rel2a", "http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri);
137 AssertEquals ("#rel2b", false, uri.UserEscaped);
138 uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
139 AssertEquals ("#rel3", "http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri);
141 uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
142 AssertEquals ("#rel5", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
143 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
144 AssertEquals ("#rel6", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
145 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
146 AssertEquals ("#rel7", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
147 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
148 AssertEquals ("#rel8", "http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
149 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
151 AssertEquals ("#rel9", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
153 AssertEquals ("#rel9", "http://www.contoso.com/../foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
155 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
156 AssertEquals ("#rel10", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
158 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
159 AssertEquals("#22", "http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString ());
161 uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
162 AssertEquals ("#rel30", "http://www.xxx.com/?x=0", uri.ToString());
163 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
164 AssertEquals ("#rel31", "http://www.xxx.com/?x=0", uri.ToString());
165 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
166 AssertEquals ("#rel32", "http://www.xxx.com/index.htm#here", uri.ToString());
168 uri = new Uri ("relative", UriKind.Relative);
169 uri = new Uri ("relative/abc", UriKind.Relative);
170 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
172 Assert ("#rel33", !uri.IsAbsoluteUri);
173 AssertEquals ("#rel34", uri.OriginalString, "relative");
174 Assert ("#rel35", !uri.UserEscaped);
179 public void Constructor_DualHostPort ()
\r
181 string relative = "foo:8080/bar/Hello World.htm";
\r
182 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
\r
183 AssertEquals ("AbsolutePath", "8080/bar/Hello%20World.htm", uri.AbsolutePath);
\r
184 AssertEquals ("AbsoluteUri", "foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri);
\r
185 AssertEquals ("Authority", String.Empty, uri.Authority);
\r
186 AssertEquals ("Fragment", String.Empty, uri.Fragment);
\r
187 AssertEquals ("Host", String.Empty, uri.Host);
\r
188 AssertEquals ("PathAndQuery", "8080/bar/Hello%20World.htm", uri.PathAndQuery);
\r
189 AssertEquals ("Port", -1, uri.Port);
\r
190 AssertEquals ("Query", String.Empty, uri.Query);
\r
191 AssertEquals ("Scheme", "foo", uri.Scheme);
\r
192 AssertEquals ("Query", String.Empty, uri.UserInfo);
\r
194 AssertEquals ("Segments[0]", "8080/", uri.Segments[0]);
\r
195 AssertEquals ("Segments[1]", "bar/", uri.Segments[1]);
\r
196 AssertEquals ("Segments[2]", "Hello%20World.htm", uri.Segments[2]);
\r
198 Assert ("IsDefaultPort", uri.IsDefaultPort);
\r
199 Assert ("IsFile", !uri.IsFile);
\r
200 Assert ("IsLoopback", !uri.IsLoopback);
\r
201 Assert ("IsUnc", !uri.IsUnc);
\r
202 Assert ("UserEscaped", !uri.UserEscaped);
\r
204 AssertEquals ("HostNameType", UriHostNameType.Unknown, uri.HostNameType);
\r
205 Assert ("IsAbsoluteUri", uri.IsAbsoluteUri);
\r
206 AssertEquals ("OriginalString", relative, uri.OriginalString);
208 AssertEquals ("HostNameType", UriHostNameType.Basic, uri.HostNameType);
\r
214 [ExpectedException (typeof (ArgumentNullException))]
\r
216 [ExpectedException (typeof (NullReferenceException))]
\r
218 public void Constructor_NullStringBool ()
\r
220 new Uri (null, "http://www.contoso.com/index.htm", false);
\r
225 [ExpectedException (typeof (NullReferenceException))]
\r
227 public void Constructor_UriNullBool ()
\r
229 new Uri (new Uri ("http://www.contoso.com"), null, false);
\r
232 // regression for bug #47573
234 public void RelativeCtor ()
236 Uri b = new Uri ("http://a/b/c/d;p?q");
237 AssertEquals ("#1", "http://a/g", new Uri (b, "/g").ToString ());
238 AssertEquals ("#2", "http://g/", new Uri (b, "//g").ToString ());
239 AssertEquals ("#3", "http://a/b/c/?y", new Uri (b, "?y").ToString ());
240 Assert ("#4", new Uri (b, "#s").ToString ().EndsWith ("#s"));
242 Uri u = new Uri (b, "/g?q=r");
243 AssertEquals ("#5", "http://a/g?q=r", u.ToString ());
244 AssertEquals ("#6", "?q=r", u.Query);
246 u = new Uri (b, "/g?q=r;. a");
247 AssertEquals ("#5", "http://a/g?q=r;. a", u.ToString ());
248 AssertEquals ("#6", "?q=r;.%20a", u.Query);
253 [ExpectedException (typeof (UriFormatException))]
\r
254 public void RelativeCtor_11_Crasher ()
256 Uri b = new Uri ("http://a/b/c/d;p?q");
\r
257 // this causes crash under MS.NET 1.1
\r
258 AssertEquals ("g:h", "g:h", new Uri (b, "g:h").ToString ());
\r
263 [ExpectedException (typeof (UriFormatException))]
\r
264 public void Bad_IPv6 ()
\r
266 new Uri ("http://0:0:0:0::127.1.2.3]/");
\r
271 [Category ("NotDotNet")]
273 public void LeadingSlashes_ShouldFailOn1x ()
275 // doesn't (but should) fail under 1.x
276 AssertEquals ("#1", "file:///", new Uri ("file:///").ToString ());
277 AssertEquals ("#2", "file:///", new Uri ("file://").ToString ());
282 [Category ("NotWorking")]
284 public void LeadingSlashes_BadResultsOn1x ()
\r
286 // strange behaviours of 1.x - it's probably not worth to fix it
\r
287 // on Mono as 2.0 has been fixed
\r
288 Uri u = new Uri ("file:///foo/bar");
\r
290 AssertEquals ("#3a", String.Empty, u.Host);
\r
291 AssertEquals ("#3b", UriHostNameType.Basic, u.HostNameType);
\r
292 AssertEquals ("#3c", "file:///foo/bar", u.ToString ());
\r
293 AssertEquals ("#3d", false, u.IsUnc);
\r
295 // 1.x misinterpret the first path element as the host name
296 AssertEquals ("#3a", "foo", u.Host);
\r
297 AssertEquals ("#3b", UriHostNameType.Dns, u.HostNameType);
\r
298 AssertEquals ("#3c", "file://foo/bar", u.ToString ());
299 AssertEquals ("#3d", true, u.IsUnc);
301 u = new Uri ("mailto:/foo");
\r
303 AssertEquals ("#13a", String.Empty, u.Host);
\r
304 AssertEquals ("#13b", UriHostNameType.Basic, u.HostNameType);
\r
305 AssertEquals ("#13c", "mailto:/foo", u.ToString ());
\r
307 // 1.x misinterpret the first path element as the host name
308 AssertEquals ("#13a", "foo", u.Host);
\r
309 AssertEquals ("#13b", UriHostNameType.Dns, u.HostNameType);
\r
310 AssertEquals ("#13c", "mailto:foo", u.ToString ());
\r
312 u = new Uri ("mailto://foo");
\r
314 AssertEquals ("#14a", String.Empty, u.Host);
\r
315 AssertEquals ("#14b", UriHostNameType.Basic, u.HostNameType);
\r
316 AssertEquals ("#14c", "mailto://foo", u.ToString ());
\r
318 // 1.x misinterpret the first path element as the host name
319 AssertEquals ("#14a", "foo", u.Host);
\r
320 AssertEquals ("#14b", UriHostNameType.Dns, u.HostNameType);
\r
321 AssertEquals ("#14c", "mailto://foo/", u.ToString ());
\r
323 u = new Uri ("news:/");
\r
324 AssertEquals ("#18a", String.Empty, u.Host);
\r
326 AssertEquals ("#18b", UriHostNameType.Unknown, u.HostNameType);
\r
327 AssertEquals ("#18c", "news:/", u.ToString ());
\r
328 AssertEquals ("#18d", "/", u.AbsolutePath);
\r
329 AssertEquals ("#18e", "news:/", u.AbsoluteUri);
\r
331 AssertEquals ("#18b", UriHostNameType.Basic, u.HostNameType);
332 AssertEquals ("#18c", "news:", u.ToString ());
\r
333 AssertEquals ("#18d", String.Empty, u.AbsolutePath);
\r
334 AssertEquals ("#18e", "news:", u.AbsoluteUri);
\r
336 u = new Uri ("news:/foo");
\r
337 AssertEquals ("#19a", String.Empty, u.Host);
\r
339 AssertEquals ("#19b", UriHostNameType.Unknown, u.HostNameType);
\r
340 AssertEquals ("#19c", "news:/foo", u.ToString ());
\r
341 AssertEquals ("#19d", "/foo", u.AbsolutePath);
\r
342 AssertEquals ("#19e", "news:/foo", u.AbsoluteUri);
\r
344 AssertEquals ("#19b", UriHostNameType.Basic, u.HostNameType);
345 AssertEquals ("#19c", "news:foo", u.ToString ());
\r
346 AssertEquals ("#19d", "foo", u.AbsolutePath);
\r
347 AssertEquals ("#19e", "news:foo", u.AbsoluteUri);
\r
349 u = new Uri ("news://foo");
\r
351 AssertEquals ("#20a", String.Empty, u.Host);
\r
352 AssertEquals ("#20b", UriHostNameType.Unknown, u.HostNameType);
\r
353 AssertEquals ("#20c", "news://foo", u.ToString ());
\r
354 AssertEquals ("#20d", "//foo", u.AbsolutePath);
\r
355 AssertEquals ("#20e", "news://foo", u.AbsoluteUri);
\r
357 AssertEquals ("#20a", "foo", u.Host);
\r
358 AssertEquals ("#20b", UriHostNameType.Dns, u.HostNameType);
359 AssertEquals ("#20c", "news://foo/", u.ToString ());
\r
360 AssertEquals ("#20d", "/", u.AbsolutePath);
\r
361 AssertEquals ("#20e", "news://foo/", u.AbsoluteUri);
\r
363 u = new Uri ("news://foo/bar");
\r
365 AssertEquals ("#22a", String.Empty, u.Host);
\r
366 AssertEquals ("#22b", UriHostNameType.Unknown, u.HostNameType);
\r
367 AssertEquals ("#22c", "news://foo/bar", u.ToString ());
\r
368 AssertEquals ("#22d", "//foo/bar", u.AbsolutePath);
\r
369 AssertEquals ("#22e", "news://foo/bar", u.AbsoluteUri);
\r
371 AssertEquals ("#22a", "foo", u.Host);
\r
372 AssertEquals ("#22b", UriHostNameType.Dns, u.HostNameType);
\r
373 AssertEquals ("#22c", "news://foo/bar", u.ToString ());
\r
374 AssertEquals ("#22d", "/bar", u.AbsolutePath);
\r
375 AssertEquals ("#22e", "news://foo/bar", u.AbsoluteUri);
381 [Category ("NotDotNet")] // does (but shouldn't) fail under 1.x
383 public void LeadingSlashes_FailOn1x ()
\r
385 // 1.x throws an UriFormatException because it can't decode the host name
\r
386 Uri u = new Uri ("mailto:");
\r
387 AssertEquals ("#10a", String.Empty, u.Host);
\r
388 AssertEquals ("#10b", UriHostNameType.Basic, u.HostNameType);
\r
389 AssertEquals ("#10c", "mailto:", u.ToString ());
\r
391 // 1.x throws an UriFormatException because it can't decode the host name
\r
392 u = new Uri ("mailto:/");
\r
393 AssertEquals ("#12a", String.Empty, u.Host);
\r
394 AssertEquals ("#12b", UriHostNameType.Basic, u.HostNameType);
\r
395 AssertEquals ("#12c", "mailto:/", u.ToString ());
\r
397 // 1.x throws an UriFormatException because it cannot detect the format
\r
398 u = new Uri ("mailto:///foo");
\r
399 AssertEquals ("#15a", String.Empty, u.Host);
\r
400 AssertEquals ("#15b", UriHostNameType.Basic, u.HostNameType);
\r
401 AssertEquals ("#15c", "mailto:///foo", u.ToString ());
\r
403 // 1.x throws an UriFormatException because it cannot detect the format
\r
404 u = new Uri ("news:///foo");
\r
405 AssertEquals ("#21a", String.Empty, u.Host);
\r
407 AssertEquals ("#21b", UriHostNameType.Unknown, u.HostNameType);
\r
409 AssertEquals ("#21b", UriHostNameType.Basic, u.HostNameType);
\r
411 AssertEquals ("#21c", "news:///foo", u.ToString ());
\r
412 AssertEquals ("#21d", "///foo", u.AbsolutePath);
\r
413 AssertEquals ("#21e", "news:///foo", u.AbsoluteUri);
\r
417 // some tests from bug 75144
418 public void LeadingSlashes ()
420 Uri u = new Uri ("file://foo/bar");
\r
421 AssertEquals ("#5a", "foo", u.Host);
\r
422 AssertEquals ("#5b", UriHostNameType.Dns, u.HostNameType);
\r
423 AssertEquals ("#5c", "file://foo/bar", u.ToString ());
424 AssertEquals ("#5d", true, u.IsUnc);
426 u = new Uri ("file:////foo/bar");
\r
427 AssertEquals ("#7a", "foo", u.Host);
\r
428 AssertEquals ("#7b", UriHostNameType.Dns, u.HostNameType);
\r
429 AssertEquals ("#7c", "file://foo/bar", u.ToString ());
430 AssertEquals ("#7d", true, u.IsUnc);
432 AssertEquals ("#9", "file://foo/bar", new Uri ("file://///foo/bar").ToString());
434 u = new Uri ("mailto:foo");
\r
435 AssertEquals ("#11a", "foo", u.Host);
\r
436 AssertEquals ("#11b", UriHostNameType.Dns, u.HostNameType);
\r
437 AssertEquals ("#11c", "mailto:foo", u.ToString ());
\r
439 u = new Uri ("news:");
440 AssertEquals ("#16a", String.Empty, u.Host);
442 AssertEquals ("#16b", UriHostNameType.Unknown, u.HostNameType);
444 AssertEquals ("#16b", UriHostNameType.Basic, u.HostNameType);
446 AssertEquals ("#16c", "news:", u.ToString ());
\r
448 u = new Uri ("news:foo");
\r
449 AssertEquals ("#17a", String.Empty, u.Host);
\r
451 AssertEquals ("#17b", UriHostNameType.Unknown, u.HostNameType);
453 AssertEquals ("#17b", UriHostNameType.Basic, u.HostNameType);
455 AssertEquals ("#17c", "news:foo", u.ToString ());
\r
456 AssertEquals ("#17d", "foo", u.AbsolutePath);
\r
457 AssertEquals ("#17e", "news:foo", u.AbsoluteUri);
\r
461 [ExpectedException (typeof (UriFormatException))]
462 public void HttpHostname1 ()
468 [ExpectedException (typeof (UriFormatException))]
470 [Category ("NotDotNet")] // doesn't fail under 1.x
472 public void HttpHostname2 ()
478 [ExpectedException (typeof (UriFormatException))]
479 public void HttpHostname3 ()
485 [ExpectedException (typeof (UriFormatException))]
487 [Category ("NotDotNet")] // doesn't fail under 1.x
489 public void HttpHostname4 ()
491 new Uri ("http:/foo");
495 [ExpectedException (typeof (UriFormatException))]
496 public void HttpHostname5 ()
502 [ExpectedException (typeof (UriFormatException))]
503 public void HttpHostname6 ()
505 new Uri ("http:///");
509 [ExpectedException (typeof (UriFormatException))]
510 public void HttpHostname7 ()
512 new Uri ("http:///foo");
516 [ExpectedException (typeof (UriFormatException))]
517 public void InvalidFile1 ()
523 [ExpectedException (typeof (UriFormatException))]
524 public void InvalidFile2 ()
530 [ExpectedException (typeof (UriFormatException))]
531 public void InvalidFile3 ()
533 new Uri ("file:/foo");
537 [ExpectedException (typeof (UriFormatException))]
538 public void InvalidScheme ()
544 public void ConstructorsRejectRelativePath ()
546 string [] reluris = new string [] {
548 "thisdir/childdir/file",
551 string [] winRelUris = new string [] {
555 for (int i = 0; i < reluris.Length; i++) {
557 new Uri (reluris [i]);
558 Fail ("Should be failed: " + reluris [i]);
559 } catch (UriFormatException) {
564 for (int i = 0; i < winRelUris.Length; i++) {
566 new Uri (winRelUris [i]);
567 Fail ("Should be failed: " + winRelUris [i]);
568 } catch (UriFormatException) {
575 public void LocalPath ()
577 Uri uri = new Uri ("c:\\tmp\\hello.txt");
578 AssertEquals ("#1a", "file:///c:/tmp/hello.txt", uri.ToString ());
579 AssertEquals ("#1b", "c:\\tmp\\hello.txt", uri.LocalPath);
580 AssertEquals ("#1c", "file", uri.Scheme);
581 AssertEquals ("#1d", "", uri.Host);
582 AssertEquals ("#1e", "c:/tmp/hello.txt", uri.AbsolutePath);
584 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
585 AssertEquals ("#3a", "file://cygwin/tmp/hello.txt", uri.ToString ());
587 AssertEquals ("#3b win32", "\\\\cygwin\\tmp\\hello.txt", uri.LocalPath);
589 AssertEquals ("#3b *nix", "/tmp/hello.txt", uri.LocalPath);
590 AssertEquals ("#3c", "file", uri.Scheme);
591 AssertEquals ("#3d", "cygwin", uri.Host);
592 AssertEquals ("#3e", "/tmp/hello.txt", uri.AbsolutePath);
594 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
595 AssertEquals ("#4a", "file://mymachine/cygwin/tmp/hello.txt", uri.ToString ());
597 AssertEquals ("#4b win32", "\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath);
599 AssertEquals ("#4b *nix", "/cygwin/tmp/hello.txt", uri.LocalPath);
600 AssertEquals ("#4c", "file", uri.Scheme);
601 AssertEquals ("#4d", "mymachine", uri.Host);
602 AssertEquals ("#4e", "/cygwin/tmp/hello.txt", uri.AbsolutePath);
604 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
605 AssertEquals ("#5a", "file:///c:/cygwin/tmp/hello.txt", uri.ToString ());
606 AssertEquals ("#5b", "c:\\cygwin\\tmp\\hello.txt", uri.LocalPath);
607 AssertEquals ("#5c", "file", uri.Scheme);
608 AssertEquals ("#5d", "", uri.Host);
609 AssertEquals ("#5e", "c:/cygwin/tmp/hello.txt", uri.AbsolutePath);
613 public void LocalPath_FileHost ()
615 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
616 Uri uri = new Uri("file://one_file.txt");
618 AssertEquals("#6a", "file://one_file.txt/", uri.ToString());
\r
619 AssertEquals ("#6e", "/", uri.AbsolutePath);
\r
620 AssertEquals ("#6f", "/", uri.PathAndQuery);
\r
621 AssertEquals ("#6g", "file://one_file.txt/", uri.GetLeftPart (UriPartial.Path));
\r
623 AssertEquals("#6a", "file://one_file.txt", uri.ToString());
624 AssertEquals("#6e", "", uri.AbsolutePath);
625 AssertEquals ("#6f", "", uri.PathAndQuery);
\r
626 AssertEquals ("#6g", "file://one_file.txt", uri.GetLeftPart (UriPartial.Path));
\r
629 AssertEquals("#6b", "\\\\one_file.txt", uri.LocalPath);
631 AssertEquals("#6b", "/", uri.LocalPath);
632 AssertEquals("#6c", "file", uri.Scheme);
633 AssertEquals("#6d", "one_file.txt", uri.Host);
\r
635 // same tests - but original Uri is now ending with a '/'
\r
637 uri = new Uri ("file://one_file.txt/");
\r
638 AssertEquals ("#7a", "file://one_file.txt/", uri.ToString ());
\r
639 AssertEquals ("#7e", "/", uri.AbsolutePath);
\r
640 AssertEquals ("#7f", "/", uri.PathAndQuery);
\r
641 AssertEquals ("#7g", "file://one_file.txt/", uri.GetLeftPart (UriPartial.Path));
\r
643 AssertEquals ("#7b", "\\\\one_file.txt\\", uri.LocalPath);
\r
645 AssertEquals ("#7b", "/", uri.LocalPath);
\r
646 AssertEquals ("#7c", "file", uri.Scheme);
\r
647 AssertEquals ("#7d", "one_file.txt", uri.Host);
\r
651 public void LocalPath_Escape ()
654 Uri uri = new Uri ("file:///tmp/a%20a");
\r
657 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/tmp/a a"));
\r
659 // actually MS.NET treats /// as \\ thus it fails here.
\r
660 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\a a"));
\r
663 AssertEquals ("#7b", "/tmp/a a", uri.LocalPath);
665 uri = new Uri ("file:///tmp/foo%25bar");
668 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/tmp/foo%bar"));
\r
669 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%25bar"));
\r
671 // actually MS.NET treats /// as \\ thus it fails here.
672 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\foo%bar"));
673 // ditto, file://tmp/foo%25bar (bug in 1.x)
\r
674 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%bar"));
\r
677 AssertEquals ("#8b", "/tmp/foo%bar", uri.LocalPath);
678 AssertEquals ("#8d", "file:///tmp/foo%25bar", uri.ToString ());
681 uri = new Uri ("file:///foo%25bar");
684 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/foo%bar"));
\r
686 // actually MS.NET treats /// as \\ thus it fails here.
687 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\foo%25bar"));
689 // ditto, file://tmp/foo%25bar (bug in 1.x)
\r
690 Assert ("#9c:" + uri.ToString (), uri.ToString ().EndsWith ("//foo%25bar"));
\r
692 AssertEquals ("#9b", "/foo%bar", uri.LocalPath);
693 AssertEquals ("#9d", "file:///foo%25bar", uri.ToString ());
698 public void UnixPath () {
700 AssertEquals ("#6a", "file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString ());
706 Uri uri = new Uri ("http://www.contoso.com");
707 Assert ("#1", !uri.IsUnc);
709 uri = new Uri ("news:123456@contoso.com");
710 Assert ("#2", !uri.IsUnc);
712 uri = new Uri ("file://server/filename.ext");
713 Assert ("#3", uri.IsUnc);
715 uri = new Uri (@"\\server\share\filename.ext");
716 Assert ("#6", uri.IsUnc);
718 uri = new Uri (@"a:\dir\filename.ext");
719 Assert ("#8", !uri.IsUnc);
723 [Category("NotDotNet")]
724 public void UncFail ()
726 Uri uri = new Uri ("/home/user/dir/filename.ext");
727 Assert ("#7", !uri.IsUnc);
731 public void FromHex ()
733 AssertEquals ("#1", 0, Uri.FromHex ('0'));
734 AssertEquals ("#2", 9, Uri.FromHex ('9'));
735 AssertEquals ("#3", 10, Uri.FromHex ('a'));
736 AssertEquals ("#4", 15, Uri.FromHex ('f'));
737 AssertEquals ("#5", 10, Uri.FromHex ('A'));
738 AssertEquals ("#6", 15, Uri.FromHex ('F'));
742 } catch (ArgumentException) {}
746 } catch (ArgumentException) {}
750 } catch (ArgumentException) {}
755 public UriEx (string s) : base (s)
759 public string UnescapeString (string s)
764 public static string UnescapeString (string uri, string target)
766 return new UriEx (uri).UnescapeString (target);
771 public void Unescape ()
773 AssertEquals ("#1", "#", UriEx.UnescapeString ("file://localhost/c#", "%23"));
774 AssertEquals ("#2", "c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"));
775 AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"));
776 AssertEquals ("#1", "#", UriEx.UnescapeString ("http://localhost/c#", "%23"));
777 AssertEquals ("#2", "c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"));
778 AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"));
782 public void HexEscape ()
784 AssertEquals ("#1","%20", Uri.HexEscape (' '));
785 AssertEquals ("#2","%A9", Uri.HexEscape ((char) 0xa9));
786 AssertEquals ("#3","%41", Uri.HexEscape ('A'));
788 Uri.HexEscape ((char) 0x0369);
790 } catch (ArgumentOutOfRangeException) {}
794 public void HexUnescape ()
797 AssertEquals ("#1", ' ', Uri.HexUnescape ("%20", ref i));
798 AssertEquals ("#2", 3, i);
800 AssertEquals ("#3", (char) 0xa9, Uri.HexUnescape ("test%a9test", ref i));
801 AssertEquals ("#4", 7, i);
802 AssertEquals ("#5", 't', Uri.HexUnescape ("test%a9test", ref i));
803 AssertEquals ("#6", 8, i);
805 AssertEquals ("#5", '%', Uri.HexUnescape ("test%a", ref i));
806 AssertEquals ("#6", 5, i);
807 AssertEquals ("#7", '%', Uri.HexUnescape ("testx%xx", ref i));
808 AssertEquals ("#8", 6, i);
810 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
812 AssertEquals ("#9", 227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i));
813 AssertEquals ("#10", 4, i);
815 AssertEquals ("#11", 240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i));
816 AssertEquals ("#12", 4, i);
820 // These won't pass exactly with MS.NET 1.x, due to differences in the
821 // handling of backslashes/forwardslashes
822 [Category ("NotDotNet")]
825 public void HexUnescapeMultiByte ()
827 // Tests from bug 74872
828 // Note: These won't pass exactly with MS.NET, due to differences in the
829 // handling of backslashes/forwardslashes
834 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
835 path = uri.LocalPath;
836 AssertEquals ("#1", 8, path.Length);
837 AssertEquals ("#2", 0x304B, path [6]);
839 // 4-byte character which should be handled as a surrogate
840 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
841 path = uri.LocalPath;
842 AssertEquals ("#3", 9, path.Length);
843 AssertEquals ("#4", 0xDB40, path [6]);
844 AssertEquals ("#5", 0xDD00, path [7]);
845 AssertEquals ("#6", 0x62, path [8]);
847 // 2-byte escape sequence, 2 individual characters
848 uri = new Uri ("file:///foo/a%C2%F8b", true);
849 path = uri.LocalPath;
850 AssertEquals ("#7", 9, path.Length);
851 AssertEquals ("#8", 0xC2, path [6]);
852 AssertEquals ("#9", 0xF8, path [7]);
856 public void IsHexDigit ()
858 Assert ("#1", Uri.IsHexDigit ('a'));
859 Assert ("#2", Uri.IsHexDigit ('f'));
860 Assert ("#3", !Uri.IsHexDigit ('g'));
861 Assert ("#4", Uri.IsHexDigit ('0'));
862 Assert ("#5", Uri.IsHexDigit ('9'));
863 Assert ("#6", Uri.IsHexDigit ('A'));
864 Assert ("#7", Uri.IsHexDigit ('F'));
865 Assert ("#8", !Uri.IsHexDigit ('G'));
869 public void IsHexEncoding ()
871 Assert ("#1", Uri.IsHexEncoding ("test%a9test", 4));
872 Assert ("#2", !Uri.IsHexEncoding ("test%a9test", 3));
873 Assert ("#3", Uri.IsHexEncoding ("test%a9", 4));
874 Assert ("#4", !Uri.IsHexEncoding ("test%a", 4));
878 public void GetLeftPart ()
880 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
881 AssertEquals ("#1", "http://", uri.GetLeftPart (UriPartial.Scheme));
882 AssertEquals ("#2", "http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority));
883 AssertEquals ("#3", "http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path));
885 uri = new Uri ("mailto:user@contoso.com?subject=uri");
886 AssertEquals ("#4", "mailto:", uri.GetLeftPart (UriPartial.Scheme));
887 AssertEquals ("#5", "", uri.GetLeftPart (UriPartial.Authority));
888 AssertEquals ("#6", "mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path));
890 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
891 AssertEquals ("#7", "nntp://", uri.GetLeftPart (UriPartial.Scheme));
892 AssertEquals ("#8", "nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority));
893 AssertEquals ("#9", "nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path));
895 uri = new Uri ("news:123456@contoso.com");
896 AssertEquals ("#10", "news:", uri.GetLeftPart (UriPartial.Scheme));
897 AssertEquals ("#11", "", uri.GetLeftPart (UriPartial.Authority));
898 AssertEquals ("#12", "news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path));
900 uri = new Uri ("file://server/filename.ext");
901 AssertEquals ("#13", "file://", uri.GetLeftPart (UriPartial.Scheme));
902 AssertEquals ("#14", "file://server", uri.GetLeftPart (UriPartial.Authority));
903 AssertEquals ("#15", "file://server/filename.ext", uri.GetLeftPart (UriPartial.Path));
905 uri = new Uri (@"\\server\share\filename.ext");
906 AssertEquals ("#20", "file://", uri.GetLeftPart (UriPartial.Scheme));
907 AssertEquals ("#21", "file://server", uri.GetLeftPart (UriPartial.Authority));
908 AssertEquals ("#22", "file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path));
910 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
911 AssertEquals ("#23", "http://", uri.GetLeftPart (UriPartial.Scheme));
912 AssertEquals ("#24", "http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority));
913 AssertEquals ("#25", "http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path));
917 public void NewsDefaultPort ()
919 Uri uri = new Uri("news://localhost:119/");
920 AssertEquals ("#1", uri.IsDefaultPort, true);
924 public void FragmentEscape ()
926 Uri u = new Uri("http://localhost/index.asp#main#start", false);
927 AssertEquals ("#1", u.Fragment, "#main%23start");
929 u = new Uri("http://localhost/index.asp#main#start", true);
930 AssertEquals ("#2", u.Fragment, "#main#start");
932 // The other code path uses a BaseUri
934 Uri b = new Uri ("http://www.gnome.org");
935 Uri n = new Uri (b, "blah#main#start");
936 AssertEquals ("#3", n.Fragment, "#main%23start");
938 n = new Uri (b, "blah#main#start", true);
939 AssertEquals ("#4", n.Fragment, "#main#start");
943 [ExpectedException(typeof(UriFormatException))]
944 public void IncompleteSchemeDelimiter ()
946 new Uri ("file:/filename.ext");
950 [Category("NotDotNet")]
951 public void CheckHostName1 ()
953 // reported to MSDN Product Feedback Center (FDBK28671)
954 AssertEquals ("#36 known to fail with ms.net: this is not a valid IPv6 address.", UriHostNameType.Unknown, Uri.CheckHostName (":11:22:33:44:55:66:77:88"));
958 public void CheckHostName2 ()
960 AssertEquals ("#1", UriHostNameType.Unknown, Uri.CheckHostName (null));
961 AssertEquals ("#2", UriHostNameType.Unknown, Uri.CheckHostName (""));
962 AssertEquals ("#3", UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"));
963 AssertEquals ("#4", UriHostNameType.Dns, Uri.CheckHostName ("x"));
964 AssertEquals ("#5", UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"));
965 AssertEquals ("#6", UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"));
966 AssertEquals ("#7", UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"));
967 AssertEquals ("#8", UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"));
968 AssertEquals ("#9", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"));
969 AssertEquals ("#10", UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"));
970 AssertEquals ("#11", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."));
971 AssertEquals ("#12", UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"));
972 AssertEquals ("#13", UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"));
973 AssertEquals ("#14", UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"));
976 AssertEquals ("#15", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"));
977 AssertEquals ("#16", UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"));
978 AssertEquals ("#17", UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"));
979 AssertEquals ("#18", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"));
980 AssertEquals ("#19", UriHostNameType.IPv6, Uri.CheckHostName ("11::88"));
981 AssertEquals ("#20", UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"));
982 AssertEquals ("#21", UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"));
983 AssertEquals ("#22", UriHostNameType.IPv6, Uri.CheckHostName ("11::"));
984 AssertEquals ("#23", UriHostNameType.IPv6, Uri.CheckHostName ("::88"));
985 AssertEquals ("#24", UriHostNameType.IPv6, Uri.CheckHostName ("::1"));
986 AssertEquals ("#25", UriHostNameType.IPv6, Uri.CheckHostName ("::"));
987 AssertEquals ("#26", UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"));
988 AssertEquals ("#27", UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"));
989 AssertEquals ("#28", UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"));
990 AssertEquals ("#29", UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"));
991 AssertEquals ("#30", UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"));
992 AssertEquals ("#33", UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"));
994 AssertEquals ("#31", UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"));
995 AssertEquals ("#32", UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"));
996 AssertEquals ("#34", UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"));
997 AssertEquals ("#35", UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"));
998 AssertEquals ("#37", UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"));
999 AssertEquals ("#38", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"));
1000 AssertEquals ("#39", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"));
1001 AssertEquals ("#40", UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"));
1002 AssertEquals ("#41", UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"));
1003 AssertEquals ("#42", UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"));
1004 AssertEquals ("#43", UriHostNameType.Unknown, Uri.CheckHostName (":::"));
1005 AssertEquals ("#44", UriHostNameType.Unknown, Uri.CheckHostName (":"));
\r
1007 AssertEquals ("#45", UriHostNameType.Unknown, Uri.CheckHostName ("*"));
\r
1008 AssertEquals ("#46", UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"));
\r
1009 AssertEquals ("#47", UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"));
\r
1013 public void IsLoopback ()
1015 Uri uri = new Uri("http://loopback:8080");
1016 AssertEquals ("#1", true, uri.IsLoopback);
1017 uri = new Uri("http://localhost:8080");
1018 AssertEquals ("#2", true, uri.IsLoopback);
1019 uri = new Uri("http://127.0.0.1:8080");
1020 AssertEquals ("#3", true, uri.IsLoopback);
1021 uri = new Uri("http://127.0.0.001:8080");
1022 AssertEquals ("#4", true, uri.IsLoopback);
1023 uri = new Uri("http://[::1]");
1024 AssertEquals ("#5", true, uri.IsLoopback);
1025 uri = new Uri("http://[::1]:8080");
1026 AssertEquals ("#6", true, uri.IsLoopback);
1027 uri = new Uri("http://[::0001]:8080");
1028 AssertEquals ("#7", true, uri.IsLoopback);
1029 uri = new Uri("http://[0:0:0:0::1]:8080");
1030 AssertEquals ("#8", true, uri.IsLoopback);
1031 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
1032 AssertEquals ("#9", true, uri.IsLoopback);
1033 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
1034 AssertEquals ("#10", false, uri.IsLoopback);
1035 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
1036 AssertEquals ("#11", false, uri.IsLoopback);
1037 uri = new Uri("http://[::ff00:7f11:2233]:8080");
1038 AssertEquals ("#12", false, uri.IsLoopback);
1039 uri = new Uri("http://[1:0:0:0::1]:8080");
1040 AssertEquals ("#13", false, uri.IsLoopback);
\r
1044 public void IsLoopback_File ()
\r
1046 Uri uri = new Uri ("file:///index.html");
1048 Assert ("file", uri.IsLoopback);
1050 Assert ("file", !uri.IsLoopback);
1055 public void IsLoopback_Relative_Http ()
\r
1057 string relative = "http:8080/bar/Hello World.htm";
\r
1058 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
\r
1059 Assert ("http", !uri.IsLoopback);
\r
1063 public void IsLoopback_Relative_Unknown ()
\r
1065 string relative = "foo:8080/bar/Hello World.htm";
\r
1066 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
\r
1067 Assert ("foo", !uri.IsLoopback);
\r
1071 public void Equals1 ()
1073 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1074 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1075 Assert ("#1", uri1.Equals (uri2));
1076 Assert ("#3", !uri2.Equals ("http://www.contoso.com/index.html?x=1"));
1077 Assert ("#4", !uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"));
1082 [Category("NotDotNet")]
1084 public void Equals2 ()
1086 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1087 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1088 Assert ("#2 known to fail with ms.net 1.x", !uri1.Equals (uri2));
1092 public void Equals3 ()
1094 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1095 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1096 Assert (uri1.Equals (uri2));
1100 public void TestEquals2 ()
1102 Uri a = new Uri ("http://www.go-mono.com");
1103 Uri b = new Uri ("http://www.go-mono.com");
1105 AssertEquals ("#1", a, b);
1107 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1108 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1110 Assert ("#2", a != b);
\r
1111 AssertEquals ("#2a", "mailto:user:pwd@go-mono.com?subject=uri", a.ToString ());
\r
1112 AssertEquals ("#2b", "mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString ());
1114 AssertEquals ("#2", a, b);
1116 a = new Uri ("http://www.go-mono.com/ports/");
1117 b = new Uri ("http://www.go-mono.com/PORTS/");
1119 Assert ("#3", !a.Equals (b));
1123 public void CaseSensitivity ()
\r
1125 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
\r
1126 AssertEquals ("#1", "mailto", mailto.Scheme);
\r
1127 AssertEquals ("#2", "go-mono.com", mailto.Host);
\r
1128 AssertEquals ("#3", "mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString ());
\r
1130 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
\r
1131 AssertEquals ("#4", "http", http.Scheme);
\r
1132 AssertEquals ("#5", "go-mono.com", http.Host);
\r
1133 AssertEquals ("#6", "http://go-mono.com/INDEX.HTML", http.ToString ());
\r
1136 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
\r
1137 AssertEquals ("#7", "ftp", ftp.Scheme);
\r
1138 AssertEquals ("#8", "[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host);
\r
1139 AssertEquals ("#9", "ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString ());
\r
1143 public void GetHashCodeTest ()
1145 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1146 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1147 AssertEquals ("#1", uri1.GetHashCode (), uri2.GetHashCode ());
1148 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1149 Assert ("#2", uri1.GetHashCode () != uri2.GetHashCode ());
1150 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1151 AssertEquals ("#3", uri1.GetHashCode (), uri2.GetHashCode ());
1152 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1153 Assert ("#4", uri1.GetHashCode () != uri2.GetHashCode ());
1157 public void MakeRelative ()
1159 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1160 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1161 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1162 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1163 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1164 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1165 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1166 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1168 AssertEquals ("#1", "foo/bar/index.htm", uri1.MakeRelative (uri2));
1169 AssertEquals ("#2", "../../index.htm", uri2.MakeRelative (uri1));
1171 AssertEquals ("#3", "../../bar/foo/index.htm", uri2.MakeRelative (uri3));
1172 AssertEquals ("#4", "../../foo/bar/index.htm", uri3.MakeRelative (uri2));
1174 AssertEquals ("#5", "../foo2/index.htm", uri3.MakeRelative (uri4));
1175 AssertEquals ("#6", "../foo/index.htm", uri4.MakeRelative (uri3));
1177 AssertEquals ("#7", "https://www.contoso.com/bar/foo/index.htm?y=1",
1178 uri4.MakeRelative (uri5));
1180 AssertEquals ("#8", "http://www.contoso2.com/bar/foo/index.htm?x=0",
1181 uri4.MakeRelative (uri6));
1183 AssertEquals ("#9", "", uri6.MakeRelative (uri6));
1184 AssertEquals ("#10", "foobar.htm", uri6.MakeRelative (uri7));
1186 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1187 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1188 AssertEquals ("#11", "", uri10.MakeRelative (uri11));
1190 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1191 AssertEquals ("#12", "mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12));
1193 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1194 AssertEquals ("#13", "/foo/bar", uri10.MakeRelative (uri13));
1196 AssertEquals ("#14", "http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8));
1200 public void RelativeUri ()
1202 Uri u = new Uri("http://localhost/../../../a");
1204 AssertEquals ("http://localhost/a", u.ToString ());
1206 AssertEquals ("http://localhost/../../../a", u.ToString ());
1209 u = new Uri ("http://localhost/../c/b/../a");
1211 AssertEquals ("http://localhost/c/a", u.ToString ());
1213 AssertEquals ("http://localhost/../c/a", u.ToString ());
1218 public void ToStringTest()
1220 Uri uri = new Uri ("dummy://xxx");
1221 AssertEquals ("#1", "dummy://xxx/", uri.ToString ());
1225 public void CheckSchemeName ()
1227 AssertEquals ("#01", false, Uri.CheckSchemeName (null));
1228 AssertEquals ("#02", false, Uri.CheckSchemeName (""));
1229 AssertEquals ("#03", true, Uri.CheckSchemeName ("http"));
1230 AssertEquals ("#04", true, Uri.CheckSchemeName ("http-"));
1231 AssertEquals ("#05", false, Uri.CheckSchemeName ("6http-"));
1232 AssertEquals ("#06", true, Uri.CheckSchemeName ("http6-"));
1233 AssertEquals ("#07", false, Uri.CheckSchemeName ("http6,"));
1234 AssertEquals ("#08", true, Uri.CheckSchemeName ("http6."));
1235 AssertEquals ("#09", false, Uri.CheckSchemeName ("+http"));
1236 AssertEquals ("#10", true, Uri.CheckSchemeName ("htt+p6"));
\r
1237 // 0x00E1 -> ã
\r
1239 Assert ("#11", !Uri.CheckSchemeName ("htt\u00E1+p6"));
1241 Assert ("#11", Uri.CheckSchemeName ("htt\u00E1+p6"));
1246 public void CheckSchemeName_FirstChar ()
\r
1248 for (int i = 0; i < 256; i++) {
\r
1249 string s = String.Format ("#{0}", i);
\r
1250 char c = (char) i;
\r
1251 bool b = Uri.CheckSchemeName (c.ToString ());
\r
1253 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
\r
1254 AssertEquals (s, valid, b);
\r
1256 AssertEquals (s, Char.IsLetter (c), b);
\r
1262 public void CheckSchemeName_AnyOtherChar ()
\r
1264 for (int i = 0; i < 256; i++) {
\r
1265 string s = String.Format ("#{0}", i);
\r
1266 char c = (char) i;
\r
1267 string scheme = String.Format ("a+b-c.d{0}", c);
\r
1268 bool b = Uri.CheckSchemeName (scheme);
\r
1269 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
\r
1271 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
\r
1272 AssertEquals (s, valid, b);
\r
1274 AssertEquals (s, (Char.IsLetter (c) || common), b);
\r
1280 [ExpectedException (typeof (UriFormatException))]
\r
1281 public void NoHostname ()
1283 Uri uri = new Uri ("http://");
1288 // MS.NET 1.x throws an IndexOutOfRangeException
1289 [Category("NotDotNet")]
1291 public void NoHostname2 ()
1294 Uri uri = new Uri ("file://");
1295 AssertEquals ("#1", true, uri.IsFile);
1296 AssertEquals ("#2", false, uri.IsUnc);
1297 AssertEquals ("#3", "file", uri.Scheme);
1298 AssertEquals ("#4", "/", uri.LocalPath);
1299 AssertEquals ("#5", string.Empty, uri.Query);
1300 AssertEquals ("#6", "/", uri.AbsolutePath);
1301 AssertEquals ("#7", "file:///", uri.AbsoluteUri);
1302 AssertEquals ("#8", string.Empty, uri.Authority);
1303 AssertEquals ("#9", string.Empty, uri.Host);
1304 AssertEquals ("#10", UriHostNameType.Basic, uri.HostNameType);
1305 AssertEquals ("#11", string.Empty, uri.Fragment);
1306 AssertEquals ("#12", true, uri.IsDefaultPort);
1308 Assert ("#13", uri.IsLoopback);
1310 Assert ("#13", !uri.IsLoopback);
1312 AssertEquals ("#14", "/", uri.PathAndQuery);
1313 AssertEquals ("#15", false, uri.UserEscaped);
1314 AssertEquals ("#16", string.Empty, uri.UserInfo);
1315 AssertEquals ("#17", "file://", uri.GetLeftPart (UriPartial.Authority));
1316 AssertEquals ("#18", "file:///", uri.GetLeftPart (UriPartial.Path));
1317 AssertEquals ("#19", "file://", uri.GetLeftPart (UriPartial.Scheme));
1321 public void Segments1 ()
1323 Uri uri = new Uri ("http://localhost/");
1324 string [] segments = uri.Segments;
1325 AssertEquals ("#01", 1, segments.Length);
1326 AssertEquals ("#02", "/", segments [0]);
1330 public void Segments2 ()
1332 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1333 string [] segments = uri.Segments;
1334 AssertEquals ("#01", 3, segments.Length);
1335 AssertEquals ("#02", "/", segments [0]);
1336 AssertEquals ("#03", "dir/", segments [1]);
1337 AssertEquals ("#04", "dummypage.html", segments [2]);
1341 public void Segments3 ()
1343 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1344 string [] segments = uri.Segments;
1345 AssertEquals ("#01", 3, segments.Length);
1346 AssertEquals ("#02", "/", segments [0]);
1347 AssertEquals ("#03", "dir/", segments [1]);
1348 AssertEquals ("#04", "dummypage/", segments [2]);
1353 [Category ("NotWorking")]
1355 public void Segments4 ()
1357 Uri uri = new Uri ("file:///c:/hello");
1358 string [] segments = uri.Segments;
1359 AssertEquals ("#01", 3, segments.Length);
1361 AssertEquals ("#02", "/", segments [0]);
\r
1362 AssertEquals ("#03", "c:/", segments[1]);
\r
1364 AssertEquals ("#02", "c:", segments [0]);
1365 AssertEquals ("#03", "/", segments [1]);
1367 AssertEquals ("#04", "hello", segments [2]);
1371 public void Segments5 ()
1373 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1374 string [] segments = uri.Segments;
1375 AssertEquals ("#01", 4, segments.Length);
1376 AssertEquals ("#02", "/", segments [0]);
1377 AssertEquals ("#03", "bar/", segments [1]);
1378 AssertEquals ("#04", "foo/", segments [2]);
1379 AssertEquals ("#05", "foobar.htm", segments [3]);
1383 [ExpectedException (typeof (UriFormatException))]
1384 public void UriStartingWithColon()
1390 [ExpectedException (typeof (UriFormatException))]
1391 public void EmptyScheme ()
1397 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1398 [ExpectedException (typeof (UriFormatException))]
1401 public void InvalidPort1 ()
1403 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1404 AssertEquals (65536, uri.Port);
1408 [ExpectedException (typeof (UriFormatException))]
1411 public void InvalidPort2 ()
1413 // UInt32.MaxValue gives port == -1 !!!
1414 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1415 AssertEquals (-1, uri.Port);
1419 [ExpectedException (typeof (UriFormatException))]
1422 public void InvalidPort3 ()
1424 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1425 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1426 AssertEquals (-2147483648, uri.Port);
1431 public void PortMax ()
1433 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1434 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1435 AssertEquals (65535, uri.Port);
1441 public UriEx2 (string s) : base (s)
1445 protected override void Parse ()
1451 // Parse method is no longer used on .NET 2.0
1452 [ExpectedException (typeof (UriFormatException))]
1455 public void ParseOverride ()
1457 // If this does not override base's Parse(), it will
1458 // fail since this argument is not Absolute URI.
1459 UriEx2 ex = new UriEx2 ("readme.txt");
1463 public void UnixLocalPath ()
1465 // This works--the location is not part of the absolute path
1466 string path = "file://localhost/tmp/foo/bar";
1467 Uri fileUri = new Uri( path );
1468 AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1470 // Empty path == localhost, in theory
1471 path = "file:///c:/tmp/foo/bar";
1472 fileUri = new Uri( path );
1473 AssertEquals (path, "c:/tmp/foo/bar", fileUri.AbsolutePath);
1476 // This test doesn't work on Linux, and arguably shouldn't work.
1477 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1478 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1479 // Apparently "tmp" is supposed to be a hostname (!)...
1480 // Since "correct" behavior would confuse all Linux developers, and having
1481 // an expected failure is evil, we'll just ignore this for now...
1483 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1485 // You are surrounded by conditional-compilation code, all alike.
1486 // You are likely to be eaten by a Grue...
1489 [Category ("NotDotNet")]
1491 public void UnixLocalPath_WTF ()
1493 // Empty path == localhost, in theory
1494 string path = "file:///tmp/foo/bar";
1495 Uri fileUri = new Uri( path );
1497 AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1499 // AssertEquals (path, "/foo/bar", fileUri.AbsolutePath);
1503 string path2 = "file:///foo%25bar";
1504 fileUri = new Uri (path2);
1505 AssertEquals (path2, "file:///foo%25bar", fileUri.ToString ());
1508 public static void Print (Uri uri)
1510 Console.WriteLine ("ToString: " + uri.ToString ());
1512 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1513 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1514 Console.WriteLine ("Authority: " + uri.Authority);
1515 Console.WriteLine ("Fragment: " + uri.Fragment);
1516 Console.WriteLine ("Host: " + uri.Host);
1517 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1518 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1519 Console.WriteLine ("IsFile: " + uri.IsFile);
1520 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1521 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1522 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1523 Console.WriteLine ("PathAndQuery: " + uri.PathAndQuery);
1524 Console.WriteLine ("Port: " + uri.Port);
1525 Console.WriteLine ("Query: " + uri.Query);
1526 Console.WriteLine ("Scheme: " + uri.Scheme);
1527 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1528 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1530 Console.WriteLine ("Segments:");
1531 string [] segments = uri.Segments;
1532 if (segments == null)
1533 Console.WriteLine ("\tNo Segments");
1535 for (int i = 0; i < segments.Length; i++)
1536 Console.WriteLine ("\t" + segments[i]);
1537 Console.WriteLine ("");