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 Equals4 ()
1033 var uri = new Uri ("http://w3.org");
1034 Assert.IsFalse (uri.Equals ("-"));
1038 public void TestEquals2 ()
1040 Uri a = new Uri ("http://www.go-mono.com");
1041 Uri b = new Uri ("http://www.go-mono.com");
1043 Assert.AreEqual (a, b, "#1");
1045 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1046 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1048 Assert.IsTrue (a != b, "#2");
1049 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1050 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1052 a = new Uri ("http://www.go-mono.com/ports/");
1053 b = new Uri ("http://www.go-mono.com/PORTS/");
1055 Assert.IsTrue (!a.Equals (b), "#3");
1059 public void CaseSensitivity ()
1061 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1062 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1063 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1064 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1066 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1067 Assert.AreEqual ("http", http.Scheme, "#4");
1068 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1069 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1072 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1073 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1074 Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
1075 Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
1079 public void GetHashCodeTest ()
1081 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1082 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1083 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1084 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1085 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1086 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1087 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1088 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1089 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1093 public void RelativeEqualsTest()
1095 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1096 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1097 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1098 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1099 Assert.IsTrue (uri1 == uri2, "#1a");
1100 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1101 Assert.IsTrue (uri1 != uri3, "#2a");
1102 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1103 Assert.IsTrue (uri1 == uri2, "#3a");
1104 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1105 Assert.IsTrue (uri1 != uri3, "#4a");
1106 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1107 Assert.IsTrue (uri3 != uri4, "#5a");
1108 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1112 [ExpectedException(typeof(InvalidOperationException))]
1113 public void GetLeftPart_Partial1 ()
1115 Uri u = new Uri ("foo", UriKind.Relative);
1116 u.GetLeftPart (UriPartial.Scheme);
1119 [ExpectedException(typeof(InvalidOperationException))]
1121 public void GetLeftPart_Partial2 ()
1123 Uri u = new Uri ("foo", UriKind.Relative);
1124 u.GetLeftPart (UriPartial.Authority);
1127 [ExpectedException(typeof(InvalidOperationException))]
1129 public void GetLeftPart_Partial3 ()
1131 Uri u = new Uri ("foo", UriKind.Relative);
1132 u.GetLeftPart (UriPartial.Path);
1136 public void TestPartialToString ()
1138 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1139 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1140 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1141 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1142 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1146 public void RelativeGetHashCodeTest()
1148 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1149 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1150 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1151 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1152 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1156 public void MakeRelative ()
1158 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1159 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1160 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1161 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1162 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1163 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1164 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1165 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1167 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1168 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1170 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1171 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1173 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1174 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1176 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1178 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1180 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1181 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1183 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1184 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1185 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1187 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1188 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1190 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1191 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1193 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1197 [Category ("NotWorking")]
1198 public void RelativeUri ()
1200 var u = new Uri ("/foo/bar");
1201 Assert.IsFalse (u.IsAbsoluteUri, "#1");
1205 public void RelativeFragmentUri ()
1207 Uri u = new Uri("http://localhost/../../../a");
1208 Assert.AreEqual ("http://localhost/a", u.ToString ());
1210 u = new Uri ("http://localhost/../c/b/../a");
1211 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1215 public void RelativeFragmentUri2 ()
1217 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1219 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1220 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1221 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1222 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1224 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1225 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1226 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1227 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1229 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1233 public void ToStringTest()
1235 Uri uri = new Uri ("dummy://xxx");
1236 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1240 public void CheckSchemeName ()
1242 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1243 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1244 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1245 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1246 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1247 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1248 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1249 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1250 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1251 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1252 // 0x00E1 -> ã
1253 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1257 public void CheckSchemeName_FirstChar ()
1259 for (int i = 0; i < 256; i++) {
1260 string s = String.Format ("#{0}", i);
1262 bool b = Uri.CheckSchemeName (c.ToString ());
1263 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1264 Assert.AreEqual (valid, b, s);
1269 public void CheckSchemeName_AnyOtherChar ()
1271 for (int i = 0; i < 256; i++) {
1272 string s = String.Format ("#{0}", i);
1274 string scheme = String.Format ("a+b-c.d{0}", c);
1275 bool b = Uri.CheckSchemeName (scheme);
1276 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1277 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1278 Assert.AreEqual (valid, b, s);
1283 [ExpectedException (typeof (UriFormatException))]
1284 public void NoHostname ()
1286 Uri uri = new Uri ("http://");
1290 public void NoHostname2 ()
1293 Uri uri = new Uri ("file://");
1294 Assert.AreEqual (true, uri.IsFile, "#1");
1295 Assert.AreEqual (false, uri.IsUnc, "#2");
1296 Assert.AreEqual ("file", uri.Scheme, "#3");
1297 Assert.AreEqual ("/", uri.LocalPath, "#4");
1298 Assert.AreEqual (string.Empty, uri.Query, "#5");
1299 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1300 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1301 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1302 Assert.AreEqual (string.Empty, uri.Host, "#9");
1303 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1304 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1305 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1306 Assert.IsTrue (uri.IsLoopback, "#13");
1307 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1308 Assert.AreEqual (false, uri.UserEscaped, "#15");
1309 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1310 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1311 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1312 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1316 public void Segments1 ()
1318 Uri uri = new Uri ("http://localhost/");
1319 string [] segments = uri.Segments;
1320 Assert.AreEqual (1, segments.Length, "#01");
1321 Assert.AreEqual ("/", segments [0], "#02");
1325 public void Segments2 ()
1327 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1328 string [] segments = uri.Segments;
1329 Assert.AreEqual (3, segments.Length, "#01");
1330 Assert.AreEqual ("/", segments [0], "#02");
1331 Assert.AreEqual ("dir/", segments [1], "#03");
1332 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1336 public void CachingSegments ()
1338 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1339 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1340 string [] segments = uri.Segments;
1341 Assert.AreEqual (3, segments.Length, "#01");
1342 Assert.AreEqual ("/", segments [0], "#02");
1343 Assert.AreEqual ("dir/", segments [1], "#03");
1344 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1348 public void Segments3 ()
1350 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1351 string [] segments = uri.Segments;
1352 Assert.AreEqual (3, segments.Length, "#01");
1353 Assert.AreEqual ("/", segments [0], "#02");
1354 Assert.AreEqual ("dir/", segments [1], "#03");
1355 Assert.AreEqual ("dummypage/", segments [2], "#04");
1359 public void Segments4 ()
1361 Uri uri = new Uri ("file:///c:/hello");
1363 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1364 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1366 string [] segments = uri.Segments;
1367 Assert.AreEqual (3, segments.Length, "#01");
1368 Assert.AreEqual ("/", segments [0], "#02");
1369 Assert.AreEqual ("c:/", segments[1], "#03");
1370 Assert.AreEqual ("hello", segments [2], "#04");
1374 public void Segments5 ()
1376 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1377 string [] segments = uri.Segments;
1378 Assert.AreEqual (4, segments.Length, "#01");
1379 Assert.AreEqual ("/", segments [0], "#02");
1380 Assert.AreEqual ("bar/", segments [1], "#03");
1381 Assert.AreEqual ("foo/", segments [2], "#04");
1382 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1386 [ExpectedException (typeof (UriFormatException))]
1387 public void UriStartingWithColon()
1393 [ExpectedException (typeof (UriFormatException))]
1394 public void EmptyScheme ()
1399 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1400 [ExpectedException (typeof (UriFormatException))]
1402 public void InvalidPort1 ()
1404 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1405 Assert.AreEqual (65536, uri.Port);
1408 [ExpectedException (typeof (UriFormatException))]
1410 public void InvalidPort2 ()
1412 // UInt32.MaxValue gives port == -1 !!!
1413 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1414 Assert.AreEqual (-1, uri.Port);
1417 [ExpectedException (typeof (UriFormatException))]
1419 public void InvalidPort3 ()
1421 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1422 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1423 Assert.AreEqual (-2147483648, uri.Port);
1427 public void PortMax ()
1429 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1430 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1431 Assert.AreEqual (65535, uri.Port);
1436 public UriEx2 (string s) : base (s)
1440 protected override void Parse ()
1445 // Parse method is no longer used on .NET 2.0
1446 [ExpectedException (typeof (UriFormatException))]
1448 public void ParseOverride ()
1450 // If this does not override base's Parse(), it will
1451 // fail since this argument is not Absolute URI.
1452 UriEx2 ex = new UriEx2 ("readme.txt");
1456 public void UnixLocalPath ()
1458 // This works--the location is not part of the absolute path
1459 string path = "file://localhost/tmp/foo/bar";
1460 Uri fileUri = new Uri( path );
1461 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1463 // Empty path == localhost, in theory
1464 path = "file:///c:/tmp/foo/bar";
1465 fileUri = new Uri( path );
1466 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1470 public void WindowsLocalPath ()
1472 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1476 public void TestEscapeDataString ()
1478 StringBuilder sb = new StringBuilder ();
1480 for (int i = 0; i < 128; i++)
1481 sb.Append ((char) i);
1485 "%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",
1487 "%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",
1489 Uri.EscapeDataString (sb.ToString ()));
1491 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1494 public void TestEscapeUriString ()
1496 StringBuilder sb = new StringBuilder ();
1497 for (int i = 0; i < 128; i++)
1498 sb.Append ((char) i);
1502 "%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",
1504 "%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",
1506 Uri.EscapeUriString (sb.ToString ()));
1508 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1513 public void TestUTF8Strings ()
1516 "file:///tmp/x (%232).jpg",
1517 "file:///tmp/ü (%232).jpg" };
1519 foreach (string test in tests) {
1520 Uri uri = new Uri (test);
1521 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1522 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1523 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1527 // This test doesn't work on Linux, and arguably shouldn't work.
1528 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1529 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1530 // Apparently "tmp" is supposed to be a hostname (!)...
1531 // Since "correct" behavior would confuse all Linux developers, and having
1532 // an expected failure is evil, we'll just ignore this for now...
1534 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1536 // You are surrounded by conditional-compilation code, all alike.
1537 // You are likely to be eaten by a Grue...
1539 public void UnixLocalPath_WTF ()
1541 // Empty path == localhost, in theory
1542 string path = "file:///tmp/foo/bar";
1543 Uri fileUri = new Uri( path );
1544 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1547 string path2 = "file:///foo%25bar";
1548 fileUri = new Uri (path2);
1549 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1552 public static void Print (Uri uri)
1554 Console.WriteLine ("ToString: " + uri.ToString ());
1556 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1557 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1558 Console.WriteLine ("Authority: " + uri.Authority);
1559 Console.WriteLine ("Fragment: " + uri.Fragment);
1560 Console.WriteLine ("Host: " + uri.Host);
1561 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1562 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1563 Console.WriteLine ("IsFile: " + uri.IsFile);
1564 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1565 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1566 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1567 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1568 Console.WriteLine ("Port: " + uri.Port);
1569 Console.WriteLine ("Query: " + uri.Query);
1570 Console.WriteLine ("Scheme: " + uri.Scheme);
1571 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1572 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1574 Console.WriteLine ("Segments:");
1575 string [] segments = uri.Segments;
1576 if (segments == null)
1577 Console.WriteLine ("\tNo Segments");
1579 for (int i = 0; i < segments.Length; i++)
1580 Console.WriteLine ("\t" + segments[i]);
1581 Console.WriteLine ("");
1585 public void FtpRootPath ()
1587 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1588 string p = u.PathAndQuery;
1589 Assert.AreEqual ("/%2fabc/def", p);
1590 p = Uri.UnescapeDataString (p).Substring (1);
1591 Assert.AreEqual ("/abc/def", p);
1592 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1593 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1598 public void LocalPath_FileNameWithAtSign1 ()
1600 string path = "/some/path/file_with_an_@_sign.mp3";
1601 string fullpath = "http://thehost" + path;
1602 Uri fileUri = new Uri (fullpath);
1604 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1605 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1606 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1607 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1608 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1610 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1611 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1612 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1613 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1617 public void LocalPath_FileNameWithAtSign2 ()
1619 string path = "/some/path/file_with_an_@_sign.mp3";
1620 string fullpath = "http://user:password@thehost" + path;
1621 Uri fileUri = new Uri (fullpath);
1623 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1624 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1625 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1626 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1627 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1629 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1630 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1631 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1632 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1636 public void LocalPath_FileNameWithAtSign3 ()
1638 string path = "/some/path/file_with_an_@_sign.mp3";
1639 string fullpath = "file://" + path;
1640 Uri fileUri = new Uri (fullpath);
1642 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1643 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1644 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1645 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1646 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1648 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1649 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1650 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1651 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1655 public void LocalPath_FileNameWithAtSign4 ()
1657 string path = "/some/path/file_with_an_@_sign.mp3";
1658 string fullpath = "file://localhost" + path;
1659 Uri fileUri = new Uri (fullpath);
1661 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1662 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1663 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1664 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1665 Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1667 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1668 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1669 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1670 //this test is marked as NotWorking below:
1671 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1675 public void LocalPath_FileNameWithAtSign5 ()
1677 string path = "/some/path/file_with_an_@_sign.mp3";
1678 string fullpath = "file://localhost" + path;
1679 Uri fileUri = new Uri (fullpath);
1681 string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1682 Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1686 [ExpectedException (typeof (UriFormatException))]
1687 public void LocalPath_FileNameWithAtSign6 ()
1689 string path = "/some/path/file_with_an_@_sign.mp3";
1690 string fullpath = "file://user:password@localhost" + path;
1691 Uri fileUri = new Uri (fullpath);
1695 [Category ("NotDotNet")]
1696 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1698 Uri unixuri = new Uri ("/home/user/a@b");
1699 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1703 [Category ("NotDotNet")]
1704 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1706 Uri unixuri = new Uri ("/home/user/a:b");
1707 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1711 public void RelativeUriWithColons ()
1713 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
1714 new Uri (s, UriKind.Relative);
1715 new Uri (":", UriKind.Relative);
1716 new Uri ("1:", UriKind.Relative);
1720 public void ConsecutiveSlashes ()
1722 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1723 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1726 public class DerivedUri : Uri {
1727 public DerivedUri (string uriString)
1732 internal string TestUnescape (string path)
1734 return base.Unescape (path);
1739 [ExpectedException (typeof (InvalidOperationException))]
1740 public void GetComponents_Relative ()
1742 Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1743 rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1747 public void GetComponents_AbsoluteUri ()
1749 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1751 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1753 string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1754 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1756 string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1757 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1759 string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1760 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1764 public void GetComponents_HttpRequestUrl ()
1766 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1768 string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1769 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1771 string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1772 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1774 string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1775 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1779 public void GetComponents_KeepDelimiter ()
1781 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1783 string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1784 Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1786 string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1787 Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1789 string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1790 Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1794 public void GetComponents_StrongAuthority ()
1796 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1798 string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1799 Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1801 string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1802 Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1804 string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1805 Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1809 public void GetComponents_Path ()
1811 Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1812 Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1814 string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1815 Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1817 string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1818 Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1820 string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1821 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1823 // same result is unescaped original string
1824 Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1825 Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1827 safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1828 Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1830 unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1831 Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1833 escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1834 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1838 public void GetComponents_PathAndQuery ()
1840 Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1842 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1844 string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1845 Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1847 string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1848 Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1850 string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1851 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1855 public void GetComponents_Query ()
1857 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1859 Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1861 string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1862 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1864 string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1865 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1867 string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1868 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1872 public void GetComponents_Fragment ()
1874 Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1876 Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1878 string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1879 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1881 string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1882 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1884 string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1885 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1890 public void LocalPathWithBaseUrl ()
1892 var mainUri = new Uri ("http://www.imdb.com");
1893 var uriPath = "/title/tt0106521";
1896 Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1897 Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");