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 System.Reflection;
14 using NUnit.Framework;
19 namespace MonoTests.System
27 public void GetReady ()
29 isWin32 = (Path.DirectorySeparatorChar == '\\');
33 public void Constructors ()
38 uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
41 uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
44 uri = new Uri("http://11.22.33.588:9090");
47 uri = new Uri("http://[11:22:33::88]:9090");
50 uri = new Uri("http://[::127.11.22.33]:8080");
53 uri = new Uri("http://[abcde::127.11.22.33]:8080");
58 uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
61 uri = new Uri ("http://www.contoso.com:1234/foo/bar");
64 uri = new Uri ("http://www.contoso.com:1234/");
67 uri = new Uri ("http://www.contoso.com:1234");
71 uri = new Uri(" \r \n http://test.com\r\n \r\r ");
72 Assert.AreEqual ("http://test.com/", uri.ToString(), "#k0");
73 Assert.AreEqual ("http", uri.GetComponents (UriComponents.Scheme, UriFormat.UriEscaped), "#k0-gc");
75 uri = new Uri ("http://contoso.com?subject=uri");
76 Assert.AreEqual ("/", uri.AbsolutePath, "#k1");
77 Assert.AreEqual ("http://contoso.com/?subject=uri", uri.AbsoluteUri, "#k2");
78 Assert.AreEqual ("contoso.com", uri.Authority, "#k3");
79 Assert.AreEqual ("", uri.Fragment, "#k4");
80 Assert.AreEqual ("contoso.com", uri.Host, "#k5");
81 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#k6");
82 Assert.AreEqual (true, uri.IsDefaultPort, "#k7");
83 Assert.AreEqual (false, uri.IsFile, "#k8");
84 Assert.AreEqual (false, uri.IsLoopback, "#k9");
85 Assert.AreEqual (false, uri.IsUnc, "#k10");
86 Assert.AreEqual ("/", uri.LocalPath, "#k11");
87 Assert.AreEqual ("/?subject=uri", uri.PathAndQuery, "#k12");
88 Assert.AreEqual (80, uri.Port, "#k13");
89 Assert.AreEqual ("?subject=uri", uri.Query, "#k14");
90 Assert.AreEqual ("http", uri.Scheme, "#k15");
91 Assert.AreEqual (false, uri.UserEscaped, "#k16");
92 Assert.AreEqual ("", uri.UserInfo, "#k17");
94 uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
95 Assert.AreEqual ("", uri.AbsolutePath, "#m1");
96 Assert.AreEqual ("mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri, "#m2");
97 Assert.AreEqual ("contoso.com", uri.Authority, "#m3");
98 Assert.AreEqual ("", uri.Fragment, "#m4");
99 Assert.AreEqual ("contoso.com", uri.Host, "#m5");
100 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#m6");
101 Assert.AreEqual (true, uri.IsDefaultPort, "#m7");
102 Assert.AreEqual (false, uri.IsFile, "#m8");
103 Assert.AreEqual (false, uri.IsLoopback, "#m9");
104 Assert.AreEqual (false, uri.IsUnc, "#m10");
105 Assert.AreEqual ("", uri.LocalPath, "#m11");
106 Assert.AreEqual ("?subject=uri", uri.PathAndQuery, "#m12");
107 Assert.AreEqual (25, uri.Port, "#m13");
108 Assert.AreEqual ("?subject=uri", uri.Query, "#m14");
109 Assert.AreEqual ("mailto", uri.Scheme, "#m15");
110 Assert.AreEqual (false, uri.UserEscaped, "#m16");
111 Assert.AreEqual ("user:pwd", uri.UserInfo, "#m17");
113 uri = new Uri("myscheme://127.0.0.1:5");
114 Assert.AreEqual ("myscheme://127.0.0.1:5/", uri.ToString(), "#c1");
116 uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
117 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.AbsolutePath, "#n1");
118 Assert.AreEqual ("file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri, "#n2");
119 Assert.AreEqual ("myserver", uri.Authority, "#n3");
120 Assert.AreEqual ("", uri.Fragment, "#n4");
121 Assert.AreEqual ("myserver", uri.Host, "#n5");
122 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#n6");
123 Assert.AreEqual (true, uri.IsDefaultPort, "#n7");
124 Assert.AreEqual (true, uri.IsFile, "#n8");
125 Assert.AreEqual (false, uri.IsLoopback, "#n9");
126 Assert.AreEqual (true, uri.IsUnc, "#n10");
129 Assert.AreEqual (@"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath, "#n11");
131 // myserver never could be the part of Unix path.
132 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.LocalPath, "#n11");
134 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.PathAndQuery, "#n12");
135 Assert.AreEqual (-1, uri.Port, "#n13");
136 Assert.AreEqual ("", uri.Query, "#n14");
137 Assert.AreEqual ("file", uri.Scheme, "#n15");
138 Assert.AreEqual (false, uri.UserEscaped, "#n16");
139 Assert.AreEqual ("", uri.UserInfo, "#n17");
141 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
142 Assert.AreEqual ("http://www.contoso.com/Hello World.htm", uri.AbsoluteUri, "#rel1a");
143 Assert.AreEqual (true, uri.UserEscaped, "#rel1b");
144 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
145 Assert.AreEqual ("http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri, "#rel2a");
146 Assert.AreEqual (false, uri.UserEscaped, "#rel2b");
147 uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
148 Assert.AreEqual ("http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri, "#rel3");
150 uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
151 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel5");
152 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
153 Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel6");
154 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
155 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel7");
156 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
157 Assert.AreEqual ("http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel8");
158 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 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
163 Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel10");
165 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
166 Assert.AreEqual ("http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString (), "#22");
168 uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
169 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel30");
170 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
171 Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
173 uri = new Uri ("relative", UriKind.Relative);
174 uri = new Uri ("relative/abc", UriKind.Relative);
175 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
177 Assert.IsTrue (!uri.IsAbsoluteUri, "#rel33");
178 Assert.AreEqual (uri.OriginalString, "relative", "#rel34");
179 Assert.IsTrue (!uri.UserEscaped, "#rel35");
183 public void Constructor_DualHostPort ()
185 string relative = "foo:8080/bar/Hello World.htm";
186 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
187 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.AbsolutePath, "AbsolutePath");
188 Assert.AreEqual ("foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri, "AbsoluteUri");
189 Assert.AreEqual (String.Empty, uri.Authority, "Authority");
190 Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
191 Assert.AreEqual (String.Empty, uri.Host, "Host");
192 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.PathAndQuery, "PathAndQuery");
193 Assert.AreEqual (-1, uri.Port, "Port");
194 Assert.AreEqual (String.Empty, uri.Query, "Query");
195 Assert.AreEqual ("foo", uri.Scheme, "Scheme");
196 Assert.AreEqual (String.Empty, uri.UserInfo, "Query");
198 Assert.AreEqual ("8080/", uri.Segments[0], "Segments[0]");
199 Assert.AreEqual ("bar/", uri.Segments[1], "Segments[1]");
200 Assert.AreEqual ("Hello%20World.htm", uri.Segments[2], "Segments[2]");
202 Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
203 Assert.IsTrue (!uri.IsFile, "IsFile");
204 Assert.IsTrue (!uri.IsLoopback, "IsLoopback");
205 Assert.IsTrue (!uri.IsUnc, "IsUnc");
206 Assert.IsTrue (!uri.UserEscaped, "UserEscaped");
208 Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
209 Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
210 Assert.AreEqual (relative, uri.OriginalString, "OriginalString");
214 [ExpectedException (typeof (ArgumentNullException))]
215 public void Constructor_NullStringBool ()
217 new Uri (null, "http://www.contoso.com/index.htm", false);
221 public void Constructor_UriNullBool ()
223 new Uri (new Uri ("http://www.contoso.com"), null, false);
226 // regression for bug #47573
228 public void RelativeCtor ()
230 Uri b = new Uri ("http://a/b/c/d;p?q");
231 Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
232 Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
233 Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
235 Uri u = new Uri (b, "/g?q=r");
236 Assert.AreEqual ("http://a/g?q=r", u.ToString (), "#5");
237 Assert.AreEqual ("?q=r", u.Query, "#6");
239 u = new Uri (b, "/g?q=r;. a");
240 Assert.AreEqual ("http://a/g?q=r;. a", u.ToString (), "#5");
241 Assert.AreEqual ("?q=r;.%20a", u.Query, "#6");
246 [ExpectedException (typeof (UriFormatException))]
247 public void RelativeCtor_11_Crasher ()
249 Uri b = new Uri ("http://a/b/c/d;p?q");
250 // this causes crash under MS.NET 1.1
251 Assert.AreEqual ("g:h", new Uri (b, "g:h").ToString (), "g:h");
255 [ExpectedException (typeof (UriFormatException))]
256 public void Bad_IPv6 ()
258 new Uri ("http://0:0:0:0::127.1.2.3]/");
262 public void LeadingSlashes_ShouldFailOn1x ()
264 // doesn't (but should) fail under 1.x
265 Assert.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
266 Assert.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
270 public void LeadingSlashes_BadResultsOn1x ()
272 // strange behaviours of 1.x - it's probably not worth to fix it
273 // on Mono as 2.0 has been fixed
274 Uri u = new Uri ("file:///foo/bar");
275 Assert.AreEqual (String.Empty, u.Host, "#3a");
276 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#3b");
277 Assert.AreEqual ("file:///foo/bar", u.ToString (), "#3c");
278 Assert.AreEqual (false, u.IsUnc, "#3d");
280 u = new Uri ("mailto:/foo");
281 Assert.AreEqual (String.Empty, u.Host, "#13a");
282 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#13b");
283 Assert.AreEqual ("mailto:/foo", u.ToString (), "#13c");
285 u = new Uri ("mailto://foo");
286 Assert.AreEqual (String.Empty, u.Host, "#14a");
287 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#14b");
288 Assert.AreEqual ("mailto://foo", u.ToString (), "#14c");
290 u = new Uri ("news:/");
291 Assert.AreEqual (String.Empty, u.Host, "#18a");
292 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#18b");
293 Assert.AreEqual ("news:/", u.ToString (), "#18c");
294 Assert.AreEqual ("/", u.AbsolutePath, "#18d");
295 Assert.AreEqual ("news:/", u.AbsoluteUri, "#18e");
297 u = new Uri ("news:/foo");
298 Assert.AreEqual (String.Empty, u.Host, "#19a");
299 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#19b");
300 Assert.AreEqual ("news:/foo", u.ToString (), "#19c");
301 Assert.AreEqual ("/foo", u.AbsolutePath, "#19d");
302 Assert.AreEqual ("news:/foo", u.AbsoluteUri, "#19e");
304 u = new Uri ("news://foo");
305 Assert.AreEqual (String.Empty, u.Host, "#20a");
306 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#20b");
307 Assert.AreEqual ("news://foo", u.ToString (), "#20c");
308 Assert.AreEqual ("//foo", u.AbsolutePath, "#20d");
309 Assert.AreEqual ("news://foo", u.AbsoluteUri, "#20e");
311 u = new Uri ("news://foo/bar");
312 Assert.AreEqual (String.Empty, u.Host, "#22a");
313 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#22b");
314 Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
315 Assert.AreEqual ("//foo/bar", u.AbsolutePath, "#22d");
316 Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
320 public void LeadingSlashes_FailOn1x ()
322 // 1.x throws an UriFormatException because it can't decode the host name
323 Uri u = new Uri ("mailto:");
324 Assert.AreEqual (String.Empty, u.Host, "#10a");
325 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#10b");
326 Assert.AreEqual ("mailto:", u.ToString (), "#10c");
328 // 1.x throws an UriFormatException because it can't decode the host name
329 u = new Uri ("mailto:/");
330 Assert.AreEqual (String.Empty, u.Host, "#12a");
331 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#12b");
332 Assert.AreEqual ("mailto:/", u.ToString (), "#12c");
334 // 1.x throws an UriFormatException because it cannot detect the format
335 u = new Uri ("mailto:///foo");
336 Assert.AreEqual (String.Empty, u.Host, "#15a");
337 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#15b");
338 Assert.AreEqual ("mailto:///foo", u.ToString (), "#15c");
340 // 1.x throws an UriFormatException because it cannot detect the format
341 u = new Uri ("news:///foo");
342 Assert.AreEqual (String.Empty, u.Host, "#21a");
343 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#21b");
344 Assert.AreEqual ("news:///foo", u.ToString (), "#21c");
345 Assert.AreEqual ("///foo", u.AbsolutePath, "#21d");
346 Assert.AreEqual ("news:///foo", u.AbsoluteUri, "#21e");
350 // some tests from bug 75144
351 public void LeadingSlashes ()
353 Uri u = new Uri ("file://foo/bar");
354 Assert.AreEqual ("foo", u.Host, "#5a");
355 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#5b");
356 Assert.AreEqual ("file://foo/bar", u.ToString (), "#5c");
357 Assert.AreEqual (isWin32, u.IsUnc, "#5d");
359 u = new Uri ("file:////foo/bar");
360 Assert.AreEqual ("foo", u.Host, "#7a");
361 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#7b");
362 Assert.AreEqual ("file://foo/bar", u.ToString (), "#7c");
363 Assert.AreEqual (isWin32, u.IsUnc, "#7d");
365 Assert.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
367 u = new Uri ("mailto:foo");
368 Assert.AreEqual ("foo", u.Host, "#11a");
369 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#11b");
370 Assert.AreEqual ("mailto:foo", u.ToString (), "#11c");
372 u = new Uri ("news:");
373 Assert.AreEqual (String.Empty, u.Host, "#16a");
374 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#16b");
375 Assert.AreEqual ("news:", u.ToString (), "#16c");
377 u = new Uri ("news:foo");
378 Assert.AreEqual (String.Empty, u.Host, "#17a");
379 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#17b");
380 Assert.AreEqual ("news:foo", u.ToString (), "#17c");
381 Assert.AreEqual ("foo", u.AbsolutePath, "#17d");
382 Assert.AreEqual ("news:foo", u.AbsoluteUri, "#17e");
386 [ExpectedException (typeof (UriFormatException))]
387 public void HttpHostname1 ()
393 [ExpectedException (typeof (UriFormatException))]
394 public void HttpHostname2 ()
400 [ExpectedException (typeof (UriFormatException))]
401 public void HttpHostname3 ()
407 [ExpectedException (typeof (UriFormatException))]
408 public void HttpHostname4 ()
410 new Uri ("http:/foo");
414 [ExpectedException (typeof (UriFormatException))]
415 public void HttpHostname5 ()
421 [ExpectedException (typeof (UriFormatException))]
422 public void HttpHostname6 ()
424 new Uri ("http:///");
428 [ExpectedException (typeof (UriFormatException))]
429 public void HttpHostname7 ()
431 new Uri ("http:///foo");
435 [ExpectedException (typeof (UriFormatException))]
436 public void InvalidFile1 ()
442 [ExpectedException (typeof (UriFormatException))]
443 public void InvalidFile2 ()
449 [ExpectedException (typeof (UriFormatException))]
450 public void InvalidFile3 ()
452 new Uri ("file:/foo");
456 [ExpectedException (typeof (UriFormatException))]
457 public void InvalidScheme ()
463 public void ConstructorsRejectRelativePath ()
465 string [] reluris = new string [] {
467 "thisdir/childdir/file",
470 string [] winRelUris = new string [] {
474 for (int i = 0; i < reluris.Length; i++) {
476 new Uri (reluris [i]);
477 Assert.Fail ("Should be failed: " + reluris [i]);
478 } catch (UriFormatException) {
483 for (int i = 0; i < winRelUris.Length; i++) {
485 new Uri (winRelUris [i]);
486 Assert.Fail ("Should be failed: " + winRelUris [i]);
487 } catch (UriFormatException) {
494 public void LocalPath ()
496 Uri uri = new Uri ("c:\\tmp\\hello.txt");
497 Assert.AreEqual ("file:///c:/tmp/hello.txt", uri.ToString (), "#1a");
498 Assert.AreEqual ("c:\\tmp\\hello.txt", uri.LocalPath, "#1b");
499 Assert.AreEqual ("file", uri.Scheme, "#1c");
500 Assert.AreEqual ("", uri.Host, "#1d");
501 Assert.AreEqual ("c:/tmp/hello.txt", uri.AbsolutePath, "#1e");
503 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
504 Assert.AreEqual ("file://cygwin/tmp/hello.txt", uri.ToString (), "#3a");
506 Assert.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#3b win32");
508 Assert.AreEqual ("/tmp/hello.txt", uri.LocalPath, "#3b *nix");
509 Assert.AreEqual ("file", uri.Scheme, "#3c");
510 Assert.AreEqual ("cygwin", uri.Host, "#3d");
511 Assert.AreEqual ("/tmp/hello.txt", uri.AbsolutePath, "#3e");
513 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
514 Assert.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri.ToString (), "#4a");
516 Assert.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#4b win32");
518 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.LocalPath, "#4b *nix");
519 Assert.AreEqual ("file", uri.Scheme, "#4c");
520 Assert.AreEqual ("mymachine", uri.Host, "#4d");
521 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.AbsolutePath, "#4e");
523 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
524 Assert.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri.ToString (), "#5a");
525 Assert.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#5b");
526 Assert.AreEqual ("file", uri.Scheme, "#5c");
527 Assert.AreEqual ("", uri.Host, "#5d");
528 Assert.AreEqual ("c:/cygwin/tmp/hello.txt", uri.AbsolutePath, "#5e");
532 public void LocalPath_FileHost ()
534 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
535 Uri uri = new Uri("file://one_file.txt");
536 Assert.AreEqual ("file://one_file.txt/", uri.ToString(), "#6a");
537 Assert.AreEqual ("/", uri.AbsolutePath, "#6e");
538 Assert.AreEqual ("/", uri.PathAndQuery, "#6f");
539 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#6g");
541 Assert.AreEqual ("\\\\one_file.txt", uri.LocalPath, "#6b");
543 Assert.AreEqual ("/", uri.LocalPath, "#6b");
544 Assert.AreEqual ("file", uri.Scheme, "#6c");
545 Assert.AreEqual ("one_file.txt", uri.Host, "#6d");
547 // same tests - but original Uri is now ending with a '/'
549 uri = new Uri ("file://one_file.txt/");
550 Assert.AreEqual ("file://one_file.txt/", uri.ToString (), "#7a");
551 Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
552 Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
553 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
555 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
557 Assert.AreEqual ("/", uri.LocalPath, "#7b");
558 Assert.AreEqual ("file", uri.Scheme, "#7c");
559 Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
563 public void LocalPath_Escape ()
566 Uri uri = new Uri ("file:///tmp/a%20a");
568 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
570 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
572 uri = new Uri ("file:///tmp/foo%25bar");
574 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
575 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
577 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
578 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
581 uri = new Uri ("file:///foo%25bar");
583 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
584 // ditto, file://tmp/foo%25bar (bug in 1.x)
585 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
587 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
588 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
592 // Novell Bugzilla #320614
594 public void QueryEscape ()
596 Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
597 Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
599 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1.ToString (), "QE1");
601 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
605 public void UnixPath () {
607 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
613 Uri uri = new Uri ("http://www.contoso.com");
614 Assert.IsTrue (!uri.IsUnc, "#1");
616 uri = new Uri ("news:123456@contoso.com");
617 Assert.IsTrue (!uri.IsUnc, "#2");
619 uri = new Uri ("file://server/filename.ext");
620 Assert.AreEqual (isWin32, uri.IsUnc, "#3");
622 uri = new Uri (@"\\server\share\filename.ext");
623 Assert.IsTrue (uri.IsUnc, "#6");
625 uri = new Uri (@"a:\dir\filename.ext");
626 Assert.IsTrue (!uri.IsUnc, "#8");
630 [Category("NotDotNet")]
631 public void UncFail ()
634 Uri uri = new Uri ("/home/user/dir/filename.ext");
635 Assert.IsTrue (!uri.IsUnc, "#7");
640 public void FromHex ()
642 Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
643 Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
644 Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
645 Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
646 Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
647 Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
651 } catch (ArgumentException) {}
655 } catch (ArgumentException) {}
659 } catch (ArgumentException) {}
664 public UriEx (string s) : base (s)
668 public string UnescapeString (string s)
673 public static string UnescapeString (string uri, string target)
675 return new UriEx (uri).UnescapeString (target);
680 public void Unescape ()
682 Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
683 Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
684 Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
685 Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
686 Assert.AreEqual ("%A9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
687 Assert.AreEqual ("%A9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
691 public void HexEscape ()
693 Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
694 Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
695 Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
697 Uri.HexEscape ((char) 0x0369);
699 } catch (ArgumentOutOfRangeException) {}
703 public void MoreHexEscape()
705 string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
706 string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
707 Uri u = new Uri(url);
708 Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
712 public void HexUnescape ()
715 Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
716 Assert.AreEqual (3, i, "#2");
718 Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
719 Assert.AreEqual (7, i, "#4");
720 Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
721 Assert.AreEqual (8, i, "#6");
723 Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
724 Assert.AreEqual (5, i, "#6");
725 Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
726 Assert.AreEqual (6, i, "#8");
728 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
730 Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
731 Assert.AreEqual (4, i, "#10");
733 Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
734 Assert.AreEqual (4, i, "#12");
738 public void HexUnescapeMultiByte ()
740 // Tests from bug 74872
741 // Note: These won't pass exactly with MS.NET, due to differences in the
742 // handling of backslashes/forwardslashes
747 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
748 path = uri.LocalPath;
749 Assert.AreEqual (8, path.Length, "#1");
750 Assert.AreEqual (0x304B, path [6], "#2");
752 // 4-byte character which should be handled as a surrogate
753 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
754 path = uri.LocalPath;
755 Assert.AreEqual (9, path.Length, "#3");
756 Assert.AreEqual (0xDB40, path [6], "#4");
757 Assert.AreEqual (0xDD00, path [7], "#5");
758 Assert.AreEqual (0x62, path [8], "#6");
760 // 2-byte escape sequence, 2 individual characters
761 uri = new Uri ("file:///foo/a%C2%F8b", true);
762 path = uri.LocalPath;
763 Assert.AreEqual ("/foo/a%C2%F8b", path, "#7");
767 public void IsHexDigit ()
769 Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
770 Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
771 Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
772 Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
773 Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
774 Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
775 Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
776 Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
780 public void IsHexEncoding ()
782 Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
783 Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
784 Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
785 Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
789 public void GetLeftPart ()
791 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
792 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
793 Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
794 Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
796 uri = new Uri ("mailto:user@contoso.com?subject=uri");
797 Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
798 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
799 Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
801 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
802 Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
803 Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
804 Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
806 uri = new Uri ("news:123456@contoso.com");
807 Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
808 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
809 Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
811 uri = new Uri ("file://server/filename.ext");
812 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
813 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
814 Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
816 uri = new Uri (@"\\server\share\filename.ext");
817 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
818 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
819 Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
821 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
822 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
823 Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
824 Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
828 public void NewsDefaultPort ()
830 Uri uri = new Uri("news://localhost:119/");
831 Assert.AreEqual (uri.IsDefaultPort, true, "#1");
835 public void Fragment_Escape ()
837 Uri u = new Uri("http://localhost/index.asp#main#start", false);
839 Assert.AreEqual (u.Fragment, "#main#start", "#1");
841 u = new Uri("http://localhost/index.asp#main#start", true);
842 Assert.AreEqual (u.Fragment, "#main#start", "#2");
844 // The other code path uses a BaseUri
846 Uri b = new Uri ("http://www.gnome.org");
847 Uri n = new Uri (b, "blah#main#start");
848 Assert.AreEqual (n.Fragment, "#main#start", "#3");
850 n = new Uri (b, "blah#main#start", true);
851 Assert.AreEqual (n.Fragment, "#main#start", "#4");
855 public void Fragment_RelativeUri ()
857 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
858 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
859 Uri relativeUri = uri1.MakeRelativeUri (uri2);
862 string fragment = relativeUri.Fragment;
863 Assert.Fail ("#1: " + fragment);
864 } catch (InvalidOperationException ex) {
865 // This operation is not supported for a relative URI
866 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
867 Assert.IsNull (ex.InnerException, "#3");
868 Assert.IsNotNull (ex.Message, "#4");
873 [ExpectedException(typeof(UriFormatException))]
874 public void IncompleteSchemeDelimiter ()
876 new Uri ("file:/filename.ext");
880 public void CheckHostName1 ()
882 // reported to MSDN Product Feedback Center (FDBK28671)
883 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName (":11:22:33:44:55:66:77:88"), "#36 known to fail with ms.net: this is not a valid IPv6 address.");
887 public void CheckHostName2 ()
889 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
890 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
891 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
892 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
893 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
894 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
895 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
896 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
897 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
898 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
899 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
900 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
901 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
902 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
905 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
906 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
907 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
908 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
909 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
910 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
911 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
912 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
913 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
914 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
915 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
916 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
917 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
918 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
919 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
920 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
921 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
923 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
924 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
925 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
926 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
927 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
928 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
929 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
930 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
931 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
932 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
933 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
934 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
936 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
937 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
938 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
942 public void IsLoopback ()
944 Uri uri = new Uri("http://loopback:8080");
945 Assert.AreEqual (true, uri.IsLoopback, "#1");
946 uri = new Uri("http://localhost:8080");
947 Assert.AreEqual (true, uri.IsLoopback, "#2");
948 uri = new Uri("http://127.0.0.1:8080");
949 Assert.AreEqual (true, uri.IsLoopback, "#3");
950 uri = new Uri("http://127.0.0.001:8080");
951 Assert.AreEqual (true, uri.IsLoopback, "#4");
952 uri = new Uri("http://[::1]");
953 Assert.AreEqual (true, uri.IsLoopback, "#5");
954 uri = new Uri("http://[::1]:8080");
955 Assert.AreEqual (true, uri.IsLoopback, "#6");
956 uri = new Uri("http://[::0001]:8080");
957 Assert.AreEqual (true, uri.IsLoopback, "#7");
958 uri = new Uri("http://[0:0:0:0::1]:8080");
959 Assert.AreEqual (true, uri.IsLoopback, "#8");
960 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
961 Assert.AreEqual (true, uri.IsLoopback, "#9");
962 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
963 Assert.AreEqual (false, uri.IsLoopback, "#10");
964 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
965 Assert.AreEqual (false, uri.IsLoopback, "#11");
966 uri = new Uri("http://[::ff00:7f11:2233]:8080");
967 Assert.AreEqual (false, uri.IsLoopback, "#12");
968 uri = new Uri("http://[1:0:0:0::1]:8080");
969 Assert.AreEqual (false, uri.IsLoopback, "#13");
973 public void IsLoopback_File ()
975 Uri uri = new Uri ("file:///index.html");
976 Assert.IsTrue (uri.IsLoopback, "file");
980 public void IsLoopback_Relative_Http ()
982 string relative = "http:8080/bar/Hello World.htm";
983 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
984 Assert.IsTrue (!uri.IsLoopback, "http");
988 public void IsLoopback_Relative_Unknown ()
990 string relative = "foo:8080/bar/Hello World.htm";
991 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
992 Assert.IsTrue (!uri.IsLoopback, "foo");
996 public void Equals1 ()
998 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
999 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1000 Assert.IsTrue (uri1.Equals (uri2), "#1");
1001 Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1002 Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1006 public void Equals2 ()
1008 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1009 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1010 Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1014 public void Equals3 ()
1016 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1017 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1018 Assert.IsTrue (uri1.Equals (uri2));
1022 public void Equals4 ()
1024 var uri = new Uri ("http://w3.org");
1025 Assert.IsFalse (uri.Equals ("-"));
1029 public void TestEquals2 ()
1031 Uri a = new Uri ("http://www.go-mono.com");
1032 Uri b = new Uri ("http://www.go-mono.com");
1034 Assert.AreEqual (a, b, "#1");
1036 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1037 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1039 Assert.IsTrue (a != b, "#2");
1040 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1041 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1043 a = new Uri ("http://www.go-mono.com/ports/");
1044 b = new Uri ("http://www.go-mono.com/PORTS/");
1046 Assert.IsTrue (!a.Equals (b), "#3");
1050 public void CaseSensitivity ()
1052 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1053 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1054 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1055 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1057 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1058 Assert.AreEqual ("http", http.Scheme, "#4");
1059 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1060 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1063 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1064 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1066 Assert.AreEqual ("[::ffff:169.32.14.5]", ftp.Host, "#8");
1067 Assert.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp.ToString (), "#9");
1071 public void GetHashCodeTest ()
1073 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1074 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1075 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1076 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1077 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1078 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1079 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1080 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1081 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1085 public void RelativeEqualsTest()
1087 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1088 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1089 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1090 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1091 Assert.IsTrue (uri1 == uri2, "#1a");
1092 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1093 Assert.IsTrue (uri1 != uri3, "#2a");
1094 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1095 Assert.IsTrue (uri1 == uri2, "#3a");
1096 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1097 Assert.IsTrue (uri1 != uri3, "#4a");
1098 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1099 Assert.IsTrue (uri3 != uri4, "#5a");
1100 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1104 [ExpectedException(typeof(InvalidOperationException))]
1105 public void GetLeftPart_Partial1 ()
1107 Uri u = new Uri ("foo", UriKind.Relative);
1108 u.GetLeftPart (UriPartial.Scheme);
1111 [ExpectedException(typeof(InvalidOperationException))]
1113 public void GetLeftPart_Partial2 ()
1115 Uri u = new Uri ("foo", UriKind.Relative);
1116 u.GetLeftPart (UriPartial.Authority);
1119 [ExpectedException(typeof(InvalidOperationException))]
1121 public void GetLeftPart_Partial3 ()
1123 Uri u = new Uri ("foo", UriKind.Relative);
1124 u.GetLeftPart (UriPartial.Path);
1128 public void TestPartialToString ()
1130 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1131 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1132 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1133 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1134 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1138 public void RelativeGetHashCodeTest()
1140 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1141 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1142 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1143 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1144 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1148 public void MakeRelative ()
1150 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1151 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1152 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1153 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1154 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1155 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1156 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1157 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1159 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1160 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1162 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1163 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1165 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1166 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1168 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1170 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1172 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1173 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1175 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1176 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1177 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1179 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1180 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1182 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1183 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1185 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1189 public void RelativeFragmentUri ()
1191 Uri u = new Uri("http://localhost/../../../a");
1192 Assert.AreEqual ("http://localhost/a", u.ToString ());
1194 u = new Uri ("http://localhost/../c/b/../a");
1195 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1199 public void RelativeFragmentUri2 ()
1201 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1203 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1204 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1205 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1206 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1208 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1209 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1210 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1211 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1213 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1217 public void ToStringTest()
1219 Uri uri = new Uri ("dummy://xxx");
1220 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1224 public void CheckSchemeName ()
1226 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1227 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1228 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1229 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1230 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1231 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1232 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1233 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1234 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1235 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1236 // 0x00E1 -> ã
1237 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1241 public void CheckSchemeName_FirstChar ()
1243 for (int i = 0; i < 256; i++) {
1244 string s = String.Format ("#{0}", i);
1246 bool b = Uri.CheckSchemeName (c.ToString ());
1247 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1248 Assert.AreEqual (valid, b, s);
1253 public void CheckSchemeName_AnyOtherChar ()
1255 for (int i = 0; i < 256; i++) {
1256 string s = String.Format ("#{0}", i);
1258 string scheme = String.Format ("a+b-c.d{0}", c);
1259 bool b = Uri.CheckSchemeName (scheme);
1260 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1261 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1262 Assert.AreEqual (valid, b, s);
1267 [ExpectedException (typeof (UriFormatException))]
1268 public void NoHostname ()
1270 Uri uri = new Uri ("http://");
1274 public void NoHostname2 ()
1277 Uri uri = new Uri ("file://");
1278 Assert.AreEqual (true, uri.IsFile, "#1");
1279 Assert.AreEqual (false, uri.IsUnc, "#2");
1280 Assert.AreEqual ("file", uri.Scheme, "#3");
1281 Assert.AreEqual ("/", uri.LocalPath, "#4");
1282 Assert.AreEqual (string.Empty, uri.Query, "#5");
1283 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1284 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1285 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1286 Assert.AreEqual (string.Empty, uri.Host, "#9");
1287 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1288 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1289 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1290 Assert.IsTrue (uri.IsLoopback, "#13");
1291 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1292 Assert.AreEqual (false, uri.UserEscaped, "#15");
1293 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1294 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1295 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1296 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1300 public void Segments1 ()
1302 Uri uri = new Uri ("http://localhost/");
1303 string [] segments = uri.Segments;
1304 Assert.AreEqual (1, segments.Length, "#01");
1305 Assert.AreEqual ("/", segments [0], "#02");
1309 public void Segments2 ()
1311 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1312 string [] segments = uri.Segments;
1313 Assert.AreEqual (3, segments.Length, "#01");
1314 Assert.AreEqual ("/", segments [0], "#02");
1315 Assert.AreEqual ("dir/", segments [1], "#03");
1316 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1320 public void CachingSegments ()
1322 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1323 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1324 string [] segments = uri.Segments;
1325 Assert.AreEqual (3, segments.Length, "#01");
1326 Assert.AreEqual ("/", segments [0], "#02");
1327 Assert.AreEqual ("dir/", segments [1], "#03");
1328 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1332 public void Segments3 ()
1334 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1335 string [] segments = uri.Segments;
1336 Assert.AreEqual (3, segments.Length, "#01");
1337 Assert.AreEqual ("/", segments [0], "#02");
1338 Assert.AreEqual ("dir/", segments [1], "#03");
1339 Assert.AreEqual ("dummypage/", segments [2], "#04");
1343 public void Segments4 ()
1345 Uri uri = new Uri ("file:///c:/hello");
1347 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1348 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1350 string [] segments = uri.Segments;
1351 Assert.AreEqual (3, segments.Length, "#01");
1352 Assert.AreEqual ("/", segments [0], "#02");
1353 Assert.AreEqual ("c:/", segments[1], "#03");
1354 Assert.AreEqual ("hello", segments [2], "#04");
1358 public void Segments5 ()
1360 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1361 string [] segments = uri.Segments;
1362 Assert.AreEqual (4, segments.Length, "#01");
1363 Assert.AreEqual ("/", segments [0], "#02");
1364 Assert.AreEqual ("bar/", segments [1], "#03");
1365 Assert.AreEqual ("foo/", segments [2], "#04");
1366 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1370 [ExpectedException (typeof (UriFormatException))]
1371 public void UriStartingWithColon()
1377 [ExpectedException (typeof (UriFormatException))]
1378 public void EmptyScheme ()
1384 public void SchemeWithDigits ()
1386 Uri uri = new Uri ("net.p2p://foobar");
1387 Assert.AreEqual ("net.p2p", uri.Scheme);
1390 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1391 [ExpectedException (typeof (UriFormatException))]
1393 public void InvalidPort1 ()
1395 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1396 Assert.AreEqual (65536, uri.Port);
1399 [ExpectedException (typeof (UriFormatException))]
1401 public void InvalidPort2 ()
1403 // UInt32.MaxValue gives port == -1 !!!
1404 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1405 Assert.AreEqual (-1, uri.Port);
1408 [ExpectedException (typeof (UriFormatException))]
1410 public void InvalidPort3 ()
1412 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1413 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1414 Assert.AreEqual (-2147483648, uri.Port);
1418 public void PortMax ()
1420 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1421 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1422 Assert.AreEqual (65535, uri.Port);
1427 public UriEx2 (string s) : base (s)
1431 protected override void Parse ()
1436 // Parse method is no longer used on .NET 2.0
1437 [ExpectedException (typeof (UriFormatException))]
1439 public void ParseOverride ()
1441 // If this does not override base's Parse(), it will
1442 // fail since this argument is not Absolute URI.
1443 UriEx2 ex = new UriEx2 ("readme.txt");
1447 public void UnixLocalPath ()
1449 // This works--the location is not part of the absolute path
1450 string path = "file://localhost/tmp/foo/bar";
1451 Uri fileUri = new Uri( path );
1452 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1454 // Empty path == localhost, in theory
1455 path = "file:///c:/tmp/foo/bar";
1456 fileUri = new Uri( path );
1457 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1461 public void WindowsLocalPath ()
1463 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1467 public void TestEscapeDataString ()
1469 StringBuilder sb = new StringBuilder ();
1471 for (int i = 0; i < 128; i++)
1472 sb.Append ((char) i);
1475 "%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%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
1476 Uri.EscapeDataString (sb.ToString ()));
1478 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1481 public void TestEscapeUriString ()
1483 StringBuilder sb = new StringBuilder ();
1484 for (int i = 0; i < 128; i++)
1485 sb.Append ((char) i);
1488 "%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[%5C]%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F",
1489 Uri.EscapeUriString (sb.ToString ()));
1491 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1496 public void TestUTF8Strings ()
1499 "file:///tmp/x (%232).jpg",
1500 "file:///tmp/ü (%232).jpg" };
1502 foreach (string test in tests) {
1503 Uri uri = new Uri (test);
1504 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1505 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1506 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1510 // This test doesn't work on Linux, and arguably shouldn't work.
1511 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1512 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1513 // Apparently "tmp" is supposed to be a hostname (!)...
1514 // Since "correct" behavior would confuse all Linux developers, and having
1515 // an expected failure is evil, we'll just ignore this for now...
1517 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1519 // You are surrounded by conditional-compilation code, all alike.
1520 // You are likely to be eaten by a Grue...
1522 public void UnixLocalPath_WTF ()
1524 // Empty path == localhost, in theory
1525 string path = "file:///tmp/foo/bar";
1526 Uri fileUri = new Uri( path );
1527 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1530 string path2 = "file:///foo%25bar";
1531 fileUri = new Uri (path2);
1532 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1535 public static void Print (Uri uri)
1537 Console.WriteLine ("ToString: " + uri.ToString ());
1539 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1540 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1541 Console.WriteLine ("Authority: " + uri.Authority);
1542 Console.WriteLine ("Fragment: " + uri.Fragment);
1543 Console.WriteLine ("Host: " + uri.Host);
1544 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1545 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1546 Console.WriteLine ("IsFile: " + uri.IsFile);
1547 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1548 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1549 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1550 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1551 Console.WriteLine ("Port: " + uri.Port);
1552 Console.WriteLine ("Query: " + uri.Query);
1553 Console.WriteLine ("Scheme: " + uri.Scheme);
1554 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1555 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1557 Console.WriteLine ("Segments:");
1558 string [] segments = uri.Segments;
1559 if (segments == null)
1560 Console.WriteLine ("\tNo Segments");
1562 for (int i = 0; i < segments.Length; i++)
1563 Console.WriteLine ("\t" + segments[i]);
1564 Console.WriteLine ("");
1568 public void FtpRootPath ()
1570 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1571 string p = u.PathAndQuery;
1572 Assert.AreEqual ("/%2fabc/def", p);
1573 p = Uri.UnescapeDataString (p).Substring (1);
1574 Assert.AreEqual ("/abc/def", p);
1575 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1576 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1581 public void LocalPath_FileNameWithAtSign1 ()
1583 string path = "/some/path/file_with_an_@_sign.mp3";
1584 string fullpath = "http://thehost" + path;
1585 Uri fileUri = new Uri (fullpath);
1587 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1588 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1589 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1590 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1591 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1593 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1594 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1595 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1596 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1600 public void LocalPath_FileNameWithAtSign2 ()
1602 string path = "/some/path/file_with_an_@_sign.mp3";
1603 string fullpath = "http://user:password@thehost" + path;
1604 Uri fileUri = new Uri (fullpath);
1606 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1607 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1608 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1609 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1610 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1612 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1613 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1614 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1615 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1619 public void LocalPath_FileNameWithAtSign3 ()
1621 string path = "/some/path/file_with_an_@_sign.mp3";
1622 string fullpath = "file://" + path;
1623 Uri fileUri = new Uri (fullpath);
1625 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1626 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1627 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1628 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1629 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1631 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1632 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1633 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1634 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1638 public void LocalPath_FileNameWithAtSign4 ()
1640 string path = "/some/path/file_with_an_@_sign.mp3";
1641 string fullpath = "file://localhost" + path;
1642 Uri fileUri = new Uri (fullpath);
1644 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1645 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1646 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1647 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1648 Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1650 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1651 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1652 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1653 //this test is marked as NotWorking below:
1654 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1658 public void LocalPath_FileNameWithAtSign5 ()
1660 string path = "/some/path/file_with_an_@_sign.mp3";
1661 string fullpath = "file://localhost" + path;
1662 Uri fileUri = new Uri (fullpath);
1664 string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1665 Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1669 [ExpectedException (typeof (UriFormatException))]
1670 public void LocalPath_FileNameWithAtSign6 ()
1672 string path = "/some/path/file_with_an_@_sign.mp3";
1673 string fullpath = "file://user:password@localhost" + path;
1674 Uri fileUri = new Uri (fullpath);
1678 [Category ("NotDotNet")]
1679 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1681 Uri unixuri = new Uri ("/home/user/a@b");
1682 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1686 [Category ("NotDotNet")]
1687 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1689 Uri unixuri = new Uri ("/home/user/a:b");
1690 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1694 [Category ("NotDotNet")]
1695 public void UnixAbsolutePath_ReplaceRelative ()
1697 var u1 = new Uri ("/Users/demo/Projects/file.xml");
1698 var u2 = new Uri (u1, "b.jpg");
1700 Assert.AreEqual ("file:///Users/demo/Projects/b.jpg", u2.ToString ());
1704 public void RelativeUriWithColons ()
1706 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
1707 new Uri (s, UriKind.Relative);
1708 new Uri (":", UriKind.Relative);
1709 new Uri ("1:", UriKind.Relative);
1713 public void ConsecutiveSlashes ()
1715 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1716 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1719 public class DerivedUri : Uri {
1720 public DerivedUri (string uriString)
1725 internal string TestUnescape (string path)
1727 return base.Unescape (path);
1732 [ExpectedException (typeof (InvalidOperationException))]
1733 public void GetComponents_Relative ()
1735 Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1736 rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1740 public void GetComponents_AbsoluteUri ()
1742 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1744 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1746 string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1747 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1749 string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1750 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1752 string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1753 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1757 public void GetComponents_HttpRequestUrl ()
1759 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1761 string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1762 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1764 string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1765 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1767 string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1768 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1772 public void GetComponents_KeepDelimiter ()
1774 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1776 string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1777 Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1779 string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1780 Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1782 string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1783 Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1787 public void GetComponents_StrongAuthority ()
1789 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1791 string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1792 Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1794 string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1795 Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1797 string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1798 Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1802 public void GetComponents_Path ()
1804 Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1805 Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1807 string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1808 Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1810 string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1811 Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1813 string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1814 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1816 // same result is unescaped original string
1817 Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1818 Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1820 safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1821 Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1823 unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1824 Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1826 escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1827 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1831 public void GetComponents_PathAndQuery ()
1833 Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1835 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1837 string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1838 Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1840 string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1841 Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1843 string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1844 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1848 public void GetComponents_Query ()
1850 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1852 Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1854 string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1855 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1857 string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1858 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1860 string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1861 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1865 public void GetComponents_Fragment ()
1867 Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1869 Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1871 string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1872 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1874 string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1875 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1877 string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1878 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1881 // When used, paths such as "/foo" are assumed relative.
1882 static UriKind DotNetRelativeOrAbsolute = (Type.GetType ("Mono.Runtime") == null)? UriKind.RelativeOrAbsolute : (UriKind) 300;
1885 public void DotNetRelativeOrAbsoluteTest ()
1887 FieldInfo useDotNetRelativeOrAbsoluteField = null;
1888 bool useDotNetRelativeOrAbsoluteOld = false;
1890 if (Type.GetType ("Mono.Runtime") != null) {
1891 useDotNetRelativeOrAbsoluteField = typeof (Uri).GetField ("useDotNetRelativeOrAbsolute",
1892 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
1893 useDotNetRelativeOrAbsoluteOld = (bool) useDotNetRelativeOrAbsoluteField.GetValue (null);
1894 useDotNetRelativeOrAbsoluteField.SetValue (null, false);
1900 uri = new Uri ("/foo", DotNetRelativeOrAbsolute);
1901 Assert.IsFalse (uri.IsAbsoluteUri, "#2");
1903 Assert.IsTrue (Uri.TryCreate("/foo", DotNetRelativeOrAbsolute, out uri), "#3");
1904 Assert.IsFalse (uri.IsAbsoluteUri, "#3a");
1906 if (useDotNetRelativeOrAbsoluteField != null) {
1907 uri = new Uri ("/foo", UriKind.RelativeOrAbsolute);
1908 Assert.IsTrue (uri.IsAbsoluteUri, "#4");
1910 Assert.IsTrue (Uri.TryCreate("/foo", UriKind.RelativeOrAbsolute, out uri), "#5");
1911 Assert.IsTrue (uri.IsAbsoluteUri, "#5a");
1913 useDotNetRelativeOrAbsoluteField.SetValue (null, true);
1916 uri = new Uri ("/foo", UriKind.RelativeOrAbsolute);
1917 Assert.IsFalse (uri.IsAbsoluteUri, "#10");
1919 Assert.IsTrue (Uri.TryCreate("/foo", UriKind.RelativeOrAbsolute, out uri), "#11");
1920 Assert.IsFalse (uri.IsAbsoluteUri, "#11a");
1922 if (useDotNetRelativeOrAbsoluteField != null)
1923 useDotNetRelativeOrAbsoluteField.SetValue (null, useDotNetRelativeOrAbsoluteOld);
1929 public void LocalPathWithBaseUrl ()
1931 var mainUri = new Uri ("http://www.imdb.com");
1932 var uriPath = "/title/tt0106521";
1935 Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1936 Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
1940 public void GetSerializationInfoStringOnRelativeUri ()
1942 var uri = new Uri ("/relative/path", UriKind.Relative);
1943 var result = uri.GetComponents (UriComponents.SerializationInfoString, UriFormat.UriEscaped);
1945 Assert.AreEqual (uri.OriginalString, result);
1949 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1950 public void GetSerializationInfoStringException ()
1952 var uri = new Uri ("/relative/path", UriKind.Relative);
1953 uri.GetComponents (UriComponents.SerializationInfoString | UriComponents.Host, UriFormat.UriEscaped);
1957 public void UserInfo_EscapedLetter ()
1959 var uri = new Uri ("https://first%61second@host");
1960 Assert.AreEqual ("firstasecond", uri.UserInfo);
1964 public void UserInfo_EscapedAt ()
1966 var userinfo = "first%40second";
1967 var uri = new Uri ("https://" + userinfo + "@host");
1968 Assert.AreEqual (userinfo, uri.UserInfo);
1972 public void UserInfo_EscapedChars ()
1974 for (var ch = (char) 1; ch < 128; ch++) {
1975 var userinfo = Uri.EscapeDataString (ch.ToString ());
1977 new Uri (string.Format("http://{0}@localhost:80/", userinfo));
1978 } catch (Exception e) {
1979 Assert.Fail (string.Format("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
1985 public void UserInfo_Spaces ()
1987 const string userinfo = "test 1:pass 1";
1988 const string expected = "test%201:pass%201";
1991 var uri = new Uri (string.Format ("rtmp://{0}@test.com:333/live", userinfo));
1992 Assert.AreEqual (expected, uri.UserInfo);
1993 } catch (Exception e) {
1994 Assert.Fail (string.Format ("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
2000 public void PathDotTrim ()
2002 var baseUri = new Uri ("http://test.com", UriKind.Absolute);
2003 var relUri = new Uri ("path/dot./", UriKind.Relative);
2004 var uri = new Uri (baseUri, relUri);
2005 Assert.AreEqual ("http://test.com/path/dot./", uri.ToString ());
2009 public void GuardedIPv6Address ()
2011 var x = new Uri ("asfd://[::1]:123/");
2012 Assert.AreEqual ("[::1]", x.Host, "#1");
2016 public void CombineWithUserSchema ()
2018 var baseUri = new Uri ("zip:mem:///");
2019 var relativeUrl = "zip:mem:///foo/bar.txt";
2021 var result = new Uri (baseUri, relativeUrl);
2023 Assert.AreEqual ("zip:mem:///foo/bar.txt", result.ToString ());
2027 public void Scheme_msapp ()
2029 var uri = new Uri ("ms-app://s-1-15-2-1613647288");
2030 Assert.AreEqual ("ms-app", uri.Scheme);
2034 public void CombineWithUnixAbsolutePath ()
2036 var a = new Uri ("http://localhost/");
2037 var b = new Uri ("/foo", UriKind.RelativeOrAbsolute);
2038 var res = new Uri (a, b);
2040 Assert.AreEqual ("http://localhost/foo", res.ToString ());
2044 public void ImplicitUnixFileWithUnicode ()
2046 string value = "/Library/Frameworks/System.Runtim…ee";
2048 Assert.IsTrue (Uri.TryCreate (value, UriKind.Absolute, out uri));
2052 public void UncValidPath ()
2054 var uri = new Uri ("https://_foo/bar.html");
2055 Assert.AreEqual ("https", uri.Scheme);