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);
158 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel9");
159 Assert.AreEqual ("/foo/bar/Hello%20World.htm", uri.AbsolutePath, "#rel9-path");
161 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
162 Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel10");
164 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
165 Assert.AreEqual ("http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString (), "#22");
167 uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
168 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel30");
170 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
171 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel31");
173 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
174 Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
176 uri = new Uri ("relative", UriKind.Relative);
177 uri = new Uri ("relative/abc", UriKind.Relative);
178 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
180 Assert.IsTrue (!uri.IsAbsoluteUri, "#rel33");
181 Assert.AreEqual (uri.OriginalString, "relative", "#rel34");
182 Assert.IsTrue (!uri.UserEscaped, "#rel35");
186 public void Constructor_DualHostPort ()
188 string relative = "foo:8080/bar/Hello World.htm";
189 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
190 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.AbsolutePath, "AbsolutePath");
191 Assert.AreEqual ("foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri, "AbsoluteUri");
192 Assert.AreEqual (String.Empty, uri.Authority, "Authority");
193 Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
194 Assert.AreEqual (String.Empty, uri.Host, "Host");
195 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.PathAndQuery, "PathAndQuery");
196 Assert.AreEqual (-1, uri.Port, "Port");
197 Assert.AreEqual (String.Empty, uri.Query, "Query");
198 Assert.AreEqual ("foo", uri.Scheme, "Scheme");
199 Assert.AreEqual (String.Empty, uri.UserInfo, "Query");
201 Assert.AreEqual ("8080/", uri.Segments[0], "Segments[0]");
202 Assert.AreEqual ("bar/", uri.Segments[1], "Segments[1]");
203 Assert.AreEqual ("Hello%20World.htm", uri.Segments[2], "Segments[2]");
205 Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
206 Assert.IsTrue (!uri.IsFile, "IsFile");
207 Assert.IsTrue (!uri.IsLoopback, "IsLoopback");
208 Assert.IsTrue (!uri.IsUnc, "IsUnc");
209 Assert.IsTrue (!uri.UserEscaped, "UserEscaped");
211 Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
212 Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
213 Assert.AreEqual (relative, uri.OriginalString, "OriginalString");
217 [ExpectedException (typeof (ArgumentNullException))]
218 public void Constructor_NullStringBool ()
220 new Uri (null, "http://www.contoso.com/index.htm", false);
224 public void Constructor_UriNullBool ()
226 new Uri (new Uri ("http://www.contoso.com"), null, false);
229 // regression for bug #47573
231 public void RelativeCtor ()
233 Uri b = new Uri ("http://a/b/c/d;p?q");
234 Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
235 Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
237 Assert.AreEqual ("http://a/b/c/?y", new Uri (b, "?y").ToString (), "#3");
239 Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
241 Uri u = new Uri (b, "/g?q=r");
242 Assert.AreEqual ("http://a/g?q=r", u.ToString (), "#5");
243 Assert.AreEqual ("?q=r", u.Query, "#6");
245 u = new Uri (b, "/g?q=r;. a");
246 Assert.AreEqual ("http://a/g?q=r;. a", u.ToString (), "#5");
247 Assert.AreEqual ("?q=r;.%20a", u.Query, "#6");
252 [ExpectedException (typeof (UriFormatException))]
253 public void RelativeCtor_11_Crasher ()
255 Uri b = new Uri ("http://a/b/c/d;p?q");
256 // this causes crash under MS.NET 1.1
257 Assert.AreEqual ("g:h", new Uri (b, "g:h").ToString (), "g:h");
261 [ExpectedException (typeof (UriFormatException))]
262 public void Bad_IPv6 ()
264 new Uri ("http://0:0:0:0::127.1.2.3]/");
268 public void LeadingSlashes_ShouldFailOn1x ()
270 // doesn't (but should) fail under 1.x
271 Assert.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
272 Assert.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
276 public void LeadingSlashes_BadResultsOn1x ()
278 // strange behaviours of 1.x - it's probably not worth to fix it
279 // on Mono as 2.0 has been fixed
280 Uri u = new Uri ("file:///foo/bar");
281 Assert.AreEqual (String.Empty, u.Host, "#3a");
282 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#3b");
283 Assert.AreEqual ("file:///foo/bar", u.ToString (), "#3c");
284 Assert.AreEqual (false, u.IsUnc, "#3d");
286 u = new Uri ("mailto:/foo");
287 Assert.AreEqual (String.Empty, u.Host, "#13a");
288 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#13b");
289 Assert.AreEqual ("mailto:/foo", u.ToString (), "#13c");
291 u = new Uri ("mailto://foo");
292 Assert.AreEqual (String.Empty, u.Host, "#14a");
293 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#14b");
294 Assert.AreEqual ("mailto://foo", u.ToString (), "#14c");
296 u = new Uri ("news:/");
297 Assert.AreEqual (String.Empty, u.Host, "#18a");
298 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#18b");
299 Assert.AreEqual ("news:/", u.ToString (), "#18c");
300 Assert.AreEqual ("/", u.AbsolutePath, "#18d");
301 Assert.AreEqual ("news:/", u.AbsoluteUri, "#18e");
303 u = new Uri ("news:/foo");
304 Assert.AreEqual (String.Empty, u.Host, "#19a");
305 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#19b");
306 Assert.AreEqual ("news:/foo", u.ToString (), "#19c");
307 Assert.AreEqual ("/foo", u.AbsolutePath, "#19d");
308 Assert.AreEqual ("news:/foo", u.AbsoluteUri, "#19e");
310 u = new Uri ("news://foo");
311 Assert.AreEqual (String.Empty, u.Host, "#20a");
312 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#20b");
313 Assert.AreEqual ("news://foo", u.ToString (), "#20c");
314 Assert.AreEqual ("//foo", u.AbsolutePath, "#20d");
315 Assert.AreEqual ("news://foo", u.AbsoluteUri, "#20e");
317 u = new Uri ("news://foo/bar");
318 Assert.AreEqual (String.Empty, u.Host, "#22a");
319 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#22b");
320 Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
321 Assert.AreEqual ("//foo/bar", u.AbsolutePath, "#22d");
322 Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
326 public void LeadingSlashes_FailOn1x ()
328 // 1.x throws an UriFormatException because it can't decode the host name
329 Uri u = new Uri ("mailto:");
330 Assert.AreEqual (String.Empty, u.Host, "#10a");
331 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#10b");
332 Assert.AreEqual ("mailto:", u.ToString (), "#10c");
334 // 1.x throws an UriFormatException because it can't decode the host name
335 u = new Uri ("mailto:/");
336 Assert.AreEqual (String.Empty, u.Host, "#12a");
337 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#12b");
338 Assert.AreEqual ("mailto:/", u.ToString (), "#12c");
340 // 1.x throws an UriFormatException because it cannot detect the format
341 u = new Uri ("mailto:///foo");
342 Assert.AreEqual (String.Empty, u.Host, "#15a");
343 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#15b");
344 Assert.AreEqual ("mailto:///foo", u.ToString (), "#15c");
346 // 1.x throws an UriFormatException because it cannot detect the format
347 u = new Uri ("news:///foo");
348 Assert.AreEqual (String.Empty, u.Host, "#21a");
349 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#21b");
350 Assert.AreEqual ("news:///foo", u.ToString (), "#21c");
351 Assert.AreEqual ("///foo", u.AbsolutePath, "#21d");
352 Assert.AreEqual ("news:///foo", u.AbsoluteUri, "#21e");
356 // some tests from bug 75144
357 public void LeadingSlashes ()
359 Uri u = new Uri ("file://foo/bar");
360 Assert.AreEqual ("foo", u.Host, "#5a");
361 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#5b");
362 Assert.AreEqual ("file://foo/bar", u.ToString (), "#5c");
363 Assert.AreEqual (isWin32, u.IsUnc, "#5d");
365 u = new Uri ("file:////foo/bar");
366 Assert.AreEqual ("foo", u.Host, "#7a");
367 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#7b");
368 Assert.AreEqual ("file://foo/bar", u.ToString (), "#7c");
369 Assert.AreEqual (isWin32, u.IsUnc, "#7d");
371 Assert.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
373 u = new Uri ("mailto:foo");
374 Assert.AreEqual ("foo", u.Host, "#11a");
375 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#11b");
376 Assert.AreEqual ("mailto:foo", u.ToString (), "#11c");
378 u = new Uri ("news:");
379 Assert.AreEqual (String.Empty, u.Host, "#16a");
380 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#16b");
381 Assert.AreEqual ("news:", u.ToString (), "#16c");
383 u = new Uri ("news:foo");
384 Assert.AreEqual (String.Empty, u.Host, "#17a");
385 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#17b");
386 Assert.AreEqual ("news:foo", u.ToString (), "#17c");
387 Assert.AreEqual ("foo", u.AbsolutePath, "#17d");
388 Assert.AreEqual ("news:foo", u.AbsoluteUri, "#17e");
392 [ExpectedException (typeof (UriFormatException))]
393 public void HttpHostname1 ()
399 [ExpectedException (typeof (UriFormatException))]
400 public void HttpHostname2 ()
406 [ExpectedException (typeof (UriFormatException))]
407 public void HttpHostname3 ()
413 [ExpectedException (typeof (UriFormatException))]
414 public void HttpHostname4 ()
416 new Uri ("http:/foo");
420 [ExpectedException (typeof (UriFormatException))]
421 public void HttpHostname5 ()
427 [ExpectedException (typeof (UriFormatException))]
428 public void HttpHostname6 ()
430 new Uri ("http:///");
434 [ExpectedException (typeof (UriFormatException))]
435 public void HttpHostname7 ()
437 new Uri ("http:///foo");
441 [ExpectedException (typeof (UriFormatException))]
442 public void InvalidFile1 ()
448 [ExpectedException (typeof (UriFormatException))]
449 public void InvalidFile2 ()
455 [ExpectedException (typeof (UriFormatException))]
456 public void InvalidFile3 ()
458 new Uri ("file:/foo");
462 [ExpectedException (typeof (UriFormatException))]
463 public void InvalidScheme ()
469 public void ConstructorsRejectRelativePath ()
471 string [] reluris = new string [] {
473 "thisdir/childdir/file",
476 string [] winRelUris = new string [] {
480 for (int i = 0; i < reluris.Length; i++) {
482 new Uri (reluris [i]);
483 Assert.Fail ("Should be failed: " + reluris [i]);
484 } catch (UriFormatException) {
489 for (int i = 0; i < winRelUris.Length; i++) {
491 new Uri (winRelUris [i]);
492 Assert.Fail ("Should be failed: " + winRelUris [i]);
493 } catch (UriFormatException) {
500 public void LocalPath ()
502 Uri uri = new Uri ("c:\\tmp\\hello.txt");
503 Assert.AreEqual ("file:///c:/tmp/hello.txt", uri.ToString (), "#1a");
504 Assert.AreEqual ("c:\\tmp\\hello.txt", uri.LocalPath, "#1b");
505 Assert.AreEqual ("file", uri.Scheme, "#1c");
506 Assert.AreEqual ("", uri.Host, "#1d");
507 Assert.AreEqual ("c:/tmp/hello.txt", uri.AbsolutePath, "#1e");
509 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
510 Assert.AreEqual ("file://cygwin/tmp/hello.txt", uri.ToString (), "#3a");
512 Assert.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#3b win32");
514 Assert.AreEqual ("/tmp/hello.txt", uri.LocalPath, "#3b *nix");
515 Assert.AreEqual ("file", uri.Scheme, "#3c");
516 Assert.AreEqual ("cygwin", uri.Host, "#3d");
517 Assert.AreEqual ("/tmp/hello.txt", uri.AbsolutePath, "#3e");
519 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
520 Assert.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri.ToString (), "#4a");
522 Assert.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#4b win32");
524 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.LocalPath, "#4b *nix");
525 Assert.AreEqual ("file", uri.Scheme, "#4c");
526 Assert.AreEqual ("mymachine", uri.Host, "#4d");
527 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.AbsolutePath, "#4e");
529 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
530 Assert.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri.ToString (), "#5a");
531 Assert.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#5b");
532 Assert.AreEqual ("file", uri.Scheme, "#5c");
533 Assert.AreEqual ("", uri.Host, "#5d");
534 Assert.AreEqual ("c:/cygwin/tmp/hello.txt", uri.AbsolutePath, "#5e");
538 public void LocalPath_FileHost ()
540 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
541 Uri uri = new Uri("file://one_file.txt");
542 Assert.AreEqual ("file://one_file.txt/", uri.ToString(), "#6a");
543 Assert.AreEqual ("/", uri.AbsolutePath, "#6e");
544 Assert.AreEqual ("/", uri.PathAndQuery, "#6f");
545 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#6g");
547 Assert.AreEqual ("\\\\one_file.txt", uri.LocalPath, "#6b");
549 Assert.AreEqual ("/", uri.LocalPath, "#6b");
550 Assert.AreEqual ("file", uri.Scheme, "#6c");
551 Assert.AreEqual ("one_file.txt", uri.Host, "#6d");
553 // same tests - but original Uri is now ending with a '/'
555 uri = new Uri ("file://one_file.txt/");
556 Assert.AreEqual ("file://one_file.txt/", uri.ToString (), "#7a");
557 Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
558 Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
559 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
562 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
564 Assert.AreEqual ("/", uri.LocalPath, "#7b");
566 Assert.AreEqual ("file", uri.Scheme, "#7c");
567 Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
571 public void LocalPath_Escape ()
574 Uri uri = new Uri ("file:///tmp/a%20a");
576 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
578 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
580 uri = new Uri ("file:///tmp/foo%25bar");
582 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
583 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
585 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
586 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
589 uri = new Uri ("file:///foo%25bar");
591 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
592 // ditto, file://tmp/foo%25bar (bug in 1.x)
593 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
595 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
596 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
600 // Novell Bugzilla #320614
602 public void QueryEscape ()
604 Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
605 Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
607 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
608 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
612 public void UnixPath () {
614 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
620 Uri uri = new Uri ("http://www.contoso.com");
621 Assert.IsTrue (!uri.IsUnc, "#1");
623 uri = new Uri ("news:123456@contoso.com");
624 Assert.IsTrue (!uri.IsUnc, "#2");
626 uri = new Uri ("file://server/filename.ext");
627 Assert.AreEqual (isWin32, uri.IsUnc, "#3");
629 uri = new Uri (@"\\server\share\filename.ext");
630 Assert.IsTrue (uri.IsUnc, "#6");
632 uri = new Uri (@"a:\dir\filename.ext");
633 Assert.IsTrue (!uri.IsUnc, "#8");
637 [Category("NotDotNet")]
638 public void UncFail ()
641 Uri uri = new Uri ("/home/user/dir/filename.ext");
642 Assert.IsTrue (!uri.IsUnc, "#7");
647 public void FromHex ()
649 Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
650 Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
651 Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
652 Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
653 Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
654 Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
658 } catch (ArgumentException) {}
662 } catch (ArgumentException) {}
666 } catch (ArgumentException) {}
671 public UriEx (string s) : base (s)
675 public string UnescapeString (string s)
680 public static string UnescapeString (string uri, string target)
682 return new UriEx (uri).UnescapeString (target);
687 public void Unescape ()
689 Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
690 Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
691 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
692 Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
693 Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
694 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
698 public void HexEscape ()
700 Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
701 Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
702 Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
704 Uri.HexEscape ((char) 0x0369);
706 } catch (ArgumentOutOfRangeException) {}
710 public void MoreHexEscape()
712 string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
713 string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
714 Uri u = new Uri(url);
715 Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
719 public void HexUnescape ()
722 Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
723 Assert.AreEqual (3, i, "#2");
725 Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
726 Assert.AreEqual (7, i, "#4");
727 Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
728 Assert.AreEqual (8, i, "#6");
730 Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
731 Assert.AreEqual (5, i, "#6");
732 Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
733 Assert.AreEqual (6, i, "#8");
735 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
737 Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
738 Assert.AreEqual (4, i, "#10");
740 Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
741 Assert.AreEqual (4, i, "#12");
745 public void HexUnescapeMultiByte ()
747 // Tests from bug 74872
748 // Note: These won't pass exactly with MS.NET, due to differences in the
749 // handling of backslashes/forwardslashes
754 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
755 path = uri.LocalPath;
756 Assert.AreEqual (8, path.Length, "#1");
757 Assert.AreEqual (0x304B, path [6], "#2");
759 // 4-byte character which should be handled as a surrogate
760 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
761 path = uri.LocalPath;
762 Assert.AreEqual (9, path.Length, "#3");
763 Assert.AreEqual (0xDB40, path [6], "#4");
764 Assert.AreEqual (0xDD00, path [7], "#5");
765 Assert.AreEqual (0x62, path [8], "#6");
767 // 2-byte escape sequence, 2 individual characters
768 uri = new Uri ("file:///foo/a%C2%F8b", true);
769 path = uri.LocalPath;
770 Assert.AreEqual (9, path.Length, "#7");
771 Assert.AreEqual (0xC2, path [6], "#8");
772 Assert.AreEqual (0xF8, path [7], "#9");
776 public void IsHexDigit ()
778 Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
779 Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
780 Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
781 Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
782 Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
783 Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
784 Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
785 Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
789 public void IsHexEncoding ()
791 Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
792 Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
793 Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
794 Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
798 public void GetLeftPart ()
800 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
801 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
802 Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
803 Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
805 uri = new Uri ("mailto:user@contoso.com?subject=uri");
806 Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
807 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
808 Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
810 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
811 Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
812 Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
813 Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
815 uri = new Uri ("news:123456@contoso.com");
816 Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
817 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
818 Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
820 uri = new Uri ("file://server/filename.ext");
821 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
822 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
823 Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
825 uri = new Uri (@"\\server\share\filename.ext");
826 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
827 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
828 Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
830 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
831 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
832 Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
833 Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
837 public void NewsDefaultPort ()
839 Uri uri = new Uri("news://localhost:119/");
840 Assert.AreEqual (uri.IsDefaultPort, true, "#1");
844 public void Fragment_Escape ()
846 Uri u = new Uri("http://localhost/index.asp#main#start", false);
847 Assert.AreEqual (u.Fragment, "#main%23start", "#1");
849 u = new Uri("http://localhost/index.asp#main#start", true);
850 Assert.AreEqual (u.Fragment, "#main#start", "#2");
852 // The other code path uses a BaseUri
854 Uri b = new Uri ("http://www.gnome.org");
855 Uri n = new Uri (b, "blah#main#start");
856 Assert.AreEqual (n.Fragment, "#main%23start", "#3");
858 n = new Uri (b, "blah#main#start", true);
859 Assert.AreEqual (n.Fragment, "#main#start", "#4");
863 public void Fragment_RelativeUri ()
865 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
866 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
867 Uri relativeUri = uri1.MakeRelativeUri (uri2);
870 string fragment = relativeUri.Fragment;
871 Assert.Fail ("#1: " + fragment);
872 } catch (InvalidOperationException ex) {
873 // This operation is not supported for a relative URI
874 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
875 Assert.IsNull (ex.InnerException, "#3");
876 Assert.IsNotNull (ex.Message, "#4");
881 [ExpectedException(typeof(UriFormatException))]
882 public void IncompleteSchemeDelimiter ()
884 new Uri ("file:/filename.ext");
888 [Category("NotDotNet")]
889 public void CheckHostName1 ()
891 // reported to MSDN Product Feedback Center (FDBK28671)
892 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.");
896 public void CheckHostName2 ()
898 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
899 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
900 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
901 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
902 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
903 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
904 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
905 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
906 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
907 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
908 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
909 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
910 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
911 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
914 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
915 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
916 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
917 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
918 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
919 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
920 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
921 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
922 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
923 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
924 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
925 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
926 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
927 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
928 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
929 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
930 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
932 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
933 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
934 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
935 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
936 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
937 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
938 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
939 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
940 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
941 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
942 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
943 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
945 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
946 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
947 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
951 public void IsLoopback ()
953 Uri uri = new Uri("http://loopback:8080");
954 Assert.AreEqual (true, uri.IsLoopback, "#1");
955 uri = new Uri("http://localhost:8080");
956 Assert.AreEqual (true, uri.IsLoopback, "#2");
957 uri = new Uri("http://127.0.0.1:8080");
958 Assert.AreEqual (true, uri.IsLoopback, "#3");
959 uri = new Uri("http://127.0.0.001:8080");
960 Assert.AreEqual (true, uri.IsLoopback, "#4");
961 uri = new Uri("http://[::1]");
962 Assert.AreEqual (true, uri.IsLoopback, "#5");
963 uri = new Uri("http://[::1]:8080");
964 Assert.AreEqual (true, uri.IsLoopback, "#6");
965 uri = new Uri("http://[::0001]:8080");
966 Assert.AreEqual (true, uri.IsLoopback, "#7");
967 uri = new Uri("http://[0:0:0:0::1]:8080");
968 Assert.AreEqual (true, uri.IsLoopback, "#8");
969 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
970 Assert.AreEqual (true, uri.IsLoopback, "#9");
971 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
972 Assert.AreEqual (false, uri.IsLoopback, "#10");
973 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
974 Assert.AreEqual (false, uri.IsLoopback, "#11");
975 uri = new Uri("http://[::ff00:7f11:2233]:8080");
976 Assert.AreEqual (false, uri.IsLoopback, "#12");
977 uri = new Uri("http://[1:0:0:0::1]:8080");
978 Assert.AreEqual (false, uri.IsLoopback, "#13");
982 public void IsLoopback_File ()
984 Uri uri = new Uri ("file:///index.html");
985 Assert.IsTrue (uri.IsLoopback, "file");
989 public void IsLoopback_Relative_Http ()
991 string relative = "http:8080/bar/Hello World.htm";
992 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
993 Assert.IsTrue (!uri.IsLoopback, "http");
997 public void IsLoopback_Relative_Unknown ()
999 string relative = "foo:8080/bar/Hello World.htm";
1000 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1001 Assert.IsTrue (!uri.IsLoopback, "foo");
1005 public void Equals1 ()
1007 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1008 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1009 Assert.IsTrue (uri1.Equals (uri2), "#1");
1010 Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1011 Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1015 public void Equals2 ()
1017 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1018 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1019 Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1023 public void Equals3 ()
1025 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1026 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1027 Assert.IsTrue (uri1.Equals (uri2));
1031 public void TestEquals2 ()
1033 Uri a = new Uri ("http://www.go-mono.com");
1034 Uri b = new Uri ("http://www.go-mono.com");
1036 Assert.AreEqual (a, b, "#1");
1038 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1039 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1041 Assert.IsTrue (a != b, "#2");
1042 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1043 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1045 a = new Uri ("http://www.go-mono.com/ports/");
1046 b = new Uri ("http://www.go-mono.com/PORTS/");
1048 Assert.IsTrue (!a.Equals (b), "#3");
1052 public void CaseSensitivity ()
1054 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1055 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1056 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1057 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1059 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1060 Assert.AreEqual ("http", http.Scheme, "#4");
1061 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1062 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1065 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1066 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1067 Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
1068 Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
1072 public void GetHashCodeTest ()
1074 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1075 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1076 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1077 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1078 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1079 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1080 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1081 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1082 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1086 public void RelativeEqualsTest()
1088 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1089 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1090 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1091 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1092 Assert.IsTrue (uri1 == uri2, "#1a");
1093 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1094 Assert.IsTrue (uri1 != uri3, "#2a");
1095 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1096 Assert.IsTrue (uri1 == uri2, "#3a");
1097 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1098 Assert.IsTrue (uri1 != uri3, "#4a");
1099 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1100 Assert.IsTrue (uri3 != uri4, "#5a");
1101 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1105 [ExpectedException(typeof(InvalidOperationException))]
1106 public void GetLeftPart_Partial1 ()
1108 Uri u = new Uri ("foo", UriKind.Relative);
1109 u.GetLeftPart (UriPartial.Scheme);
1112 [ExpectedException(typeof(InvalidOperationException))]
1114 public void GetLeftPart_Partial2 ()
1116 Uri u = new Uri ("foo", UriKind.Relative);
1117 u.GetLeftPart (UriPartial.Authority);
1120 [ExpectedException(typeof(InvalidOperationException))]
1122 public void GetLeftPart_Partial3 ()
1124 Uri u = new Uri ("foo", UriKind.Relative);
1125 u.GetLeftPart (UriPartial.Path);
1129 public void TestPartialToString ()
1131 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1132 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1133 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1134 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1135 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1139 public void RelativeGetHashCodeTest()
1141 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1142 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1143 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1144 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1145 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1149 public void MakeRelative ()
1151 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1152 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1153 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1154 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1155 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1156 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1157 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1158 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1160 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1161 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1163 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1164 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1166 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1167 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1169 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1171 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1173 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1174 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1176 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1177 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1178 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1180 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1181 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1183 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1184 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1186 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1190 public void RelativeUri ()
1192 Uri u = new Uri("http://localhost/../../../a");
1193 Assert.AreEqual ("http://localhost/a", u.ToString ());
1195 u = new Uri ("http://localhost/../c/b/../a");
1196 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1200 public void RelativeUri2 ()
1202 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1204 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1205 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1206 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1207 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1209 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1210 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1211 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1212 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1214 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1218 public void ToStringTest()
1220 Uri uri = new Uri ("dummy://xxx");
1221 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1225 public void CheckSchemeName ()
1227 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1228 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1229 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1230 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1231 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1232 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1233 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1234 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1235 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1236 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1237 // 0x00E1 -> ã
1238 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1242 public void CheckSchemeName_FirstChar ()
1244 for (int i = 0; i < 256; i++) {
1245 string s = String.Format ("#{0}", i);
1247 bool b = Uri.CheckSchemeName (c.ToString ());
1248 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1249 Assert.AreEqual (valid, b, s);
1254 public void CheckSchemeName_AnyOtherChar ()
1256 for (int i = 0; i < 256; i++) {
1257 string s = String.Format ("#{0}", i);
1259 string scheme = String.Format ("a+b-c.d{0}", c);
1260 bool b = Uri.CheckSchemeName (scheme);
1261 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1262 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1263 Assert.AreEqual (valid, b, s);
1268 [ExpectedException (typeof (UriFormatException))]
1269 public void NoHostname ()
1271 Uri uri = new Uri ("http://");
1275 public void NoHostname2 ()
1278 Uri uri = new Uri ("file://");
1279 Assert.AreEqual (true, uri.IsFile, "#1");
1280 Assert.AreEqual (false, uri.IsUnc, "#2");
1281 Assert.AreEqual ("file", uri.Scheme, "#3");
1282 Assert.AreEqual ("/", uri.LocalPath, "#4");
1283 Assert.AreEqual (string.Empty, uri.Query, "#5");
1284 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1285 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1286 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1287 Assert.AreEqual (string.Empty, uri.Host, "#9");
1288 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1289 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1290 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1291 Assert.IsTrue (uri.IsLoopback, "#13");
1292 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1293 Assert.AreEqual (false, uri.UserEscaped, "#15");
1294 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1295 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1296 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1297 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1301 public void Segments1 ()
1303 Uri uri = new Uri ("http://localhost/");
1304 string [] segments = uri.Segments;
1305 Assert.AreEqual (1, segments.Length, "#01");
1306 Assert.AreEqual ("/", segments [0], "#02");
1310 public void Segments2 ()
1312 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1313 string [] segments = uri.Segments;
1314 Assert.AreEqual (3, segments.Length, "#01");
1315 Assert.AreEqual ("/", segments [0], "#02");
1316 Assert.AreEqual ("dir/", segments [1], "#03");
1317 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1321 public void CachingSegments ()
1323 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1324 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1325 string [] segments = uri.Segments;
1326 Assert.AreEqual (3, segments.Length, "#01");
1327 Assert.AreEqual ("/", segments [0], "#02");
1328 Assert.AreEqual ("dir/", segments [1], "#03");
1329 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1333 public void Segments3 ()
1335 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1336 string [] segments = uri.Segments;
1337 Assert.AreEqual (3, segments.Length, "#01");
1338 Assert.AreEqual ("/", segments [0], "#02");
1339 Assert.AreEqual ("dir/", segments [1], "#03");
1340 Assert.AreEqual ("dummypage/", segments [2], "#04");
1344 public void Segments4 ()
1346 Uri uri = new Uri ("file:///c:/hello");
1348 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1349 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1351 string [] segments = uri.Segments;
1352 Assert.AreEqual (3, segments.Length, "#01");
1353 Assert.AreEqual ("/", segments [0], "#02");
1354 Assert.AreEqual ("c:/", segments[1], "#03");
1355 Assert.AreEqual ("hello", segments [2], "#04");
1359 public void Segments5 ()
1361 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1362 string [] segments = uri.Segments;
1363 Assert.AreEqual (4, segments.Length, "#01");
1364 Assert.AreEqual ("/", segments [0], "#02");
1365 Assert.AreEqual ("bar/", segments [1], "#03");
1366 Assert.AreEqual ("foo/", segments [2], "#04");
1367 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1371 [ExpectedException (typeof (UriFormatException))]
1372 public void UriStartingWithColon()
1378 [ExpectedException (typeof (UriFormatException))]
1379 public void EmptyScheme ()
1384 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1385 [ExpectedException (typeof (UriFormatException))]
1387 public void InvalidPort1 ()
1389 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1390 Assert.AreEqual (65536, uri.Port);
1393 [ExpectedException (typeof (UriFormatException))]
1395 public void InvalidPort2 ()
1397 // UInt32.MaxValue gives port == -1 !!!
1398 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1399 Assert.AreEqual (-1, uri.Port);
1402 [ExpectedException (typeof (UriFormatException))]
1404 public void InvalidPort3 ()
1406 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1407 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1408 Assert.AreEqual (-2147483648, uri.Port);
1412 public void PortMax ()
1414 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1415 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1416 Assert.AreEqual (65535, uri.Port);
1421 public UriEx2 (string s) : base (s)
1425 protected override void Parse ()
1430 // Parse method is no longer used on .NET 2.0
1431 [ExpectedException (typeof (UriFormatException))]
1433 public void ParseOverride ()
1435 // If this does not override base's Parse(), it will
1436 // fail since this argument is not Absolute URI.
1437 UriEx2 ex = new UriEx2 ("readme.txt");
1441 public void UnixLocalPath ()
1443 // This works--the location is not part of the absolute path
1444 string path = "file://localhost/tmp/foo/bar";
1445 Uri fileUri = new Uri( path );
1446 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1448 // Empty path == localhost, in theory
1449 path = "file:///c:/tmp/foo/bar";
1450 fileUri = new Uri( path );
1451 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1455 public void WindowsLocalPath ()
1457 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1461 public void TestEscapeDataString ()
1463 StringBuilder sb = new StringBuilder ();
1465 for (int i = 0; i < 128; i++)
1466 sb.Append ((char) i);
1468 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",
1469 Uri.EscapeDataString (sb.ToString ()));
1471 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1474 public void TestEscapeUriString ()
1476 StringBuilder sb = new StringBuilder ();
1477 for (int i = 0; i < 128; i++)
1478 sb.Append ((char) i);
1480 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",
1481 Uri.EscapeUriString (sb.ToString ()));
1482 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1487 public void TestUTF8Strings ()
1490 "file:///tmp/x (%232).jpg",
1491 "file:///tmp/ü (%232).jpg" };
1493 foreach (string test in tests) {
1494 Uri uri = new Uri (test);
1495 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1496 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1497 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1501 // This test doesn't work on Linux, and arguably shouldn't work.
1502 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1503 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1504 // Apparently "tmp" is supposed to be a hostname (!)...
1505 // Since "correct" behavior would confuse all Linux developers, and having
1506 // an expected failure is evil, we'll just ignore this for now...
1508 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1510 // You are surrounded by conditional-compilation code, all alike.
1511 // You are likely to be eaten by a Grue...
1513 public void UnixLocalPath_WTF ()
1515 // Empty path == localhost, in theory
1516 string path = "file:///tmp/foo/bar";
1517 Uri fileUri = new Uri( path );
1518 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1521 string path2 = "file:///foo%25bar";
1522 fileUri = new Uri (path2);
1523 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1526 public static void Print (Uri uri)
1528 Console.WriteLine ("ToString: " + uri.ToString ());
1530 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1531 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1532 Console.WriteLine ("Authority: " + uri.Authority);
1533 Console.WriteLine ("Fragment: " + uri.Fragment);
1534 Console.WriteLine ("Host: " + uri.Host);
1535 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1536 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1537 Console.WriteLine ("IsFile: " + uri.IsFile);
1538 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1539 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1540 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1541 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1542 Console.WriteLine ("Port: " + uri.Port);
1543 Console.WriteLine ("Query: " + uri.Query);
1544 Console.WriteLine ("Scheme: " + uri.Scheme);
1545 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1546 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1548 Console.WriteLine ("Segments:");
1549 string [] segments = uri.Segments;
1550 if (segments == null)
1551 Console.WriteLine ("\tNo Segments");
1553 for (int i = 0; i < segments.Length; i++)
1554 Console.WriteLine ("\t" + segments[i]);
1555 Console.WriteLine ("");
1559 public void FtpRootPath ()
1561 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1562 string p = u.PathAndQuery;
1563 Assert.AreEqual ("/%2fabc/def", p);
1564 p = Uri.UnescapeDataString (p).Substring (1);
1565 Assert.AreEqual ("/abc/def", p);
1566 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1567 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1572 public void LocalPath_FileNameWithAtSign1 ()
1574 string path = "/some/path/file_with_an_@_sign.mp3";
1575 string fullpath = "http://thehost" + path;
1576 Uri fileUri = new Uri (fullpath);
1578 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1579 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1580 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1581 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1582 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1584 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1585 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1586 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1587 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1591 public void LocalPath_FileNameWithAtSign2 ()
1593 string path = "/some/path/file_with_an_@_sign.mp3";
1594 string fullpath = "http://user:password@thehost" + path;
1595 Uri fileUri = new Uri (fullpath);
1597 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1598 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1599 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1600 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1601 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1603 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1604 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1605 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1606 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1610 public void LocalPath_FileNameWithAtSign3 ()
1612 string path = "/some/path/file_with_an_@_sign.mp3";
1613 string fullpath = "file://" + path;
1614 Uri fileUri = new Uri (fullpath);
1616 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1617 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1618 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1619 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1620 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1622 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1623 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1624 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1625 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1629 public void LocalPath_FileNameWithAtSign4 ()
1631 string path = "/some/path/file_with_an_@_sign.mp3";
1632 string fullpath = "file://localhost" + path;
1633 Uri fileUri = new Uri (fullpath);
1635 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1636 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1637 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1638 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1639 Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1641 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1642 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1643 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1644 //this test is marked as NotWorking below:
1645 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1649 public void LocalPath_FileNameWithAtSign5 ()
1651 string path = "/some/path/file_with_an_@_sign.mp3";
1652 string fullpath = "file://localhost" + path;
1653 Uri fileUri = new Uri (fullpath);
1655 string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1656 Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1660 [ExpectedException (typeof (UriFormatException))]
1661 public void LocalPath_FileNameWithAtSign6 ()
1663 string path = "/some/path/file_with_an_@_sign.mp3";
1664 string fullpath = "file://user:password@localhost" + path;
1665 Uri fileUri = new Uri (fullpath);
1669 [Category ("NotDotNet")]
1670 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1672 Uri unixuri = new Uri ("/home/user/a@b");
1673 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1677 [Category ("NotDotNet")]
1678 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1680 Uri unixuri = new Uri ("/home/user/a:b");
1681 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1685 public void RelativeUriWithColons ()
1687 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
1688 new Uri (s, UriKind.Relative);
1689 new Uri (":", UriKind.Relative);
1690 new Uri ("1:", UriKind.Relative);
1694 public void ConsecutiveSlashes ()
1696 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1697 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1700 public class DerivedUri : Uri {
1701 public DerivedUri (string uriString)
1706 internal string TestUnescape (string path)
1708 return base.Unescape (path);