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;
18 namespace MonoTests.System
21 public class UriTest : Assertion
23 protected bool isWin32 = false;
26 public void GetReady ()
28 isWin32 = (Path.DirectorySeparatorChar == '\\');
32 public void Constructors ()
37 uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
40 uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
43 uri = new Uri("http://11.22.33.588:9090");
46 uri = new Uri("http://[11:22:33::88]:9090");
49 uri = new Uri("http://[::127.11.22.33]:8080");
52 uri = new Uri("http://[abcde::127.11.22.33]:8080");
57 uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
60 uri = new Uri ("http://www.contoso.com:1234/foo/bar");
63 uri = new Uri ("http://www.contoso.com:1234/");
66 uri = new Uri ("http://www.contoso.com:1234");
70 uri = new Uri ("http://contoso.com?subject=uri");
71 AssertEquals ("#k1", "/", uri.AbsolutePath);
72 AssertEquals ("#k2", "http://contoso.com/?subject=uri", uri.AbsoluteUri);
73 AssertEquals ("#k3", "contoso.com", uri.Authority);
74 AssertEquals ("#k4", "", uri.Fragment);
75 AssertEquals ("#k5", "contoso.com", uri.Host);
76 AssertEquals ("#k6", UriHostNameType.Dns, uri.HostNameType);
77 AssertEquals ("#k7", true, uri.IsDefaultPort);
78 AssertEquals ("#k8", false, uri.IsFile);
79 AssertEquals ("#k9", false, uri.IsLoopback);
80 AssertEquals ("#k10", false, uri.IsUnc);
81 AssertEquals ("#k11", "/", uri.LocalPath);
82 AssertEquals ("#k12", "/?subject=uri", uri.PathAndQuery);
83 AssertEquals ("#k13", 80, uri.Port);
84 AssertEquals ("#k14", "?subject=uri", uri.Query);
85 AssertEquals ("#k15", "http", uri.Scheme);
86 AssertEquals ("#k16", false, uri.UserEscaped);
87 AssertEquals ("#k17", "", uri.UserInfo);
89 uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
90 AssertEquals ("#m1", "", uri.AbsolutePath);
91 AssertEquals ("#m2", "mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri);
92 AssertEquals ("#m3", "contoso.com", uri.Authority);
93 AssertEquals ("#m4", "", uri.Fragment);
94 AssertEquals ("#m5", "contoso.com", uri.Host);
95 AssertEquals ("#m6", UriHostNameType.Dns, uri.HostNameType);
96 AssertEquals ("#m7", true, uri.IsDefaultPort);
97 AssertEquals ("#m8", false, uri.IsFile);
98 AssertEquals ("#m9", false, uri.IsLoopback);
99 AssertEquals ("#m10", false, uri.IsUnc);
100 AssertEquals ("#m11", "", uri.LocalPath);
101 AssertEquals ("#m12", "?subject=uri", uri.PathAndQuery);
102 AssertEquals ("#m13", 25, uri.Port);
103 AssertEquals ("#m14", "?subject=uri", uri.Query);
104 AssertEquals ("#m15", "mailto", uri.Scheme);
105 AssertEquals ("#m16", false, uri.UserEscaped);
106 AssertEquals ("#m17", "user:pwd", uri.UserInfo);
108 uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
109 AssertEquals ("#n1", "/mydir/mysubdir/myfile.ext", uri.AbsolutePath);
110 AssertEquals ("#n2", "file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri);
111 AssertEquals ("#n3", "myserver", uri.Authority);
112 AssertEquals ("#n4", "", uri.Fragment);
113 AssertEquals ("#n5", "myserver", uri.Host);
114 AssertEquals ("#n6", UriHostNameType.Dns, uri.HostNameType);
115 AssertEquals ("#n7", true, uri.IsDefaultPort);
116 AssertEquals ("#n8", true, uri.IsFile);
117 AssertEquals ("#n9", false, uri.IsLoopback);
118 AssertEquals ("#n10", true, uri.IsUnc);
121 AssertEquals ("#n11", @"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath);
123 // myserver never could be the part of Unix path.
124 AssertEquals ("#n11", "/mydir/mysubdir/myfile.ext", uri.LocalPath);
126 AssertEquals ("#n12", "/mydir/mysubdir/myfile.ext", uri.PathAndQuery);
127 AssertEquals ("#n13", -1, uri.Port);
128 AssertEquals ("#n14", "", uri.Query);
129 AssertEquals ("#n15", "file", uri.Scheme);
130 AssertEquals ("#n16", false, uri.UserEscaped);
131 AssertEquals ("#n17", "", uri.UserInfo);
133 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
134 AssertEquals ("#rel1a", "http://www.contoso.com/Hello World.htm", uri.AbsoluteUri);
135 AssertEquals ("#rel1b", true, uri.UserEscaped);
136 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
137 AssertEquals ("#rel2a", "http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri);
138 AssertEquals ("#rel2b", false, uri.UserEscaped);
139 uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
140 AssertEquals ("#rel3", "http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri);
142 uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
143 AssertEquals ("#rel5", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
144 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
145 AssertEquals ("#rel6", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
146 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
147 AssertEquals ("#rel7", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
148 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
149 AssertEquals ("#rel8", "http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
150 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
152 AssertEquals ("#rel9", "http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
154 AssertEquals ("#rel9", "http://www.contoso.com/../foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
156 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
157 AssertEquals ("#rel10", "http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri);
159 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
160 AssertEquals("#22", "http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString ());
162 uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
163 AssertEquals ("#rel30", "http://www.xxx.com/?x=0", uri.ToString());
164 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
165 AssertEquals ("#rel31", "http://www.xxx.com/?x=0", uri.ToString());
166 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
167 AssertEquals ("#rel32", "http://www.xxx.com/index.htm#here", uri.ToString());
169 uri = new Uri ("relative", UriKind.Relative);
170 uri = new Uri ("relative/abc", UriKind.Relative);
171 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
173 Assert ("#rel33", !uri.IsAbsoluteUri);
174 AssertEquals ("#rel34", uri.OriginalString, "relative");
175 Assert ("#rel35", !uri.UserEscaped);
180 public void Constructor_DualHostPort ()
\r
182 string relative = "foo:8080/bar/Hello World.htm";
\r
183 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
\r
184 AssertEquals ("AbsolutePath", "8080/bar/Hello%20World.htm", uri.AbsolutePath);
\r
185 AssertEquals ("AbsoluteUri", "foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri);
\r
186 AssertEquals ("Authority", String.Empty, uri.Authority);
\r
187 AssertEquals ("Fragment", String.Empty, uri.Fragment);
\r
188 AssertEquals ("Host", String.Empty, uri.Host);
\r
189 AssertEquals ("PathAndQuery", "8080/bar/Hello%20World.htm", uri.PathAndQuery);
\r
190 AssertEquals ("Port", -1, uri.Port);
\r
191 AssertEquals ("Query", String.Empty, uri.Query);
\r
192 AssertEquals ("Scheme", "foo", uri.Scheme);
\r
193 AssertEquals ("Query", String.Empty, uri.UserInfo);
\r
195 AssertEquals ("Segments[0]", "8080/", uri.Segments[0]);
\r
196 AssertEquals ("Segments[1]", "bar/", uri.Segments[1]);
\r
197 AssertEquals ("Segments[2]", "Hello%20World.htm", uri.Segments[2]);
\r
199 Assert ("IsDefaultPort", uri.IsDefaultPort);
\r
200 Assert ("IsFile", !uri.IsFile);
\r
201 Assert ("IsLoopback", !uri.IsLoopback);
\r
202 Assert ("IsUnc", !uri.IsUnc);
\r
203 Assert ("UserEscaped", !uri.UserEscaped);
\r
205 AssertEquals ("HostNameType", UriHostNameType.Unknown, uri.HostNameType);
\r
206 Assert ("IsAbsoluteUri", uri.IsAbsoluteUri);
\r
207 AssertEquals ("OriginalString", relative, uri.OriginalString);
209 AssertEquals ("HostNameType", UriHostNameType.Basic, uri.HostNameType);
\r
215 [ExpectedException (typeof (ArgumentNullException))]
\r
217 [ExpectedException (typeof (NullReferenceException))]
\r
219 public void Constructor_NullStringBool ()
\r
221 new Uri (null, "http://www.contoso.com/index.htm", false);
\r
226 [ExpectedException (typeof (NullReferenceException))]
\r
228 public void Constructor_UriNullBool ()
\r
230 new Uri (new Uri ("http://www.contoso.com"), null, false);
\r
233 // regression for bug #47573
235 public void RelativeCtor ()
237 Uri b = new Uri ("http://a/b/c/d;p?q");
238 AssertEquals ("#1", "http://a/g", new Uri (b, "/g").ToString ());
239 AssertEquals ("#2", "http://g/", new Uri (b, "//g").ToString ());
240 AssertEquals ("#3", "http://a/b/c/?y", new Uri (b, "?y").ToString ());
241 Assert ("#4", new Uri (b, "#s").ToString ().EndsWith ("#s"));
243 Uri u = new Uri (b, "/g?q=r");
244 AssertEquals ("#5", "http://a/g?q=r", u.ToString ());
245 AssertEquals ("#6", "?q=r", u.Query);
247 u = new Uri (b, "/g?q=r;. a");
248 AssertEquals ("#5", "http://a/g?q=r;. a", u.ToString ());
249 AssertEquals ("#6", "?q=r;.%20a", u.Query);
254 [ExpectedException (typeof (UriFormatException))]
\r
255 public void RelativeCtor_11_Crasher ()
257 Uri b = new Uri ("http://a/b/c/d;p?q");
\r
258 // this causes crash under MS.NET 1.1
\r
259 AssertEquals ("g:h", "g:h", new Uri (b, "g:h").ToString ());
\r
264 [ExpectedException (typeof (UriFormatException))]
\r
265 public void Bad_IPv6 ()
\r
267 new Uri ("http://0:0:0:0::127.1.2.3]/");
\r
272 [Category ("NotDotNet")]
274 public void LeadingSlashes_ShouldFailOn1x ()
276 // doesn't (but should) fail under 1.x
277 AssertEquals ("#1", "file:///", new Uri ("file:///").ToString ());
278 AssertEquals ("#2", "file:///", new Uri ("file://").ToString ());
283 [Category ("NotWorking")]
285 public void LeadingSlashes_BadResultsOn1x ()
\r
287 // strange behaviours of 1.x - it's probably not worth to fix it
\r
288 // on Mono as 2.0 has been fixed
\r
289 Uri u = new Uri ("file:///foo/bar");
\r
291 AssertEquals ("#3a", String.Empty, u.Host);
\r
292 AssertEquals ("#3b", UriHostNameType.Basic, u.HostNameType);
\r
293 AssertEquals ("#3c", "file:///foo/bar", u.ToString ());
\r
294 AssertEquals ("#3d", false, u.IsUnc);
\r
296 // 1.x misinterpret the first path element as the host name
297 AssertEquals ("#3a", "foo", u.Host);
\r
298 AssertEquals ("#3b", UriHostNameType.Dns, u.HostNameType);
\r
299 AssertEquals ("#3c", "file://foo/bar", u.ToString ());
300 AssertEquals ("#3d", true, u.IsUnc);
302 u = new Uri ("mailto:/foo");
\r
304 AssertEquals ("#13a", String.Empty, u.Host);
\r
305 AssertEquals ("#13b", UriHostNameType.Basic, u.HostNameType);
\r
306 AssertEquals ("#13c", "mailto:/foo", u.ToString ());
\r
308 // 1.x misinterpret the first path element as the host name
309 AssertEquals ("#13a", "foo", u.Host);
\r
310 AssertEquals ("#13b", UriHostNameType.Dns, u.HostNameType);
\r
311 AssertEquals ("#13c", "mailto:foo", u.ToString ());
\r
313 u = new Uri ("mailto://foo");
\r
315 AssertEquals ("#14a", String.Empty, u.Host);
\r
316 AssertEquals ("#14b", UriHostNameType.Basic, u.HostNameType);
\r
317 AssertEquals ("#14c", "mailto://foo", u.ToString ());
\r
319 // 1.x misinterpret the first path element as the host name
320 AssertEquals ("#14a", "foo", u.Host);
\r
321 AssertEquals ("#14b", UriHostNameType.Dns, u.HostNameType);
\r
322 AssertEquals ("#14c", "mailto://foo/", u.ToString ());
\r
324 u = new Uri ("news:/");
\r
325 AssertEquals ("#18a", String.Empty, u.Host);
\r
327 AssertEquals ("#18b", UriHostNameType.Unknown, u.HostNameType);
\r
328 AssertEquals ("#18c", "news:/", u.ToString ());
\r
329 AssertEquals ("#18d", "/", u.AbsolutePath);
\r
330 AssertEquals ("#18e", "news:/", u.AbsoluteUri);
\r
332 AssertEquals ("#18b", UriHostNameType.Basic, u.HostNameType);
333 AssertEquals ("#18c", "news:", u.ToString ());
\r
334 AssertEquals ("#18d", String.Empty, u.AbsolutePath);
\r
335 AssertEquals ("#18e", "news:", u.AbsoluteUri);
\r
337 u = new Uri ("news:/foo");
\r
338 AssertEquals ("#19a", String.Empty, u.Host);
\r
340 AssertEquals ("#19b", UriHostNameType.Unknown, u.HostNameType);
\r
341 AssertEquals ("#19c", "news:/foo", u.ToString ());
\r
342 AssertEquals ("#19d", "/foo", u.AbsolutePath);
\r
343 AssertEquals ("#19e", "news:/foo", u.AbsoluteUri);
\r
345 AssertEquals ("#19b", UriHostNameType.Basic, u.HostNameType);
346 AssertEquals ("#19c", "news:foo", u.ToString ());
\r
347 AssertEquals ("#19d", "foo", u.AbsolutePath);
\r
348 AssertEquals ("#19e", "news:foo", u.AbsoluteUri);
\r
350 u = new Uri ("news://foo");
\r
352 AssertEquals ("#20a", String.Empty, u.Host);
\r
353 AssertEquals ("#20b", UriHostNameType.Unknown, u.HostNameType);
\r
354 AssertEquals ("#20c", "news://foo", u.ToString ());
\r
355 AssertEquals ("#20d", "//foo", u.AbsolutePath);
\r
356 AssertEquals ("#20e", "news://foo", u.AbsoluteUri);
\r
358 AssertEquals ("#20a", "foo", u.Host);
\r
359 AssertEquals ("#20b", UriHostNameType.Dns, u.HostNameType);
360 AssertEquals ("#20c", "news://foo/", u.ToString ());
\r
361 AssertEquals ("#20d", "/", u.AbsolutePath);
\r
362 AssertEquals ("#20e", "news://foo/", u.AbsoluteUri);
\r
364 u = new Uri ("news://foo/bar");
\r
366 AssertEquals ("#22a", String.Empty, u.Host);
\r
367 AssertEquals ("#22b", UriHostNameType.Unknown, u.HostNameType);
\r
368 AssertEquals ("#22c", "news://foo/bar", u.ToString ());
\r
369 AssertEquals ("#22d", "//foo/bar", u.AbsolutePath);
\r
370 AssertEquals ("#22e", "news://foo/bar", u.AbsoluteUri);
\r
372 AssertEquals ("#22a", "foo", u.Host);
\r
373 AssertEquals ("#22b", UriHostNameType.Dns, u.HostNameType);
\r
374 AssertEquals ("#22c", "news://foo/bar", u.ToString ());
\r
375 AssertEquals ("#22d", "/bar", u.AbsolutePath);
\r
376 AssertEquals ("#22e", "news://foo/bar", u.AbsoluteUri);
382 [Category ("NotDotNet")] // does (but shouldn't) fail under 1.x
384 public void LeadingSlashes_FailOn1x ()
\r
386 // 1.x throws an UriFormatException because it can't decode the host name
\r
387 Uri u = new Uri ("mailto:");
\r
388 AssertEquals ("#10a", String.Empty, u.Host);
\r
389 AssertEquals ("#10b", UriHostNameType.Basic, u.HostNameType);
\r
390 AssertEquals ("#10c", "mailto:", u.ToString ());
\r
392 // 1.x throws an UriFormatException because it can't decode the host name
\r
393 u = new Uri ("mailto:/");
\r
394 AssertEquals ("#12a", String.Empty, u.Host);
\r
395 AssertEquals ("#12b", UriHostNameType.Basic, u.HostNameType);
\r
396 AssertEquals ("#12c", "mailto:/", u.ToString ());
\r
398 // 1.x throws an UriFormatException because it cannot detect the format
\r
399 u = new Uri ("mailto:///foo");
\r
400 AssertEquals ("#15a", String.Empty, u.Host);
\r
401 AssertEquals ("#15b", UriHostNameType.Basic, u.HostNameType);
\r
402 AssertEquals ("#15c", "mailto:///foo", u.ToString ());
\r
404 // 1.x throws an UriFormatException because it cannot detect the format
\r
405 u = new Uri ("news:///foo");
\r
406 AssertEquals ("#21a", String.Empty, u.Host);
\r
408 AssertEquals ("#21b", UriHostNameType.Unknown, u.HostNameType);
\r
410 AssertEquals ("#21b", UriHostNameType.Basic, u.HostNameType);
\r
412 AssertEquals ("#21c", "news:///foo", u.ToString ());
\r
413 AssertEquals ("#21d", "///foo", u.AbsolutePath);
\r
414 AssertEquals ("#21e", "news:///foo", u.AbsoluteUri);
\r
418 // some tests from bug 75144
419 public void LeadingSlashes ()
421 Uri u = new Uri ("file://foo/bar");
\r
422 AssertEquals ("#5a", "foo", u.Host);
\r
423 AssertEquals ("#5b", UriHostNameType.Dns, u.HostNameType);
\r
424 AssertEquals ("#5c", "file://foo/bar", u.ToString ());
425 AssertEquals ("#5d", true, u.IsUnc);
427 u = new Uri ("file:////foo/bar");
\r
428 AssertEquals ("#7a", "foo", u.Host);
\r
429 AssertEquals ("#7b", UriHostNameType.Dns, u.HostNameType);
\r
430 AssertEquals ("#7c", "file://foo/bar", u.ToString ());
431 AssertEquals ("#7d", true, u.IsUnc);
433 AssertEquals ("#9", "file://foo/bar", new Uri ("file://///foo/bar").ToString());
435 u = new Uri ("mailto:foo");
\r
436 AssertEquals ("#11a", "foo", u.Host);
\r
437 AssertEquals ("#11b", UriHostNameType.Dns, u.HostNameType);
\r
438 AssertEquals ("#11c", "mailto:foo", u.ToString ());
\r
440 u = new Uri ("news:");
441 AssertEquals ("#16a", String.Empty, u.Host);
443 AssertEquals ("#16b", UriHostNameType.Unknown, u.HostNameType);
445 AssertEquals ("#16b", UriHostNameType.Basic, u.HostNameType);
447 AssertEquals ("#16c", "news:", u.ToString ());
\r
449 u = new Uri ("news:foo");
\r
450 AssertEquals ("#17a", String.Empty, u.Host);
\r
452 AssertEquals ("#17b", UriHostNameType.Unknown, u.HostNameType);
454 AssertEquals ("#17b", UriHostNameType.Basic, u.HostNameType);
456 AssertEquals ("#17c", "news:foo", u.ToString ());
\r
457 AssertEquals ("#17d", "foo", u.AbsolutePath);
\r
458 AssertEquals ("#17e", "news:foo", u.AbsoluteUri);
\r
462 [ExpectedException (typeof (UriFormatException))]
463 public void HttpHostname1 ()
469 [ExpectedException (typeof (UriFormatException))]
471 [Category ("NotDotNet")] // doesn't fail under 1.x
473 public void HttpHostname2 ()
479 [ExpectedException (typeof (UriFormatException))]
480 public void HttpHostname3 ()
486 [ExpectedException (typeof (UriFormatException))]
488 [Category ("NotDotNet")] // doesn't fail under 1.x
490 public void HttpHostname4 ()
492 new Uri ("http:/foo");
496 [ExpectedException (typeof (UriFormatException))]
497 public void HttpHostname5 ()
503 [ExpectedException (typeof (UriFormatException))]
504 public void HttpHostname6 ()
506 new Uri ("http:///");
510 [ExpectedException (typeof (UriFormatException))]
511 public void HttpHostname7 ()
513 new Uri ("http:///foo");
517 [ExpectedException (typeof (UriFormatException))]
518 public void InvalidFile1 ()
524 [ExpectedException (typeof (UriFormatException))]
525 public void InvalidFile2 ()
531 [ExpectedException (typeof (UriFormatException))]
532 public void InvalidFile3 ()
534 new Uri ("file:/foo");
538 [ExpectedException (typeof (UriFormatException))]
539 public void InvalidScheme ()
545 public void ConstructorsRejectRelativePath ()
547 string [] reluris = new string [] {
549 "thisdir/childdir/file",
552 string [] winRelUris = new string [] {
556 for (int i = 0; i < reluris.Length; i++) {
558 new Uri (reluris [i]);
559 Fail ("Should be failed: " + reluris [i]);
560 } catch (UriFormatException) {
565 for (int i = 0; i < winRelUris.Length; i++) {
567 new Uri (winRelUris [i]);
568 Fail ("Should be failed: " + winRelUris [i]);
569 } catch (UriFormatException) {
576 public void LocalPath ()
578 Uri uri = new Uri ("c:\\tmp\\hello.txt");
579 AssertEquals ("#1a", "file:///c:/tmp/hello.txt", uri.ToString ());
580 AssertEquals ("#1b", "c:\\tmp\\hello.txt", uri.LocalPath);
581 AssertEquals ("#1c", "file", uri.Scheme);
582 AssertEquals ("#1d", "", uri.Host);
583 AssertEquals ("#1e", "c:/tmp/hello.txt", uri.AbsolutePath);
585 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
586 AssertEquals ("#3a", "file://cygwin/tmp/hello.txt", uri.ToString ());
588 AssertEquals ("#3b win32", "\\\\cygwin\\tmp\\hello.txt", uri.LocalPath);
590 AssertEquals ("#3b *nix", "/tmp/hello.txt", uri.LocalPath);
591 AssertEquals ("#3c", "file", uri.Scheme);
592 AssertEquals ("#3d", "cygwin", uri.Host);
593 AssertEquals ("#3e", "/tmp/hello.txt", uri.AbsolutePath);
595 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
596 AssertEquals ("#4a", "file://mymachine/cygwin/tmp/hello.txt", uri.ToString ());
598 AssertEquals ("#4b win32", "\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath);
600 AssertEquals ("#4b *nix", "/cygwin/tmp/hello.txt", uri.LocalPath);
601 AssertEquals ("#4c", "file", uri.Scheme);
602 AssertEquals ("#4d", "mymachine", uri.Host);
603 AssertEquals ("#4e", "/cygwin/tmp/hello.txt", uri.AbsolutePath);
605 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
606 AssertEquals ("#5a", "file:///c:/cygwin/tmp/hello.txt", uri.ToString ());
607 AssertEquals ("#5b", "c:\\cygwin\\tmp\\hello.txt", uri.LocalPath);
608 AssertEquals ("#5c", "file", uri.Scheme);
609 AssertEquals ("#5d", "", uri.Host);
610 AssertEquals ("#5e", "c:/cygwin/tmp/hello.txt", uri.AbsolutePath);
614 public void LocalPath_FileHost ()
616 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
617 Uri uri = new Uri("file://one_file.txt");
619 AssertEquals("#6a", "file://one_file.txt/", uri.ToString());
\r
620 AssertEquals ("#6e", "/", uri.AbsolutePath);
\r
621 AssertEquals ("#6f", "/", uri.PathAndQuery);
\r
622 AssertEquals ("#6g", "file://one_file.txt/", uri.GetLeftPart (UriPartial.Path));
\r
624 AssertEquals("#6a", "file://one_file.txt", uri.ToString());
625 AssertEquals("#6e", "", uri.AbsolutePath);
626 AssertEquals ("#6f", "", uri.PathAndQuery);
\r
627 AssertEquals ("#6g", "file://one_file.txt", uri.GetLeftPart (UriPartial.Path));
\r
630 AssertEquals("#6b", "\\\\one_file.txt", uri.LocalPath);
632 AssertEquals("#6b", "/", uri.LocalPath);
633 AssertEquals("#6c", "file", uri.Scheme);
634 AssertEquals("#6d", "one_file.txt", uri.Host);
\r
636 // same tests - but original Uri is now ending with a '/'
\r
638 uri = new Uri ("file://one_file.txt/");
\r
639 AssertEquals ("#7a", "file://one_file.txt/", uri.ToString ());
\r
640 AssertEquals ("#7e", "/", uri.AbsolutePath);
\r
641 AssertEquals ("#7f", "/", uri.PathAndQuery);
\r
642 AssertEquals ("#7g", "file://one_file.txt/", uri.GetLeftPart (UriPartial.Path));
\r
644 AssertEquals ("#7b", "\\\\one_file.txt\\", uri.LocalPath);
\r
646 AssertEquals ("#7b", "/", uri.LocalPath);
\r
647 AssertEquals ("#7c", "file", uri.Scheme);
\r
648 AssertEquals ("#7d", "one_file.txt", uri.Host);
\r
652 public void LocalPath_Escape ()
655 Uri uri = new Uri ("file:///tmp/a%20a");
\r
658 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/tmp/a a"));
\r
660 // actually MS.NET treats /// as \\ thus it fails here.
\r
661 Assert ("#7a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\a a"));
\r
664 AssertEquals ("#7b", "/tmp/a a", uri.LocalPath);
666 uri = new Uri ("file:///tmp/foo%25bar");
669 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/tmp/foo%bar"));
\r
670 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%25bar"));
\r
672 // actually MS.NET treats /// as \\ thus it fails here.
673 Assert ("#8a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\tmp\\foo%bar"));
674 // ditto, file://tmp/foo%25bar (bug in 1.x)
\r
675 Assert ("#8c:" + uri.ToString (), uri.ToString ().EndsWith ("//tmp/foo%bar"));
\r
678 AssertEquals ("#8b", "/tmp/foo%bar", uri.LocalPath);
679 AssertEquals ("#8d", "file:///tmp/foo%25bar", uri.ToString ());
682 uri = new Uri ("file:///foo%25bar");
685 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("/foo%bar"));
\r
687 // actually MS.NET treats /// as \\ thus it fails here.
688 Assert ("#9a:" + uri.LocalPath, uri.LocalPath.EndsWith ("\\foo%25bar"));
690 // ditto, file://tmp/foo%25bar (bug in 1.x)
\r
691 Assert ("#9c:" + uri.ToString (), uri.ToString ().EndsWith ("//foo%25bar"));
\r
693 AssertEquals ("#9b", "/foo%bar", uri.LocalPath);
694 AssertEquals ("#9d", "file:///foo%25bar", uri.ToString ());
699 public void UnixPath () {
701 AssertEquals ("#6a", "file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString ());
707 Uri uri = new Uri ("http://www.contoso.com");
708 Assert ("#1", !uri.IsUnc);
710 uri = new Uri ("news:123456@contoso.com");
711 Assert ("#2", !uri.IsUnc);
713 uri = new Uri ("file://server/filename.ext");
714 Assert ("#3", uri.IsUnc);
716 uri = new Uri (@"\\server\share\filename.ext");
717 Assert ("#6", uri.IsUnc);
719 uri = new Uri (@"a:\dir\filename.ext");
720 Assert ("#8", !uri.IsUnc);
724 [Category("NotDotNet")]
725 public void UncFail ()
727 Uri uri = new Uri ("/home/user/dir/filename.ext");
728 Assert ("#7", !uri.IsUnc);
732 public void FromHex ()
734 AssertEquals ("#1", 0, Uri.FromHex ('0'));
735 AssertEquals ("#2", 9, Uri.FromHex ('9'));
736 AssertEquals ("#3", 10, Uri.FromHex ('a'));
737 AssertEquals ("#4", 15, Uri.FromHex ('f'));
738 AssertEquals ("#5", 10, Uri.FromHex ('A'));
739 AssertEquals ("#6", 15, Uri.FromHex ('F'));
743 } catch (ArgumentException) {}
747 } catch (ArgumentException) {}
751 } catch (ArgumentException) {}
756 public UriEx (string s) : base (s)
760 public string UnescapeString (string s)
765 public static string UnescapeString (string uri, string target)
767 return new UriEx (uri).UnescapeString (target);
772 public void Unescape ()
774 AssertEquals ("#1", "#", UriEx.UnescapeString ("file://localhost/c#", "%23"));
775 AssertEquals ("#2", "c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"));
776 AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"));
777 AssertEquals ("#1", "#", UriEx.UnescapeString ("http://localhost/c#", "%23"));
778 AssertEquals ("#2", "c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"));
779 AssertEquals ("#3", "\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"));
783 public void HexEscape ()
785 AssertEquals ("#1","%20", Uri.HexEscape (' '));
786 AssertEquals ("#2","%A9", Uri.HexEscape ((char) 0xa9));
787 AssertEquals ("#3","%41", Uri.HexEscape ('A'));
789 Uri.HexEscape ((char) 0x0369);
791 } catch (ArgumentOutOfRangeException) {}
795 public void HexUnescape ()
798 AssertEquals ("#1", ' ', Uri.HexUnescape ("%20", ref i));
799 AssertEquals ("#2", 3, i);
801 AssertEquals ("#3", (char) 0xa9, Uri.HexUnescape ("test%a9test", ref i));
802 AssertEquals ("#4", 7, i);
803 AssertEquals ("#5", 't', Uri.HexUnescape ("test%a9test", ref i));
804 AssertEquals ("#6", 8, i);
806 AssertEquals ("#5", '%', Uri.HexUnescape ("test%a", ref i));
807 AssertEquals ("#6", 5, i);
808 AssertEquals ("#7", '%', Uri.HexUnescape ("testx%xx", ref i));
809 AssertEquals ("#8", 6, i);
811 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
813 AssertEquals ("#9", 227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i));
814 AssertEquals ("#10", 4, i);
816 AssertEquals ("#11", 240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i));
817 AssertEquals ("#12", 4, i);
821 // These won't pass exactly with MS.NET 1.x, due to differences in the
822 // handling of backslashes/forwardslashes
823 [Category ("NotDotNet")]
826 public void HexUnescapeMultiByte ()
828 // Tests from bug 74872
829 // Note: These won't pass exactly with MS.NET, due to differences in the
830 // handling of backslashes/forwardslashes
835 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
836 path = uri.LocalPath;
837 AssertEquals ("#1", 8, path.Length);
838 AssertEquals ("#2", 0x304B, path [6]);
840 // 4-byte character which should be handled as a surrogate
841 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
842 path = uri.LocalPath;
843 AssertEquals ("#3", 9, path.Length);
844 AssertEquals ("#4", 0xDB40, path [6]);
845 AssertEquals ("#5", 0xDD00, path [7]);
846 AssertEquals ("#6", 0x62, path [8]);
848 // 2-byte escape sequence, 2 individual characters
849 uri = new Uri ("file:///foo/a%C2%F8b", true);
850 path = uri.LocalPath;
851 AssertEquals ("#7", 9, path.Length);
852 AssertEquals ("#8", 0xC2, path [6]);
853 AssertEquals ("#9", 0xF8, path [7]);
857 public void IsHexDigit ()
859 Assert ("#1", Uri.IsHexDigit ('a'));
860 Assert ("#2", Uri.IsHexDigit ('f'));
861 Assert ("#3", !Uri.IsHexDigit ('g'));
862 Assert ("#4", Uri.IsHexDigit ('0'));
863 Assert ("#5", Uri.IsHexDigit ('9'));
864 Assert ("#6", Uri.IsHexDigit ('A'));
865 Assert ("#7", Uri.IsHexDigit ('F'));
866 Assert ("#8", !Uri.IsHexDigit ('G'));
870 public void IsHexEncoding ()
872 Assert ("#1", Uri.IsHexEncoding ("test%a9test", 4));
873 Assert ("#2", !Uri.IsHexEncoding ("test%a9test", 3));
874 Assert ("#3", Uri.IsHexEncoding ("test%a9", 4));
875 Assert ("#4", !Uri.IsHexEncoding ("test%a", 4));
879 public void GetLeftPart ()
881 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
882 AssertEquals ("#1", "http://", uri.GetLeftPart (UriPartial.Scheme));
883 AssertEquals ("#2", "http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority));
884 AssertEquals ("#3", "http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path));
886 uri = new Uri ("mailto:user@contoso.com?subject=uri");
887 AssertEquals ("#4", "mailto:", uri.GetLeftPart (UriPartial.Scheme));
888 AssertEquals ("#5", "", uri.GetLeftPart (UriPartial.Authority));
889 AssertEquals ("#6", "mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path));
891 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
892 AssertEquals ("#7", "nntp://", uri.GetLeftPart (UriPartial.Scheme));
893 AssertEquals ("#8", "nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority));
894 AssertEquals ("#9", "nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path));
896 uri = new Uri ("news:123456@contoso.com");
897 AssertEquals ("#10", "news:", uri.GetLeftPart (UriPartial.Scheme));
898 AssertEquals ("#11", "", uri.GetLeftPart (UriPartial.Authority));
899 AssertEquals ("#12", "news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path));
901 uri = new Uri ("file://server/filename.ext");
902 AssertEquals ("#13", "file://", uri.GetLeftPart (UriPartial.Scheme));
903 AssertEquals ("#14", "file://server", uri.GetLeftPart (UriPartial.Authority));
904 AssertEquals ("#15", "file://server/filename.ext", uri.GetLeftPart (UriPartial.Path));
906 uri = new Uri (@"\\server\share\filename.ext");
907 AssertEquals ("#20", "file://", uri.GetLeftPart (UriPartial.Scheme));
908 AssertEquals ("#21", "file://server", uri.GetLeftPart (UriPartial.Authority));
909 AssertEquals ("#22", "file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path));
911 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
912 AssertEquals ("#23", "http://", uri.GetLeftPart (UriPartial.Scheme));
913 AssertEquals ("#24", "http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority));
914 AssertEquals ("#25", "http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path));
918 public void NewsDefaultPort ()
920 Uri uri = new Uri("news://localhost:119/");
921 AssertEquals ("#1", uri.IsDefaultPort, true);
925 public void FragmentEscape ()
927 Uri u = new Uri("http://localhost/index.asp#main#start", false);
928 AssertEquals ("#1", u.Fragment, "#main%23start");
930 u = new Uri("http://localhost/index.asp#main#start", true);
931 AssertEquals ("#2", u.Fragment, "#main#start");
933 // The other code path uses a BaseUri
935 Uri b = new Uri ("http://www.gnome.org");
936 Uri n = new Uri (b, "blah#main#start");
937 AssertEquals ("#3", n.Fragment, "#main%23start");
939 n = new Uri (b, "blah#main#start", true);
940 AssertEquals ("#4", n.Fragment, "#main#start");
944 [ExpectedException(typeof(UriFormatException))]
945 public void IncompleteSchemeDelimiter ()
947 new Uri ("file:/filename.ext");
951 [Category("NotDotNet")]
952 public void CheckHostName1 ()
954 // reported to MSDN Product Feedback Center (FDBK28671)
955 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"));
959 public void CheckHostName2 ()
961 AssertEquals ("#1", UriHostNameType.Unknown, Uri.CheckHostName (null));
962 AssertEquals ("#2", UriHostNameType.Unknown, Uri.CheckHostName (""));
963 AssertEquals ("#3", UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"));
964 AssertEquals ("#4", UriHostNameType.Dns, Uri.CheckHostName ("x"));
965 AssertEquals ("#5", UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"));
966 AssertEquals ("#6", UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"));
967 AssertEquals ("#7", UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"));
968 AssertEquals ("#8", UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"));
969 AssertEquals ("#9", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"));
970 AssertEquals ("#10", UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"));
971 AssertEquals ("#11", UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."));
972 AssertEquals ("#12", UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"));
973 AssertEquals ("#13", UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"));
974 AssertEquals ("#14", UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"));
977 AssertEquals ("#15", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"));
978 AssertEquals ("#16", UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"));
979 AssertEquals ("#17", UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"));
980 AssertEquals ("#18", UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"));
981 AssertEquals ("#19", UriHostNameType.IPv6, Uri.CheckHostName ("11::88"));
982 AssertEquals ("#20", UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"));
983 AssertEquals ("#21", UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"));
984 AssertEquals ("#22", UriHostNameType.IPv6, Uri.CheckHostName ("11::"));
985 AssertEquals ("#23", UriHostNameType.IPv6, Uri.CheckHostName ("::88"));
986 AssertEquals ("#24", UriHostNameType.IPv6, Uri.CheckHostName ("::1"));
987 AssertEquals ("#25", UriHostNameType.IPv6, Uri.CheckHostName ("::"));
988 AssertEquals ("#26", UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"));
989 AssertEquals ("#27", UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"));
990 AssertEquals ("#28", UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"));
991 AssertEquals ("#29", UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"));
992 AssertEquals ("#30", UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"));
993 AssertEquals ("#33", UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"));
995 AssertEquals ("#31", UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"));
996 AssertEquals ("#32", UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"));
997 AssertEquals ("#34", UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"));
998 AssertEquals ("#35", UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"));
999 AssertEquals ("#37", UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"));
1000 AssertEquals ("#38", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"));
1001 AssertEquals ("#39", UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"));
1002 AssertEquals ("#40", UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"));
1003 AssertEquals ("#41", UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"));
1004 AssertEquals ("#42", UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"));
1005 AssertEquals ("#43", UriHostNameType.Unknown, Uri.CheckHostName (":::"));
1006 AssertEquals ("#44", UriHostNameType.Unknown, Uri.CheckHostName (":"));
\r
1008 AssertEquals ("#45", UriHostNameType.Unknown, Uri.CheckHostName ("*"));
\r
1009 AssertEquals ("#46", UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"));
\r
1010 AssertEquals ("#47", UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"));
\r
1014 public void IsLoopback ()
1016 Uri uri = new Uri("http://loopback:8080");
1017 AssertEquals ("#1", true, uri.IsLoopback);
1018 uri = new Uri("http://localhost:8080");
1019 AssertEquals ("#2", true, uri.IsLoopback);
1020 uri = new Uri("http://127.0.0.1:8080");
1021 AssertEquals ("#3", true, uri.IsLoopback);
1022 uri = new Uri("http://127.0.0.001:8080");
1023 AssertEquals ("#4", true, uri.IsLoopback);
1024 uri = new Uri("http://[::1]");
1025 AssertEquals ("#5", true, uri.IsLoopback);
1026 uri = new Uri("http://[::1]:8080");
1027 AssertEquals ("#6", true, uri.IsLoopback);
1028 uri = new Uri("http://[::0001]:8080");
1029 AssertEquals ("#7", true, uri.IsLoopback);
1030 uri = new Uri("http://[0:0:0:0::1]:8080");
1031 AssertEquals ("#8", true, uri.IsLoopback);
1032 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
1033 AssertEquals ("#9", true, uri.IsLoopback);
1034 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
1035 AssertEquals ("#10", false, uri.IsLoopback);
1036 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
1037 AssertEquals ("#11", false, uri.IsLoopback);
1038 uri = new Uri("http://[::ff00:7f11:2233]:8080");
1039 AssertEquals ("#12", false, uri.IsLoopback);
1040 uri = new Uri("http://[1:0:0:0::1]:8080");
1041 AssertEquals ("#13", false, uri.IsLoopback);
\r
1045 public void IsLoopback_File ()
\r
1047 Uri uri = new Uri ("file:///index.html");
1049 Assert ("file", uri.IsLoopback);
1051 Assert ("file", !uri.IsLoopback);
1056 public void IsLoopback_Relative_Http ()
\r
1058 string relative = "http:8080/bar/Hello World.htm";
\r
1059 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
\r
1060 Assert ("http", !uri.IsLoopback);
\r
1064 public void IsLoopback_Relative_Unknown ()
\r
1066 string relative = "foo:8080/bar/Hello World.htm";
\r
1067 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
\r
1068 Assert ("foo", !uri.IsLoopback);
\r
1072 public void Equals1 ()
1074 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1075 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1076 Assert ("#1", uri1.Equals (uri2));
1077 Assert ("#3", !uri2.Equals ("http://www.contoso.com/index.html?x=1"));
1078 Assert ("#4", !uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"));
1083 [Category("NotDotNet")]
1085 public void Equals2 ()
1087 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1088 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1089 Assert ("#2 known to fail with ms.net 1.x", !uri1.Equals (uri2));
1093 public void Equals3 ()
1095 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1096 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1097 Assert (uri1.Equals (uri2));
1101 public void TestEquals2 ()
1103 Uri a = new Uri ("http://www.go-mono.com");
1104 Uri b = new Uri ("http://www.go-mono.com");
1106 AssertEquals ("#1", a, b);
1108 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1109 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1111 Assert ("#2", a != b);
\r
1112 AssertEquals ("#2a", "mailto:user:pwd@go-mono.com?subject=uri", a.ToString ());
\r
1113 AssertEquals ("#2b", "mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString ());
1115 AssertEquals ("#2", a, b);
1117 a = new Uri ("http://www.go-mono.com/ports/");
1118 b = new Uri ("http://www.go-mono.com/PORTS/");
1120 Assert ("#3", !a.Equals (b));
1124 public void CaseSensitivity ()
\r
1126 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
\r
1127 AssertEquals ("#1", "mailto", mailto.Scheme);
\r
1128 AssertEquals ("#2", "go-mono.com", mailto.Host);
\r
1129 AssertEquals ("#3", "mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString ());
\r
1131 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
\r
1132 AssertEquals ("#4", "http", http.Scheme);
\r
1133 AssertEquals ("#5", "go-mono.com", http.Host);
\r
1134 AssertEquals ("#6", "http://go-mono.com/INDEX.HTML", http.ToString ());
\r
1137 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
\r
1138 AssertEquals ("#7", "ftp", ftp.Scheme);
\r
1139 AssertEquals ("#8", "[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host);
\r
1140 AssertEquals ("#9", "ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString ());
\r
1144 public void GetHashCodeTest ()
1146 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1147 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1148 AssertEquals ("#1", uri1.GetHashCode (), uri2.GetHashCode ());
1149 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1150 Assert ("#2", uri1.GetHashCode () != uri2.GetHashCode ());
1151 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1152 AssertEquals ("#3", uri1.GetHashCode (), uri2.GetHashCode ());
1153 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1154 Assert ("#4", uri1.GetHashCode () != uri2.GetHashCode ());
1159 public void RelativeEqualsTest()
1161 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1162 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1163 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1164 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1165 Assert ("#1a", uri1 == uri2);
1166 Assert ("#1b", uri1.Equals(uri2));
1167 Assert ("#2a", uri1 != uri3);
1168 Assert ("#2b", !uri1.Equals(uri3));
1169 Assert ("#3a", uri1 == uri2);
1170 Assert ("#3b", uri1.Equals(uri2));
1171 Assert ("#4a", uri1 != uri3);
1172 Assert ("#4b", !uri1.Equals(uri3));
1173 Assert ("#5a", uri3 != uri4);
1174 Assert ("#5b", !uri3.Equals(uri4));
1178 public void RelativeGetHashCodeTest()
1180 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1181 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1182 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1183 AssertEquals ("#1", uri1.GetHashCode(), uri2.GetHashCode());
1184 Assert ("#2", uri1.GetHashCode() != uri3.GetHashCode());
1189 public void MakeRelative ()
1191 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1192 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1193 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1194 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1195 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1196 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1197 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1198 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1200 AssertEquals ("#1", "foo/bar/index.htm", uri1.MakeRelative (uri2));
1201 AssertEquals ("#2", "../../index.htm", uri2.MakeRelative (uri1));
1203 AssertEquals ("#3", "../../bar/foo/index.htm", uri2.MakeRelative (uri3));
1204 AssertEquals ("#4", "../../foo/bar/index.htm", uri3.MakeRelative (uri2));
1206 AssertEquals ("#5", "../foo2/index.htm", uri3.MakeRelative (uri4));
1207 AssertEquals ("#6", "../foo/index.htm", uri4.MakeRelative (uri3));
1209 AssertEquals ("#7", "https://www.contoso.com/bar/foo/index.htm?y=1",
1210 uri4.MakeRelative (uri5));
1212 AssertEquals ("#8", "http://www.contoso2.com/bar/foo/index.htm?x=0",
1213 uri4.MakeRelative (uri6));
1215 AssertEquals ("#9", "", uri6.MakeRelative (uri6));
1216 AssertEquals ("#10", "foobar.htm", uri6.MakeRelative (uri7));
1218 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1219 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1220 AssertEquals ("#11", "", uri10.MakeRelative (uri11));
1222 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1223 AssertEquals ("#12", "mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12));
1225 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1226 AssertEquals ("#13", "/foo/bar", uri10.MakeRelative (uri13));
1228 AssertEquals ("#14", "http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8));
1232 public void RelativeUri ()
1234 Uri u = new Uri("http://localhost/../../../a");
1236 AssertEquals ("http://localhost/a", u.ToString ());
1238 AssertEquals ("http://localhost/../../../a", u.ToString ());
1241 u = new Uri ("http://localhost/../c/b/../a");
1243 AssertEquals ("http://localhost/c/a", u.ToString ());
1245 AssertEquals ("http://localhost/../c/a", u.ToString ());
1250 public void ToStringTest()
1252 Uri uri = new Uri ("dummy://xxx");
1253 AssertEquals ("#1", "dummy://xxx/", uri.ToString ());
1257 public void CheckSchemeName ()
1259 AssertEquals ("#01", false, Uri.CheckSchemeName (null));
1260 AssertEquals ("#02", false, Uri.CheckSchemeName (""));
1261 AssertEquals ("#03", true, Uri.CheckSchemeName ("http"));
1262 AssertEquals ("#04", true, Uri.CheckSchemeName ("http-"));
1263 AssertEquals ("#05", false, Uri.CheckSchemeName ("6http-"));
1264 AssertEquals ("#06", true, Uri.CheckSchemeName ("http6-"));
1265 AssertEquals ("#07", false, Uri.CheckSchemeName ("http6,"));
1266 AssertEquals ("#08", true, Uri.CheckSchemeName ("http6."));
1267 AssertEquals ("#09", false, Uri.CheckSchemeName ("+http"));
1268 AssertEquals ("#10", true, Uri.CheckSchemeName ("htt+p6"));
\r
1269 // 0x00E1 -> ã
\r
1271 Assert ("#11", !Uri.CheckSchemeName ("htt\u00E1+p6"));
1273 Assert ("#11", Uri.CheckSchemeName ("htt\u00E1+p6"));
1278 public void CheckSchemeName_FirstChar ()
\r
1280 for (int i = 0; i < 256; i++) {
\r
1281 string s = String.Format ("#{0}", i);
\r
1282 char c = (char) i;
\r
1283 bool b = Uri.CheckSchemeName (c.ToString ());
\r
1285 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
\r
1286 AssertEquals (s, valid, b);
\r
1288 AssertEquals (s, Char.IsLetter (c), b);
\r
1294 public void CheckSchemeName_AnyOtherChar ()
\r
1296 for (int i = 0; i < 256; i++) {
\r
1297 string s = String.Format ("#{0}", i);
\r
1298 char c = (char) i;
\r
1299 string scheme = String.Format ("a+b-c.d{0}", c);
\r
1300 bool b = Uri.CheckSchemeName (scheme);
\r
1301 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
\r
1303 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
\r
1304 AssertEquals (s, valid, b);
\r
1306 AssertEquals (s, (Char.IsLetter (c) || common), b);
\r
1312 [ExpectedException (typeof (UriFormatException))]
\r
1313 public void NoHostname ()
1315 Uri uri = new Uri ("http://");
1320 // MS.NET 1.x throws an IndexOutOfRangeException
1321 [Category("NotDotNet")]
1323 public void NoHostname2 ()
1326 Uri uri = new Uri ("file://");
1327 AssertEquals ("#1", true, uri.IsFile);
1328 AssertEquals ("#2", false, uri.IsUnc);
1329 AssertEquals ("#3", "file", uri.Scheme);
1330 AssertEquals ("#4", "/", uri.LocalPath);
1331 AssertEquals ("#5", string.Empty, uri.Query);
1332 AssertEquals ("#6", "/", uri.AbsolutePath);
1333 AssertEquals ("#7", "file:///", uri.AbsoluteUri);
1334 AssertEquals ("#8", string.Empty, uri.Authority);
1335 AssertEquals ("#9", string.Empty, uri.Host);
1336 AssertEquals ("#10", UriHostNameType.Basic, uri.HostNameType);
1337 AssertEquals ("#11", string.Empty, uri.Fragment);
1338 AssertEquals ("#12", true, uri.IsDefaultPort);
1340 Assert ("#13", uri.IsLoopback);
1342 Assert ("#13", !uri.IsLoopback);
1344 AssertEquals ("#14", "/", uri.PathAndQuery);
1345 AssertEquals ("#15", false, uri.UserEscaped);
1346 AssertEquals ("#16", string.Empty, uri.UserInfo);
1347 AssertEquals ("#17", "file://", uri.GetLeftPart (UriPartial.Authority));
1348 AssertEquals ("#18", "file:///", uri.GetLeftPart (UriPartial.Path));
1349 AssertEquals ("#19", "file://", uri.GetLeftPart (UriPartial.Scheme));
1353 public void Segments1 ()
1355 Uri uri = new Uri ("http://localhost/");
1356 string [] segments = uri.Segments;
1357 AssertEquals ("#01", 1, segments.Length);
1358 AssertEquals ("#02", "/", segments [0]);
1362 public void Segments2 ()
1364 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1365 string [] segments = uri.Segments;
1366 AssertEquals ("#01", 3, segments.Length);
1367 AssertEquals ("#02", "/", segments [0]);
1368 AssertEquals ("#03", "dir/", segments [1]);
1369 AssertEquals ("#04", "dummypage.html", segments [2]);
1373 public void Segments3 ()
1375 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1376 string [] segments = uri.Segments;
1377 AssertEquals ("#01", 3, segments.Length);
1378 AssertEquals ("#02", "/", segments [0]);
1379 AssertEquals ("#03", "dir/", segments [1]);
1380 AssertEquals ("#04", "dummypage/", segments [2]);
1385 [Category ("NotWorking")]
1387 public void Segments4 ()
1389 Uri uri = new Uri ("file:///c:/hello");
1390 string [] segments = uri.Segments;
1391 AssertEquals ("#01", 3, segments.Length);
1393 AssertEquals ("#02", "/", segments [0]);
\r
1394 AssertEquals ("#03", "c:/", segments[1]);
\r
1396 AssertEquals ("#02", "c:", segments [0]);
1397 AssertEquals ("#03", "/", segments [1]);
1399 AssertEquals ("#04", "hello", segments [2]);
1403 public void Segments5 ()
1405 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1406 string [] segments = uri.Segments;
1407 AssertEquals ("#01", 4, segments.Length);
1408 AssertEquals ("#02", "/", segments [0]);
1409 AssertEquals ("#03", "bar/", segments [1]);
1410 AssertEquals ("#04", "foo/", segments [2]);
1411 AssertEquals ("#05", "foobar.htm", segments [3]);
1415 [ExpectedException (typeof (UriFormatException))]
1416 public void UriStartingWithColon()
1422 [ExpectedException (typeof (UriFormatException))]
1423 public void EmptyScheme ()
1429 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1430 [ExpectedException (typeof (UriFormatException))]
1433 public void InvalidPort1 ()
1435 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1436 AssertEquals (65536, uri.Port);
1440 [ExpectedException (typeof (UriFormatException))]
1443 public void InvalidPort2 ()
1445 // UInt32.MaxValue gives port == -1 !!!
1446 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1447 AssertEquals (-1, uri.Port);
1451 [ExpectedException (typeof (UriFormatException))]
1454 public void InvalidPort3 ()
1456 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1457 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1458 AssertEquals (-2147483648, uri.Port);
1463 public void PortMax ()
1465 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1466 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1467 AssertEquals (65535, uri.Port);
1473 public UriEx2 (string s) : base (s)
1477 protected override void Parse ()
1483 // Parse method is no longer used on .NET 2.0
1484 [ExpectedException (typeof (UriFormatException))]
1487 public void ParseOverride ()
1489 // If this does not override base's Parse(), it will
1490 // fail since this argument is not Absolute URI.
1491 UriEx2 ex = new UriEx2 ("readme.txt");
1495 public void UnixLocalPath ()
1497 // This works--the location is not part of the absolute path
1498 string path = "file://localhost/tmp/foo/bar";
1499 Uri fileUri = new Uri( path );
1500 AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1502 // Empty path == localhost, in theory
1503 path = "file:///c:/tmp/foo/bar";
1504 fileUri = new Uri( path );
1505 AssertEquals (path, "c:/tmp/foo/bar", fileUri.AbsolutePath);
1510 public void TestEscapeDataString ()
1512 StringBuilder sb = new StringBuilder ();
1514 for (int i = 0; i < 128; i++)
1515 sb.Append ((char) i);
1517 AssertEquals ("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
1518 Uri.EscapeDataString (sb.ToString ()));
1520 AssertEquals ("%C3%A1", Uri.EscapeDataString ("á"));
1523 public void TestEscapeUriString ()
1525 StringBuilder sb = new StringBuilder ();
1526 for (int i = 0; i < 128; i++)
1527 sb.Append ((char) i);
1529 AssertEquals ("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
1530 Uri.EscapeUriString (sb.ToString ()));
1532 AssertEquals ("%C3%A1", Uri.EscapeDataString ("á"));
1538 // This test doesn't work on Linux, and arguably shouldn't work.
1539 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1540 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1541 // Apparently "tmp" is supposed to be a hostname (!)...
1542 // Since "correct" behavior would confuse all Linux developers, and having
1543 // an expected failure is evil, we'll just ignore this for now...
1545 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1547 // You are surrounded by conditional-compilation code, all alike.
1548 // You are likely to be eaten by a Grue...
1551 [Category ("NotDotNet")]
1553 public void UnixLocalPath_WTF ()
1555 // Empty path == localhost, in theory
1556 string path = "file:///tmp/foo/bar";
1557 Uri fileUri = new Uri( path );
1559 AssertEquals (path, "/tmp/foo/bar", fileUri.AbsolutePath);
1561 // AssertEquals (path, "/foo/bar", fileUri.AbsolutePath);
1565 string path2 = "file:///foo%25bar";
1566 fileUri = new Uri (path2);
1567 AssertEquals (path2, "file:///foo%25bar", fileUri.ToString ());
1570 public static void Print (Uri uri)
1572 Console.WriteLine ("ToString: " + uri.ToString ());
1574 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1575 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1576 Console.WriteLine ("Authority: " + uri.Authority);
1577 Console.WriteLine ("Fragment: " + uri.Fragment);
1578 Console.WriteLine ("Host: " + uri.Host);
1579 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1580 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1581 Console.WriteLine ("IsFile: " + uri.IsFile);
1582 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1583 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1584 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1585 Console.WriteLine ("PathAndQuery: " + uri.PathAndQuery);
1586 Console.WriteLine ("Port: " + uri.Port);
1587 Console.WriteLine ("Query: " + uri.Query);
1588 Console.WriteLine ("Scheme: " + uri.Scheme);
1589 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1590 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1592 Console.WriteLine ("Segments:");
1593 string [] segments = uri.Segments;
1594 if (segments == null)
1595 Console.WriteLine ("\tNo Segments");
1597 for (int i = 0; i < segments.Length; i++)
1598 Console.WriteLine ("\t" + segments[i]);
1599 Console.WriteLine ("");