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");
572 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
574 Assert.AreEqual ("/", uri.LocalPath, "#7b");
576 Assert.AreEqual ("file", uri.Scheme, "#7c");
577 Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
581 public void LocalPath_Escape ()
584 Uri uri = new Uri ("file:///tmp/a%20a");
586 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
588 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
590 uri = new Uri ("file:///tmp/foo%25bar");
592 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
593 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
595 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
596 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
599 uri = new Uri ("file:///foo%25bar");
601 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
602 // ditto, file://tmp/foo%25bar (bug in 1.x)
603 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
605 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
606 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
610 // Novell Bugzilla #320614
612 public void QueryEscape ()
614 Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
615 Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
618 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1.ToString (), "QE1");
620 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
622 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
626 public void UnixPath () {
628 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
634 Uri uri = new Uri ("http://www.contoso.com");
635 Assert.IsTrue (!uri.IsUnc, "#1");
637 uri = new Uri ("news:123456@contoso.com");
638 Assert.IsTrue (!uri.IsUnc, "#2");
640 uri = new Uri ("file://server/filename.ext");
641 Assert.AreEqual (isWin32, uri.IsUnc, "#3");
643 uri = new Uri (@"\\server\share\filename.ext");
644 Assert.IsTrue (uri.IsUnc, "#6");
646 uri = new Uri (@"a:\dir\filename.ext");
647 Assert.IsTrue (!uri.IsUnc, "#8");
651 [Category("NotDotNet")]
652 public void UncFail ()
655 Uri uri = new Uri ("/home/user/dir/filename.ext");
656 Assert.IsTrue (!uri.IsUnc, "#7");
661 public void FromHex ()
663 Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
664 Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
665 Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
666 Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
667 Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
668 Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
672 } catch (ArgumentException) {}
676 } catch (ArgumentException) {}
680 } catch (ArgumentException) {}
685 public UriEx (string s) : base (s)
689 public string UnescapeString (string s)
694 public static string UnescapeString (string uri, string target)
696 return new UriEx (uri).UnescapeString (target);
701 public void Unescape ()
703 Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
704 Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
705 Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
706 Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
708 Assert.AreEqual ("%A9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
709 Assert.AreEqual ("%A9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
711 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
712 Assert.AreEqual ("\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
717 public void HexEscape ()
719 Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
720 Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
721 Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
723 Uri.HexEscape ((char) 0x0369);
725 } catch (ArgumentOutOfRangeException) {}
729 public void MoreHexEscape()
731 string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
732 string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
733 Uri u = new Uri(url);
734 Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
738 public void HexUnescape ()
741 Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
742 Assert.AreEqual (3, i, "#2");
744 Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
745 Assert.AreEqual (7, i, "#4");
746 Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
747 Assert.AreEqual (8, i, "#6");
749 Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
750 Assert.AreEqual (5, i, "#6");
751 Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
752 Assert.AreEqual (6, i, "#8");
754 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
756 Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
757 Assert.AreEqual (4, i, "#10");
759 Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
760 Assert.AreEqual (4, i, "#12");
764 public void HexUnescapeMultiByte ()
766 // Tests from bug 74872
767 // Note: These won't pass exactly with MS.NET, due to differences in the
768 // handling of backslashes/forwardslashes
773 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
774 path = uri.LocalPath;
775 Assert.AreEqual (8, path.Length, "#1");
776 Assert.AreEqual (0x304B, path [6], "#2");
778 // 4-byte character which should be handled as a surrogate
779 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
780 path = uri.LocalPath;
781 Assert.AreEqual (9, path.Length, "#3");
782 Assert.AreEqual (0xDB40, path [6], "#4");
783 Assert.AreEqual (0xDD00, path [7], "#5");
784 Assert.AreEqual (0x62, path [8], "#6");
786 // 2-byte escape sequence, 2 individual characters
787 uri = new Uri ("file:///foo/a%C2%F8b", true);
788 path = uri.LocalPath;
790 Assert.AreEqual ("/foo/a%C2%F8b", path, "#7");
792 Assert.AreEqual (9, path.Length, "#7");
793 Assert.AreEqual (0xC2, path [6], "#8");
794 Assert.AreEqual (0xF8, path [7], "#9");
799 public void IsHexDigit ()
801 Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
802 Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
803 Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
804 Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
805 Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
806 Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
807 Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
808 Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
812 public void IsHexEncoding ()
814 Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
815 Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
816 Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
817 Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
821 public void GetLeftPart ()
823 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
824 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
825 Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
826 Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
828 uri = new Uri ("mailto:user@contoso.com?subject=uri");
829 Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
830 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
831 Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
833 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
834 Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
835 Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
836 Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
838 uri = new Uri ("news:123456@contoso.com");
839 Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
840 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
841 Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
843 uri = new Uri ("file://server/filename.ext");
844 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
845 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
846 Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
848 uri = new Uri (@"\\server\share\filename.ext");
849 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
850 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
851 Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
853 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
854 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
855 Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
856 Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
860 public void NewsDefaultPort ()
862 Uri uri = new Uri("news://localhost:119/");
863 Assert.AreEqual (uri.IsDefaultPort, true, "#1");
867 public void Fragment_Escape ()
869 Uri u = new Uri("http://localhost/index.asp#main#start", false);
872 Assert.AreEqual (u.Fragment, "#main#start", "#1");
874 Assert.AreEqual (u.Fragment, "#main%23start", "#1");
877 u = new Uri("http://localhost/index.asp#main#start", true);
878 Assert.AreEqual (u.Fragment, "#main#start", "#2");
880 // The other code path uses a BaseUri
882 Uri b = new Uri ("http://www.gnome.org");
883 Uri n = new Uri (b, "blah#main#start");
885 Assert.AreEqual (n.Fragment, "#main#start", "#3");
887 Assert.AreEqual (n.Fragment, "#main%23start", "#3");
890 n = new Uri (b, "blah#main#start", true);
891 Assert.AreEqual (n.Fragment, "#main#start", "#4");
895 public void Fragment_RelativeUri ()
897 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
898 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
899 Uri relativeUri = uri1.MakeRelativeUri (uri2);
902 string fragment = relativeUri.Fragment;
903 Assert.Fail ("#1: " + fragment);
904 } catch (InvalidOperationException ex) {
905 // This operation is not supported for a relative URI
906 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
907 Assert.IsNull (ex.InnerException, "#3");
908 Assert.IsNotNull (ex.Message, "#4");
913 [ExpectedException(typeof(UriFormatException))]
914 public void IncompleteSchemeDelimiter ()
916 new Uri ("file:/filename.ext");
920 [Category("NotDotNet")]
921 public void CheckHostName1 ()
923 // reported to MSDN Product Feedback Center (FDBK28671)
924 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.");
928 public void CheckHostName2 ()
930 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
931 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
932 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
933 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
934 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
935 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
936 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
937 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
938 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
939 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
940 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
941 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
942 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
943 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
946 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
947 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
948 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
949 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
950 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
951 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
952 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
953 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
954 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
955 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
956 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
957 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
958 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
959 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
960 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
961 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
962 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
964 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
965 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
966 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
967 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
968 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
969 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
970 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
971 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
972 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
973 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
974 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
975 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
977 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
978 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
979 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
983 public void IsLoopback ()
985 Uri uri = new Uri("http://loopback:8080");
986 Assert.AreEqual (true, uri.IsLoopback, "#1");
987 uri = new Uri("http://localhost:8080");
988 Assert.AreEqual (true, uri.IsLoopback, "#2");
989 uri = new Uri("http://127.0.0.1:8080");
990 Assert.AreEqual (true, uri.IsLoopback, "#3");
991 uri = new Uri("http://127.0.0.001:8080");
992 Assert.AreEqual (true, uri.IsLoopback, "#4");
993 uri = new Uri("http://[::1]");
994 Assert.AreEqual (true, uri.IsLoopback, "#5");
995 uri = new Uri("http://[::1]:8080");
996 Assert.AreEqual (true, uri.IsLoopback, "#6");
997 uri = new Uri("http://[::0001]:8080");
998 Assert.AreEqual (true, uri.IsLoopback, "#7");
999 uri = new Uri("http://[0:0:0:0::1]:8080");
1000 Assert.AreEqual (true, uri.IsLoopback, "#8");
1001 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
1002 Assert.AreEqual (true, uri.IsLoopback, "#9");
1003 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
1004 Assert.AreEqual (false, uri.IsLoopback, "#10");
1005 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
1006 Assert.AreEqual (false, uri.IsLoopback, "#11");
1007 uri = new Uri("http://[::ff00:7f11:2233]:8080");
1008 Assert.AreEqual (false, uri.IsLoopback, "#12");
1009 uri = new Uri("http://[1:0:0:0::1]:8080");
1010 Assert.AreEqual (false, uri.IsLoopback, "#13");
1014 public void IsLoopback_File ()
1016 Uri uri = new Uri ("file:///index.html");
1017 Assert.IsTrue (uri.IsLoopback, "file");
1021 public void IsLoopback_Relative_Http ()
1023 string relative = "http:8080/bar/Hello World.htm";
1024 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1025 Assert.IsTrue (!uri.IsLoopback, "http");
1029 public void IsLoopback_Relative_Unknown ()
1031 string relative = "foo:8080/bar/Hello World.htm";
1032 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1033 Assert.IsTrue (!uri.IsLoopback, "foo");
1037 public void Equals1 ()
1039 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1040 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1041 Assert.IsTrue (uri1.Equals (uri2), "#1");
1042 Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1043 Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1047 public void Equals2 ()
1049 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1050 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1051 Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1055 public void Equals3 ()
1057 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1058 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1059 Assert.IsTrue (uri1.Equals (uri2));
1063 public void Equals4 ()
1065 var uri = new Uri ("http://w3.org");
1066 Assert.IsFalse (uri.Equals ("-"));
1070 public void TestEquals2 ()
1072 Uri a = new Uri ("http://www.go-mono.com");
1073 Uri b = new Uri ("http://www.go-mono.com");
1075 Assert.AreEqual (a, b, "#1");
1077 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1078 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1080 Assert.IsTrue (a != b, "#2");
1081 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1082 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1084 a = new Uri ("http://www.go-mono.com/ports/");
1085 b = new Uri ("http://www.go-mono.com/PORTS/");
1087 Assert.IsTrue (!a.Equals (b), "#3");
1091 public void CaseSensitivity ()
1093 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1094 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1095 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1096 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1098 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1099 Assert.AreEqual ("http", http.Scheme, "#4");
1100 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1101 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1104 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1105 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1108 Assert.AreEqual ("[::ffff:169.32.14.5]", ftp.Host, "#8");
1109 Assert.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp.ToString (), "#9");
1111 Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
1112 Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
1117 public void GetHashCodeTest ()
1119 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1120 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1121 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1122 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1123 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1124 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1125 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1126 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1127 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1131 public void RelativeEqualsTest()
1133 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1134 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1135 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1136 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1137 Assert.IsTrue (uri1 == uri2, "#1a");
1138 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1139 Assert.IsTrue (uri1 != uri3, "#2a");
1140 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1141 Assert.IsTrue (uri1 == uri2, "#3a");
1142 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1143 Assert.IsTrue (uri1 != uri3, "#4a");
1144 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1145 Assert.IsTrue (uri3 != uri4, "#5a");
1146 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1150 [ExpectedException(typeof(InvalidOperationException))]
1151 public void GetLeftPart_Partial1 ()
1153 Uri u = new Uri ("foo", UriKind.Relative);
1154 u.GetLeftPart (UriPartial.Scheme);
1157 [ExpectedException(typeof(InvalidOperationException))]
1159 public void GetLeftPart_Partial2 ()
1161 Uri u = new Uri ("foo", UriKind.Relative);
1162 u.GetLeftPart (UriPartial.Authority);
1165 [ExpectedException(typeof(InvalidOperationException))]
1167 public void GetLeftPart_Partial3 ()
1169 Uri u = new Uri ("foo", UriKind.Relative);
1170 u.GetLeftPart (UriPartial.Path);
1174 public void TestPartialToString ()
1176 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1177 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1178 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1179 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1180 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1184 public void RelativeGetHashCodeTest()
1186 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1187 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1188 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1189 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1190 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1194 public void MakeRelative ()
1196 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1197 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1198 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1199 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1200 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1201 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1202 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1203 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1205 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1206 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1208 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1209 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1211 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1212 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1214 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1216 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1218 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1219 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1221 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1222 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1223 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1225 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1226 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1228 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1229 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1231 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1235 [Category ("NotWorking")]
1236 public void RelativeUri ()
1238 var u = new Uri ("/foo/bar");
1239 Assert.IsFalse (u.IsAbsoluteUri, "#1");
1243 public void RelativeFragmentUri ()
1245 Uri u = new Uri("http://localhost/../../../a");
1246 Assert.AreEqual ("http://localhost/a", u.ToString ());
1248 u = new Uri ("http://localhost/../c/b/../a");
1249 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1253 public void RelativeFragmentUri2 ()
1255 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1257 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1258 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1259 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1260 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1262 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1263 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1264 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1265 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1267 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1271 public void ToStringTest()
1273 Uri uri = new Uri ("dummy://xxx");
1274 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1278 public void CheckSchemeName ()
1280 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1281 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1282 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1283 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1284 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1285 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1286 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1287 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1288 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1289 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1290 // 0x00E1 -> ã
1291 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1295 public void CheckSchemeName_FirstChar ()
1297 for (int i = 0; i < 256; i++) {
1298 string s = String.Format ("#{0}", i);
1300 bool b = Uri.CheckSchemeName (c.ToString ());
1301 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1302 Assert.AreEqual (valid, b, s);
1307 public void CheckSchemeName_AnyOtherChar ()
1309 for (int i = 0; i < 256; i++) {
1310 string s = String.Format ("#{0}", i);
1312 string scheme = String.Format ("a+b-c.d{0}", c);
1313 bool b = Uri.CheckSchemeName (scheme);
1314 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1315 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1316 Assert.AreEqual (valid, b, s);
1321 [ExpectedException (typeof (UriFormatException))]
1322 public void NoHostname ()
1324 Uri uri = new Uri ("http://");
1328 public void NoHostname2 ()
1331 Uri uri = new Uri ("file://");
1332 Assert.AreEqual (true, uri.IsFile, "#1");
1333 Assert.AreEqual (false, uri.IsUnc, "#2");
1334 Assert.AreEqual ("file", uri.Scheme, "#3");
1335 Assert.AreEqual ("/", uri.LocalPath, "#4");
1336 Assert.AreEqual (string.Empty, uri.Query, "#5");
1337 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1338 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1339 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1340 Assert.AreEqual (string.Empty, uri.Host, "#9");
1341 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1342 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1343 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1344 Assert.IsTrue (uri.IsLoopback, "#13");
1345 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1346 Assert.AreEqual (false, uri.UserEscaped, "#15");
1347 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1348 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1349 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1350 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1354 public void Segments1 ()
1356 Uri uri = new Uri ("http://localhost/");
1357 string [] segments = uri.Segments;
1358 Assert.AreEqual (1, segments.Length, "#01");
1359 Assert.AreEqual ("/", segments [0], "#02");
1363 public void Segments2 ()
1365 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1366 string [] segments = uri.Segments;
1367 Assert.AreEqual (3, segments.Length, "#01");
1368 Assert.AreEqual ("/", segments [0], "#02");
1369 Assert.AreEqual ("dir/", segments [1], "#03");
1370 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1374 public void CachingSegments ()
1376 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1377 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1378 string [] segments = uri.Segments;
1379 Assert.AreEqual (3, segments.Length, "#01");
1380 Assert.AreEqual ("/", segments [0], "#02");
1381 Assert.AreEqual ("dir/", segments [1], "#03");
1382 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1386 public void Segments3 ()
1388 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1389 string [] segments = uri.Segments;
1390 Assert.AreEqual (3, segments.Length, "#01");
1391 Assert.AreEqual ("/", segments [0], "#02");
1392 Assert.AreEqual ("dir/", segments [1], "#03");
1393 Assert.AreEqual ("dummypage/", segments [2], "#04");
1397 public void Segments4 ()
1399 Uri uri = new Uri ("file:///c:/hello");
1401 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1402 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1404 string [] segments = uri.Segments;
1405 Assert.AreEqual (3, segments.Length, "#01");
1406 Assert.AreEqual ("/", segments [0], "#02");
1407 Assert.AreEqual ("c:/", segments[1], "#03");
1408 Assert.AreEqual ("hello", segments [2], "#04");
1412 public void Segments5 ()
1414 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1415 string [] segments = uri.Segments;
1416 Assert.AreEqual (4, segments.Length, "#01");
1417 Assert.AreEqual ("/", segments [0], "#02");
1418 Assert.AreEqual ("bar/", segments [1], "#03");
1419 Assert.AreEqual ("foo/", segments [2], "#04");
1420 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1424 [ExpectedException (typeof (UriFormatException))]
1425 public void UriStartingWithColon()
1431 [ExpectedException (typeof (UriFormatException))]
1432 public void EmptyScheme ()
1437 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1438 [ExpectedException (typeof (UriFormatException))]
1440 public void InvalidPort1 ()
1442 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1443 Assert.AreEqual (65536, uri.Port);
1446 [ExpectedException (typeof (UriFormatException))]
1448 public void InvalidPort2 ()
1450 // UInt32.MaxValue gives port == -1 !!!
1451 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1452 Assert.AreEqual (-1, uri.Port);
1455 [ExpectedException (typeof (UriFormatException))]
1457 public void InvalidPort3 ()
1459 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1460 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1461 Assert.AreEqual (-2147483648, uri.Port);
1465 public void PortMax ()
1467 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1468 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1469 Assert.AreEqual (65535, uri.Port);
1474 public UriEx2 (string s) : base (s)
1478 protected override void Parse ()
1483 // Parse method is no longer used on .NET 2.0
1484 [ExpectedException (typeof (UriFormatException))]
1486 public void ParseOverride ()
1488 // If this does not override base's Parse(), it will
1489 // fail since this argument is not Absolute URI.
1490 UriEx2 ex = new UriEx2 ("readme.txt");
1494 public void UnixLocalPath ()
1496 // This works--the location is not part of the absolute path
1497 string path = "file://localhost/tmp/foo/bar";
1498 Uri fileUri = new Uri( path );
1499 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1501 // Empty path == localhost, in theory
1502 path = "file:///c:/tmp/foo/bar";
1503 fileUri = new Uri( path );
1504 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1508 public void WindowsLocalPath ()
1510 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1514 public void TestEscapeDataString ()
1516 StringBuilder sb = new StringBuilder ();
1518 for (int i = 0; i < 128; i++)
1519 sb.Append ((char) i);
1523 "%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",
1524 Uri.EscapeDataString (sb.ToString ()));
1527 "%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",
1528 Uri.EscapeDataString (sb.ToString ()));
1531 "%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",
1532 Uri.EscapeDataString (sb.ToString ()));
1535 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1538 public void TestEscapeUriString ()
1540 StringBuilder sb = new StringBuilder ();
1541 for (int i = 0; i < 128; i++)
1542 sb.Append ((char) i);
1546 "%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",
1547 Uri.EscapeUriString (sb.ToString ()));
1550 "%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",
1551 Uri.EscapeUriString (sb.ToString ()));
1554 "%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",
1555 Uri.EscapeUriString (sb.ToString ()));
1558 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1563 public void TestUTF8Strings ()
1566 "file:///tmp/x (%232).jpg",
1567 "file:///tmp/ü (%232).jpg" };
1569 foreach (string test in tests) {
1570 Uri uri = new Uri (test);
1571 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1572 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1573 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1577 // This test doesn't work on Linux, and arguably shouldn't work.
1578 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1579 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1580 // Apparently "tmp" is supposed to be a hostname (!)...
1581 // Since "correct" behavior would confuse all Linux developers, and having
1582 // an expected failure is evil, we'll just ignore this for now...
1584 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1586 // You are surrounded by conditional-compilation code, all alike.
1587 // You are likely to be eaten by a Grue...
1589 public void UnixLocalPath_WTF ()
1591 // Empty path == localhost, in theory
1592 string path = "file:///tmp/foo/bar";
1593 Uri fileUri = new Uri( path );
1594 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1597 string path2 = "file:///foo%25bar";
1598 fileUri = new Uri (path2);
1599 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1602 public static void Print (Uri uri)
1604 Console.WriteLine ("ToString: " + uri.ToString ());
1606 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1607 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1608 Console.WriteLine ("Authority: " + uri.Authority);
1609 Console.WriteLine ("Fragment: " + uri.Fragment);
1610 Console.WriteLine ("Host: " + uri.Host);
1611 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1612 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1613 Console.WriteLine ("IsFile: " + uri.IsFile);
1614 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1615 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1616 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1617 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1618 Console.WriteLine ("Port: " + uri.Port);
1619 Console.WriteLine ("Query: " + uri.Query);
1620 Console.WriteLine ("Scheme: " + uri.Scheme);
1621 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1622 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1624 Console.WriteLine ("Segments:");
1625 string [] segments = uri.Segments;
1626 if (segments == null)
1627 Console.WriteLine ("\tNo Segments");
1629 for (int i = 0; i < segments.Length; i++)
1630 Console.WriteLine ("\t" + segments[i]);
1631 Console.WriteLine ("");
1635 public void FtpRootPath ()
1637 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1638 string p = u.PathAndQuery;
1639 Assert.AreEqual ("/%2fabc/def", p);
1640 p = Uri.UnescapeDataString (p).Substring (1);
1641 Assert.AreEqual ("/abc/def", p);
1642 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1643 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1648 public void LocalPath_FileNameWithAtSign1 ()
1650 string path = "/some/path/file_with_an_@_sign.mp3";
1651 string fullpath = "http://thehost" + path;
1652 Uri fileUri = new Uri (fullpath);
1654 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1655 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1656 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1657 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1658 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1660 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1661 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1662 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1663 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1667 public void LocalPath_FileNameWithAtSign2 ()
1669 string path = "/some/path/file_with_an_@_sign.mp3";
1670 string fullpath = "http://user:password@thehost" + path;
1671 Uri fileUri = new Uri (fullpath);
1673 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1674 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1675 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1676 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1677 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1679 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1680 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1681 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1682 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1686 public void LocalPath_FileNameWithAtSign3 ()
1688 string path = "/some/path/file_with_an_@_sign.mp3";
1689 string fullpath = "file://" + path;
1690 Uri fileUri = new Uri (fullpath);
1692 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1693 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1694 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1695 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1696 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1698 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1699 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1700 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1701 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1705 public void LocalPath_FileNameWithAtSign4 ()
1707 string path = "/some/path/file_with_an_@_sign.mp3";
1708 string fullpath = "file://localhost" + path;
1709 Uri fileUri = new Uri (fullpath);
1711 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1712 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1713 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1714 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1715 Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1717 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1718 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1719 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1720 //this test is marked as NotWorking below:
1721 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1725 public void LocalPath_FileNameWithAtSign5 ()
1727 string path = "/some/path/file_with_an_@_sign.mp3";
1728 string fullpath = "file://localhost" + path;
1729 Uri fileUri = new Uri (fullpath);
1731 string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1732 Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1736 [ExpectedException (typeof (UriFormatException))]
1737 public void LocalPath_FileNameWithAtSign6 ()
1739 string path = "/some/path/file_with_an_@_sign.mp3";
1740 string fullpath = "file://user:password@localhost" + path;
1741 Uri fileUri = new Uri (fullpath);
1745 [Category ("NotDotNet")]
1746 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1748 Uri unixuri = new Uri ("/home/user/a@b");
1749 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1753 [Category ("NotDotNet")]
1754 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1756 Uri unixuri = new Uri ("/home/user/a:b");
1757 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1761 public void RelativeUriWithColons ()
1763 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
1764 new Uri (s, UriKind.Relative);
1765 new Uri (":", UriKind.Relative);
1766 new Uri ("1:", UriKind.Relative);
1770 public void ConsecutiveSlashes ()
1772 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1773 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1776 public class DerivedUri : Uri {
1777 public DerivedUri (string uriString)
1782 internal string TestUnescape (string path)
1784 return base.Unescape (path);
1789 [ExpectedException (typeof (InvalidOperationException))]
1790 public void GetComponents_Relative ()
1792 Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1793 rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1797 public void GetComponents_AbsoluteUri ()
1799 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1801 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1803 string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1804 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1806 string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1807 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1809 string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1810 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1814 public void GetComponents_HttpRequestUrl ()
1816 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1818 string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1819 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1821 string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1822 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1824 string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1825 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1829 public void GetComponents_KeepDelimiter ()
1831 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1833 string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1834 Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1836 string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1837 Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1839 string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1840 Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1844 public void GetComponents_StrongAuthority ()
1846 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1848 string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1849 Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1851 string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1852 Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1854 string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1855 Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1859 public void GetComponents_Path ()
1861 Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1862 Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1864 string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1865 Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1867 string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1868 Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1870 string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1871 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1873 // same result is unescaped original string
1874 Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1875 Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1877 safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1878 Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1880 unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1881 Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1883 escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1884 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1888 public void GetComponents_PathAndQuery ()
1890 Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1892 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1894 string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1895 Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1897 string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1898 Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1900 string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1901 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1905 public void GetComponents_Query ()
1907 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1909 Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1911 string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1912 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1914 string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1915 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1917 string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1918 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1922 public void GetComponents_Fragment ()
1924 Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1926 Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1928 string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1929 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1931 string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1932 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1934 string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1935 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1940 public void LocalPathWithBaseUrl ()
1942 var mainUri = new Uri ("http://www.imdb.com");
1943 var uriPath = "/title/tt0106521";
1946 Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1947 Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
1951 // Tests non default IriParsing
1953 public class UriTestAux : UriTest
1955 private FieldInfo iriParsingField;
1956 private bool originalIriParsing;
1959 public void GetReady ()
1961 isWin32 = (Path.DirectorySeparatorChar == '\\');
1963 //Make sure Uri static constructor is called
1964 Uri.EscapeDataString ("");
1966 iriParsingField = typeof (Uri).GetField ("s_IriParsing",
1967 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
1969 originalIriParsing = (bool) iriParsingField.GetValue (null);
1971 IriParsing = !originalIriParsing;
1973 iriParsingField.SetValue (null, IriParsing);
1976 [TestFixtureTearDown]
1977 public void TearDown ()
1979 iriParsingField.SetValue (null, originalIriParsing);