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");
179 uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
180 Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
182 uri = new Uri ("relative", UriKind.Relative);
183 uri = new Uri ("relative/abc", UriKind.Relative);
184 uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
186 Assert.IsTrue (!uri.IsAbsoluteUri, "#rel33");
187 Assert.AreEqual (uri.OriginalString, "relative", "#rel34");
188 Assert.IsTrue (!uri.UserEscaped, "#rel35");
192 public void Constructor_DualHostPort ()
194 string relative = "foo:8080/bar/Hello World.htm";
195 Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
196 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.AbsolutePath, "AbsolutePath");
197 Assert.AreEqual ("foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri, "AbsoluteUri");
198 Assert.AreEqual (String.Empty, uri.Authority, "Authority");
199 Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
200 Assert.AreEqual (String.Empty, uri.Host, "Host");
201 Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.PathAndQuery, "PathAndQuery");
202 Assert.AreEqual (-1, uri.Port, "Port");
203 Assert.AreEqual (String.Empty, uri.Query, "Query");
204 Assert.AreEqual ("foo", uri.Scheme, "Scheme");
205 Assert.AreEqual (String.Empty, uri.UserInfo, "Query");
207 Assert.AreEqual ("8080/", uri.Segments[0], "Segments[0]");
208 Assert.AreEqual ("bar/", uri.Segments[1], "Segments[1]");
209 Assert.AreEqual ("Hello%20World.htm", uri.Segments[2], "Segments[2]");
211 Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
212 Assert.IsTrue (!uri.IsFile, "IsFile");
213 Assert.IsTrue (!uri.IsLoopback, "IsLoopback");
214 Assert.IsTrue (!uri.IsUnc, "IsUnc");
215 Assert.IsTrue (!uri.UserEscaped, "UserEscaped");
217 Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
218 Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
219 Assert.AreEqual (relative, uri.OriginalString, "OriginalString");
223 [ExpectedException (typeof (ArgumentNullException))]
224 public void Constructor_NullStringBool ()
226 new Uri (null, "http://www.contoso.com/index.htm", false);
230 public void Constructor_UriNullBool ()
232 new Uri (new Uri ("http://www.contoso.com"), null, false);
235 // regression for bug #47573
237 public void RelativeCtor ()
239 Uri b = new Uri ("http://a/b/c/d;p?q");
240 Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
241 Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
242 Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
244 Uri u = new Uri (b, "/g?q=r");
245 Assert.AreEqual ("http://a/g?q=r", u.ToString (), "#5");
246 Assert.AreEqual ("?q=r", u.Query, "#6");
248 u = new Uri (b, "/g?q=r;. a");
249 Assert.AreEqual ("http://a/g?q=r;. a", u.ToString (), "#5");
250 Assert.AreEqual ("?q=r;.%20a", u.Query, "#6");
255 [ExpectedException (typeof (UriFormatException))]
256 public void RelativeCtor_11_Crasher ()
258 Uri b = new Uri ("http://a/b/c/d;p?q");
259 // this causes crash under MS.NET 1.1
260 Assert.AreEqual ("g:h", new Uri (b, "g:h").ToString (), "g:h");
264 [ExpectedException (typeof (UriFormatException))]
265 public void Bad_IPv6 ()
267 new Uri ("http://0:0:0:0::127.1.2.3]/");
271 public void LeadingSlashes_ShouldFailOn1x ()
273 // doesn't (but should) fail under 1.x
274 Assert.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
275 Assert.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
279 public void LeadingSlashes_BadResultsOn1x ()
281 // strange behaviours of 1.x - it's probably not worth to fix it
282 // on Mono as 2.0 has been fixed
283 Uri u = new Uri ("file:///foo/bar");
284 Assert.AreEqual (String.Empty, u.Host, "#3a");
285 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#3b");
286 Assert.AreEqual ("file:///foo/bar", u.ToString (), "#3c");
287 Assert.AreEqual (false, u.IsUnc, "#3d");
289 u = new Uri ("mailto:/foo");
290 Assert.AreEqual (String.Empty, u.Host, "#13a");
291 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#13b");
292 Assert.AreEqual ("mailto:/foo", u.ToString (), "#13c");
294 u = new Uri ("mailto://foo");
295 Assert.AreEqual (String.Empty, u.Host, "#14a");
296 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#14b");
297 Assert.AreEqual ("mailto://foo", u.ToString (), "#14c");
299 u = new Uri ("news:/");
300 Assert.AreEqual (String.Empty, u.Host, "#18a");
301 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#18b");
302 Assert.AreEqual ("news:/", u.ToString (), "#18c");
303 Assert.AreEqual ("/", u.AbsolutePath, "#18d");
304 Assert.AreEqual ("news:/", u.AbsoluteUri, "#18e");
306 u = new Uri ("news:/foo");
307 Assert.AreEqual (String.Empty, u.Host, "#19a");
308 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#19b");
309 Assert.AreEqual ("news:/foo", u.ToString (), "#19c");
310 Assert.AreEqual ("/foo", u.AbsolutePath, "#19d");
311 Assert.AreEqual ("news:/foo", u.AbsoluteUri, "#19e");
313 u = new Uri ("news://foo");
314 Assert.AreEqual (String.Empty, u.Host, "#20a");
315 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#20b");
316 Assert.AreEqual ("news://foo", u.ToString (), "#20c");
317 Assert.AreEqual ("//foo", u.AbsolutePath, "#20d");
318 Assert.AreEqual ("news://foo", u.AbsoluteUri, "#20e");
320 u = new Uri ("news://foo/bar");
321 Assert.AreEqual (String.Empty, u.Host, "#22a");
322 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#22b");
323 Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
324 Assert.AreEqual ("//foo/bar", u.AbsolutePath, "#22d");
325 Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
329 public void LeadingSlashes_FailOn1x ()
331 // 1.x throws an UriFormatException because it can't decode the host name
332 Uri u = new Uri ("mailto:");
333 Assert.AreEqual (String.Empty, u.Host, "#10a");
334 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#10b");
335 Assert.AreEqual ("mailto:", u.ToString (), "#10c");
337 // 1.x throws an UriFormatException because it can't decode the host name
338 u = new Uri ("mailto:/");
339 Assert.AreEqual (String.Empty, u.Host, "#12a");
340 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#12b");
341 Assert.AreEqual ("mailto:/", u.ToString (), "#12c");
343 // 1.x throws an UriFormatException because it cannot detect the format
344 u = new Uri ("mailto:///foo");
345 Assert.AreEqual (String.Empty, u.Host, "#15a");
346 Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#15b");
347 Assert.AreEqual ("mailto:///foo", u.ToString (), "#15c");
349 // 1.x throws an UriFormatException because it cannot detect the format
350 u = new Uri ("news:///foo");
351 Assert.AreEqual (String.Empty, u.Host, "#21a");
352 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#21b");
353 Assert.AreEqual ("news:///foo", u.ToString (), "#21c");
354 Assert.AreEqual ("///foo", u.AbsolutePath, "#21d");
355 Assert.AreEqual ("news:///foo", u.AbsoluteUri, "#21e");
359 // some tests from bug 75144
360 public void LeadingSlashes ()
362 Uri u = new Uri ("file://foo/bar");
363 Assert.AreEqual ("foo", u.Host, "#5a");
364 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#5b");
365 Assert.AreEqual ("file://foo/bar", u.ToString (), "#5c");
366 Assert.AreEqual (isWin32, u.IsUnc, "#5d");
368 u = new Uri ("file:////foo/bar");
369 Assert.AreEqual ("foo", u.Host, "#7a");
370 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#7b");
371 Assert.AreEqual ("file://foo/bar", u.ToString (), "#7c");
372 Assert.AreEqual (isWin32, u.IsUnc, "#7d");
374 Assert.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
376 u = new Uri ("mailto:foo");
377 Assert.AreEqual ("foo", u.Host, "#11a");
378 Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#11b");
379 Assert.AreEqual ("mailto:foo", u.ToString (), "#11c");
381 u = new Uri ("news:");
382 Assert.AreEqual (String.Empty, u.Host, "#16a");
383 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#16b");
384 Assert.AreEqual ("news:", u.ToString (), "#16c");
386 u = new Uri ("news:foo");
387 Assert.AreEqual (String.Empty, u.Host, "#17a");
388 Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#17b");
389 Assert.AreEqual ("news:foo", u.ToString (), "#17c");
390 Assert.AreEqual ("foo", u.AbsolutePath, "#17d");
391 Assert.AreEqual ("news:foo", u.AbsoluteUri, "#17e");
395 [ExpectedException (typeof (UriFormatException))]
396 public void HttpHostname1 ()
402 [ExpectedException (typeof (UriFormatException))]
403 public void HttpHostname2 ()
409 [ExpectedException (typeof (UriFormatException))]
410 public void HttpHostname3 ()
416 [ExpectedException (typeof (UriFormatException))]
417 public void HttpHostname4 ()
419 new Uri ("http:/foo");
423 [ExpectedException (typeof (UriFormatException))]
424 public void HttpHostname5 ()
430 [ExpectedException (typeof (UriFormatException))]
431 public void HttpHostname6 ()
433 new Uri ("http:///");
437 [ExpectedException (typeof (UriFormatException))]
438 public void HttpHostname7 ()
440 new Uri ("http:///foo");
444 [ExpectedException (typeof (UriFormatException))]
445 public void InvalidFile1 ()
451 [ExpectedException (typeof (UriFormatException))]
452 public void InvalidFile2 ()
458 [ExpectedException (typeof (UriFormatException))]
459 public void InvalidFile3 ()
461 new Uri ("file:/foo");
465 [ExpectedException (typeof (UriFormatException))]
466 public void InvalidScheme ()
472 public void ConstructorsRejectRelativePath ()
474 string [] reluris = new string [] {
476 "thisdir/childdir/file",
479 string [] winRelUris = new string [] {
483 for (int i = 0; i < reluris.Length; i++) {
485 new Uri (reluris [i]);
486 Assert.Fail ("Should be failed: " + reluris [i]);
487 } catch (UriFormatException) {
492 for (int i = 0; i < winRelUris.Length; i++) {
494 new Uri (winRelUris [i]);
495 Assert.Fail ("Should be failed: " + winRelUris [i]);
496 } catch (UriFormatException) {
503 public void LocalPath ()
505 Uri uri = new Uri ("c:\\tmp\\hello.txt");
506 Assert.AreEqual ("file:///c:/tmp/hello.txt", uri.ToString (), "#1a");
507 Assert.AreEqual ("c:\\tmp\\hello.txt", uri.LocalPath, "#1b");
508 Assert.AreEqual ("file", uri.Scheme, "#1c");
509 Assert.AreEqual ("", uri.Host, "#1d");
510 Assert.AreEqual ("c:/tmp/hello.txt", uri.AbsolutePath, "#1e");
512 uri = new Uri ("file:////////cygwin/tmp/hello.txt");
513 Assert.AreEqual ("file://cygwin/tmp/hello.txt", uri.ToString (), "#3a");
515 Assert.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#3b win32");
517 Assert.AreEqual ("/tmp/hello.txt", uri.LocalPath, "#3b *nix");
518 Assert.AreEqual ("file", uri.Scheme, "#3c");
519 Assert.AreEqual ("cygwin", uri.Host, "#3d");
520 Assert.AreEqual ("/tmp/hello.txt", uri.AbsolutePath, "#3e");
522 uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
523 Assert.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri.ToString (), "#4a");
525 Assert.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#4b win32");
527 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.LocalPath, "#4b *nix");
528 Assert.AreEqual ("file", uri.Scheme, "#4c");
529 Assert.AreEqual ("mymachine", uri.Host, "#4d");
530 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.AbsolutePath, "#4e");
532 uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
533 Assert.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri.ToString (), "#5a");
534 Assert.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#5b");
535 Assert.AreEqual ("file", uri.Scheme, "#5c");
536 Assert.AreEqual ("", uri.Host, "#5d");
537 Assert.AreEqual ("c:/cygwin/tmp/hello.txt", uri.AbsolutePath, "#5e");
541 public void LocalPath_FileHost ()
543 // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
544 Uri uri = new Uri("file://one_file.txt");
545 Assert.AreEqual ("file://one_file.txt/", uri.ToString(), "#6a");
546 Assert.AreEqual ("/", uri.AbsolutePath, "#6e");
547 Assert.AreEqual ("/", uri.PathAndQuery, "#6f");
548 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#6g");
550 Assert.AreEqual ("\\\\one_file.txt", uri.LocalPath, "#6b");
552 Assert.AreEqual ("/", uri.LocalPath, "#6b");
553 Assert.AreEqual ("file", uri.Scheme, "#6c");
554 Assert.AreEqual ("one_file.txt", uri.Host, "#6d");
556 // same tests - but original Uri is now ending with a '/'
558 uri = new Uri ("file://one_file.txt/");
559 Assert.AreEqual ("file://one_file.txt/", uri.ToString (), "#7a");
560 Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
561 Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
562 Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
564 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
566 Assert.AreEqual ("/", uri.LocalPath, "#7b");
567 Assert.AreEqual ("file", uri.Scheme, "#7c");
568 Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
572 public void LocalPath_Escape ()
575 Uri uri = new Uri ("file:///tmp/a%20a");
577 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
579 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
581 uri = new Uri ("file:///tmp/foo%25bar");
583 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
584 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
586 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
587 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
590 uri = new Uri ("file:///foo%25bar");
592 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
593 // ditto, file://tmp/foo%25bar (bug in 1.x)
594 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
596 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
597 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
601 // Novell Bugzilla #320614
603 public void QueryEscape ()
605 Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
606 Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
609 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1.ToString (), "QE1");
611 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
613 Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
617 public void UnixPath () {
619 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
625 Uri uri = new Uri ("http://www.contoso.com");
626 Assert.IsTrue (!uri.IsUnc, "#1");
628 uri = new Uri ("news:123456@contoso.com");
629 Assert.IsTrue (!uri.IsUnc, "#2");
631 uri = new Uri ("file://server/filename.ext");
632 Assert.AreEqual (isWin32, uri.IsUnc, "#3");
634 uri = new Uri (@"\\server\share\filename.ext");
635 Assert.IsTrue (uri.IsUnc, "#6");
637 uri = new Uri (@"a:\dir\filename.ext");
638 Assert.IsTrue (!uri.IsUnc, "#8");
642 [Category("NotDotNet")]
643 public void UncFail ()
646 Uri uri = new Uri ("/home/user/dir/filename.ext");
647 Assert.IsTrue (!uri.IsUnc, "#7");
652 public void FromHex ()
654 Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
655 Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
656 Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
657 Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
658 Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
659 Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
663 } catch (ArgumentException) {}
667 } catch (ArgumentException) {}
671 } catch (ArgumentException) {}
676 public UriEx (string s) : base (s)
680 public string UnescapeString (string s)
685 public static string UnescapeString (string uri, string target)
687 return new UriEx (uri).UnescapeString (target);
692 public void Unescape ()
694 Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
695 Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
696 Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
697 Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
698 Assert.AreEqual ("%A9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
699 Assert.AreEqual ("%A9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
703 public void HexEscape ()
705 Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
706 Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
707 Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
709 Uri.HexEscape ((char) 0x0369);
711 } catch (ArgumentOutOfRangeException) {}
715 public void MoreHexEscape()
717 string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
718 string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
719 Uri u = new Uri(url);
720 Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
724 public void HexUnescape ()
727 Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
728 Assert.AreEqual (3, i, "#2");
730 Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
731 Assert.AreEqual (7, i, "#4");
732 Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
733 Assert.AreEqual (8, i, "#6");
735 Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
736 Assert.AreEqual (5, i, "#6");
737 Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
738 Assert.AreEqual (6, i, "#8");
740 // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
742 Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
743 Assert.AreEqual (4, i, "#10");
745 Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
746 Assert.AreEqual (4, i, "#12");
750 public void HexUnescapeMultiByte ()
752 // Tests from bug 74872
753 // Note: These won't pass exactly with MS.NET, due to differences in the
754 // handling of backslashes/forwardslashes
759 uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
760 path = uri.LocalPath;
761 Assert.AreEqual (8, path.Length, "#1");
762 Assert.AreEqual (0x304B, path [6], "#2");
764 // 4-byte character which should be handled as a surrogate
765 uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
766 path = uri.LocalPath;
767 Assert.AreEqual (9, path.Length, "#3");
768 Assert.AreEqual (0xDB40, path [6], "#4");
769 Assert.AreEqual (0xDD00, path [7], "#5");
770 Assert.AreEqual (0x62, path [8], "#6");
772 // 2-byte escape sequence, 2 individual characters
773 uri = new Uri ("file:///foo/a%C2%F8b", true);
774 path = uri.LocalPath;
775 Assert.AreEqual ("/foo/a%C2%F8b", path, "#7");
779 public void IsHexDigit ()
781 Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
782 Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
783 Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
784 Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
785 Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
786 Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
787 Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
788 Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
792 public void IsHexEncoding ()
794 Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
795 Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
796 Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
797 Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
801 public void GetLeftPart ()
803 Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
804 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
805 Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
806 Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
808 uri = new Uri ("mailto:user@contoso.com?subject=uri");
809 Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
810 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
811 Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
813 uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
814 Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
815 Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
816 Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
818 uri = new Uri ("news:123456@contoso.com");
819 Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
820 Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
821 Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
823 uri = new Uri ("file://server/filename.ext");
824 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
825 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
826 Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
828 uri = new Uri (@"\\server\share\filename.ext");
829 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
830 Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
831 Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
833 uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
834 Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
835 Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
836 Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
840 public void NewsDefaultPort ()
842 Uri uri = new Uri("news://localhost:119/");
843 Assert.AreEqual (uri.IsDefaultPort, true, "#1");
847 public void Fragment_Escape ()
849 Uri u = new Uri("http://localhost/index.asp#main#start", false);
852 Assert.AreEqual (u.Fragment, "#main#start", "#1");
854 Assert.AreEqual (u.Fragment, "#main%23start", "#1");
857 u = new Uri("http://localhost/index.asp#main#start", true);
858 Assert.AreEqual (u.Fragment, "#main#start", "#2");
860 // The other code path uses a BaseUri
862 Uri b = new Uri ("http://www.gnome.org");
863 Uri n = new Uri (b, "blah#main#start");
865 Assert.AreEqual (n.Fragment, "#main#start", "#3");
867 Assert.AreEqual (n.Fragment, "#main%23start", "#3");
870 n = new Uri (b, "blah#main#start", true);
871 Assert.AreEqual (n.Fragment, "#main#start", "#4");
875 public void Fragment_RelativeUri ()
877 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
878 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
879 Uri relativeUri = uri1.MakeRelativeUri (uri2);
882 string fragment = relativeUri.Fragment;
883 Assert.Fail ("#1: " + fragment);
884 } catch (InvalidOperationException ex) {
885 // This operation is not supported for a relative URI
886 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
887 Assert.IsNull (ex.InnerException, "#3");
888 Assert.IsNotNull (ex.Message, "#4");
893 [ExpectedException(typeof(UriFormatException))]
894 public void IncompleteSchemeDelimiter ()
896 new Uri ("file:/filename.ext");
900 [Category("NotDotNet")]
901 public void CheckHostName1 ()
903 // reported to MSDN Product Feedback Center (FDBK28671)
904 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.");
908 public void CheckHostName2 ()
910 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
911 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
912 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
913 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
914 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
915 Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
916 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
917 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
918 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
919 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
920 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
921 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
922 Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
923 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
926 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
927 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
928 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
929 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
930 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
931 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
932 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
933 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
934 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
935 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
936 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
937 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
938 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
939 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
940 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
941 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
942 Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
944 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
945 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
946 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
947 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
948 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
949 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
950 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
951 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
952 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
953 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
954 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
955 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
957 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
958 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
959 Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
963 public void IsLoopback ()
965 Uri uri = new Uri("http://loopback:8080");
966 Assert.AreEqual (true, uri.IsLoopback, "#1");
967 uri = new Uri("http://localhost:8080");
968 Assert.AreEqual (true, uri.IsLoopback, "#2");
969 uri = new Uri("http://127.0.0.1:8080");
970 Assert.AreEqual (true, uri.IsLoopback, "#3");
971 uri = new Uri("http://127.0.0.001:8080");
972 Assert.AreEqual (true, uri.IsLoopback, "#4");
973 uri = new Uri("http://[::1]");
974 Assert.AreEqual (true, uri.IsLoopback, "#5");
975 uri = new Uri("http://[::1]:8080");
976 Assert.AreEqual (true, uri.IsLoopback, "#6");
977 uri = new Uri("http://[::0001]:8080");
978 Assert.AreEqual (true, uri.IsLoopback, "#7");
979 uri = new Uri("http://[0:0:0:0::1]:8080");
980 Assert.AreEqual (true, uri.IsLoopback, "#8");
981 uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
982 Assert.AreEqual (true, uri.IsLoopback, "#9");
983 uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
984 Assert.AreEqual (false, uri.IsLoopback, "#10");
985 uri = new Uri("http://[::ffff:127.11.22.33]:8080");
986 Assert.AreEqual (false, uri.IsLoopback, "#11");
987 uri = new Uri("http://[::ff00:7f11:2233]:8080");
988 Assert.AreEqual (false, uri.IsLoopback, "#12");
989 uri = new Uri("http://[1:0:0:0::1]:8080");
990 Assert.AreEqual (false, uri.IsLoopback, "#13");
994 public void IsLoopback_File ()
996 Uri uri = new Uri ("file:///index.html");
997 Assert.IsTrue (uri.IsLoopback, "file");
1001 public void IsLoopback_Relative_Http ()
1003 string relative = "http:8080/bar/Hello World.htm";
1004 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1005 Assert.IsTrue (!uri.IsLoopback, "http");
1009 public void IsLoopback_Relative_Unknown ()
1011 string relative = "foo:8080/bar/Hello World.htm";
1012 Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
1013 Assert.IsTrue (!uri.IsLoopback, "foo");
1017 public void Equals1 ()
1019 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1020 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1021 Assert.IsTrue (uri1.Equals (uri2), "#1");
1022 Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1023 Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1027 public void Equals2 ()
1029 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1030 Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1031 Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1035 public void Equals3 ()
1037 Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1038 Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1039 Assert.IsTrue (uri1.Equals (uri2));
1043 public void Equals4 ()
1045 var uri = new Uri ("http://w3.org");
1046 Assert.IsFalse (uri.Equals ("-"));
1050 public void TestEquals2 ()
1052 Uri a = new Uri ("http://www.go-mono.com");
1053 Uri b = new Uri ("http://www.go-mono.com");
1055 Assert.AreEqual (a, b, "#1");
1057 a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1058 b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1060 Assert.IsTrue (a != b, "#2");
1061 Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1062 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1064 a = new Uri ("http://www.go-mono.com/ports/");
1065 b = new Uri ("http://www.go-mono.com/PORTS/");
1067 Assert.IsTrue (!a.Equals (b), "#3");
1071 public void CaseSensitivity ()
1073 Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1074 Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1075 Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1076 Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1078 Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1079 Assert.AreEqual ("http", http.Scheme, "#4");
1080 Assert.AreEqual ("go-mono.com", http.Host, "#5");
1081 Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1084 Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1085 Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1088 Assert.AreEqual ("[::ffff:169.32.14.5]", ftp.Host, "#8");
1089 Assert.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp.ToString (), "#9");
1091 Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
1092 Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
1097 public void GetHashCodeTest ()
1099 Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1100 Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1101 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1102 uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1103 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1104 uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1105 Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1106 uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1107 Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1111 public void RelativeEqualsTest()
1113 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1114 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1115 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1116 Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1117 Assert.IsTrue (uri1 == uri2, "#1a");
1118 Assert.IsTrue (uri1.Equals(uri2), "#1b");
1119 Assert.IsTrue (uri1 != uri3, "#2a");
1120 Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1121 Assert.IsTrue (uri1 == uri2, "#3a");
1122 Assert.IsTrue (uri1.Equals(uri2), "#3b");
1123 Assert.IsTrue (uri1 != uri3, "#4a");
1124 Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1125 Assert.IsTrue (uri3 != uri4, "#5a");
1126 Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1130 [ExpectedException(typeof(InvalidOperationException))]
1131 public void GetLeftPart_Partial1 ()
1133 Uri u = new Uri ("foo", UriKind.Relative);
1134 u.GetLeftPart (UriPartial.Scheme);
1137 [ExpectedException(typeof(InvalidOperationException))]
1139 public void GetLeftPart_Partial2 ()
1141 Uri u = new Uri ("foo", UriKind.Relative);
1142 u.GetLeftPart (UriPartial.Authority);
1145 [ExpectedException(typeof(InvalidOperationException))]
1147 public void GetLeftPart_Partial3 ()
1149 Uri u = new Uri ("foo", UriKind.Relative);
1150 u.GetLeftPart (UriPartial.Path);
1154 public void TestPartialToString ()
1156 Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1157 Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1158 Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1159 Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1160 Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1164 public void RelativeGetHashCodeTest()
1166 Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1167 Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1168 Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1169 Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1170 Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1174 public void MakeRelative ()
1176 Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1177 Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1178 Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1179 Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1180 Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1181 Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1182 Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1183 Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1185 Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1186 Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1188 Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1189 Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1191 Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1192 Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1194 Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1196 Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1198 Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1199 Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1201 Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1202 Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1203 Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1205 Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1206 Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1208 Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1209 Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1211 Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1215 [Category ("NotWorking")]
1216 public void RelativeUri ()
1218 var u = new Uri ("/foo/bar");
1219 Assert.IsFalse (u.IsAbsoluteUri, "#1");
1223 public void RelativeFragmentUri ()
1225 Uri u = new Uri("http://localhost/../../../a");
1226 Assert.AreEqual ("http://localhost/a", u.ToString ());
1228 u = new Uri ("http://localhost/../c/b/../a");
1229 Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1233 public void RelativeFragmentUri2 ()
1235 Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1237 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1238 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1239 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1240 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1242 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1243 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1244 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1245 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1247 Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1251 public void ToStringTest()
1253 Uri uri = new Uri ("dummy://xxx");
1254 Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1258 public void CheckSchemeName ()
1260 Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1261 Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1262 Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1263 Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1264 Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1265 Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1266 Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1267 Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1268 Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1269 Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1270 // 0x00E1 -> ã
1271 Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1275 public void CheckSchemeName_FirstChar ()
1277 for (int i = 0; i < 256; i++) {
1278 string s = String.Format ("#{0}", i);
1280 bool b = Uri.CheckSchemeName (c.ToString ());
1281 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1282 Assert.AreEqual (valid, b, s);
1287 public void CheckSchemeName_AnyOtherChar ()
1289 for (int i = 0; i < 256; i++) {
1290 string s = String.Format ("#{0}", i);
1292 string scheme = String.Format ("a+b-c.d{0}", c);
1293 bool b = Uri.CheckSchemeName (scheme);
1294 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1295 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1296 Assert.AreEqual (valid, b, s);
1301 [ExpectedException (typeof (UriFormatException))]
1302 public void NoHostname ()
1304 Uri uri = new Uri ("http://");
1308 public void NoHostname2 ()
1311 Uri uri = new Uri ("file://");
1312 Assert.AreEqual (true, uri.IsFile, "#1");
1313 Assert.AreEqual (false, uri.IsUnc, "#2");
1314 Assert.AreEqual ("file", uri.Scheme, "#3");
1315 Assert.AreEqual ("/", uri.LocalPath, "#4");
1316 Assert.AreEqual (string.Empty, uri.Query, "#5");
1317 Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1318 Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1319 Assert.AreEqual (string.Empty, uri.Authority, "#8");
1320 Assert.AreEqual (string.Empty, uri.Host, "#9");
1321 Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1322 Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1323 Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1324 Assert.IsTrue (uri.IsLoopback, "#13");
1325 Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1326 Assert.AreEqual (false, uri.UserEscaped, "#15");
1327 Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1328 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1329 Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1330 Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1334 public void Segments1 ()
1336 Uri uri = new Uri ("http://localhost/");
1337 string [] segments = uri.Segments;
1338 Assert.AreEqual (1, segments.Length, "#01");
1339 Assert.AreEqual ("/", segments [0], "#02");
1343 public void Segments2 ()
1345 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1346 string [] segments = uri.Segments;
1347 Assert.AreEqual (3, segments.Length, "#01");
1348 Assert.AreEqual ("/", segments [0], "#02");
1349 Assert.AreEqual ("dir/", segments [1], "#03");
1350 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1354 public void CachingSegments ()
1356 Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1357 uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1358 string [] segments = uri.Segments;
1359 Assert.AreEqual (3, segments.Length, "#01");
1360 Assert.AreEqual ("/", segments [0], "#02");
1361 Assert.AreEqual ("dir/", segments [1], "#03");
1362 Assert.AreEqual ("dummypage.html", segments [2], "#04");
1366 public void Segments3 ()
1368 Uri uri = new Uri ("http://localhost/dir/dummypage/");
1369 string [] segments = uri.Segments;
1370 Assert.AreEqual (3, segments.Length, "#01");
1371 Assert.AreEqual ("/", segments [0], "#02");
1372 Assert.AreEqual ("dir/", segments [1], "#03");
1373 Assert.AreEqual ("dummypage/", segments [2], "#04");
1377 public void Segments4 ()
1379 Uri uri = new Uri ("file:///c:/hello");
1381 Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1382 Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1384 string [] segments = uri.Segments;
1385 Assert.AreEqual (3, segments.Length, "#01");
1386 Assert.AreEqual ("/", segments [0], "#02");
1387 Assert.AreEqual ("c:/", segments[1], "#03");
1388 Assert.AreEqual ("hello", segments [2], "#04");
1392 public void Segments5 ()
1394 Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1395 string [] segments = uri.Segments;
1396 Assert.AreEqual (4, segments.Length, "#01");
1397 Assert.AreEqual ("/", segments [0], "#02");
1398 Assert.AreEqual ("bar/", segments [1], "#03");
1399 Assert.AreEqual ("foo/", segments [2], "#04");
1400 Assert.AreEqual ("foobar.htm", segments [3], "#05");
1404 [ExpectedException (typeof (UriFormatException))]
1405 public void UriStartingWithColon()
1411 [ExpectedException (typeof (UriFormatException))]
1412 public void EmptyScheme ()
1418 public void SchemeWithDigits ()
1420 Uri uri = new Uri ("net.p2p://foobar");
1421 Assert.AreEqual ("net.p2p", uri.Scheme);
1424 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1425 [ExpectedException (typeof (UriFormatException))]
1427 public void InvalidPort1 ()
1429 Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1430 Assert.AreEqual (65536, uri.Port);
1433 [ExpectedException (typeof (UriFormatException))]
1435 public void InvalidPort2 ()
1437 // UInt32.MaxValue gives port == -1 !!!
1438 Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1439 Assert.AreEqual (-1, uri.Port);
1442 [ExpectedException (typeof (UriFormatException))]
1444 public void InvalidPort3 ()
1446 // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1447 Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1448 Assert.AreEqual (-2147483648, uri.Port);
1452 public void PortMax ()
1454 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1455 Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1456 Assert.AreEqual (65535, uri.Port);
1461 public UriEx2 (string s) : base (s)
1465 protected override void Parse ()
1470 // Parse method is no longer used on .NET 2.0
1471 [ExpectedException (typeof (UriFormatException))]
1473 public void ParseOverride ()
1475 // If this does not override base's Parse(), it will
1476 // fail since this argument is not Absolute URI.
1477 UriEx2 ex = new UriEx2 ("readme.txt");
1481 public void UnixLocalPath ()
1483 // This works--the location is not part of the absolute path
1484 string path = "file://localhost/tmp/foo/bar";
1485 Uri fileUri = new Uri( path );
1486 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1488 // Empty path == localhost, in theory
1489 path = "file:///c:/tmp/foo/bar";
1490 fileUri = new Uri( path );
1491 Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1495 public void WindowsLocalPath ()
1497 new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1501 public void TestEscapeDataString ()
1503 StringBuilder sb = new StringBuilder ();
1505 for (int i = 0; i < 128; i++)
1506 sb.Append ((char) i);
1510 "%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",
1511 Uri.EscapeDataString (sb.ToString ()));
1514 "%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",
1515 Uri.EscapeDataString (sb.ToString ()));
1518 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1521 public void TestEscapeUriString ()
1523 StringBuilder sb = new StringBuilder ();
1524 for (int i = 0; i < 128; i++)
1525 sb.Append ((char) i);
1529 "%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",
1530 Uri.EscapeUriString (sb.ToString ()));
1533 "%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",
1534 Uri.EscapeUriString (sb.ToString ()));
1537 Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1542 public void TestUTF8Strings ()
1545 "file:///tmp/x (%232).jpg",
1546 "file:///tmp/ü (%232).jpg" };
1548 foreach (string test in tests) {
1549 Uri uri = new Uri (test);
1550 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1551 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1552 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1556 // This test doesn't work on Linux, and arguably shouldn't work.
1557 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1558 // on Linux, as anyone sane would expect. It *doesn't* under .NET 1.1
1559 // Apparently "tmp" is supposed to be a hostname (!)...
1560 // Since "correct" behavior would confuse all Linux developers, and having
1561 // an expected failure is evil, we'll just ignore this for now...
1563 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1565 // You are surrounded by conditional-compilation code, all alike.
1566 // You are likely to be eaten by a Grue...
1568 public void UnixLocalPath_WTF ()
1570 // Empty path == localhost, in theory
1571 string path = "file:///tmp/foo/bar";
1572 Uri fileUri = new Uri( path );
1573 Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1576 string path2 = "file:///foo%25bar";
1577 fileUri = new Uri (path2);
1578 Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1581 public static void Print (Uri uri)
1583 Console.WriteLine ("ToString: " + uri.ToString ());
1585 Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1586 Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1587 Console.WriteLine ("Authority: " + uri.Authority);
1588 Console.WriteLine ("Fragment: " + uri.Fragment);
1589 Console.WriteLine ("Host: " + uri.Host);
1590 Console.WriteLine ("HostNameType: " + uri.HostNameType);
1591 Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1592 Console.WriteLine ("IsFile: " + uri.IsFile);
1593 Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1594 Console.WriteLine ("IsUnc: " + uri.IsUnc);
1595 Console.WriteLine ("LocalPath: " + uri.LocalPath);
1596 Console.WriteLine ("PathAndQuery : " + uri.PathAndQuery);
1597 Console.WriteLine ("Port: " + uri.Port);
1598 Console.WriteLine ("Query: " + uri.Query);
1599 Console.WriteLine ("Scheme: " + uri.Scheme);
1600 Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1601 Console.WriteLine ("UserInfo: " + uri.UserInfo);
1603 Console.WriteLine ("Segments:");
1604 string [] segments = uri.Segments;
1605 if (segments == null)
1606 Console.WriteLine ("\tNo Segments");
1608 for (int i = 0; i < segments.Length; i++)
1609 Console.WriteLine ("\t" + segments[i]);
1610 Console.WriteLine ("");
1614 public void FtpRootPath ()
1616 Uri u = new Uri ("ftp://a.b/%2fabc/def");
1617 string p = u.PathAndQuery;
1618 Assert.AreEqual ("/%2fabc/def", p);
1619 p = Uri.UnescapeDataString (p).Substring (1);
1620 Assert.AreEqual ("/abc/def", p);
1621 u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1622 Assert.AreEqual ("/abc/def", u.PathAndQuery);
1627 public void LocalPath_FileNameWithAtSign1 ()
1629 string path = "/some/path/file_with_an_@_sign.mp3";
1630 string fullpath = "http://thehost" + path;
1631 Uri fileUri = new Uri (fullpath);
1633 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1634 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1635 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1636 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1637 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1639 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1640 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1641 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1642 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1646 public void LocalPath_FileNameWithAtSign2 ()
1648 string path = "/some/path/file_with_an_@_sign.mp3";
1649 string fullpath = "http://user:password@thehost" + path;
1650 Uri fileUri = new Uri (fullpath);
1652 Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1653 Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1654 Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1655 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1656 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1658 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1659 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1660 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1661 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1665 public void LocalPath_FileNameWithAtSign3 ()
1667 string path = "/some/path/file_with_an_@_sign.mp3";
1668 string fullpath = "file://" + path;
1669 Uri fileUri = new Uri (fullpath);
1671 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1672 Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1673 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1674 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1675 Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1677 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1678 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1679 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1680 Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1684 public void LocalPath_FileNameWithAtSign4 ()
1686 string path = "/some/path/file_with_an_@_sign.mp3";
1687 string fullpath = "file://localhost" + path;
1688 Uri fileUri = new Uri (fullpath);
1690 Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1691 Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1692 Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1693 Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1694 Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1696 Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1697 Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1698 Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1699 //this test is marked as NotWorking below:
1700 //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1704 public void LocalPath_FileNameWithAtSign5 ()
1706 string path = "/some/path/file_with_an_@_sign.mp3";
1707 string fullpath = "file://localhost" + path;
1708 Uri fileUri = new Uri (fullpath);
1710 string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1711 Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1715 [ExpectedException (typeof (UriFormatException))]
1716 public void LocalPath_FileNameWithAtSign6 ()
1718 string path = "/some/path/file_with_an_@_sign.mp3";
1719 string fullpath = "file://user:password@localhost" + path;
1720 Uri fileUri = new Uri (fullpath);
1724 [Category ("NotDotNet")]
1725 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1727 Uri unixuri = new Uri ("/home/user/a@b");
1728 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1732 [Category ("NotDotNet")]
1733 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1735 Uri unixuri = new Uri ("/home/user/a:b");
1736 Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1740 public void RelativeUriWithColons ()
1742 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
1743 new Uri (s, UriKind.Relative);
1744 new Uri (":", UriKind.Relative);
1745 new Uri ("1:", UriKind.Relative);
1749 public void ConsecutiveSlashes ()
1751 Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1752 Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1755 public class DerivedUri : Uri {
1756 public DerivedUri (string uriString)
1761 internal string TestUnescape (string path)
1763 return base.Unescape (path);
1768 [ExpectedException (typeof (InvalidOperationException))]
1769 public void GetComponents_Relative ()
1771 Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1772 rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1776 public void GetComponents_AbsoluteUri ()
1778 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1780 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1782 string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1783 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1785 string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1786 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1788 string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1789 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1793 public void GetComponents_HttpRequestUrl ()
1795 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1797 string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1798 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1800 string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1801 Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1803 string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1804 Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1808 public void GetComponents_KeepDelimiter ()
1810 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1812 string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1813 Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1815 string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1816 Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1818 string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1819 Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1823 public void GetComponents_StrongAuthority ()
1825 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1827 string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1828 Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1830 string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1831 Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1833 string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1834 Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1838 public void GetComponents_Path ()
1840 Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1841 Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1843 string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1844 Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1846 string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1847 Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1849 string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1850 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1852 // same result is unescaped original string
1853 Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1854 Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1856 safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1857 Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1859 unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1860 Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1862 escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1863 Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1867 public void GetComponents_PathAndQuery ()
1869 Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1871 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1873 string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1874 Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1876 string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1877 Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1879 string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1880 Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1884 public void GetComponents_Query ()
1886 Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1888 Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1890 string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1891 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1893 string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1894 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1896 string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1897 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1901 public void GetComponents_Fragment ()
1903 Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1905 Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1907 string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1908 Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1910 string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1911 Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1913 string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1914 Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1917 // When used, paths such as "/foo" are assumed relative.
1918 static UriKind DotNetRelativeOrAbsolute = (Type.GetType ("Mono.Runtime") == null)? UriKind.RelativeOrAbsolute : (UriKind) 300;
1921 public void DotNetRelativeOrAbsoluteTest ()
1923 FieldInfo useDotNetRelativeOrAbsoluteField = null;
1924 bool useDotNetRelativeOrAbsoluteOld = false;
1926 if (Type.GetType ("Mono.Runtime") != null) {
1927 useDotNetRelativeOrAbsoluteField = typeof (Uri).GetField ("useDotNetRelativeOrAbsolute",
1928 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
1929 useDotNetRelativeOrAbsoluteOld = (bool) useDotNetRelativeOrAbsoluteField.GetValue (null);
1930 useDotNetRelativeOrAbsoluteField.SetValue (null, false);
1936 uri = new Uri ("/foo", DotNetRelativeOrAbsolute);
1937 Assert.IsFalse (uri.IsAbsoluteUri);
1939 Uri.TryCreate("/foo", DotNetRelativeOrAbsolute, out uri);
1940 Assert.IsFalse (uri.IsAbsoluteUri);
1942 if (useDotNetRelativeOrAbsoluteField != null) {
1943 uri = new Uri ("/foo", UriKind.RelativeOrAbsolute);
1944 Assert.IsTrue (uri.IsAbsoluteUri);
1946 Uri.TryCreate("/foo", UriKind.RelativeOrAbsolute, out uri);
1947 Assert.IsTrue (uri.IsAbsoluteUri);
1949 useDotNetRelativeOrAbsoluteField.SetValue (null, true);
1952 uri = new Uri ("/foo", UriKind.RelativeOrAbsolute);
1953 Assert.IsFalse (uri.IsAbsoluteUri);
1955 Uri.TryCreate("/foo", DotNetRelativeOrAbsolute, out uri);
1956 Assert.IsFalse (uri.IsAbsoluteUri);
1958 if (useDotNetRelativeOrAbsoluteField != null)
1959 useDotNetRelativeOrAbsoluteField.SetValue (null, useDotNetRelativeOrAbsoluteOld);
1965 public void LocalPathWithBaseUrl ()
1967 var mainUri = new Uri ("http://www.imdb.com");
1968 var uriPath = "/title/tt0106521";
1971 Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1972 Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
1976 public void GetSerializationInfoStringOnRelativeUri ()
1978 var uri = new Uri ("/relative/path", UriKind.Relative);
1979 var result = uri.GetComponents (UriComponents.SerializationInfoString, UriFormat.UriEscaped);
1981 Assert.AreEqual (uri.OriginalString, result);
1985 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1986 public void GetSerializationInfoStringException ()
1988 var uri = new Uri ("/relative/path", UriKind.Relative);
1989 uri.GetComponents (UriComponents.SerializationInfoString | UriComponents.Host, UriFormat.UriEscaped);
1993 public void UserInfo_EscapedLetter ()
1995 var uri = new Uri ("https://first%61second@host");
1996 Assert.AreEqual ("firstasecond", uri.UserInfo);
2000 public void UserInfo_EscapedAt ()
2002 var userinfo = "first%40second";
2003 var uri = new Uri ("https://" + userinfo + "@host");
2004 Assert.AreEqual (userinfo, uri.UserInfo);
2008 public void UserInfo_EscapedChars ()
2010 for (var ch = (char) 1; ch < 128; ch++) {
2011 var userinfo = Uri.EscapeDataString (ch.ToString ());
2013 new Uri (string.Format("http://{0}@localhost:80/", userinfo));
2014 } catch (Exception e) {
2015 Assert.Fail (string.Format("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
2021 public void UserInfo_Spaces ()
2023 const string userinfo = "test 1:pass 1";
2024 const string expected = "test%201:pass%201";
2027 var uri = new Uri (string.Format ("rtmp://{0}@test.com:333/live", userinfo));
2028 Assert.AreEqual (expected, uri.UserInfo);
2029 } catch (Exception e) {
2030 Assert.Fail (string.Format ("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
2035 // Tests non default IriParsing
2037 public class UriTestAux : UriTest
2039 private FieldInfo iriParsingField;
2040 private bool originalIriParsing;
2043 public void GetReady2 ()
2045 isWin32 = (Path.DirectorySeparatorChar == '\\');
2047 //Make sure Uri static constructor is called
2048 Uri.EscapeDataString ("");
2050 iriParsingField = typeof (Uri).GetField ("s_IriParsing",
2051 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
2053 originalIriParsing = (bool) iriParsingField.GetValue (null);
2055 IriParsing = !originalIriParsing;
2057 iriParsingField.SetValue (null, IriParsing);
2060 [TestFixtureTearDown]
2061 public void TearDown ()
2063 iriParsingField.SetValue (null, originalIriParsing);