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
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(" \r \n http://test.com\r\n \r\r ");
71 Assert.AreEqual ("http://test.com/", uri.ToString(), "#k0");
72 Assert.AreEqual ("http", uri.GetComponents (UriComponents.Scheme, UriFormat.UriEscaped), "#k0-gc");
74 uri = new Uri ("http://contoso.com?subject=uri");
75 Assert.AreEqual ("/", uri.AbsolutePath, "#k1");
76 Assert.AreEqual ("http://contoso.com/?subject=uri", uri.AbsoluteUri, "#k2");
77 Assert.AreEqual ("contoso.com", uri.Authority, "#k3");
78 Assert.AreEqual ("", uri.Fragment, "#k4");
79 Assert.AreEqual ("contoso.com", uri.Host, "#k5");
80 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#k6");
81 Assert.AreEqual (true, uri.IsDefaultPort, "#k7");
82 Assert.AreEqual (false, uri.IsFile, "#k8");
83 Assert.AreEqual (false, uri.IsLoopback, "#k9");
84 Assert.AreEqual (false, uri.IsUnc, "#k10");
85 Assert.AreEqual ("/", uri.LocalPath, "#k11");
86 Assert.AreEqual ("/?subject=uri", uri.PathAndQuery, "#k12");
87 Assert.AreEqual (80, uri.Port, "#k13");
88 Assert.AreEqual ("?subject=uri", uri.Query, "#k14");
89 Assert.AreEqual ("http", uri.Scheme, "#k15");
90 Assert.AreEqual (false, uri.UserEscaped, "#k16");
91 Assert.AreEqual ("", uri.UserInfo, "#k17");
93 uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
94 Assert.AreEqual ("", uri.AbsolutePath, "#m1");
95 Assert.AreEqual ("mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri, "#m2");
96 Assert.AreEqual ("contoso.com", uri.Authority, "#m3");
97 Assert.AreEqual ("", uri.Fragment, "#m4");
98 Assert.AreEqual ("contoso.com", uri.Host, "#m5");
99 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#m6");
100 Assert.AreEqual (true, uri.IsDefaultPort, "#m7");
101 Assert.AreEqual (false, uri.IsFile, "#m8");
102 Assert.AreEqual (false, uri.IsLoopback, "#m9");
103 Assert.AreEqual (false, uri.IsUnc, "#m10");
104 Assert.AreEqual ("", uri.LocalPath, "#m11");
105 Assert.AreEqual ("?subject=uri", uri.PathAndQuery, "#m12");
106 Assert.AreEqual (25, uri.Port, "#m13");
107 Assert.AreEqual ("?subject=uri", uri.Query, "#m14");
108 Assert.AreEqual ("mailto", uri.Scheme, "#m15");
109 Assert.AreEqual (false, uri.UserEscaped, "#m16");
110 Assert.AreEqual ("user:pwd", uri.UserInfo, "#m17");
112 uri = new Uri("myscheme://127.0.0.1:5");
113 Assert.AreEqual ("myscheme://127.0.0.1:5/", uri.ToString(), "#c1");
115 uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
116 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.AbsolutePath, "#n1");
117 Assert.AreEqual ("file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri, "#n2");
118 Assert.AreEqual ("myserver", uri.Authority, "#n3");
119 Assert.AreEqual ("", uri.Fragment, "#n4");
120 Assert.AreEqual ("myserver", uri.Host, "#n5");
121 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#n6");
122 Assert.AreEqual (true, uri.IsDefaultPort, "#n7");
123 Assert.AreEqual (true, uri.IsFile, "#n8");
124 Assert.AreEqual (false, uri.IsLoopback, "#n9");
125 Assert.AreEqual (true, uri.IsUnc, "#n10");
128 Assert.AreEqual (@"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath, "#n11");
130 // myserver never could be the part of Unix path.
131 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.LocalPath, "#n11");
133 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.PathAndQuery, "#n12");
134 Assert.AreEqual (-1, uri.Port, "#n13");
135 Assert.AreEqual ("", uri.Query, "#n14");
136 Assert.AreEqual ("file", uri.Scheme, "#n15");
137 Assert.AreEqual (false, uri.UserEscaped, "#n16");
138 Assert.AreEqual ("", uri.UserInfo, "#n17");
140 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
141 Assert.AreEqual ("http://www.contoso.com/Hello World.htm", uri.AbsoluteUri, "#rel1a");
142 Assert.AreEqual (true, uri.UserEscaped, "#rel1b");
143 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
144 Assert.AreEqual ("http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri, "#rel2a");
145 Assert.AreEqual (false, uri.UserEscaped, "#rel2b");
146 uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
147 Assert.AreEqual ("http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri, "#rel3");
149 uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
150 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel5");
151 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
152 Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel6");
153 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
154 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel7");
155 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
156 Assert.AreEqual ("http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel8");
157 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
159 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel9");
160 Assert.AreEqual ("/foo/bar/Hello%20World.htm", uri.AbsolutePath, "#rel9-path");
162 Assert.AreEqual ("http://www.contoso.com/../foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel9");
164 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
165 Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel10");
167 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
168 Assert.AreEqual ("http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString (), "#22");
170 uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
171 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel30");
173 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
174 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel31");
176 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
177 Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
179 uri = new Uri ("relative", UriKind.Relative);
180 uri = new Uri ("relative/abc", UriKind.Relative);
181 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
183 Assert.IsTrue (!uri.IsAbsoluteUri, "#rel33");
184 Assert.AreEqual (uri.OriginalString, "relative", "#rel34");
185 Assert.IsTrue (!uri.UserEscaped, "#rel35");
190 public void Constructor_DualHostPort ()
192 string relative = "foo:8080/bar/Hello World.htm";
193 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
194 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.AbsolutePath, "AbsolutePath");
195 Assert.AreEqual ("foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri, "AbsoluteUri");
196 Assert.AreEqual (String.Empty, uri.Authority, "Authority");
197 Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
198 Assert.AreEqual (String.Empty, uri.Host, "Host");
199 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.PathAndQuery, "PathAndQuery");
200 Assert.AreEqual (-1, uri.Port, "Port");
201 Assert.AreEqual (String.Empty, uri.Query, "Query");
202 Assert.AreEqual ("foo", uri.Scheme, "Scheme");
203 Assert.AreEqual (String.Empty, uri.UserInfo, "Query");
205 Assert.AreEqual ("8080/", uri.Segments[0], "Segments[0]");
206 Assert.AreEqual ("bar/", uri.Segments[1], "Segments[1]");
207 Assert.AreEqual ("Hello%20World.htm", uri.Segments[2], "Segments[2]");
209 Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
210 Assert.IsTrue (!uri.IsFile, "IsFile");
211 Assert.IsTrue (!uri.IsLoopback, "IsLoopback");
212 Assert.IsTrue (!uri.IsUnc, "IsUnc");
213 Assert.IsTrue (!uri.UserEscaped, "UserEscaped");
215 Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
216 Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
217 Assert.AreEqual (relative, uri.OriginalString, "OriginalString");
219 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "HostNameType");
225 [ExpectedException (typeof (ArgumentNullException))]
227 [ExpectedException (typeof (NullReferenceException))]
229 public void Constructor_NullStringBool ()
231 new Uri (null, "http://www.contoso.com/index.htm", false);
236 [ExpectedException (typeof (NullReferenceException))]
238 public void Constructor_UriNullBool ()
240 new Uri (new Uri ("http://www.contoso.com"), null, false);
243 // regression for bug #47573
245 public void RelativeCtor ()
247 Uri b = new Uri ("http://a/b/c/d;p?q");
248 Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
249 Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
251 Assert.AreEqual ("http://a/b/c/?y", new Uri (b, "?y").ToString (), "#3");
253 Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
255 Uri u = new Uri (b, "/g?q=r");
256 Assert.AreEqual ("http://a/g?q=r", u.ToString (), "#5");
257 Assert.AreEqual ("?q=r", u.Query, "#6");
259 u = new Uri (b, "/g?q=r;. a");
260 Assert.AreEqual ("http://a/g?q=r;. a", u.ToString (), "#5");
261 Assert.AreEqual ("?q=r;.%20a", u.Query, "#6");
266 [ExpectedException (typeof (UriFormatException))]
267 public void RelativeCtor_11_Crasher ()
269 Uri b = new Uri ("http://a/b/c/d;p?q");
270 // this causes crash under MS.NET 1.1
271 Assert.AreEqual ("g:h", new Uri (b, "g:h").ToString (), "g:h");
276 [ExpectedException (typeof (UriFormatException))]
277 public void Bad_IPv6 ()
279 new Uri ("http://0:0:0:0::127.1.2.3]/");
284 [Category ("NotDotNet")]
286 public void LeadingSlashes_ShouldFailOn1x ()
288 // doesn't (but should) fail under 1.x
289 Assert.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
290 Assert.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
295 [Category ("NotWorking")]
297 public void LeadingSlashes_BadResultsOn1x ()
299 // strange behaviours of 1.x - it's probably not worth to fix it
300 // on Mono as 2.0 has been fixed
301 Uri u = new Uri ("file:///foo/bar");
303 Assert.AreEqual (String.Empty, u.Host, "#3a");
304 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#3b");
305 Assert.AreEqual ("file:///foo/bar", u.ToString (), "#3c");
306 Assert.AreEqual (false, u.IsUnc, "#3d");
308 // 1.x misinterpret the first path element as the host name
309 Assert.AreEqual ("foo", u.Host, "#3a");
310 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#3b");
311 Assert.AreEqual ("file://foo/bar", u.ToString (), "#3c");
312 Assert.AreEqual (true, u.IsUnc, "#3d");
314 u = new Uri ("mailto:/foo");
316 Assert.AreEqual (String.Empty, u.Host, "#13a");
317 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#13b");
318 Assert.AreEqual ("mailto:/foo", u.ToString (), "#13c");
320 // 1.x misinterpret the first path element as the host name
321 Assert.AreEqual ("foo", u.Host, "#13a");
322 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#13b");
323 Assert.AreEqual ("mailto:foo", u.ToString (), "#13c");
325 u = new Uri ("mailto://foo");
327 Assert.AreEqual (String.Empty, u.Host, "#14a");
328 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#14b");
329 Assert.AreEqual ("mailto://foo", u.ToString (), "#14c");
331 // 1.x misinterpret the first path element as the host name
332 Assert.AreEqual ("foo", u.Host, "#14a");
333 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#14b");
334 Assert.AreEqual ("mailto://foo/", u.ToString (), "#14c");
336 u = new Uri ("news:/");
337 Assert.AreEqual (String.Empty, u.Host, "#18a");
339 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#18b");
340 Assert.AreEqual ("news:/", u.ToString (), "#18c");
341 Assert.AreEqual ("/", u.AbsolutePath, "#18d");
342 Assert.AreEqual ("news:/", u.AbsoluteUri, "#18e");
344 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#18b");
345 Assert.AreEqual ("news:", u.ToString (), "#18c");
346 Assert.AreEqual (String.Empty, u.AbsolutePath, "#18d");
347 Assert.AreEqual ("news:", u.AbsoluteUri, "#18e");
349 u = new Uri ("news:/foo");
350 Assert.AreEqual (String.Empty, u.Host, "#19a");
352 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#19b");
353 Assert.AreEqual ("news:/foo", u.ToString (), "#19c");
354 Assert.AreEqual ("/foo", u.AbsolutePath, "#19d");
355 Assert.AreEqual ("news:/foo", u.AbsoluteUri, "#19e");
357 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#19b");
358 Assert.AreEqual ("news:foo", u.ToString (), "#19c");
359 Assert.AreEqual ("foo", u.AbsolutePath, "#19d");
360 Assert.AreEqual ("news:foo", u.AbsoluteUri, "#19e");
362 u = new Uri ("news://foo");
364 Assert.AreEqual (String.Empty, u.Host, "#20a");
365 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#20b");
366 Assert.AreEqual ("news://foo", u.ToString (), "#20c");
367 Assert.AreEqual ("//foo", u.AbsolutePath, "#20d");
368 Assert.AreEqual ("news://foo", u.AbsoluteUri, "#20e");
370 Assert.AreEqual ("foo", u.Host, "#20a");
371 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#20b");
372 Assert.AreEqual ("news://foo/", u.ToString (), "#20c");
373 Assert.AreEqual ("/", u.AbsolutePath, "#20d");
374 Assert.AreEqual ("news://foo/", u.AbsoluteUri, "#20e");
376 u = new Uri ("news://foo/bar");
378 Assert.AreEqual (String.Empty, u.Host, "#22a");
379 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#22b");
380 Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
381 Assert.AreEqual ("//foo/bar", u.AbsolutePath, "#22d");
382 Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
384 Assert.AreEqual ("foo", u.Host, "#22a");
385 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#22b");
386 Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
387 Assert.AreEqual ("/bar", u.AbsolutePath, "#22d");
388 Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
394 [Category ("NotDotNet")] // does (but shouldn't) fail under 1.x
396 public void LeadingSlashes_FailOn1x ()
398 // 1.x throws an UriFormatException because it can't decode the host name
399 Uri u = new Uri ("mailto:");
400 Assert.AreEqual (String.Empty, u.Host, "#10a");
401 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#10b");
402 Assert.AreEqual ("mailto:", u.ToString (), "#10c");
404 // 1.x throws an UriFormatException because it can't decode the host name
405 u = new Uri ("mailto:/");
406 Assert.AreEqual (String.Empty, u.Host, "#12a");
407 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#12b");
408 Assert.AreEqual ("mailto:/", u.ToString (), "#12c");
410 // 1.x throws an UriFormatException because it cannot detect the format
411 u = new Uri ("mailto:///foo");
412 Assert.AreEqual (String.Empty, u.Host, "#15a");
413 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#15b");
414 Assert.AreEqual ("mailto:///foo", u.ToString (), "#15c");
416 // 1.x throws an UriFormatException because it cannot detect the format
417 u = new Uri ("news:///foo");
418 Assert.AreEqual (String.Empty, u.Host, "#21a");
420 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#21b");
422 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#21b");
424 Assert.AreEqual ("news:///foo", u.ToString (), "#21c");
425 Assert.AreEqual ("///foo", u.AbsolutePath, "#21d");
426 Assert.AreEqual ("news:///foo", u.AbsoluteUri, "#21e");
430 // some tests from bug 75144
431 public void LeadingSlashes ()
433 Uri u = new Uri ("file://foo/bar");
434 Assert.AreEqual ("foo", u.Host, "#5a");
435 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#5b");
436 Assert.AreEqual ("file://foo/bar", u.ToString (), "#5c");
437 Assert.AreEqual (true, u.IsUnc, "#5d");
439 u = new Uri ("file:////foo/bar");
440 Assert.AreEqual ("foo", u.Host, "#7a");
441 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#7b");
442 Assert.AreEqual ("file://foo/bar", u.ToString (), "#7c");
443 Assert.AreEqual (true, u.IsUnc, "#7d");
445 Assert.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
447 u = new Uri ("mailto:foo");
448 Assert.AreEqual ("foo", u.Host, "#11a");
449 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#11b");
450 Assert.AreEqual ("mailto:foo", u.ToString (), "#11c");
452 u = new Uri ("news:");
453 Assert.AreEqual (String.Empty, u.Host, "#16a");
455 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#16b");
457 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#16b");
459 Assert.AreEqual ("news:", u.ToString (), "#16c");
461 u = new Uri ("news:foo");
462 Assert.AreEqual (String.Empty, u.Host, "#17a");
464 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#17b");
466 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#17b");
468 Assert.AreEqual ("news:foo", u.ToString (), "#17c");
469 Assert.AreEqual ("foo", u.AbsolutePath, "#17d");
470 Assert.AreEqual ("news:foo", u.AbsoluteUri, "#17e");
474 [ExpectedException (typeof (UriFormatException))]
475 public void HttpHostname1 ()
481 [ExpectedException (typeof (UriFormatException))]
483 [Category ("NotDotNet")] // doesn't fail under 1.x
485 public void HttpHostname2 ()
491 [ExpectedException (typeof (UriFormatException))]
492 public void HttpHostname3 ()
498 [ExpectedException (typeof (UriFormatException))]
500 [Category ("NotDotNet")] // doesn't fail under 1.x
502 public void HttpHostname4 ()
504 new Uri ("http:/foo");
508 [ExpectedException (typeof (UriFormatException))]
509 public void HttpHostname5 ()
515 [ExpectedException (typeof (UriFormatException))]
516 public void HttpHostname6 ()
518 new Uri ("http:///");
522 [ExpectedException (typeof (UriFormatException))]
523 public void HttpHostname7 ()
525 new Uri ("http:///foo");
529 [ExpectedException (typeof (UriFormatException))]
530 public void InvalidFile1 ()
536 [ExpectedException (typeof (UriFormatException))]
537 public void InvalidFile2 ()
543 [ExpectedException (typeof (UriFormatException))]
544 public void InvalidFile3 ()
546 new Uri ("file:/foo");
550 [ExpectedException (typeof (UriFormatException))]
551 public void InvalidScheme ()
557 public void ConstructorsRejectRelativePath ()
559 string [] reluris = new string [] {
561 "thisdir/childdir/file",
564 string [] winRelUris = new string [] {
568 for (int i = 0; i < reluris.Length; i++) {
570 new Uri (reluris [i]);
571 Assert.Fail ("Should be failed: " + reluris [i]);
572 } catch (UriFormatException) {
577 for (int i = 0; i < winRelUris.Length; i++) {
579 new Uri (winRelUris [i]);
580 Assert.Fail ("Should be failed: " + winRelUris [i]);
581 } catch (UriFormatException) {
588 public void LocalPath ()
590 Uri uri = new Uri ("c:\\tmp\\hello.txt");
591 Assert.AreEqual ("file:///c:/tmp/hello.txt", uri.ToString (), "#1a");
592 Assert.AreEqual ("c:\\tmp\\hello.txt", uri.LocalPath, "#1b");
593 Assert.AreEqual ("file", uri.Scheme, "#1c");
594 Assert.AreEqual ("", uri.Host, "#1d");
595 Assert.AreEqual ("c:/tmp/hello.txt", uri.AbsolutePath, "#1e");
597 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
598 Assert.AreEqual ("file://cygwin/tmp/hello.txt", uri.ToString (), "#3a");
600 Assert.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#3b win32");
602 Assert.AreEqual ("/tmp/hello.txt", uri.LocalPath, "#3b *nix");
603 Assert.AreEqual ("file", uri.Scheme, "#3c");
604 Assert.AreEqual ("cygwin", uri.Host, "#3d");
605 Assert.AreEqual ("/tmp/hello.txt", uri.AbsolutePath, "#3e");
607 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
608 Assert.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri.ToString (), "#4a");
610 Assert.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#4b win32");
612 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.LocalPath, "#4b *nix");
613 Assert.AreEqual ("file", uri.Scheme, "#4c");
614 Assert.AreEqual ("mymachine", uri.Host, "#4d");
615 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.AbsolutePath, "#4e");
617 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
618 Assert.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri.ToString (), "#5a");
619 Assert.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#5b");
620 Assert.AreEqual ("file", uri.Scheme, "#5c");
621 Assert.AreEqual ("", uri.Host, "#5d");
622 Assert.AreEqual ("c:/cygwin/tmp/hello.txt", uri.AbsolutePath, "#5e");
626 public void LocalPath_FileHost ()
628 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
629 Uri uri = new Uri("file://one_file.txt");
631 Assert.AreEqual ("file://one_file.txt/", uri.ToString(), "#6a");
632 Assert.AreEqual ("/", uri.AbsolutePath, "#6e");
633 Assert.AreEqual ("/", uri.PathAndQuery, "#6f");
634 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#6g");
636 Assert.AreEqual ("file://one_file.txt", uri.ToString(), "#6a");
637 Assert.AreEqual ("", uri.AbsolutePath, "#6e");
638 Assert.AreEqual ("", uri.PathAndQuery, "#6f");
639 Assert.AreEqual ("file://one_file.txt", uri.GetLeftPart (UriPartial.Path), "#6g");
642 Assert.AreEqual ("\\\\one_file.txt", uri.LocalPath, "#6b");
644 Assert.AreEqual ("/", uri.LocalPath, "#6b");
645 Assert.AreEqual ("file", uri.Scheme, "#6c");
646 Assert.AreEqual ("one_file.txt", uri.Host, "#6d");
648 // same tests - but original Uri is now ending with a '/'
650 uri = new Uri ("file://one_file.txt/");
651 Assert.AreEqual ("file://one_file.txt/", uri.ToString (), "#7a");
652 Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
653 Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
654 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
657 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
659 Assert.AreEqual ("/", uri.LocalPath, "#7b");
661 Assert.AreEqual ("file", uri.Scheme, "#7c");
662 Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
666 public void LocalPath_Escape ()
669 Uri uri = new Uri ("file:///tmp/a%20a");
672 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
674 // actually MS.NET treats /// as \\ thus it fails here.
675 Assert.IsTrue (uri.LocalPath.EndsWith ("\\tmp\\a a"), "#7a:" + uri.LocalPath);
678 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
680 uri = new Uri ("file:///tmp/foo%25bar");
683 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
684 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
686 // actually MS.NET treats /// as \\ thus it fails here.
687 Assert.IsTrue (uri.LocalPath.EndsWith ("\\tmp\\foo%bar"), "#8a:" + uri.LocalPath);
688 // ditto, file://tmp/foo%25bar (bug in 1.x)
689 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%bar"), "#8c:" + uri.ToString ());
692 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
693 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
696 uri = new Uri ("file:///foo%25bar");
699 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
701 // actually MS.NET treats /// as \\ thus it fails here.
702 Assert.IsTrue (uri.LocalPath.EndsWith ("\\foo%25bar"), "#9a:" + uri.LocalPath);
704 // ditto, file://tmp/foo%25bar (bug in 1.x)
705 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
707 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
708 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
712 // Novell Bugzilla #320614
714 public void QueryEscape ()
716 Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
717 Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
719 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
721 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
723 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u2.ToString (), "QE2");
728 public void UnixPath () {
730 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
736 Uri uri = new Uri ("http://www.contoso.com");
737 Assert.IsTrue (!uri.IsUnc, "#1");
739 uri = new Uri ("news:123456@contoso.com");
740 Assert.IsTrue (!uri.IsUnc, "#2");
742 uri = new Uri ("file://server/filename.ext");
743 Assert.IsTrue (uri.IsUnc, "#3");
745 uri = new Uri (@"\\server\share\filename.ext");
746 Assert.IsTrue (uri.IsUnc, "#6");
748 uri = new Uri (@"a:\dir\filename.ext");
749 Assert.IsTrue (!uri.IsUnc, "#8");
753 [Category("NotDotNet")]
754 public void UncFail ()
757 Uri uri = new Uri ("/home/user/dir/filename.ext");
758 Assert.IsTrue (!uri.IsUnc, "#7");
763 public void FromHex ()
765 Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
766 Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
767 Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
768 Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
769 Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
770 Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
774 } catch (ArgumentException) {}
778 } catch (ArgumentException) {}
782 } catch (ArgumentException) {}
787 public UriEx (string s) : base (s)
791 public string UnescapeString (string s)
796 public static string UnescapeString (string uri, string target)
798 return new UriEx (uri).UnescapeString (target);
803 public void Unescape ()
805 Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
806 Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
807 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
808 Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
809 Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
810 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
814 public void HexEscape ()
816 Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
817 Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
818 Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
820 Uri.HexEscape ((char) 0x0369);
822 } catch (ArgumentOutOfRangeException) {}
826 public void MoreHexEscape()
828 string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
829 string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
830 Uri u = new Uri(url);
831 Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
835 public void HexUnescape ()
838 Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
839 Assert.AreEqual (3, i, "#2");
841 Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
842 Assert.AreEqual (7, i, "#4");
843 Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
844 Assert.AreEqual (8, i, "#6");
846 Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
847 Assert.AreEqual (5, i, "#6");
848 Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
849 Assert.AreEqual (6, i, "#8");
851 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
853 Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
854 Assert.AreEqual (4, i, "#10");
856 Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
857 Assert.AreEqual (4, i, "#12");
861 // These won't pass exactly with MS.NET 1.x, due to differences in the
862 // handling of backslashes/forwardslashes
863 [Category ("NotDotNet")]
866 public void HexUnescapeMultiByte ()
868 // Tests from bug 74872
869 // Note: These won't pass exactly with MS.NET, due to differences in the
870 // handling of backslashes/forwardslashes
875 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
876 path = uri.LocalPath;
877 Assert.AreEqual (8, path.Length, "#1");
878 Assert.AreEqual (0x304B, path [6], "#2");
880 // 4-byte character which should be handled as a surrogate
881 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
882 path = uri.LocalPath;
883 Assert.AreEqual (9, path.Length, "#3");
884 Assert.AreEqual (0xDB40, path [6], "#4");
885 Assert.AreEqual (0xDD00, path [7], "#5");
886 Assert.AreEqual (0x62, path [8], "#6");
888 // 2-byte escape sequence, 2 individual characters
889 uri = new Uri ("file:///foo/a%C2%F8b", true);
890 path = uri.LocalPath;
891 Assert.AreEqual (9, path.Length, "#7");
892 Assert.AreEqual (0xC2, path [6], "#8");
893 Assert.AreEqual (0xF8, path [7], "#9");
897 public void IsHexDigit ()
899 Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
900 Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
901 Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
902 Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
903 Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
904 Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
905 Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
906 Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
910 public void IsHexEncoding ()
912 Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
913 Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
914 Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
915 Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
919 public void GetLeftPart ()
921 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
922 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
923 Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
924 Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
926 uri = new Uri ("mailto:user@contoso.com?subject=uri");
927 Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
928 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
929 Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
931 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
932 Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
933 Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
934 Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
936 uri = new Uri ("news:123456@contoso.com");
937 Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
938 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
939 Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
941 uri = new Uri ("file://server/filename.ext");
942 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
943 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
944 Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
946 uri = new Uri (@"\\server\share\filename.ext");
947 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
948 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
949 Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
951 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
952 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
953 Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
954 Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
958 public void NewsDefaultPort ()
960 Uri uri = new Uri("news://localhost:119/");
961 Assert.AreEqual (uri.IsDefaultPort, true, "#1");
965 public void Fragment_Escape ()
967 Uri u = new Uri("http://localhost/index.asp#main#start", false);
968 Assert.AreEqual (u.Fragment, "#main%23start", "#1");
970 u = new Uri("http://localhost/index.asp#main#start", true);
971 Assert.AreEqual (u.Fragment, "#main#start", "#2");
973 // The other code path uses a BaseUri
975 Uri b = new Uri ("http://www.gnome.org");
976 Uri n = new Uri (b, "blah#main#start");
977 Assert.AreEqual (n.Fragment, "#main%23start", "#3");
979 n = new Uri (b, "blah#main#start", true);
980 Assert.AreEqual (n.Fragment, "#main#start", "#4");
985 public void Fragment_RelativeUri ()
987 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
988 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
989 Uri relativeUri = uri1.MakeRelativeUri (uri2);
992 string fragment = relativeUri.Fragment;
993 Assert.Fail ("#1: " + fragment);
994 } catch (InvalidOperationException ex) {
995 // This operation is not supported for a relative URI
996 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
997 Assert.IsNull (ex.InnerException, "#3");
998 Assert.IsNotNull (ex.Message, "#4");
1004 [ExpectedException(typeof(UriFormatException))]
1005 public void IncompleteSchemeDelimiter ()
1007 new Uri ("file:/filename.ext");
1011 [Category("NotDotNet")]
1012 public void CheckHostName1 ()
1014 // reported to MSDN Product Feedback Center (FDBK28671)
1015 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":11:22:33:44:55:66:77:88"), "#36 known to fail with ms.net: this is not a valid IPv6 address.");
1019 public void CheckHostName2 ()
1021 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
1022 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
1023 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
1024 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
1025 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
1026 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
1027 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
1028 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
1029 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
1030 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
1031 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
1032 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
1033 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
1034 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
1037 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
1038 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
1039 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
1040 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
1041 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
1042 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
1043 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
1044 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
1045 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
1046 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
1047 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
1048 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
1049 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
1050 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
1051 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
1052 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
1053 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
1055 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
1056 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
1057 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
1058 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
1059 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
1060 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
1061 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
1062 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
1063 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
1064 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
1065 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
1066 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
1068 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
1069 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
1070 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
1074 public void IsLoopback ()
1076 Uri uri = new Uri("http://loopback:8080");
1077 Assert.AreEqual (true, uri.IsLoopback, "#1");
1078 uri = new Uri("http://localhost:8080");
1079 Assert.AreEqual (true, uri.IsLoopback, "#2");
1080 uri = new Uri("http://127.0.0.1:8080");
1081 Assert.AreEqual (true, uri.IsLoopback, "#3");
1082 uri = new Uri("http://127.0.0.001:8080");
1083 Assert.AreEqual (true, uri.IsLoopback, "#4");
1084 uri = new Uri("http://[::1]");
1085 Assert.AreEqual (true, uri.IsLoopback, "#5");
1086 uri = new Uri("http://[::1]:8080");
1087 Assert.AreEqual (true, uri.IsLoopback, "#6");
1088 uri = new Uri("http://[::0001]:8080");
1089 Assert.AreEqual (true, uri.IsLoopback, "#7");
1090 uri = new Uri("http://[0:0:0:0::1]:8080");
1091 Assert.AreEqual (true, uri.IsLoopback, "#8");
1092 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
1093 Assert.AreEqual (true, uri.IsLoopback, "#9");
1094 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
1095 Assert.AreEqual (false, uri.IsLoopback, "#10");
1096 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
1097 Assert.AreEqual (false, uri.IsLoopback, "#11");
1098 uri = new Uri("http://[::ff00:7f11:2233]:8080");
1099 Assert.AreEqual (false, uri.IsLoopback, "#12");
1100 uri = new Uri("http://[1:0:0:0::1]:8080");
1101 Assert.AreEqual (false, uri.IsLoopback, "#13");
1105 public void IsLoopback_File ()
1107 Uri uri = new Uri ("file:///index.html");
1109 Assert.IsTrue (uri.IsLoopback, "file");
1111 Assert.IsTrue (!uri.IsLoopback, "file");
1116 public void IsLoopback_Relative_Http ()
1118 string relative = "http:8080/bar/Hello World.htm";
1119 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1120 Assert.IsTrue (!uri.IsLoopback, "http");
1124 public void IsLoopback_Relative_Unknown ()
1126 string relative = "foo:8080/bar/Hello World.htm";
1127 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1128 Assert.IsTrue (!uri.IsLoopback, "foo");
1132 public void Equals1 ()
1134 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1135 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1136 Assert.IsTrue (uri1.Equals (uri2), "#1");
1137 Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1138 Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1143 [Category("NotDotNet")]
1145 public void Equals2 ()
1147 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1148 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1149 Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1153 public void Equals3 ()
1155 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1156 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1157 Assert.IsTrue (uri1.Equals (uri2));
1161 public void TestEquals2 ()
1163 Uri a = new Uri ("http://www.go-mono.com");
1164 Uri b = new Uri ("http://www.go-mono.com");
1166 Assert.AreEqual (a, b, "#1");
1168 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1169 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1171 Assert.IsTrue (a != b, "#2");
1172 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1173 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1175 Assert.AreEqual (a, b, "#2");
1177 a = new Uri ("http://www.go-mono.com/ports/");
1178 b = new Uri ("http://www.go-mono.com/PORTS/");
1180 Assert.IsTrue (!a.Equals (b), "#3");
1184 public void CaseSensitivity ()
1186 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1187 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1188 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1189 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1191 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1192 Assert.AreEqual ("http", http.Scheme, "#4");
1193 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1194 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1197 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1198 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1199 Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
1200 Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
1204 public void GetHashCodeTest ()
1206 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1207 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1208 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1209 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1210 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1211 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1212 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1213 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1214 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1219 public void RelativeEqualsTest()
1221 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1222 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1223 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1224 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1225 Assert.IsTrue (uri1 == uri2, "#1a");
1226 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1227 Assert.IsTrue (uri1 != uri3, "#2a");
1228 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1229 Assert.IsTrue (uri1 == uri2, "#3a");
1230 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1231 Assert.IsTrue (uri1 != uri3, "#4a");
1232 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1233 Assert.IsTrue (uri3 != uri4, "#5a");
1234 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1238 [ExpectedException(typeof(InvalidOperationException))]
1239 public void GetLeftPart_Partial1 ()
1241 Uri u = new Uri ("foo", UriKind.Relative);
1242 u.GetLeftPart (UriPartial.Scheme);
1245 [ExpectedException(typeof(InvalidOperationException))]
1247 public void GetLeftPart_Partial2 ()
1249 Uri u = new Uri ("foo", UriKind.Relative);
1250 u.GetLeftPart (UriPartial.Authority);
1253 [ExpectedException(typeof(InvalidOperationException))]
1255 public void GetLeftPart_Partial3 ()
1257 Uri u = new Uri ("foo", UriKind.Relative);
1258 u.GetLeftPart (UriPartial.Path);
1262 public void TestPartialToString ()
1264 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1265 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1266 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1267 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1268 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1272 public void RelativeGetHashCodeTest()
1274 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1275 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1276 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1277 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1278 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1283 public void MakeRelative ()
1285 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1286 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1287 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1288 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1289 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1290 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1291 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1292 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1294 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1295 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1297 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1298 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1300 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1301 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1303 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1305 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1307 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1308 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1310 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1311 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1312 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1314 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1315 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1317 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1318 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1320 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1324 public void RelativeUri ()
1326 Uri u = new Uri("http://localhost/../../../a");
1328 Assert.AreEqual ("http://localhost/a", u.ToString ());
1330 Assert.AreEqual ("http://localhost/../../../a", u.ToString ());
1333 u = new Uri ("http://localhost/../c/b/../a");
1335 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1337 Assert.AreEqual ("http://localhost/../c/a", u.ToString ());
1342 public void RelativeUri2 ()
1344 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1346 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1347 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1348 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1349 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1351 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1352 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1353 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1354 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1356 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1360 public void ToStringTest()
1362 Uri uri = new Uri ("dummy://xxx");
1363 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1367 public void CheckSchemeName ()
1369 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1370 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1371 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1372 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1373 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1374 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1375 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1376 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1377 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1378 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1379 // 0x00E1 -> ã
1381 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1383 Assert.IsTrue (Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1388 public void CheckSchemeName_FirstChar ()
1390 for (int i = 0; i < 256; i++) {
1391 string s = String.Format ("#{0}", i);
1393 bool b = Uri.CheckSchemeName (c.ToString ());
1395 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1396 Assert.AreEqual (valid, b, s);
1398 Assert.AreEqual (Char.IsLetter (c), b, s);
1404 public void CheckSchemeName_AnyOtherChar ()
1406 for (int i = 0; i < 256; i++) {
1407 string s = String.Format ("#{0}", i);
1409 string scheme = String.Format ("a+b-c.d{0}", c);
1410 bool b = Uri.CheckSchemeName (scheme);
1411 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1413 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1414 Assert.AreEqual (valid, b, s);
1416 Assert.AreEqual ((Char.IsLetter (c) || common), b, s);
1422 [ExpectedException (typeof (UriFormatException))]
1423 public void NoHostname ()
1425 Uri uri = new Uri ("http://");
1430 // MS.NET 1.x throws an IndexOutOfRangeException
1431 [Category("NotDotNet")]
1433 public void NoHostname2 ()
1436 Uri uri = new Uri ("file://");
1437 Assert.AreEqual (true, uri.IsFile, "#1");
1438 Assert.AreEqual (false, uri.IsUnc, "#2");
1439 Assert.AreEqual ("file", uri.Scheme, "#3");
1440 Assert.AreEqual ("/", uri.LocalPath, "#4");
1441 Assert.AreEqual (string.Empty, uri.Query, "#5");
1442 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1443 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1444 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1445 Assert.AreEqual (string.Empty, uri.Host, "#9");
1446 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1447 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1448 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1450 Assert.IsTrue (uri.IsLoopback, "#13");
1452 Assert.IsTrue (!uri.IsLoopback, "#13");
1454 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1455 Assert.AreEqual (false, uri.UserEscaped, "#15");
1456 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1457 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1458 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1459 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1463 public void Segments1 ()
1465 Uri uri = new Uri ("http://localhost/");
1466 string [] segments = uri.Segments;
1467 Assert.AreEqual (1, segments.Length, "#01");
1468 Assert.AreEqual ("/", segments [0], "#02");
1472 public void Segments2 ()
1474 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1475 string [] segments = uri.Segments;
1476 Assert.AreEqual (3, segments.Length, "#01");
1477 Assert.AreEqual ("/", segments [0], "#02");
1478 Assert.AreEqual ("dir/", segments [1], "#03");
1479 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1483 public void CachingSegments ()
1485 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1486 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1487 string [] segments = uri.Segments;
1488 Assert.AreEqual (3, segments.Length, "#01");
1489 Assert.AreEqual ("/", segments [0], "#02");
1490 Assert.AreEqual ("dir/", segments [1], "#03");
1491 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1495 public void Segments3 ()
1497 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1498 string [] segments = uri.Segments;
1499 Assert.AreEqual (3, segments.Length, "#01");
1500 Assert.AreEqual ("/", segments [0], "#02");
1501 Assert.AreEqual ("dir/", segments [1], "#03");
1502 Assert.AreEqual ("dummypage/", segments [2], "#04");
1506 public void Segments4 ()
1508 Uri uri = new Uri ("file:///c:/hello");
1510 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1511 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1513 string [] segments = uri.Segments;
1514 Assert.AreEqual (3, segments.Length, "#01");
1515 Assert.AreEqual ("/", segments [0], "#02");
1516 Assert.AreEqual ("c:/", segments[1], "#03");
1517 Assert.AreEqual ("hello", segments [2], "#04");
1521 public void Segments5 ()
1523 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1524 string [] segments = uri.Segments;
1525 Assert.AreEqual (4, segments.Length, "#01");
1526 Assert.AreEqual ("/", segments [0], "#02");
1527 Assert.AreEqual ("bar/", segments [1], "#03");
1528 Assert.AreEqual ("foo/", segments [2], "#04");
1529 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1533 [ExpectedException (typeof (UriFormatException))]
1534 public void UriStartingWithColon()
1540 [ExpectedException (typeof (UriFormatException))]
1541 public void EmptyScheme ()
1547 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1548 [ExpectedException (typeof (UriFormatException))]
1551 public void InvalidPort1 ()
1553 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1554 Assert.AreEqual (65536, uri.Port);
1558 [ExpectedException (typeof (UriFormatException))]
1561 public void InvalidPort2 ()
1563 // UInt32.MaxValue gives port == -1 !!!
1564 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1565 Assert.AreEqual (-1, uri.Port);
1569 [ExpectedException (typeof (UriFormatException))]
1572 public void InvalidPort3 ()
1574 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1575 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1576 Assert.AreEqual (-2147483648, uri.Port);
1581 public void PortMax ()
1583 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1584 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1585 Assert.AreEqual (65535, uri.Port);
1591 public UriEx2 (string s) : base (s)
1595 protected override void Parse ()
1601 // Parse method is no longer used on .NET 2.0
1602 [ExpectedException (typeof (UriFormatException))]
1605 public void ParseOverride ()
1607 // If this does not override base's Parse(), it will
1608 // fail since this argument is not Absolute URI.
1609 UriEx2 ex = new UriEx2 ("readme.txt");
1613 public void UnixLocalPath ()
1615 // This works--the location is not part of the absolute path
1616 string path = "file://localhost/tmp/foo/bar";
1617 Uri fileUri = new Uri( path );
1618 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1620 // Empty path == localhost, in theory
1621 path = "file:///c:/tmp/foo/bar";
1622 fileUri = new Uri( path );
1623 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1627 public void WindowsLocalPath ()
1629 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1634 public void TestEscapeDataString ()
1636 StringBuilder sb = new StringBuilder ();
1638 for (int i = 0; i < 128; i++)
1639 sb.Append ((char) i);
1641 Assert.AreEqual ("%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",
1642 Uri.EscapeDataString (sb.ToString ()));
1644 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1647 public void TestEscapeUriString ()
1649 StringBuilder sb = new StringBuilder ();
1650 for (int i = 0; i < 128; i++)
1651 sb.Append ((char) i);
1653 Assert.AreEqual ("%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",
1654 Uri.EscapeUriString (sb.ToString ()));
1655 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1661 public void TestUTF8Strings ()
1664 "file:///tmp/x (%232).jpg",
1665 "file:///tmp/ü (%232).jpg" };
1667 foreach (string test in tests) {
1668 Uri uri = new Uri (test);
1669 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1670 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1671 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1675 // This test doesn't work on Linux, and arguably shouldn't work.
1676 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1677 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1678 // Apparently "tmp" is supposed to be a hostname (!)...
1679 // Since "correct" behavior would confuse all Linux developers, and having
1680 // an expected failure is evil, we'll just ignore this for now...
1682 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1684 // You are surrounded by conditional-compilation code, all alike.
1685 // You are likely to be eaten by a Grue...
1688 [Category ("NotDotNet")]
1690 public void UnixLocalPath_WTF ()
1692 // Empty path == localhost, in theory
1693 string path = "file:///tmp/foo/bar";
1694 Uri fileUri = new Uri( path );
1696 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1698 // Assert.AreEqual ("/foo/bar", fileUri.AbsolutePath, path);
1702 string path2 = "file:///foo%25bar";
1703 fileUri = new Uri (path2);
1704 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1707 public static void Print (Uri uri)
1709 Console.WriteLine ("ToString: " + uri.ToString ());
1711 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1712 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1713 Console.WriteLine ("Authority: " + uri.Authority);
1714 Console.WriteLine ("Fragment: " + uri.Fragment);
1715 Console.WriteLine ("Host: " + uri.Host);
1716 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1717 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1718 Console.WriteLine ("IsFile: " + uri.IsFile);
1719 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1720 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1721 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1722 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1723 Console.WriteLine ("Port: " + uri.Port);
1724 Console.WriteLine ("Query: " + uri.Query);
1725 Console.WriteLine ("Scheme: " + uri.Scheme);
1726 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1727 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1729 Console.WriteLine ("Segments:");
1730 string [] segments = uri.Segments;
1731 if (segments == null)
1732 Console.WriteLine ("\tNo Segments");
1734 for (int i = 0; i < segments.Length; i++)
1735 Console.WriteLine ("\t" + segments[i]);
1736 Console.WriteLine ("");
1740 public void FtpRootPath ()
1742 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1743 string p = u.PathAndQuery;
1744 Assert.AreEqual ("/%2fabc/def", p);
1745 p = Uri.UnescapeDataString (p).Substring (1);
1746 Assert.AreEqual ("/abc/def", p);
1747 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1748 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1754 public void LocalPath_FileNameWithAtSign1 ()
1756 string path = "/some/path/file_with_an_@_sign.mp3";
1757 string fullpath = "http://thehost" + path;
1758 Uri fileUri = new Uri (fullpath);
1760 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1761 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1762 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1763 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1764 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1766 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1767 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1768 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1769 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1773 public void LocalPath_FileNameWithAtSign2 ()
1775 string path = "/some/path/file_with_an_@_sign.mp3";
1776 string fullpath = "http://user:password@thehost" + path;
1777 Uri fileUri = new Uri (fullpath);
1779 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1780 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1781 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1782 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1783 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1785 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1786 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1787 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1788 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1792 public void LocalPath_FileNameWithAtSign3 ()
1794 string path = "/some/path/file_with_an_@_sign.mp3";
1795 string fullpath = "file://" + path;
1796 Uri fileUri = new Uri (fullpath);
1798 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1799 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1800 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1801 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1802 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1804 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1805 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1806 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1807 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1811 public void LocalPath_FileNameWithAtSign4 ()
1813 string path = "/some/path/file_with_an_@_sign.mp3";
1814 string fullpath = "file://localhost" + path;
1815 Uri fileUri = new Uri (fullpath);
1817 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1818 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1819 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1820 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1821 Assert.IsTrue (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1823 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1824 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1825 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1826 //this test is marked as NotWorking below:
1827 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1831 [Category ("NotWorking")]
1832 public void LocalPath_FileNameWithAtSign5 ()
1834 string path = "/some/path/file_with_an_@_sign.mp3";
1835 string fullpath = "file://localhost" + path;
1836 Uri fileUri = new Uri (fullpath);
1838 Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1842 [ExpectedException (typeof (UriFormatException))]
1843 public void LocalPath_FileNameWithAtSign6 ()
1845 string path = "/some/path/file_with_an_@_sign.mp3";
1846 string fullpath = "file://user:password@localhost" + path;
1847 Uri fileUri = new Uri (fullpath);
1851 [Category ("NotDotNet")]
1852 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1854 Uri unixuri = new Uri ("/home/user/a@b");
1855 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1859 [Category ("NotDotNet")]
1860 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1862 Uri unixuri = new Uri ("/home/user/a:b");
1863 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1867 public void RelativeUriWithColons ()
1869 string s = @"Transform?args=[{""__type"":""Record:#Nostr"",""Code"":""%22test%22SomeGloss"",""ID"":""1"",""Table"":""Glossary""},{""__type"":""Record:#Nostr"",""Code"":""%22test%22All"",""ID"":""2"",""Table"":""GlossView""}, {""__type"":""Record:#Nostr"",""Code"":""%22test%22Q"",""ID"":""3"",""Table"":""Glossary""}]"; // with related to bug #573795
1870 new Uri (s, UriKind.Relative);
1871 new Uri (":", UriKind.Relative);
1872 new Uri ("1:", UriKind.Relative);
1876 public void ConsecutiveSlashes ()
1878 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1879 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1882 public class DerivedUri : Uri {
1883 public DerivedUri (string uriString)
1888 internal string TestUnescape (string path)
1890 return base.Unescape (path);