2 // UriTest.cs - NUnit Test Cases for System.Uri
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // Ben Maurer (bmaurer@users.sourceforge.net)
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Ben Maurer
13 using System.Reflection;
14 using NUnit.Framework;
19 namespace MonoTests.System
24 protected bool isWin32 = false;
25 public bool IriParsing;
28 public void GetReady ()
30 isWin32 = (Path.DirectorySeparatorChar == '\\');
32 //Make sure Uri static constructor is called
33 Uri.EscapeDataString ("");
35 FieldInfo iriParsingField = typeof (Uri).GetField ("s_IriParsing",
36 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
37 if (iriParsingField != null)
38 IriParsing = (bool)iriParsingField.GetValue (null);
42 public void Constructors ()
47 uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
50 uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
53 uri = new Uri("http://11.22.33.588:9090");
56 uri = new Uri("http://[11:22:33::88]:9090");
59 uri = new Uri("http://[::127.11.22.33]:8080");
62 uri = new Uri("http://[abcde::127.11.22.33]:8080");
67 uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
70 uri = new Uri ("http://www.contoso.com:1234/foo/bar");
73 uri = new Uri ("http://www.contoso.com:1234/");
76 uri = new Uri ("http://www.contoso.com:1234");
80 uri = new Uri(" \r \n http://test.com\r\n \r\r ");
81 Assert.AreEqual ("http://test.com/", uri.ToString(), "#k0");
82 Assert.AreEqual ("http", uri.GetComponents (UriComponents.Scheme, UriFormat.UriEscaped), "#k0-gc");
84 uri = new Uri ("http://contoso.com?subject=uri");
85 Assert.AreEqual ("/", uri.AbsolutePath, "#k1");
86 Assert.AreEqual ("http://contoso.com/?subject=uri", uri.AbsoluteUri, "#k2");
87 Assert.AreEqual ("contoso.com", uri.Authority, "#k3");
88 Assert.AreEqual ("", uri.Fragment, "#k4");
89 Assert.AreEqual ("contoso.com", uri.Host, "#k5");
90 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#k6");
91 Assert.AreEqual (true, uri.IsDefaultPort, "#k7");
92 Assert.AreEqual (false, uri.IsFile, "#k8");
93 Assert.AreEqual (false, uri.IsLoopback, "#k9");
94 Assert.AreEqual (false, uri.IsUnc, "#k10");
95 Assert.AreEqual ("/", uri.LocalPath, "#k11");
96 Assert.AreEqual ("/?subject=uri", uri.PathAndQuery, "#k12");
97 Assert.AreEqual (80, uri.Port, "#k13");
98 Assert.AreEqual ("?subject=uri", uri.Query, "#k14");
99 Assert.AreEqual ("http", uri.Scheme, "#k15");
100 Assert.AreEqual (false, uri.UserEscaped, "#k16");
101 Assert.AreEqual ("", uri.UserInfo, "#k17");
103 uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
104 Assert.AreEqual ("", uri.AbsolutePath, "#m1");
105 Assert.AreEqual ("mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri, "#m2");
106 Assert.AreEqual ("contoso.com", uri.Authority, "#m3");
107 Assert.AreEqual ("", uri.Fragment, "#m4");
108 Assert.AreEqual ("contoso.com", uri.Host, "#m5");
109 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#m6");
110 Assert.AreEqual (true, uri.IsDefaultPort, "#m7");
111 Assert.AreEqual (false, uri.IsFile, "#m8");
112 Assert.AreEqual (false, uri.IsLoopback, "#m9");
113 Assert.AreEqual (false, uri.IsUnc, "#m10");
114 Assert.AreEqual ("", uri.LocalPath, "#m11");
115 Assert.AreEqual ("?subject=uri", uri.PathAndQuery, "#m12");
116 Assert.AreEqual (25, uri.Port, "#m13");
117 Assert.AreEqual ("?subject=uri", uri.Query, "#m14");
118 Assert.AreEqual ("mailto", uri.Scheme, "#m15");
119 Assert.AreEqual (false, uri.UserEscaped, "#m16");
120 Assert.AreEqual ("user:pwd", uri.UserInfo, "#m17");
122 uri = new Uri("myscheme://127.0.0.1:5");
123 Assert.AreEqual ("myscheme://127.0.0.1:5/", uri.ToString(), "#c1");
125 uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
126 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.AbsolutePath, "#n1");
127 Assert.AreEqual ("file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri, "#n2");
128 Assert.AreEqual ("myserver", uri.Authority, "#n3");
129 Assert.AreEqual ("", uri.Fragment, "#n4");
130 Assert.AreEqual ("myserver", uri.Host, "#n5");
131 Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#n6");
132 Assert.AreEqual (true, uri.IsDefaultPort, "#n7");
133 Assert.AreEqual (true, uri.IsFile, "#n8");
134 Assert.AreEqual (false, uri.IsLoopback, "#n9");
135 Assert.AreEqual (true, uri.IsUnc, "#n10");
138 Assert.AreEqual (@"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath, "#n11");
140 // myserver never could be the part of Unix path.
141 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.LocalPath, "#n11");
143 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.PathAndQuery, "#n12");
144 Assert.AreEqual (-1, uri.Port, "#n13");
145 Assert.AreEqual ("", uri.Query, "#n14");
146 Assert.AreEqual ("file", uri.Scheme, "#n15");
147 Assert.AreEqual (false, uri.UserEscaped, "#n16");
148 Assert.AreEqual ("", uri.UserInfo, "#n17");
150 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
151 Assert.AreEqual ("http://www.contoso.com/Hello World.htm", uri.AbsoluteUri, "#rel1a");
152 Assert.AreEqual (true, uri.UserEscaped, "#rel1b");
153 uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
154 Assert.AreEqual ("http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri, "#rel2a");
155 Assert.AreEqual (false, uri.UserEscaped, "#rel2b");
156 uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
157 Assert.AreEqual ("http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri, "#rel3");
159 uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
160 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel5");
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, "#rel6");
163 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
164 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel7");
165 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
166 Assert.AreEqual ("http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel8");
167 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
168 Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel9");
169 Assert.AreEqual ("/foo/bar/Hello%20World.htm", uri.AbsolutePath, "#rel9-path");
171 uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
172 Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel10");
174 uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
175 Assert.AreEqual ("http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString (), "#22");
177 uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
178 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel30");
180 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
181 Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel31");
183 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
184 Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
186 uri = new Uri ("relative", UriKind.Relative);
187 uri = new Uri ("relative/abc", UriKind.Relative);
188 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
190 Assert.IsTrue (!uri.IsAbsoluteUri, "#rel33");
191 Assert.AreEqual (uri.OriginalString, "relative", "#rel34");
192 Assert.IsTrue (!uri.UserEscaped, "#rel35");
196 public void Constructor_DualHostPort ()
198 string relative = "foo:8080/bar/Hello World.htm";
199 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
200 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.AbsolutePath, "AbsolutePath");
201 Assert.AreEqual ("foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri, "AbsoluteUri");
202 Assert.AreEqual (String.Empty, uri.Authority, "Authority");
203 Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
204 Assert.AreEqual (String.Empty, uri.Host, "Host");
205 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.PathAndQuery, "PathAndQuery");
206 Assert.AreEqual (-1, uri.Port, "Port");
207 Assert.AreEqual (String.Empty, uri.Query, "Query");
208 Assert.AreEqual ("foo", uri.Scheme, "Scheme");
209 Assert.AreEqual (String.Empty, uri.UserInfo, "Query");
211 Assert.AreEqual ("8080/", uri.Segments[0], "Segments[0]");
212 Assert.AreEqual ("bar/", uri.Segments[1], "Segments[1]");
213 Assert.AreEqual ("Hello%20World.htm", uri.Segments[2], "Segments[2]");
215 Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
216 Assert.IsTrue (!uri.IsFile, "IsFile");
217 Assert.IsTrue (!uri.IsLoopback, "IsLoopback");
218 Assert.IsTrue (!uri.IsUnc, "IsUnc");
219 Assert.IsTrue (!uri.UserEscaped, "UserEscaped");
221 Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
222 Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
223 Assert.AreEqual (relative, uri.OriginalString, "OriginalString");
227 [ExpectedException (typeof (ArgumentNullException))]
228 public void Constructor_NullStringBool ()
230 new Uri (null, "http://www.contoso.com/index.htm", false);
234 public void Constructor_UriNullBool ()
236 new Uri (new Uri ("http://www.contoso.com"), null, false);
239 // regression for bug #47573
241 public void RelativeCtor ()
243 Uri b = new Uri ("http://a/b/c/d;p?q");
244 Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
245 Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
247 Assert.AreEqual ("http://a/b/c/?y", new Uri (b, "?y").ToString (), "#3");
249 Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
251 Uri u = new Uri (b, "/g?q=r");
252 Assert.AreEqual ("http://a/g?q=r", u.ToString (), "#5");
253 Assert.AreEqual ("?q=r", u.Query, "#6");
255 u = new Uri (b, "/g?q=r;. a");
256 Assert.AreEqual ("http://a/g?q=r;. a", u.ToString (), "#5");
257 Assert.AreEqual ("?q=r;.%20a", u.Query, "#6");
262 [ExpectedException (typeof (UriFormatException))]
263 public void RelativeCtor_11_Crasher ()
265 Uri b = new Uri ("http://a/b/c/d;p?q");
266 // this causes crash under MS.NET 1.1
267 Assert.AreEqual ("g:h", new Uri (b, "g:h").ToString (), "g:h");
271 [ExpectedException (typeof (UriFormatException))]
272 public void Bad_IPv6 ()
274 new Uri ("http://0:0:0:0::127.1.2.3]/");
278 public void LeadingSlashes_ShouldFailOn1x ()
280 // doesn't (but should) fail under 1.x
281 Assert.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
282 Assert.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
286 public void LeadingSlashes_BadResultsOn1x ()
288 // strange behaviours of 1.x - it's probably not worth to fix it
289 // on Mono as 2.0 has been fixed
290 Uri u = new Uri ("file:///foo/bar");
291 Assert.AreEqual (String.Empty, u.Host, "#3a");
292 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#3b");
293 Assert.AreEqual ("file:///foo/bar", u.ToString (), "#3c");
294 Assert.AreEqual (false, u.IsUnc, "#3d");
296 u = new Uri ("mailto:/foo");
297 Assert.AreEqual (String.Empty, u.Host, "#13a");
298 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#13b");
299 Assert.AreEqual ("mailto:/foo", u.ToString (), "#13c");
301 u = new Uri ("mailto://foo");
302 Assert.AreEqual (String.Empty, u.Host, "#14a");
303 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#14b");
304 Assert.AreEqual ("mailto://foo", u.ToString (), "#14c");
306 u = new Uri ("news:/");
307 Assert.AreEqual (String.Empty, u.Host, "#18a");
308 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#18b");
309 Assert.AreEqual ("news:/", u.ToString (), "#18c");
310 Assert.AreEqual ("/", u.AbsolutePath, "#18d");
311 Assert.AreEqual ("news:/", u.AbsoluteUri, "#18e");
313 u = new Uri ("news:/foo");
314 Assert.AreEqual (String.Empty, u.Host, "#19a");
315 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#19b");
316 Assert.AreEqual ("news:/foo", u.ToString (), "#19c");
317 Assert.AreEqual ("/foo", u.AbsolutePath, "#19d");
318 Assert.AreEqual ("news:/foo", u.AbsoluteUri, "#19e");
320 u = new Uri ("news://foo");
321 Assert.AreEqual (String.Empty, u.Host, "#20a");
322 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#20b");
323 Assert.AreEqual ("news://foo", u.ToString (), "#20c");
324 Assert.AreEqual ("//foo", u.AbsolutePath, "#20d");
325 Assert.AreEqual ("news://foo", u.AbsoluteUri, "#20e");
327 u = new Uri ("news://foo/bar");
328 Assert.AreEqual (String.Empty, u.Host, "#22a");
329 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#22b");
330 Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
331 Assert.AreEqual ("//foo/bar", u.AbsolutePath, "#22d");
332 Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
336 public void LeadingSlashes_FailOn1x ()
338 // 1.x throws an UriFormatException because it can't decode the host name
339 Uri u = new Uri ("mailto:");
340 Assert.AreEqual (String.Empty, u.Host, "#10a");
341 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#10b");
342 Assert.AreEqual ("mailto:", u.ToString (), "#10c");
344 // 1.x throws an UriFormatException because it can't decode the host name
345 u = new Uri ("mailto:/");
346 Assert.AreEqual (String.Empty, u.Host, "#12a");
347 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#12b");
348 Assert.AreEqual ("mailto:/", u.ToString (), "#12c");
350 // 1.x throws an UriFormatException because it cannot detect the format
351 u = new Uri ("mailto:///foo");
352 Assert.AreEqual (String.Empty, u.Host, "#15a");
353 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#15b");
354 Assert.AreEqual ("mailto:///foo", u.ToString (), "#15c");
356 // 1.x throws an UriFormatException because it cannot detect the format
357 u = new Uri ("news:///foo");
358 Assert.AreEqual (String.Empty, u.Host, "#21a");
359 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#21b");
360 Assert.AreEqual ("news:///foo", u.ToString (), "#21c");
361 Assert.AreEqual ("///foo", u.AbsolutePath, "#21d");
362 Assert.AreEqual ("news:///foo", u.AbsoluteUri, "#21e");
366 // some tests from bug 75144
367 public void LeadingSlashes ()
369 Uri u = new Uri ("file://foo/bar");
370 Assert.AreEqual ("foo", u.Host, "#5a");
371 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#5b");
372 Assert.AreEqual ("file://foo/bar", u.ToString (), "#5c");
373 Assert.AreEqual (isWin32, u.IsUnc, "#5d");
375 u = new Uri ("file:////foo/bar");
376 Assert.AreEqual ("foo", u.Host, "#7a");
377 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#7b");
378 Assert.AreEqual ("file://foo/bar", u.ToString (), "#7c");
379 Assert.AreEqual (isWin32, u.IsUnc, "#7d");
381 Assert.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
383 u = new Uri ("mailto:foo");
384 Assert.AreEqual ("foo", u.Host, "#11a");
385 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#11b");
386 Assert.AreEqual ("mailto:foo", u.ToString (), "#11c");
388 u = new Uri ("news:");
389 Assert.AreEqual (String.Empty, u.Host, "#16a");
390 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#16b");
391 Assert.AreEqual ("news:", u.ToString (), "#16c");
393 u = new Uri ("news:foo");
394 Assert.AreEqual (String.Empty, u.Host, "#17a");
395 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#17b");
396 Assert.AreEqual ("news:foo", u.ToString (), "#17c");
397 Assert.AreEqual ("foo", u.AbsolutePath, "#17d");
398 Assert.AreEqual ("news:foo", u.AbsoluteUri, "#17e");
402 [ExpectedException (typeof (UriFormatException))]
403 public void HttpHostname1 ()
409 [ExpectedException (typeof (UriFormatException))]
410 public void HttpHostname2 ()
416 [ExpectedException (typeof (UriFormatException))]
417 public void HttpHostname3 ()
423 [ExpectedException (typeof (UriFormatException))]
424 public void HttpHostname4 ()
426 new Uri ("http:/foo");
430 [ExpectedException (typeof (UriFormatException))]
431 public void HttpHostname5 ()
437 [ExpectedException (typeof (UriFormatException))]
438 public void HttpHostname6 ()
440 new Uri ("http:///");
444 [ExpectedException (typeof (UriFormatException))]
445 public void HttpHostname7 ()
447 new Uri ("http:///foo");
451 [ExpectedException (typeof (UriFormatException))]
452 public void InvalidFile1 ()
458 [ExpectedException (typeof (UriFormatException))]
459 public void InvalidFile2 ()
465 [ExpectedException (typeof (UriFormatException))]
466 public void InvalidFile3 ()
468 new Uri ("file:/foo");
472 [ExpectedException (typeof (UriFormatException))]
473 public void InvalidScheme ()
479 public void ConstructorsRejectRelativePath ()
481 string [] reluris = new string [] {
483 "thisdir/childdir/file",
486 string [] winRelUris = new string [] {
490 for (int i = 0; i < reluris.Length; i++) {
492 new Uri (reluris [i]);
493 Assert.Fail ("Should be failed: " + reluris [i]);
494 } catch (UriFormatException) {
499 for (int i = 0; i < winRelUris.Length; i++) {
501 new Uri (winRelUris [i]);
502 Assert.Fail ("Should be failed: " + winRelUris [i]);
503 } catch (UriFormatException) {
510 public void LocalPath ()
512 Uri uri = new Uri ("c:\\tmp\\hello.txt");
513 Assert.AreEqual ("file:///c:/tmp/hello.txt", uri.ToString (), "#1a");
514 Assert.AreEqual ("c:\\tmp\\hello.txt", uri.LocalPath, "#1b");
515 Assert.AreEqual ("file", uri.Scheme, "#1c");
516 Assert.AreEqual ("", uri.Host, "#1d");
517 Assert.AreEqual ("c:/tmp/hello.txt", uri.AbsolutePath, "#1e");
519 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
520 Assert.AreEqual ("file://cygwin/tmp/hello.txt", uri.ToString (), "#3a");
522 Assert.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#3b win32");
524 Assert.AreEqual ("/tmp/hello.txt", uri.LocalPath, "#3b *nix");
525 Assert.AreEqual ("file", uri.Scheme, "#3c");
526 Assert.AreEqual ("cygwin", uri.Host, "#3d");
527 Assert.AreEqual ("/tmp/hello.txt", uri.AbsolutePath, "#3e");
529 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
530 Assert.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri.ToString (), "#4a");
532 Assert.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#4b win32");
534 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.LocalPath, "#4b *nix");
535 Assert.AreEqual ("file", uri.Scheme, "#4c");
536 Assert.AreEqual ("mymachine", uri.Host, "#4d");
537 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.AbsolutePath, "#4e");
539 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
540 Assert.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri.ToString (), "#5a");
541 Assert.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#5b");
542 Assert.AreEqual ("file", uri.Scheme, "#5c");
543 Assert.AreEqual ("", uri.Host, "#5d");
544 Assert.AreEqual ("c:/cygwin/tmp/hello.txt", uri.AbsolutePath, "#5e");
548 public void LocalPath_FileHost ()
550 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
551 Uri uri = new Uri("file://one_file.txt");
552 Assert.AreEqual ("file://one_file.txt/", uri.ToString(), "#6a");
553 Assert.AreEqual ("/", uri.AbsolutePath, "#6e");
554 Assert.AreEqual ("/", uri.PathAndQuery, "#6f");
555 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#6g");
557 Assert.AreEqual ("\\\\one_file.txt", uri.LocalPath, "#6b");
559 Assert.AreEqual ("/", uri.LocalPath, "#6b");
560 Assert.AreEqual ("file", uri.Scheme, "#6c");
561 Assert.AreEqual ("one_file.txt", uri.Host, "#6d");
563 // same tests - but original Uri is now ending with a '/'
565 uri = new Uri ("file://one_file.txt/");
566 Assert.AreEqual ("file://one_file.txt/", uri.ToString (), "#7a");
567 Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
568 Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
569 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
571 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
573 Assert.AreEqual ("/", uri.LocalPath, "#7b");
574 Assert.AreEqual ("file", uri.Scheme, "#7c");
575 Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
579 public void LocalPath_Escape ()
582 Uri uri = new Uri ("file:///tmp/a%20a");
584 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
586 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
588 uri = new Uri ("file:///tmp/foo%25bar");
590 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
591 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
593 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
594 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
597 uri = new Uri ("file:///foo%25bar");
599 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
600 // ditto, file://tmp/foo%25bar (bug in 1.x)
601 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
603 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
604 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
608 // Novell Bugzilla #320614
610 public void QueryEscape ()
612 Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
613 Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
616 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1.ToString (), "QE1");
618 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
620 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
624 public void UnixPath () {
626 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
632 Uri uri = new Uri ("http://www.contoso.com");
633 Assert.IsTrue (!uri.IsUnc, "#1");
635 uri = new Uri ("news:123456@contoso.com");
636 Assert.IsTrue (!uri.IsUnc, "#2");
638 uri = new Uri ("file://server/filename.ext");
639 Assert.AreEqual (isWin32, uri.IsUnc, "#3");
641 uri = new Uri (@"\\server\share\filename.ext");
642 Assert.IsTrue (uri.IsUnc, "#6");
644 uri = new Uri (@"a:\dir\filename.ext");
645 Assert.IsTrue (!uri.IsUnc, "#8");
649 [Category("NotDotNet")]
650 public void UncFail ()
653 Uri uri = new Uri ("/home/user/dir/filename.ext");
654 Assert.IsTrue (!uri.IsUnc, "#7");
659 public void FromHex ()
661 Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
662 Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
663 Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
664 Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
665 Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
666 Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
670 } catch (ArgumentException) {}
674 } catch (ArgumentException) {}
678 } catch (ArgumentException) {}
683 public UriEx (string s) : base (s)
687 public string UnescapeString (string s)
692 public static string UnescapeString (string uri, string target)
694 return new UriEx (uri).UnescapeString (target);
699 public void Unescape ()
701 Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
702 Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
703 Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
704 Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
706 Assert.AreEqual ("%A9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
707 Assert.AreEqual ("%A9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
709 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
710 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
715 public void HexEscape ()
717 Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
718 Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
719 Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
721 Uri.HexEscape ((char) 0x0369);
723 } catch (ArgumentOutOfRangeException) {}
727 public void MoreHexEscape()
729 string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
730 string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
731 Uri u = new Uri(url);
732 Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
736 public void HexUnescape ()
739 Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
740 Assert.AreEqual (3, i, "#2");
742 Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
743 Assert.AreEqual (7, i, "#4");
744 Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
745 Assert.AreEqual (8, i, "#6");
747 Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
748 Assert.AreEqual (5, i, "#6");
749 Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
750 Assert.AreEqual (6, i, "#8");
752 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
754 Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
755 Assert.AreEqual (4, i, "#10");
757 Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
758 Assert.AreEqual (4, i, "#12");
762 public void HexUnescapeMultiByte ()
764 // Tests from bug 74872
765 // Note: These won't pass exactly with MS.NET, due to differences in the
766 // handling of backslashes/forwardslashes
771 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
772 path = uri.LocalPath;
773 Assert.AreEqual (8, path.Length, "#1");
774 Assert.AreEqual (0x304B, path [6], "#2");
776 // 4-byte character which should be handled as a surrogate
777 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
778 path = uri.LocalPath;
779 Assert.AreEqual (9, path.Length, "#3");
780 Assert.AreEqual (0xDB40, path [6], "#4");
781 Assert.AreEqual (0xDD00, path [7], "#5");
782 Assert.AreEqual (0x62, path [8], "#6");
784 // 2-byte escape sequence, 2 individual characters
785 uri = new Uri ("file:///foo/a%C2%F8b", true);
786 path = uri.LocalPath;
788 Assert.AreEqual ("/foo/a%C2%F8b", path, "#7");
790 Assert.AreEqual (9, path.Length, "#7");
791 Assert.AreEqual (0xC2, path [6], "#8");
792 Assert.AreEqual (0xF8, path [7], "#9");
797 public void IsHexDigit ()
799 Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
800 Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
801 Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
802 Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
803 Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
804 Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
805 Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
806 Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
810 public void IsHexEncoding ()
812 Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
813 Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
814 Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
815 Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
819 public void GetLeftPart ()
821 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
822 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
823 Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
824 Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
826 uri = new Uri ("mailto:user@contoso.com?subject=uri");
827 Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
828 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
829 Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
831 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
832 Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
833 Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
834 Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
836 uri = new Uri ("news:123456@contoso.com");
837 Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
838 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
839 Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
841 uri = new Uri ("file://server/filename.ext");
842 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
843 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
844 Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
846 uri = new Uri (@"\\server\share\filename.ext");
847 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
848 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
849 Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
851 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
852 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
853 Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
854 Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
858 public void NewsDefaultPort ()
860 Uri uri = new Uri("news://localhost:119/");
861 Assert.AreEqual (uri.IsDefaultPort, true, "#1");
865 public void Fragment_Escape ()
867 Uri u = new Uri("http://localhost/index.asp#main#start", false);
870 Assert.AreEqual (u.Fragment, "#main#start", "#1");
872 Assert.AreEqual (u.Fragment, "#main%23start", "#1");
875 u = new Uri("http://localhost/index.asp#main#start", true);
876 Assert.AreEqual (u.Fragment, "#main#start", "#2");
878 // The other code path uses a BaseUri
880 Uri b = new Uri ("http://www.gnome.org");
881 Uri n = new Uri (b, "blah#main#start");
883 Assert.AreEqual (n.Fragment, "#main#start", "#3");
885 Assert.AreEqual (n.Fragment, "#main%23start", "#3");
888 n = new Uri (b, "blah#main#start", true);
889 Assert.AreEqual (n.Fragment, "#main#start", "#4");
893 public void Fragment_RelativeUri ()
895 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
896 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
897 Uri relativeUri = uri1.MakeRelativeUri (uri2);
900 string fragment = relativeUri.Fragment;
901 Assert.Fail ("#1: " + fragment);
902 } catch (InvalidOperationException ex) {
903 // This operation is not supported for a relative URI
904 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
905 Assert.IsNull (ex.InnerException, "#3");
906 Assert.IsNotNull (ex.Message, "#4");
911 [ExpectedException(typeof(UriFormatException))]
912 public void IncompleteSchemeDelimiter ()
914 new Uri ("file:/filename.ext");
918 [Category("NotDotNet")]
919 public void CheckHostName1 ()
921 // reported to MSDN Product Feedback Center (FDBK28671)
922 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.");
926 public void CheckHostName2 ()
928 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
929 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
930 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
931 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
932 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
933 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
934 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
935 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
936 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
937 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
938 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
939 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
940 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
941 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
944 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
945 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
946 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
947 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
948 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
949 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
950 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
951 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
952 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
953 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
954 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
955 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
956 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
957 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
958 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
959 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
960 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
962 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
963 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
964 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
965 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
966 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
967 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
968 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
969 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
970 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
971 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
972 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
973 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
975 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
976 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
977 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
981 public void IsLoopback ()
983 Uri uri = new Uri("http://loopback:8080");
984 Assert.AreEqual (true, uri.IsLoopback, "#1");
985 uri = new Uri("http://localhost:8080");
986 Assert.AreEqual (true, uri.IsLoopback, "#2");
987 uri = new Uri("http://127.0.0.1:8080");
988 Assert.AreEqual (true, uri.IsLoopback, "#3");
989 uri = new Uri("http://127.0.0.001:8080");
990 Assert.AreEqual (true, uri.IsLoopback, "#4");
991 uri = new Uri("http://[::1]");
992 Assert.AreEqual (true, uri.IsLoopback, "#5");
993 uri = new Uri("http://[::1]:8080");
994 Assert.AreEqual (true, uri.IsLoopback, "#6");
995 uri = new Uri("http://[::0001]:8080");
996 Assert.AreEqual (true, uri.IsLoopback, "#7");
997 uri = new Uri("http://[0:0:0:0::1]:8080");
998 Assert.AreEqual (true, uri.IsLoopback, "#8");
999 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
1000 Assert.AreEqual (true, uri.IsLoopback, "#9");
1001 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
1002 Assert.AreEqual (false, uri.IsLoopback, "#10");
1003 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
1004 Assert.AreEqual (false, uri.IsLoopback, "#11");
1005 uri = new Uri("http://[::ff00:7f11:2233]:8080");
1006 Assert.AreEqual (false, uri.IsLoopback, "#12");
1007 uri = new Uri("http://[1:0:0:0::1]:8080");
1008 Assert.AreEqual (false, uri.IsLoopback, "#13");
1012 public void IsLoopback_File ()
1014 Uri uri = new Uri ("file:///index.html");
1015 Assert.IsTrue (uri.IsLoopback, "file");
1019 public void IsLoopback_Relative_Http ()
1021 string relative = "http:8080/bar/Hello World.htm";
1022 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1023 Assert.IsTrue (!uri.IsLoopback, "http");
1027 public void IsLoopback_Relative_Unknown ()
1029 string relative = "foo:8080/bar/Hello World.htm";
1030 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1031 Assert.IsTrue (!uri.IsLoopback, "foo");
1035 public void Equals1 ()
1037 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1038 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1039 Assert.IsTrue (uri1.Equals (uri2), "#1");
1040 Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1041 Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1045 public void Equals2 ()
1047 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1048 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1049 Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1053 public void Equals3 ()
1055 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1056 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1057 Assert.IsTrue (uri1.Equals (uri2));
1061 public void Equals4 ()
1063 var uri = new Uri ("http://w3.org");
1064 Assert.IsFalse (uri.Equals ("-"));
1068 public void TestEquals2 ()
1070 Uri a = new Uri ("http://www.go-mono.com");
1071 Uri b = new Uri ("http://www.go-mono.com");
1073 Assert.AreEqual (a, b, "#1");
1075 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1076 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1078 Assert.IsTrue (a != b, "#2");
1079 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1080 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1082 a = new Uri ("http://www.go-mono.com/ports/");
1083 b = new Uri ("http://www.go-mono.com/PORTS/");
1085 Assert.IsTrue (!a.Equals (b), "#3");
1089 public void CaseSensitivity ()
1091 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1092 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1093 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1094 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1096 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1097 Assert.AreEqual ("http", http.Scheme, "#4");
1098 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1099 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1102 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1103 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1106 Assert.AreEqual ("[::ffff:169.32.14.5]", ftp.Host, "#8");
1107 Assert.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp.ToString (), "#9");
1109 Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
1110 Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
1115 public void GetHashCodeTest ()
1117 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1118 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1119 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1120 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1121 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1122 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1123 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1124 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1125 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1129 public void RelativeEqualsTest()
1131 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1132 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1133 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1134 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1135 Assert.IsTrue (uri1 == uri2, "#1a");
1136 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1137 Assert.IsTrue (uri1 != uri3, "#2a");
1138 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1139 Assert.IsTrue (uri1 == uri2, "#3a");
1140 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1141 Assert.IsTrue (uri1 != uri3, "#4a");
1142 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1143 Assert.IsTrue (uri3 != uri4, "#5a");
1144 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1148 [ExpectedException(typeof(InvalidOperationException))]
1149 public void GetLeftPart_Partial1 ()
1151 Uri u = new Uri ("foo", UriKind.Relative);
1152 u.GetLeftPart (UriPartial.Scheme);
1155 [ExpectedException(typeof(InvalidOperationException))]
1157 public void GetLeftPart_Partial2 ()
1159 Uri u = new Uri ("foo", UriKind.Relative);
1160 u.GetLeftPart (UriPartial.Authority);
1163 [ExpectedException(typeof(InvalidOperationException))]
1165 public void GetLeftPart_Partial3 ()
1167 Uri u = new Uri ("foo", UriKind.Relative);
1168 u.GetLeftPart (UriPartial.Path);
1172 public void TestPartialToString ()
1174 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1175 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1176 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1177 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1178 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1182 public void RelativeGetHashCodeTest()
1184 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1185 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1186 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1187 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1188 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1192 public void MakeRelative ()
1194 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1195 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1196 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1197 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1198 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1199 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1200 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1201 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1203 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1204 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1206 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1207 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1209 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1210 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1212 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1214 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1216 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1217 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1219 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1220 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1221 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1223 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1224 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1226 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1227 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1229 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1233 [Category ("NotWorking")]
1234 public void RelativeUri ()
1236 var u = new Uri ("/foo/bar");
1237 Assert.IsFalse (u.IsAbsoluteUri, "#1");
1241 public void RelativeFragmentUri ()
1243 Uri u = new Uri("http://localhost/../../../a");
1244 Assert.AreEqual ("http://localhost/a", u.ToString ());
1246 u = new Uri ("http://localhost/../c/b/../a");
1247 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1251 public void RelativeFragmentUri2 ()
1253 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1255 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1256 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1257 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1258 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1260 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1261 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1262 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1263 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1265 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1269 public void ToStringTest()
1271 Uri uri = new Uri ("dummy://xxx");
1272 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1276 public void CheckSchemeName ()
1278 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1279 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1280 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1281 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1282 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1283 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1284 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1285 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1286 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1287 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1288 // 0x00E1 -> ã
1289 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1293 public void CheckSchemeName_FirstChar ()
1295 for (int i = 0; i < 256; i++) {
1296 string s = String.Format ("#{0}", i);
1298 bool b = Uri.CheckSchemeName (c.ToString ());
1299 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1300 Assert.AreEqual (valid, b, s);
1305 public void CheckSchemeName_AnyOtherChar ()
1307 for (int i = 0; i < 256; i++) {
1308 string s = String.Format ("#{0}", i);
1310 string scheme = String.Format ("a+b-c.d{0}", c);
1311 bool b = Uri.CheckSchemeName (scheme);
1312 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1313 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1314 Assert.AreEqual (valid, b, s);
1319 [ExpectedException (typeof (UriFormatException))]
1320 public void NoHostname ()
1322 Uri uri = new Uri ("http://");
1326 public void NoHostname2 ()
1329 Uri uri = new Uri ("file://");
1330 Assert.AreEqual (true, uri.IsFile, "#1");
1331 Assert.AreEqual (false, uri.IsUnc, "#2");
1332 Assert.AreEqual ("file", uri.Scheme, "#3");
1333 Assert.AreEqual ("/", uri.LocalPath, "#4");
1334 Assert.AreEqual (string.Empty, uri.Query, "#5");
1335 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1336 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1337 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1338 Assert.AreEqual (string.Empty, uri.Host, "#9");
1339 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1340 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1341 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1342 Assert.IsTrue (uri.IsLoopback, "#13");
1343 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1344 Assert.AreEqual (false, uri.UserEscaped, "#15");
1345 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1346 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1347 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1348 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1352 public void Segments1 ()
1354 Uri uri = new Uri ("http://localhost/");
1355 string [] segments = uri.Segments;
1356 Assert.AreEqual (1, segments.Length, "#01");
1357 Assert.AreEqual ("/", segments [0], "#02");
1361 public void Segments2 ()
1363 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1364 string [] segments = uri.Segments;
1365 Assert.AreEqual (3, segments.Length, "#01");
1366 Assert.AreEqual ("/", segments [0], "#02");
1367 Assert.AreEqual ("dir/", segments [1], "#03");
1368 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1372 public void CachingSegments ()
1374 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1375 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1376 string [] segments = uri.Segments;
1377 Assert.AreEqual (3, segments.Length, "#01");
1378 Assert.AreEqual ("/", segments [0], "#02");
1379 Assert.AreEqual ("dir/", segments [1], "#03");
1380 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1384 public void Segments3 ()
1386 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1387 string [] segments = uri.Segments;
1388 Assert.AreEqual (3, segments.Length, "#01");
1389 Assert.AreEqual ("/", segments [0], "#02");
1390 Assert.AreEqual ("dir/", segments [1], "#03");
1391 Assert.AreEqual ("dummypage/", segments [2], "#04");
1395 public void Segments4 ()
1397 Uri uri = new Uri ("file:///c:/hello");
1399 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1400 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1402 string [] segments = uri.Segments;
1403 Assert.AreEqual (3, segments.Length, "#01");
1404 Assert.AreEqual ("/", segments [0], "#02");
1405 Assert.AreEqual ("c:/", segments[1], "#03");
1406 Assert.AreEqual ("hello", segments [2], "#04");
1410 public void Segments5 ()
1412 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1413 string [] segments = uri.Segments;
1414 Assert.AreEqual (4, segments.Length, "#01");
1415 Assert.AreEqual ("/", segments [0], "#02");
1416 Assert.AreEqual ("bar/", segments [1], "#03");
1417 Assert.AreEqual ("foo/", segments [2], "#04");
1418 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1422 [ExpectedException (typeof (UriFormatException))]
1423 public void UriStartingWithColon()
1429 [ExpectedException (typeof (UriFormatException))]
1430 public void EmptyScheme ()
1436 public void SchemeWithDigits ()
1438 Uri uri = new Uri ("net.p2p://foobar");
1439 Assert.AreEqual ("net.p2p", uri.Scheme);
1442 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1443 [ExpectedException (typeof (UriFormatException))]
1445 public void InvalidPort1 ()
1447 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1448 Assert.AreEqual (65536, uri.Port);
1451 [ExpectedException (typeof (UriFormatException))]
1453 public void InvalidPort2 ()
1455 // UInt32.MaxValue gives port == -1 !!!
1456 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1457 Assert.AreEqual (-1, uri.Port);
1460 [ExpectedException (typeof (UriFormatException))]
1462 public void InvalidPort3 ()
1464 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1465 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1466 Assert.AreEqual (-2147483648, uri.Port);
1470 public void PortMax ()
1472 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1473 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1474 Assert.AreEqual (65535, uri.Port);
1479 public UriEx2 (string s) : base (s)
1483 protected override void Parse ()
1488 // Parse method is no longer used on .NET 2.0
1489 [ExpectedException (typeof (UriFormatException))]
1491 public void ParseOverride ()
1493 // If this does not override base's Parse(), it will
1494 // fail since this argument is not Absolute URI.
1495 UriEx2 ex = new UriEx2 ("readme.txt");
1499 public void UnixLocalPath ()
1501 // This works--the location is not part of the absolute path
1502 string path = "file://localhost/tmp/foo/bar";
1503 Uri fileUri = new Uri( path );
1504 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1506 // Empty path == localhost, in theory
1507 path = "file:///c:/tmp/foo/bar";
1508 fileUri = new Uri( path );
1509 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1513 public void WindowsLocalPath ()
1515 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1519 public void TestEscapeDataString ()
1521 StringBuilder sb = new StringBuilder ();
1523 for (int i = 0; i < 128; i++)
1524 sb.Append ((char) i);
1528 "%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",
1529 Uri.EscapeDataString (sb.ToString ()));
1532 "%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",
1533 Uri.EscapeDataString (sb.ToString ()));
1536 "%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",
1537 Uri.EscapeDataString (sb.ToString ()));
1540 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1543 public void TestEscapeUriString ()
1545 StringBuilder sb = new StringBuilder ();
1546 for (int i = 0; i < 128; i++)
1547 sb.Append ((char) i);
1551 "%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",
1552 Uri.EscapeUriString (sb.ToString ()));
1555 "%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",
1556 Uri.EscapeUriString (sb.ToString ()));
1559 "%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",
1560 Uri.EscapeUriString (sb.ToString ()));
1563 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1568 public void TestUTF8Strings ()
1571 "file:///tmp/x (%232).jpg",
1572 "file:///tmp/ü (%232).jpg" };
1574 foreach (string test in tests) {
1575 Uri uri = new Uri (test);
1576 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1577 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1578 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1582 // This test doesn't work on Linux, and arguably shouldn't work.
1583 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1584 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1585 // Apparently "tmp" is supposed to be a hostname (!)...
1586 // Since "correct" behavior would confuse all Linux developers, and having
1587 // an expected failure is evil, we'll just ignore this for now...
1589 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1591 // You are surrounded by conditional-compilation code, all alike.
1592 // You are likely to be eaten by a Grue...
1594 public void UnixLocalPath_WTF ()
1596 // Empty path == localhost, in theory
1597 string path = "file:///tmp/foo/bar";
1598 Uri fileUri = new Uri( path );
1599 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1602 string path2 = "file:///foo%25bar";
1603 fileUri = new Uri (path2);
1604 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1607 public static void Print (Uri uri)
1609 Console.WriteLine ("ToString: " + uri.ToString ());
1611 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1612 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1613 Console.WriteLine ("Authority: " + uri.Authority);
1614 Console.WriteLine ("Fragment: " + uri.Fragment);
1615 Console.WriteLine ("Host: " + uri.Host);
1616 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1617 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1618 Console.WriteLine ("IsFile: " + uri.IsFile);
1619 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1620 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1621 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1622 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1623 Console.WriteLine ("Port: " + uri.Port);
1624 Console.WriteLine ("Query: " + uri.Query);
1625 Console.WriteLine ("Scheme: " + uri.Scheme);
1626 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1627 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1629 Console.WriteLine ("Segments:");
1630 string [] segments = uri.Segments;
1631 if (segments == null)
1632 Console.WriteLine ("\tNo Segments");
1634 for (int i = 0; i < segments.Length; i++)
1635 Console.WriteLine ("\t" + segments[i]);
1636 Console.WriteLine ("");
1640 public void FtpRootPath ()
1642 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1643 string p = u.PathAndQuery;
1644 Assert.AreEqual ("/%2fabc/def", p);
1645 p = Uri.UnescapeDataString (p).Substring (1);
1646 Assert.AreEqual ("/abc/def", p);
1647 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1648 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1653 public void LocalPath_FileNameWithAtSign1 ()
1655 string path = "/some/path/file_with_an_@_sign.mp3";
1656 string fullpath = "http://thehost" + path;
1657 Uri fileUri = new Uri (fullpath);
1659 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1660 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1661 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1662 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1663 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1665 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1666 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1667 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1668 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1672 public void LocalPath_FileNameWithAtSign2 ()
1674 string path = "/some/path/file_with_an_@_sign.mp3";
1675 string fullpath = "http://user:password@thehost" + path;
1676 Uri fileUri = new Uri (fullpath);
1678 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1679 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1680 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1681 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1682 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1684 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1685 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1686 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1687 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1691 public void LocalPath_FileNameWithAtSign3 ()
1693 string path = "/some/path/file_with_an_@_sign.mp3";
1694 string fullpath = "file://" + path;
1695 Uri fileUri = new Uri (fullpath);
1697 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1698 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1699 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1700 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1701 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1703 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1704 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1705 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1706 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1710 public void LocalPath_FileNameWithAtSign4 ()
1712 string path = "/some/path/file_with_an_@_sign.mp3";
1713 string fullpath = "file://localhost" + path;
1714 Uri fileUri = new Uri (fullpath);
1716 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1717 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1718 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1719 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1720 Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1722 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1723 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1724 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1725 //this test is marked as NotWorking below:
1726 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1730 public void LocalPath_FileNameWithAtSign5 ()
1732 string path = "/some/path/file_with_an_@_sign.mp3";
1733 string fullpath = "file://localhost" + path;
1734 Uri fileUri = new Uri (fullpath);
1736 string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1737 Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1741 [ExpectedException (typeof (UriFormatException))]
1742 public void LocalPath_FileNameWithAtSign6 ()
1744 string path = "/some/path/file_with_an_@_sign.mp3";
1745 string fullpath = "file://user:password@localhost" + path;
1746 Uri fileUri = new Uri (fullpath);
1750 [Category ("NotDotNet")]
1751 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1753 Uri unixuri = new Uri ("/home/user/a@b");
1754 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1758 [Category ("NotDotNet")]
1759 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1761 Uri unixuri = new Uri ("/home/user/a:b");
1762 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1766 public void RelativeUriWithColons ()
1768 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
1769 new Uri (s, UriKind.Relative);
1770 new Uri (":", UriKind.Relative);
1771 new Uri ("1:", UriKind.Relative);
1775 public void ConsecutiveSlashes ()
1777 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1778 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1781 public class DerivedUri : Uri {
1782 public DerivedUri (string uriString)
1787 internal string TestUnescape (string path)
1789 return base.Unescape (path);
1794 [ExpectedException (typeof (InvalidOperationException))]
1795 public void GetComponents_Relative ()
1797 Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1798 rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1802 public void GetComponents_AbsoluteUri ()
1804 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1806 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1808 string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1809 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1811 string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1812 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1814 string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1815 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1819 public void GetComponents_HttpRequestUrl ()
1821 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1823 string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1824 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1826 string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1827 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1829 string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1830 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1834 public void GetComponents_KeepDelimiter ()
1836 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1838 string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1839 Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1841 string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1842 Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1844 string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1845 Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1849 public void GetComponents_StrongAuthority ()
1851 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1853 string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1854 Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1856 string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1857 Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1859 string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1860 Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1864 public void GetComponents_Path ()
1866 Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1867 Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1869 string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1870 Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1872 string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1873 Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1875 string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1876 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1878 // same result is unescaped original string
1879 Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1880 Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1882 safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1883 Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1885 unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1886 Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1888 escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1889 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1893 public void GetComponents_PathAndQuery ()
1895 Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1897 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1899 string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1900 Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1902 string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1903 Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1905 string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1906 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1910 public void GetComponents_Query ()
1912 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1914 Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1916 string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1917 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1919 string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1920 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1922 string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1923 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1927 public void GetComponents_Fragment ()
1929 Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1931 Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1933 string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1934 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1936 string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1937 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1939 string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1940 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1945 public void LocalPathWithBaseUrl ()
1947 var mainUri = new Uri ("http://www.imdb.com");
1948 var uriPath = "/title/tt0106521";
1951 Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1952 Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
1956 public void GetSerializationInfoStringOnRelativeUri ()
1958 var uri = new Uri ("/relative/path", UriKind.Relative);
1959 var result = uri.GetComponents (UriComponents.SerializationInfoString, UriFormat.UriEscaped);
1961 Assert.AreEqual (uri.OriginalString, result);
1965 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1966 public void GetSerializationInfoStringException ()
1968 var uri = new Uri ("/relative/path", UriKind.Relative);
1969 uri.GetComponents (UriComponents.SerializationInfoString | UriComponents.Host, UriFormat.UriEscaped);
1973 public void UserInfo_EscapedLetter ()
1975 var uri = new Uri ("https://first%61second@host");
1976 Assert.AreEqual ("firstasecond", uri.UserInfo);
1980 public void UserInfo_EscapedAt ()
1982 var userinfo = "first%40second";
1983 var uri = new Uri ("https://" + userinfo + "@host");
1984 Assert.AreEqual (userinfo, uri.UserInfo);
1988 // Tests non default IriParsing
1990 public class UriTestAux : UriTest
1992 private FieldInfo iriParsingField;
1993 private bool originalIriParsing;
1996 public void GetReady2 ()
1998 isWin32 = (Path.DirectorySeparatorChar == '\\');
2000 //Make sure Uri static constructor is called
2001 Uri.EscapeDataString ("");
2003 iriParsingField = typeof (Uri).GetField ("s_IriParsing",
2004 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
2006 originalIriParsing = (bool) iriParsingField.GetValue (null);
2008 IriParsing = !originalIriParsing;
2010 iriParsingField.SetValue (null, IriParsing);
2013 [TestFixtureTearDown]
2014 public void TearDown ()
2016 iriParsingField.SetValue (null, originalIriParsing);