Resolve "you are registering twice the same counter address" warning on Windows Relea...
[mono.git] / mcs / class / System / Test / System / UriTest.cs
1 //
2 // UriTest.cs - NUnit Test Cases for System.Uri
3 //
4 // Authors:
5 //   Lawrence Pit (loz@cable.a2000.nl)
6 //   Martin Willemoes Hansen (mwh@sysrq.dk)
7 //   Ben Maurer (bmaurer@users.sourceforge.net)
8 //
9 // (C) 2003 Martin Willemoes Hansen
10 // (C) 2003 Ben Maurer
11 //
12
13 using System.Reflection;
14 using NUnit.Framework;
15 using System;
16 using System.IO;
17 using System.Text;
18
19 namespace MonoTests.System
20 {
21         [TestFixture]
22         public class UriTest
23         {
24                 bool isWin32;
25
26                 [TestFixtureSetUp]
27                 public void GetReady ()
28                 {
29                         isWin32 = (Path.DirectorySeparatorChar == '\\');
30                 }
31
32                 [Test]
33                 public void Constructors ()
34                 {
35                         Uri uri = null;
36
37                         /*
38                         uri = new Uri ("http://www.ximian.com/foo" + ((char) 0xa9) + "/bar/index.cgi?a=1&b=" + ((char) 0xa9) + "left#fragm?ent2");
39                         Print (uri);
40
41                         uri = new Uri ("http://www.ximian.com/foo/xxx\"()-._;<=>@{|}~-,.`_^]\\[xx/" + ((char) 0xa9) + "/bar/index.cgi#fra+\">=@[gg]~gment2");
42                         Print (uri);
43
44                         uri = new Uri("http://11.22.33.588:9090");
45                         Print (uri);
46
47                         uri = new Uri("http://[11:22:33::88]:9090");
48                         Print (uri);
49
50                         uri = new Uri("http://[::127.11.22.33]:8080");
51                         Print (uri);
52
53                         uri = new Uri("http://[abcde::127.11.22.33]:8080");
54                         Print (uri);
55                         */
56
57                         /*
58                         uri = new Uri ("http://www.contoso.com:1234/foo/bar/");
59                         Print (uri);
60
61                         uri = new Uri ("http://www.contoso.com:1234/foo/bar");
62                         Print (uri);
63
64                         uri = new Uri ("http://www.contoso.com:1234/");
65                         Print (uri);
66
67                         uri = new Uri ("http://www.contoso.com:1234");
68                         Print (uri);
69                         */
70
71                         uri = new Uri("  \r  \n http://test.com\r\n \r\r  ");
72                         Assert.AreEqual ("http://test.com/", uri.ToString(), "#k0");
73                         Assert.AreEqual ("http", uri.GetComponents (UriComponents.Scheme, UriFormat.UriEscaped), "#k0-gc");
74
75                         uri = new Uri ("http://contoso.com?subject=uri");
76                         Assert.AreEqual ("/", uri.AbsolutePath, "#k1");
77                         Assert.AreEqual ("http://contoso.com/?subject=uri", uri.AbsoluteUri, "#k2");
78                         Assert.AreEqual ("contoso.com", uri.Authority, "#k3");
79                         Assert.AreEqual ("", uri.Fragment, "#k4");
80                         Assert.AreEqual ("contoso.com", uri.Host, "#k5");
81                         Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#k6");
82                         Assert.AreEqual (true, uri.IsDefaultPort, "#k7");
83                         Assert.AreEqual (false, uri.IsFile, "#k8");
84                         Assert.AreEqual (false, uri.IsLoopback, "#k9");
85                         Assert.AreEqual (false, uri.IsUnc, "#k10");
86                         Assert.AreEqual ("/", uri.LocalPath, "#k11");
87                         Assert.AreEqual ("/?subject=uri", uri.PathAndQuery, "#k12");
88                         Assert.AreEqual (80, uri.Port, "#k13");
89                         Assert.AreEqual ("?subject=uri", uri.Query, "#k14");
90                         Assert.AreEqual ("http", uri.Scheme, "#k15");
91                         Assert.AreEqual (false, uri.UserEscaped, "#k16");
92                         Assert.AreEqual ("", uri.UserInfo, "#k17");
93
94                         uri = new Uri ("mailto:user:pwd@contoso.com?subject=uri");
95                         Assert.AreEqual ("", uri.AbsolutePath, "#m1");
96                         Assert.AreEqual ("mailto:user:pwd@contoso.com?subject=uri", uri.AbsoluteUri, "#m2");
97                         Assert.AreEqual ("contoso.com", uri.Authority, "#m3");
98                         Assert.AreEqual ("", uri.Fragment, "#m4");
99                         Assert.AreEqual ("contoso.com", uri.Host, "#m5");
100                         Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#m6");
101                         Assert.AreEqual (true, uri.IsDefaultPort, "#m7");
102                         Assert.AreEqual (false, uri.IsFile, "#m8");
103                         Assert.AreEqual (false, uri.IsLoopback, "#m9");
104                         Assert.AreEqual (false, uri.IsUnc, "#m10");
105                         Assert.AreEqual ("", uri.LocalPath, "#m11");
106                         Assert.AreEqual ("?subject=uri", uri.PathAndQuery, "#m12");
107                         Assert.AreEqual (25, uri.Port, "#m13");
108                         Assert.AreEqual ("?subject=uri", uri.Query, "#m14");
109                         Assert.AreEqual ("mailto", uri.Scheme, "#m15");
110                         Assert.AreEqual (false, uri.UserEscaped, "#m16");
111                         Assert.AreEqual ("user:pwd", uri.UserInfo, "#m17");
112
113                         uri = new Uri("myscheme://127.0.0.1:5");
114                         Assert.AreEqual ("myscheme://127.0.0.1:5/", uri.ToString(), "#c1");
115
116                         uri = new Uri (@"\\myserver\mydir\mysubdir\myfile.ext");
117                         Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.AbsolutePath, "#n1");
118                         Assert.AreEqual ("file://myserver/mydir/mysubdir/myfile.ext", uri.AbsoluteUri, "#n2");
119                         Assert.AreEqual ("myserver", uri.Authority, "#n3");
120                         Assert.AreEqual ("", uri.Fragment, "#n4");
121                         Assert.AreEqual ("myserver", uri.Host, "#n5");
122                         Assert.AreEqual (UriHostNameType.Dns, uri.HostNameType, "#n6");
123                         Assert.AreEqual (true, uri.IsDefaultPort, "#n7");
124                         Assert.AreEqual (true, uri.IsFile, "#n8");
125                         Assert.AreEqual (false, uri.IsLoopback, "#n9");
126                         Assert.AreEqual (true, uri.IsUnc, "#n10");
127
128                         if (isWin32)
129                                 Assert.AreEqual (@"\\myserver\mydir\mysubdir\myfile.ext", uri.LocalPath, "#n11");
130                         else
131                                 // myserver never could be the part of Unix path.
132                                 Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.LocalPath, "#n11");
133
134                         Assert.AreEqual ("/mydir/mysubdir/myfile.ext", uri.PathAndQuery, "#n12");
135                         Assert.AreEqual (-1, uri.Port, "#n13");
136                         Assert.AreEqual ("", uri.Query, "#n14");
137                         Assert.AreEqual ("file", uri.Scheme, "#n15");
138                         Assert.AreEqual (false, uri.UserEscaped, "#n16");
139                         Assert.AreEqual ("", uri.UserInfo, "#n17");
140
141                         uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", true);
142                         Assert.AreEqual ("http://www.contoso.com/Hello World.htm", uri.AbsoluteUri, "#rel1a");
143                         Assert.AreEqual (true, uri.UserEscaped, "#rel1b");
144                         uri = new Uri (new Uri("http://www.contoso.com"), "Hello World.htm", false);
145                         Assert.AreEqual ("http://www.contoso.com/Hello%20World.htm", uri.AbsoluteUri, "#rel2a");
146                         Assert.AreEqual (false, uri.UserEscaped, "#rel2b");
147                         uri = new Uri (new Uri("http://www.contoso.com"), "http://www.xxx.com/Hello World.htm", false);
148                         Assert.AreEqual ("http://www.xxx.com/Hello%20World.htm", uri.AbsoluteUri, "#rel3");
149
150                         uri = new Uri (new Uri("http://www.contoso.com"), "foo/bar/Hello World.htm?x=0:8", false);
151                         Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel5");
152                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "foo/bar/Hello World.htm?x=0:8", false);
153                         Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel6");
154                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "/foo/bar/Hello World.htm?x=0:8", false);
155                         Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel7");
156                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../foo/bar/Hello World.htm?x=0:8", false);
157                         Assert.AreEqual ("http://www.contoso.com/xxx/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel8");
158                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "../../../foo/bar/Hello World.htm?x=0:8", false);
159                         Assert.AreEqual ("http://www.contoso.com/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel9");
160                         Assert.AreEqual ("/foo/bar/Hello%20World.htm", uri.AbsolutePath, "#rel9-path");
161
162                         uri = new Uri (new Uri("http://www.contoso.com/xxx/yyy/index.htm"), "./foo/bar/Hello World.htm?x=0:8", false);
163                         Assert.AreEqual ("http://www.contoso.com/xxx/yyy/foo/bar/Hello%20World.htm?x=0:8", uri.AbsoluteUri, "#rel10");
164
165                         uri = new Uri (new Uri("http://www.contoso.com/foo/bar/index.html?x=0"), String.Empty, false);
166                         Assert.AreEqual ("http://www.contoso.com/foo/bar/index.html?x=0", uri.ToString (), "#22");
167
168                         uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
169                         Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel30");
170                         uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
171                         Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
172
173                         uri = new Uri ("relative", UriKind.Relative);
174                         uri = new Uri ("relative/abc", UriKind.Relative);
175                         uri = new Uri ("relative", UriKind.RelativeOrAbsolute);
176
177                         Assert.IsTrue (!uri.IsAbsoluteUri, "#rel33");
178                         Assert.AreEqual (uri.OriginalString, "relative", "#rel34");
179                         Assert.IsTrue (!uri.UserEscaped, "#rel35");
180                 }
181
182                 [Test]
183                 public void Constructor_DualHostPort ()
184                 {
185                         string relative = "foo:8080/bar/Hello World.htm";
186                         Uri uri = new Uri (new Uri("http://www.contoso.com"), relative, false);
187                         Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.AbsolutePath, "AbsolutePath");
188                         Assert.AreEqual ("foo:8080/bar/Hello%20World.htm", uri.AbsoluteUri, "AbsoluteUri");
189                         Assert.AreEqual (String.Empty, uri.Authority, "Authority");
190                         Assert.AreEqual (String.Empty, uri.Fragment, "Fragment");
191                         Assert.AreEqual (String.Empty, uri.Host, "Host");
192                         Assert.AreEqual ("8080/bar/Hello%20World.htm", uri.PathAndQuery, "PathAndQuery");
193                         Assert.AreEqual (-1, uri.Port, "Port");
194                         Assert.AreEqual (String.Empty, uri.Query, "Query");
195                         Assert.AreEqual ("foo", uri.Scheme, "Scheme");
196                         Assert.AreEqual (String.Empty, uri.UserInfo, "Query");
197
198                         Assert.AreEqual ("8080/", uri.Segments[0], "Segments[0]");
199                         Assert.AreEqual ("bar/", uri.Segments[1], "Segments[1]");
200                         Assert.AreEqual ("Hello%20World.htm", uri.Segments[2], "Segments[2]");
201
202                         Assert.IsTrue (uri.IsDefaultPort, "IsDefaultPort");
203                         Assert.IsTrue (!uri.IsFile, "IsFile");
204                         Assert.IsTrue (!uri.IsLoopback, "IsLoopback");
205                         Assert.IsTrue (!uri.IsUnc, "IsUnc");
206                         Assert.IsTrue (!uri.UserEscaped, "UserEscaped");
207
208                         Assert.AreEqual (UriHostNameType.Unknown, uri.HostNameType, "HostNameType");
209                         Assert.IsTrue (uri.IsAbsoluteUri, "IsAbsoluteUri");
210                         Assert.AreEqual (relative, uri.OriginalString, "OriginalString");
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (ArgumentNullException))]
215                 public void Constructor_NullStringBool ()
216                 {
217                         new Uri (null, "http://www.contoso.com/index.htm", false);
218                 }
219
220                 [Test]
221                 public void Constructor_UriNullBool ()
222                 {
223                         new Uri (new Uri ("http://www.contoso.com"), null, false);
224                 }
225
226                 // regression for bug #47573
227                 [Test]
228                 public void RelativeCtor ()
229                 {
230                         Uri b = new Uri ("http://a/b/c/d;p?q");
231                         Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
232                         Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
233                         Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
234
235                         Uri u = new Uri (b, "/g?q=r");
236                         Assert.AreEqual ("http://a/g?q=r", u.ToString (), "#5");
237                         Assert.AreEqual ("?q=r", u.Query, "#6");
238
239                         u = new Uri (b, "/g?q=r;. a");
240                         Assert.AreEqual ("http://a/g?q=r;. a", u.ToString (), "#5");
241                         Assert.AreEqual ("?q=r;.%20a", u.Query, "#6");
242                 }
243
244
245                 [Test]
246                 [ExpectedException (typeof (UriFormatException))]
247                 public void RelativeCtor_11_Crasher ()
248                 {
249                         Uri b = new Uri ("http://a/b/c/d;p?q");
250                         // this causes crash under MS.NET 1.1
251                         Assert.AreEqual ("g:h", new Uri (b, "g:h").ToString (), "g:h");
252                 }
253
254                 [Test]
255                 [ExpectedException (typeof (UriFormatException))]
256                 public void Bad_IPv6 ()
257                 {
258                         new Uri ("http://0:0:0:0::127.1.2.3]/");
259                 }
260
261                 [Test]
262                 public void LeadingSlashes_ShouldFailOn1x ()
263                 {
264                         // doesn't (but should) fail under 1.x
265                         Assert.AreEqual ("file:///", new Uri ("file:///").ToString (), "#1");
266                         Assert.AreEqual ("file:///", new Uri ("file://").ToString (), "#2");
267                 }
268
269                 [Test]
270                 public void LeadingSlashes_BadResultsOn1x ()
271                 {
272                         // strange behaviours of 1.x - it's probably not worth to fix it
273                         // on Mono as 2.0 has been fixed
274                         Uri u = new Uri ("file:///foo/bar");
275                         Assert.AreEqual (String.Empty, u.Host, "#3a");
276                         Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#3b");
277                         Assert.AreEqual ("file:///foo/bar", u.ToString (), "#3c");
278                         Assert.AreEqual (false, u.IsUnc, "#3d");
279
280                         u = new Uri ("mailto:/foo");
281                         Assert.AreEqual (String.Empty, u.Host, "#13a");
282                         Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#13b");
283                         Assert.AreEqual ("mailto:/foo", u.ToString (), "#13c");
284
285                         u = new Uri ("mailto://foo");
286                         Assert.AreEqual (String.Empty, u.Host, "#14a");
287                         Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#14b");
288                         Assert.AreEqual ("mailto://foo", u.ToString (), "#14c");
289
290                         u = new Uri ("news:/");
291                         Assert.AreEqual (String.Empty, u.Host, "#18a");
292                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#18b");
293                         Assert.AreEqual ("news:/", u.ToString (), "#18c");
294                         Assert.AreEqual ("/", u.AbsolutePath, "#18d");
295                         Assert.AreEqual ("news:/", u.AbsoluteUri, "#18e");
296
297                         u = new Uri ("news:/foo");
298                         Assert.AreEqual (String.Empty, u.Host, "#19a");
299                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#19b");
300                         Assert.AreEqual ("news:/foo", u.ToString (), "#19c");
301                         Assert.AreEqual ("/foo", u.AbsolutePath, "#19d");
302                         Assert.AreEqual ("news:/foo", u.AbsoluteUri, "#19e");
303
304                         u = new Uri ("news://foo");
305                         Assert.AreEqual (String.Empty, u.Host, "#20a");
306                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#20b");
307                         Assert.AreEqual ("news://foo", u.ToString (), "#20c");
308                         Assert.AreEqual ("//foo", u.AbsolutePath, "#20d");
309                         Assert.AreEqual ("news://foo", u.AbsoluteUri, "#20e");
310
311                         u = new Uri ("news://foo/bar");
312                         Assert.AreEqual (String.Empty, u.Host, "#22a");
313                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#22b");
314                         Assert.AreEqual ("news://foo/bar", u.ToString (), "#22c");
315                         Assert.AreEqual ("//foo/bar", u.AbsolutePath, "#22d");
316                         Assert.AreEqual ("news://foo/bar", u.AbsoluteUri, "#22e");
317                 }
318
319                 [Test]
320                 public void LeadingSlashes_FailOn1x ()
321                 {
322                         // 1.x throws an UriFormatException because it can't decode the host name
323                         Uri u = new Uri ("mailto:");
324                         Assert.AreEqual (String.Empty, u.Host, "#10a");
325                         Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#10b");
326                         Assert.AreEqual ("mailto:", u.ToString (), "#10c");
327
328                         // 1.x throws an UriFormatException because it can't decode the host name
329                         u = new Uri ("mailto:/");
330                         Assert.AreEqual (String.Empty, u.Host, "#12a");
331                         Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#12b");
332                         Assert.AreEqual ("mailto:/", u.ToString (), "#12c");
333
334                         // 1.x throws an UriFormatException because it cannot detect the format
335                         u = new Uri ("mailto:///foo");
336                         Assert.AreEqual (String.Empty, u.Host, "#15a");
337                         Assert.AreEqual (UriHostNameType.Basic, u.HostNameType, "#15b");
338                         Assert.AreEqual ("mailto:///foo", u.ToString (), "#15c");
339
340                         // 1.x throws an UriFormatException because it cannot detect the format
341                         u = new Uri ("news:///foo");
342                         Assert.AreEqual (String.Empty, u.Host, "#21a");
343                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#21b");
344                         Assert.AreEqual ("news:///foo", u.ToString (), "#21c");
345                         Assert.AreEqual ("///foo", u.AbsolutePath, "#21d");
346                         Assert.AreEqual ("news:///foo", u.AbsoluteUri, "#21e");
347                 }
348
349                 [Test]
350                 // some tests from bug 75144
351                 public void LeadingSlashes ()
352                 {
353                         Uri u = new Uri ("file://foo/bar");
354                         Assert.AreEqual ("foo", u.Host, "#5a");
355                         Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#5b");
356                         Assert.AreEqual ("file://foo/bar", u.ToString (), "#5c");
357                         Assert.AreEqual (isWin32, u.IsUnc, "#5d");
358
359                         u = new Uri ("file:////foo/bar");
360                         Assert.AreEqual ("foo", u.Host, "#7a");
361                         Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#7b");
362                         Assert.AreEqual ("file://foo/bar", u.ToString (), "#7c");
363                         Assert.AreEqual (isWin32, u.IsUnc, "#7d");
364
365                         Assert.AreEqual ("file://foo/bar", new Uri ("file://///foo/bar").ToString(), "#9");
366
367                         u = new Uri ("mailto:foo");
368                         Assert.AreEqual ("foo", u.Host, "#11a");
369                         Assert.AreEqual (UriHostNameType.Dns, u.HostNameType, "#11b");
370                         Assert.AreEqual ("mailto:foo", u.ToString (), "#11c");
371
372                         u = new Uri ("news:");
373                         Assert.AreEqual (String.Empty, u.Host, "#16a");
374                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#16b");
375                         Assert.AreEqual ("news:", u.ToString (), "#16c");
376
377                         u = new Uri ("news:foo");
378                         Assert.AreEqual (String.Empty, u.Host, "#17a");
379                         Assert.AreEqual (UriHostNameType.Unknown, u.HostNameType, "#17b");
380                         Assert.AreEqual ("news:foo", u.ToString (), "#17c");
381                         Assert.AreEqual ("foo", u.AbsolutePath, "#17d");
382                         Assert.AreEqual ("news:foo", u.AbsoluteUri, "#17e");
383                 }
384
385                 [Test]
386                 [ExpectedException (typeof (UriFormatException))]
387                 public void HttpHostname1 ()
388                 {
389                         new Uri ("http:");
390                 }
391
392                 [Test]
393                 [ExpectedException (typeof (UriFormatException))]
394                 public void HttpHostname2 ()
395                 {
396                         new Uri ("http:a");
397                 }
398
399                 [Test]
400                 [ExpectedException (typeof (UriFormatException))]
401                 public void HttpHostname3 ()
402                 {
403                         new Uri ("http:/");
404                 }
405
406                 [Test]
407                 [ExpectedException (typeof (UriFormatException))]
408                 public void HttpHostname4 ()
409                 {
410                         new Uri ("http:/foo");
411                 }
412
413                 [Test]
414                 [ExpectedException (typeof (UriFormatException))]
415                 public void HttpHostname5 ()
416                 {
417                         new Uri ("http://");
418                 }
419
420                 [Test]
421                 [ExpectedException (typeof (UriFormatException))]
422                 public void HttpHostname6 ()
423                 {
424                         new Uri ("http:///");
425                 }
426
427                 [Test]
428                 [ExpectedException (typeof (UriFormatException))]
429                 public void HttpHostname7 ()
430                 {
431                         new Uri ("http:///foo");
432                 }
433
434                 [Test]
435                 [ExpectedException (typeof (UriFormatException))]
436                 public void InvalidFile1 ()
437                 {
438                         new Uri ("file:");
439                 }
440
441                 [Test]
442                 [ExpectedException (typeof (UriFormatException))]
443                 public void InvalidFile2 ()
444                 {
445                         new Uri ("file:/");
446                 }
447
448                 [Test]
449                 [ExpectedException (typeof (UriFormatException))]
450                 public void InvalidFile3 ()
451                 {
452                         new Uri ("file:/foo");
453                 }
454
455                 [Test]
456                 [ExpectedException (typeof (UriFormatException))]
457                 public void InvalidScheme ()
458                 {
459                         new Uri ("_:/");
460                 }
461
462                 [Test]
463                 public void ConstructorsRejectRelativePath ()
464                 {
465                         string [] reluris = new string [] {
466                                 "readme.txt",
467                                 "thisdir/childdir/file",
468                                 "./testfile"
469                         };
470                         string [] winRelUris = new string [] {
471                                 "c:readme.txt"
472                         };
473
474                         for (int i = 0; i < reluris.Length; i++) {
475                                 try {
476                                         new Uri (reluris [i]);
477                                         Assert.Fail ("Should be failed: " + reluris [i]);
478                                 } catch (UriFormatException) {
479                                 }
480                         }
481
482                         if (isWin32) {
483                                 for (int i = 0; i < winRelUris.Length; i++) {
484                                         try {
485                                                 new Uri (winRelUris [i]);
486                                                 Assert.Fail ("Should be failed: " + winRelUris [i]);
487                                         } catch (UriFormatException) {
488                                         }
489                                 }
490                         }
491                 }
492
493                 [Test]
494                 public void LocalPath ()
495                 {
496                         Uri uri = new Uri ("c:\\tmp\\hello.txt");
497                         Assert.AreEqual ("file:///c:/tmp/hello.txt", uri.ToString (), "#1a");
498                         Assert.AreEqual ("c:\\tmp\\hello.txt", uri.LocalPath, "#1b");
499                         Assert.AreEqual ("file", uri.Scheme, "#1c");
500                         Assert.AreEqual ("", uri.Host, "#1d");
501                         Assert.AreEqual ("c:/tmp/hello.txt", uri.AbsolutePath, "#1e");
502
503                         uri = new Uri ("file:////////cygwin/tmp/hello.txt");
504                         Assert.AreEqual ("file://cygwin/tmp/hello.txt", uri.ToString (), "#3a");
505                         if (isWin32)
506                                 Assert.AreEqual ("\\\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#3b win32");
507                         else
508                                 Assert.AreEqual ("/tmp/hello.txt", uri.LocalPath, "#3b *nix");
509                         Assert.AreEqual ("file", uri.Scheme, "#3c");
510                         Assert.AreEqual ("cygwin", uri.Host, "#3d");
511                         Assert.AreEqual ("/tmp/hello.txt", uri.AbsolutePath, "#3e");
512
513                         uri = new Uri ("file://mymachine/cygwin/tmp/hello.txt");
514                         Assert.AreEqual ("file://mymachine/cygwin/tmp/hello.txt", uri.ToString (), "#4a");
515                         if (isWin32)
516                                 Assert.AreEqual ("\\\\mymachine\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#4b win32");
517                         else
518                                 Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.LocalPath, "#4b *nix");
519                         Assert.AreEqual ("file", uri.Scheme, "#4c");
520                         Assert.AreEqual ("mymachine", uri.Host, "#4d");
521                         Assert.AreEqual ("/cygwin/tmp/hello.txt", uri.AbsolutePath, "#4e");
522
523                         uri = new Uri ("file://///c:/cygwin/tmp/hello.txt");
524                         Assert.AreEqual ("file:///c:/cygwin/tmp/hello.txt", uri.ToString (), "#5a");
525                         Assert.AreEqual ("c:\\cygwin\\tmp\\hello.txt", uri.LocalPath, "#5b");
526                         Assert.AreEqual ("file", uri.Scheme, "#5c");
527                         Assert.AreEqual ("", uri.Host, "#5d");
528                         Assert.AreEqual ("c:/cygwin/tmp/hello.txt", uri.AbsolutePath, "#5e");
529                 }
530
531                 [Test]
532                 public void LocalPath_FileHost ()
533                 {
534                         // Hmm, they should be regarded just as a host name, since all URIs are base on absolute path.
535                         Uri uri = new Uri("file://one_file.txt");
536                         Assert.AreEqual ("file://one_file.txt/", uri.ToString(), "#6a");
537                         Assert.AreEqual ("/", uri.AbsolutePath, "#6e");
538                         Assert.AreEqual ("/", uri.PathAndQuery, "#6f");
539                         Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#6g");
540                         if (isWin32)
541                                 Assert.AreEqual ("\\\\one_file.txt", uri.LocalPath, "#6b");
542                         else
543                                 Assert.AreEqual ("/", uri.LocalPath, "#6b");
544                         Assert.AreEqual ("file", uri.Scheme, "#6c");
545                         Assert.AreEqual ("one_file.txt", uri.Host, "#6d");
546
547                         // same tests - but original Uri is now ending with a '/'
548
549                         uri = new Uri ("file://one_file.txt/");
550                         Assert.AreEqual ("file://one_file.txt/", uri.ToString (), "#7a");
551                         Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
552                         Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
553                         Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
554                         if (isWin32)
555                                 Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
556                         else
557                                 Assert.AreEqual ("/", uri.LocalPath, "#7b");
558                         Assert.AreEqual ("file", uri.Scheme, "#7c");
559                         Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
560                 }
561
562                 [Test]
563                 public void LocalPath_Escape ()
564                 {
565                         // escape
566                         Uri uri = new Uri ("file:///tmp/a%20a");
567                         if (isWin32) {
568                                 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/a a"), "#7a:" + uri.LocalPath);
569                         } else
570                                 Assert.AreEqual ("/tmp/a a", uri.LocalPath, "#7b");
571
572                         uri = new Uri ("file:///tmp/foo%25bar");
573                         if (isWin32) {
574                                 Assert.IsTrue (uri.LocalPath.EndsWith ("/tmp/foo%bar"), "#8a:" + uri.LocalPath);
575                                 Assert.IsTrue (uri.ToString ().EndsWith ("//tmp/foo%25bar"), "#8c:" + uri.ToString ());
576                         } else {
577                                 Assert.AreEqual ("/tmp/foo%bar", uri.LocalPath, "#8b");
578                                 Assert.AreEqual ("file:///tmp/foo%25bar", uri.ToString (), "#8d");
579                         }
580                         // bug #76643
581                         uri = new Uri ("file:///foo%25bar");
582                         if (isWin32) {
583                                 Assert.IsTrue (uri.LocalPath.EndsWith ("/foo%bar"), "#9a:" + uri.LocalPath);
584                                 // ditto, file://tmp/foo%25bar (bug in 1.x)
585                                 Assert.IsTrue (uri.ToString ().EndsWith ("//foo%25bar"), "#9c:" + uri.ToString ());
586                         } else {
587                                 Assert.AreEqual ("/foo%bar", uri.LocalPath, "#9b");
588                                 Assert.AreEqual ("file:///foo%25bar", uri.ToString (), "#9d");
589                         }
590                 }
591
592                 // Novell Bugzilla #320614
593                 [Test]
594                 public void QueryEscape ()
595                 {
596                         Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
597                         Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
598
599                         Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1.ToString (), "QE1");
600
601                         Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
602                 }
603
604                 [Test]
605                 public void UnixPath () {
606                         if (!isWin32)
607                                 Assert.AreEqual ("file:///cygwin/tmp/hello.txt", new Uri ("/cygwin/tmp/hello.txt").ToString (), "#6a");
608                 }
609
610                 [Test]
611                 public void Unc ()
612                 {
613                         Uri uri = new Uri ("http://www.contoso.com");
614                         Assert.IsTrue (!uri.IsUnc, "#1");
615
616                         uri = new Uri ("news:123456@contoso.com");
617                         Assert.IsTrue (!uri.IsUnc, "#2");
618
619                         uri = new Uri ("file://server/filename.ext");
620                         Assert.AreEqual (isWin32, uri.IsUnc, "#3");
621
622                         uri = new Uri (@"\\server\share\filename.ext");
623                         Assert.IsTrue (uri.IsUnc, "#6");
624
625                         uri = new Uri (@"a:\dir\filename.ext");
626                         Assert.IsTrue (!uri.IsUnc, "#8");
627                 }
628
629                 [Test]
630                 [Category("NotDotNet")]
631                 public void UncFail ()
632                 {
633                         if (!isWin32) {
634                                 Uri uri = new Uri ("/home/user/dir/filename.ext");
635                                 Assert.IsTrue (!uri.IsUnc, "#7");
636                         }
637                 }
638
639                 [Test]
640                 public void FromHex ()
641                 {
642                         Assert.AreEqual (0, Uri.FromHex ('0'), "#1");
643                         Assert.AreEqual (9, Uri.FromHex ('9'), "#2");
644                         Assert.AreEqual (10, Uri.FromHex ('a'), "#3");
645                         Assert.AreEqual (15, Uri.FromHex ('f'), "#4");
646                         Assert.AreEqual (10, Uri.FromHex ('A'), "#5");
647                         Assert.AreEqual (15, Uri.FromHex ('F'), "#6");
648                         try {
649                                 Uri.FromHex ('G');
650                                 Assert.Fail ("#7");
651                         } catch (ArgumentException) {}
652                         try {
653                                 Uri.FromHex (' ');
654                                 Assert.Fail ("#8");
655                         } catch (ArgumentException) {}
656                         try {
657                                 Uri.FromHex ('%');
658                                 Assert.Fail ("#8");
659                         } catch (ArgumentException) {}
660                 }
661
662                 class UriEx : Uri
663                 {
664                         public UriEx (string s) : base (s)
665                         {
666                         }
667
668                         public string UnescapeString (string s)
669                         {
670                                 return Unescape (s);
671                         }
672
673                         public static string UnescapeString (string uri, string target)
674                         {
675                                 return new UriEx (uri).UnescapeString (target);
676                         }
677                 }
678
679                 [Test]
680                 public void Unescape ()
681                 {
682                         Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
683                         Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
684                         Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
685                         Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
686                         Assert.AreEqual ("%A9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
687                         Assert.AreEqual ("%A9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
688                 }
689
690                 [Test]
691                 public void HexEscape ()
692                 {
693                         Assert.AreEqual ("%20", Uri.HexEscape (' '), "#1");
694                         Assert.AreEqual ("%A9", Uri.HexEscape ((char) 0xa9), "#2");
695                         Assert.AreEqual ("%41", Uri.HexEscape ('A'), "#3");
696                         try {
697                                 Uri.HexEscape ((char) 0x0369);
698                                 Assert.Fail ("#4");
699                         } catch (ArgumentOutOfRangeException) {}
700                 }
701
702         [Test]
703         public void MoreHexEscape()
704         {
705             string url = "http://guyc-2003-sp/wiki/wiki%20document%20library/בד%20יקה.docx";
706             string escapedAbsolutePath = "/wiki/wiki%20document%20library/%D7%91%D7%93%20%D7%99%D7%A7%D7%94.docx";
707             Uri u = new Uri(url);
708             Assert.AreEqual (escapedAbsolutePath, u.AbsolutePath, "Escaped non-english combo");
709         }
710
711                 [Test]
712                 public void HexUnescape ()
713                 {
714                         int i = 0;
715                         Assert.AreEqual (' ', Uri.HexUnescape ("%20", ref i), "#1");
716                         Assert.AreEqual (3, i, "#2");
717                         i = 4;
718                         Assert.AreEqual ((char) 0xa9, Uri.HexUnescape ("test%a9test", ref i), "#3");
719                         Assert.AreEqual (7, i, "#4");
720                         Assert.AreEqual ('t', Uri.HexUnescape ("test%a9test", ref i), "#5");
721                         Assert.AreEqual (8, i, "#6");
722                         i = 4;
723                         Assert.AreEqual ('%', Uri.HexUnescape ("test%a", ref i), "#5");
724                         Assert.AreEqual (5, i, "#6");
725                         Assert.AreEqual ('%', Uri.HexUnescape ("testx%xx", ref i), "#7");
726                         Assert.AreEqual (6, i, "#8");
727
728                         // Tests from bug 74872 - don't handle multi-byte characters as multi-byte
729                         i = 1;
730                         Assert.AreEqual (227, (int) Uri.HexUnescape ("a%E3%81%8B", ref i), "#9");
731                         Assert.AreEqual (4, i, "#10");
732                         i = 1;
733                         Assert.AreEqual (240, (int) Uri.HexUnescape ("a%F0%90%84%80", ref i), "#11");
734                         Assert.AreEqual (4, i, "#12");
735                 }
736
737                 [Test]
738                 public void HexUnescapeMultiByte ()
739                 {
740                         // Tests from bug 74872
741                         // Note: These won't pass exactly with MS.NET, due to differences in the
742                         // handling of backslashes/forwardslashes
743                         Uri uri;
744                         string path;
745
746                         // 3-byte character
747                         uri = new Uri ("file:///foo/a%E3%81%8Bb", true);
748                         path = uri.LocalPath;
749                         Assert.AreEqual (8, path.Length, "#1");
750                         Assert.AreEqual (0x304B, path [6], "#2");
751
752                         // 4-byte character which should be handled as a surrogate
753                         uri = new Uri ("file:///foo/a%F3%A0%84%80b", true);
754                         path = uri.LocalPath;
755                         Assert.AreEqual (9, path.Length, "#3");
756                         Assert.AreEqual (0xDB40, path [6], "#4");
757                         Assert.AreEqual (0xDD00, path [7], "#5");
758                         Assert.AreEqual (0x62, path [8], "#6");
759
760                         // 2-byte escape sequence, 2 individual characters
761                         uri = new Uri ("file:///foo/a%C2%F8b", true);
762                         path = uri.LocalPath;
763                         Assert.AreEqual ("/foo/a%C2%F8b", path, "#7");
764                 }
765
766                 [Test]
767                 public void IsHexDigit ()
768                 {
769                         Assert.IsTrue (Uri.IsHexDigit ('a'), "#1");
770                         Assert.IsTrue (Uri.IsHexDigit ('f'), "#2");
771                         Assert.IsTrue (!Uri.IsHexDigit ('g'), "#3");
772                         Assert.IsTrue (Uri.IsHexDigit ('0'), "#4");
773                         Assert.IsTrue (Uri.IsHexDigit ('9'), "#5");
774                         Assert.IsTrue (Uri.IsHexDigit ('A'), "#6");
775                         Assert.IsTrue (Uri.IsHexDigit ('F'), "#7");
776                         Assert.IsTrue (!Uri.IsHexDigit ('G'), "#8");
777                 }
778
779                 [Test]
780                 public void IsHexEncoding ()
781                 {
782                         Assert.IsTrue (Uri.IsHexEncoding ("test%a9test", 4), "#1");
783                         Assert.IsTrue (!Uri.IsHexEncoding ("test%a9test", 3), "#2");
784                         Assert.IsTrue (Uri.IsHexEncoding ("test%a9", 4), "#3");
785                         Assert.IsTrue (!Uri.IsHexEncoding ("test%a", 4), "#4");
786                 }
787
788                 [Test]
789                 public void GetLeftPart ()
790                 {
791                         Uri uri = new Uri ("http://www.contoso.com/index.htm#main");
792                         Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#1");
793                         Assert.AreEqual ("http://www.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#2");
794                         Assert.AreEqual ("http://www.contoso.com/index.htm", uri.GetLeftPart (UriPartial.Path), "#3");
795
796                         uri = new Uri ("mailto:user@contoso.com?subject=uri");
797                         Assert.AreEqual ("mailto:", uri.GetLeftPart (UriPartial.Scheme), "#4");
798                         Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#5");
799                         Assert.AreEqual ("mailto:user@contoso.com", uri.GetLeftPart (UriPartial.Path), "#6");
800
801                         uri = new Uri ("nntp://news.contoso.com/123456@contoso.com");
802                         Assert.AreEqual ("nntp://", uri.GetLeftPart (UriPartial.Scheme), "#7");
803                         Assert.AreEqual ("nntp://news.contoso.com", uri.GetLeftPart (UriPartial.Authority), "#8");
804                         Assert.AreEqual ("nntp://news.contoso.com/123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#9");
805
806                         uri = new Uri ("news:123456@contoso.com");
807                         Assert.AreEqual ("news:", uri.GetLeftPart (UriPartial.Scheme), "#10");
808                         Assert.AreEqual ("", uri.GetLeftPart (UriPartial.Authority), "#11");
809                         Assert.AreEqual ("news:123456@contoso.com", uri.GetLeftPart (UriPartial.Path), "#12");
810
811                         uri = new Uri ("file://server/filename.ext");
812                         Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#13");
813                         Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#14");
814                         Assert.AreEqual ("file://server/filename.ext", uri.GetLeftPart (UriPartial.Path), "#15");
815
816                         uri = new Uri (@"\\server\share\filename.ext");
817                         Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#20");
818                         Assert.AreEqual ("file://server", uri.GetLeftPart (UriPartial.Authority), "#21");
819                         Assert.AreEqual ("file://server/share/filename.ext", uri.GetLeftPart (UriPartial.Path), "#22");
820
821                         uri = new Uri ("http://www.contoso.com:8080/index.htm#main");
822                         Assert.AreEqual ("http://", uri.GetLeftPart (UriPartial.Scheme), "#23");
823                         Assert.AreEqual ("http://www.contoso.com:8080", uri.GetLeftPart (UriPartial.Authority), "#24");
824                         Assert.AreEqual ("http://www.contoso.com:8080/index.htm", uri.GetLeftPart (UriPartial.Path), "#25");
825                 }
826
827                 [Test]
828                 public void NewsDefaultPort ()
829                 {
830                         Uri uri = new Uri("news://localhost:119/");
831                         Assert.AreEqual (uri.IsDefaultPort, true, "#1");
832                 }
833
834                 [Test]
835                 public void Fragment_Escape ()
836                 {
837                         Uri u = new Uri("http://localhost/index.asp#main#start", false);
838
839                                 Assert.AreEqual (u.Fragment, "#main#start", "#1");
840
841                         u = new Uri("http://localhost/index.asp#main#start", true);
842                         Assert.AreEqual (u.Fragment, "#main#start", "#2");
843
844                         // The other code path uses a BaseUri
845
846                         Uri b = new Uri ("http://www.gnome.org");
847                         Uri n = new Uri (b, "blah#main#start");
848                                 Assert.AreEqual (n.Fragment, "#main#start", "#3");
849
850                         n = new Uri (b, "blah#main#start", true);
851                         Assert.AreEqual (n.Fragment, "#main#start", "#4");
852                 }
853
854                 [Test]
855                 public void Fragment_RelativeUri ()
856                 {
857                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
858                         Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
859                         Uri relativeUri = uri1.MakeRelativeUri (uri2);
860
861                         try {
862                                 string fragment = relativeUri.Fragment;
863                                 Assert.Fail ("#1: " + fragment);
864                         } catch (InvalidOperationException ex) {
865                                 // This operation is not supported for a relative URI
866                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
867                                 Assert.IsNull (ex.InnerException, "#3");
868                                 Assert.IsNotNull (ex.Message, "#4");
869                         }
870                 }
871
872                 [Test]
873                 [ExpectedException(typeof(UriFormatException))]
874                 public void IncompleteSchemeDelimiter ()
875                 {
876                         new Uri ("file:/filename.ext");
877                 }
878
879                 [Test]
880                 public void CheckHostName1 ()
881                 {
882                         // reported to MSDN Product Feedback Center (FDBK28671)
883                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName (":11:22:33:44:55:66:77:88"), "#36 known to fail with ms.net: this is not a valid IPv6 address.");
884                 }
885
886                 [Test]
887                 public void CheckHostName2 ()
888                 {
889                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (null), "#1");
890                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (""), "#2");
891                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("^&()~`!@"), "#3");
892                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("x"), "#4");
893                         Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("1.2.3.4"), "#5");
894                         Assert.AreEqual (UriHostNameType.IPv4, Uri.CheckHostName ("0001.002.03.4"), "#6");
895                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("0001.002.03.256"), "#7");
896                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("9001.002.03.4"), "#8");
897                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com"), "#9");
898                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (".www.contoso.com"), "#10");
899                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.contoso.com."), "#11");
900                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con-toso.com"), "#12");
901                         Assert.AreEqual (UriHostNameType.Dns, Uri.CheckHostName ("www.con_toso.com"), "#13");
902                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www.con,toso.com"), "#14");
903
904                         // test IPv6
905                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77:88"), "#15");
906                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::33:44:55:66:77:88"), "#16");
907                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::22:33:44:55:66:77:88"), "#17");
908                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22:33:44:55:66:77::"), "#18");
909                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::88"), "#19");
910                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::77:88"), "#20");
911                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11:22::88"), "#21");
912                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("11::"), "#22");
913                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::88"), "#23");
914                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::1"), "#24");
915                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::"), "#25");
916                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("0:0:0:0:0:0:127.0.0.1"), "#26");
917                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::127.0.0.1"), "#27");
918                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("::ffFF:169.32.14.5"), "#28");
919                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001:03A0::/35"), "#29");
920                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("[2001:03A0::/35]"), "#30");
921                         Assert.AreEqual (UriHostNameType.IPv6, Uri.CheckHostName ("2001::03A0:1.2.3.4"), "#33");
922
923                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001::03A0::/35"), "#31");
924                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("2001:03A0::/35a"), "#32");
925                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::ffff:123.256.155.43"), "#34");
926                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":127.0.0.1"), "#35");
927                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::11:22:33:44:55:66:77:88"), "#37");
928                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88::"), "#38");
929                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("11:22:33:44:55:66:77:88:"), "#39");
930                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::acbde"), "#40");
931                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abce:"), "#41");
932                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("::abcg"), "#42");
933                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":::"), "#43");
934                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName (":"), "#44");
935
936                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*"), "#45");
937                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("*.go-mono.com"), "#46");
938                         Assert.AreEqual (UriHostNameType.Unknown, Uri.CheckHostName ("www*.go-mono.com"), "#47");
939                 }
940
941                 [Test]
942                 public void IsLoopback ()
943                 {
944                         Uri uri = new Uri("http://loopback:8080");
945                         Assert.AreEqual (true, uri.IsLoopback, "#1");
946                         uri = new Uri("http://localhost:8080");
947                         Assert.AreEqual (true, uri.IsLoopback, "#2");
948                         uri = new Uri("http://127.0.0.1:8080");
949                         Assert.AreEqual (true, uri.IsLoopback, "#3");
950                         uri = new Uri("http://127.0.0.001:8080");
951                         Assert.AreEqual (true, uri.IsLoopback, "#4");
952                         uri = new Uri("http://[::1]");
953                         Assert.AreEqual (true, uri.IsLoopback, "#5");
954                         uri = new Uri("http://[::1]:8080");
955                         Assert.AreEqual (true, uri.IsLoopback, "#6");
956                         uri = new Uri("http://[::0001]:8080");
957                         Assert.AreEqual (true, uri.IsLoopback, "#7");
958                         uri = new Uri("http://[0:0:0:0::1]:8080");
959                         Assert.AreEqual (true, uri.IsLoopback, "#8");
960                         uri = new Uri("http://[0:0:0:0::127.0.0.1]:8080");
961                         Assert.AreEqual (true, uri.IsLoopback, "#9");
962                         uri = new Uri("http://[0:0:0:0::127.11.22.33]:8080");
963                         Assert.AreEqual (false, uri.IsLoopback, "#10");
964                         uri = new Uri("http://[::ffff:127.11.22.33]:8080");
965                         Assert.AreEqual (false, uri.IsLoopback, "#11");
966                         uri = new Uri("http://[::ff00:7f11:2233]:8080");
967                         Assert.AreEqual (false, uri.IsLoopback, "#12");
968                         uri = new Uri("http://[1:0:0:0::1]:8080");
969                         Assert.AreEqual (false, uri.IsLoopback, "#13");
970                 }
971
972                 [Test]
973                 public void IsLoopback_File ()
974                 {
975                         Uri uri = new Uri ("file:///index.html");
976                         Assert.IsTrue (uri.IsLoopback, "file");
977                 }
978
979                 [Test]
980                 public void IsLoopback_Relative_Http ()
981                 {
982                         string relative = "http:8080/bar/Hello World.htm";
983                         Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
984                         Assert.IsTrue (!uri.IsLoopback, "http");
985                 }
986
987                 [Test]
988                 public void IsLoopback_Relative_Unknown ()
989                 {
990                         string relative = "foo:8080/bar/Hello World.htm";
991                         Uri uri = new Uri (new Uri ("http://www.contoso.com"), relative, false);
992                         Assert.IsTrue (!uri.IsLoopback, "foo");
993                 }
994
995                 [Test]
996                 public void Equals1 ()
997                 {
998                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
999                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1000                         Assert.IsTrue (uri1.Equals (uri2), "#1");
1001                         Assert.IsTrue (!uri2.Equals ("http://www.contoso.com/index.html?x=1"), "#3");
1002                         Assert.IsTrue (!uri1.Equals ("http://www.contoso.com:8080/index.htm?x=1"), "#4");
1003                 }
1004
1005                 [Test]
1006                 public void Equals2 ()
1007                 {
1008                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1009                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1010                         Assert.IsTrue (!uri1.Equals (uri2), "#2 known to fail with ms.net 1.x");
1011                 }
1012
1013                 [Test]
1014                 public void Equals3 ()
1015                 {
1016                         Uri uri1 = new Uri ("svn+ssh://atsushi@mono-cvs.ximian.com");
1017                         Uri uri2 = new Uri ("svn+ssh://anonymous@mono-cvs.ximian.com");
1018                         Assert.IsTrue (uri1.Equals (uri2));
1019                 }
1020
1021                 [Test]
1022                 public void Equals4 ()
1023                 {
1024                         var uri = new Uri ("http://w3.org");
1025                         Assert.IsFalse (uri.Equals ("-"));
1026                 }
1027
1028                 [Test]
1029                 public void TestEquals2 ()
1030                 {
1031                         Uri a = new Uri ("http://www.go-mono.com");
1032                         Uri b = new Uri ("http://www.go-mono.com");
1033
1034                         Assert.AreEqual (a, b, "#1");
1035
1036                         a = new Uri ("mailto:user:pwd@go-mono.com?subject=uri");
1037                         b = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1038
1039                         Assert.IsTrue (a != b, "#2");
1040                         Assert.AreEqual ("mailto:user:pwd@go-mono.com?subject=uri", a.ToString (), "#2a");
1041                         Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", b.ToString (), "#2b");
1042
1043                         a = new Uri ("http://www.go-mono.com/ports/");
1044                         b = new Uri ("http://www.go-mono.com/PORTS/");
1045
1046                         Assert.IsTrue (!a.Equals (b), "#3");
1047                 }
1048
1049                 [Test]
1050                 public void CaseSensitivity ()
1051                 {
1052                         Uri mailto = new Uri ("MAILTO:USER:PWD@GO-MONO.COM?SUBJECT=URI");
1053                         Assert.AreEqual ("mailto", mailto.Scheme, "#1");
1054                         Assert.AreEqual ("go-mono.com", mailto.Host, "#2");
1055                         Assert.AreEqual ("mailto:USER:PWD@go-mono.com?SUBJECT=URI", mailto.ToString (), "#3");
1056
1057                         Uri http = new Uri ("HTTP://GO-MONO.COM/INDEX.HTML");
1058                         Assert.AreEqual ("http", http.Scheme, "#4");
1059                         Assert.AreEqual ("go-mono.com", http.Host, "#5");
1060                         Assert.AreEqual ("http://go-mono.com/INDEX.HTML", http.ToString (), "#6");
1061
1062                         // IPv6 Address
1063                         Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
1064                         Assert.AreEqual ("ftp", ftp.Scheme, "#7");
1065
1066                         Assert.AreEqual ("[::ffff:169.32.14.5]", ftp.Host, "#8");
1067                         Assert.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp.ToString (), "#9");
1068                 }
1069
1070                 [Test]
1071                 public void GetHashCodeTest ()
1072                 {
1073                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm#main");
1074                         Uri uri2 = new Uri ("http://www.contoso.com/index.htm#fragment");
1075                         Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#1");
1076                         uri2 = new Uri ("http://www.contoso.com/index.htm?x=1");
1077                         Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#2");
1078                         uri2 = new Uri ("http://www.contoso.com:80/index.htm");
1079                         Assert.AreEqual (uri1.GetHashCode (), uri2.GetHashCode (), "#3");
1080                         uri2 = new Uri ("http://www.contoso.com:8080/index.htm");
1081                         Assert.IsTrue (uri1.GetHashCode () != uri2.GetHashCode (), "#4");
1082                 }
1083
1084                 [Test]
1085                 public void RelativeEqualsTest()
1086                 {
1087                         Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1088                         Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1089                         Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1090                         Uri uri4 = new Uri ("BAR/MAN", UriKind.Relative);
1091                         Assert.IsTrue (uri1 == uri2, "#1a");
1092                         Assert.IsTrue (uri1.Equals(uri2), "#1b");
1093                         Assert.IsTrue (uri1 != uri3, "#2a");
1094                         Assert.IsTrue (!uri1.Equals(uri3), "#2b");
1095                         Assert.IsTrue (uri1 == uri2, "#3a");
1096                         Assert.IsTrue (uri1.Equals(uri2), "#3b");
1097                         Assert.IsTrue (uri1 != uri3, "#4a");
1098                         Assert.IsTrue (!uri1.Equals(uri3), "#4b");
1099                         Assert.IsTrue (uri3 != uri4, "#5a");
1100                         Assert.IsTrue (!uri3.Equals(uri4), "#5b");
1101                 }
1102
1103                 [Test]
1104                 [ExpectedException(typeof(InvalidOperationException))]
1105                 public void GetLeftPart_Partial1 ()
1106                 {
1107                         Uri u = new Uri ("foo", UriKind.Relative);
1108                         u.GetLeftPart (UriPartial.Scheme);
1109                 }
1110
1111                 [ExpectedException(typeof(InvalidOperationException))]
1112                 [Test]
1113                 public void GetLeftPart_Partial2 ()
1114                 {
1115                         Uri u = new Uri ("foo", UriKind.Relative);
1116                         u.GetLeftPart (UriPartial.Authority);
1117                 }
1118
1119                 [ExpectedException(typeof(InvalidOperationException))]
1120                 [Test]
1121                 public void GetLeftPart_Partial3 ()
1122                 {
1123                         Uri u = new Uri ("foo", UriKind.Relative);
1124                         u.GetLeftPart (UriPartial.Path);
1125                 }
1126
1127                 [Test]
1128                 public void TestPartialToString ()
1129                 {
1130                         Assert.AreEqual (new Uri ("foo", UriKind.Relative).ToString (), "foo", "#1");
1131                         Assert.AreEqual (new Uri ("foo#aa", UriKind.Relative).ToString (), "foo#aa", "#2");
1132                         Assert.AreEqual (new Uri ("foo?aa", UriKind.Relative).ToString (), "foo?aa", "#3");
1133                         Assert.AreEqual (new Uri ("foo#dingus?aa", UriKind.Relative).ToString (), "foo#dingus?aa", "#4");
1134                         Assert.AreEqual (new Uri ("foo?dingus#aa", UriKind.Relative).ToString (), "foo?dingus#aa", "#4");
1135                 }
1136
1137                 [Test]
1138                 public void RelativeGetHashCodeTest()
1139                 {
1140                         Uri uri1 = new Uri ("foo/bar", UriKind.Relative);
1141                         Uri uri2 = new Uri ("foo/bar", UriKind.Relative);
1142                         Uri uri3 = new Uri ("bar/man", UriKind.Relative);
1143                         Assert.AreEqual (uri1.GetHashCode(), uri2.GetHashCode(), "#1");
1144                         Assert.IsTrue (uri1.GetHashCode() != uri3.GetHashCode(), "#2");
1145                 }
1146
1147                 [Test]
1148                 public void MakeRelative ()
1149                 {
1150                         Uri uri1 = new Uri ("http://www.contoso.com/index.htm?x=2");
1151                         Uri uri2 = new Uri ("http://www.contoso.com/foo/bar/index.htm#fragment");
1152                         Uri uri3 = new Uri ("http://www.contoso.com/bar/foo/index.htm?y=1");
1153                         Uri uri4 = new Uri ("http://www.contoso.com/bar/foo2/index.htm?x=0");
1154                         Uri uri5 = new Uri ("https://www.contoso.com/bar/foo/index.htm?y=1");
1155                         Uri uri6 = new Uri ("http://www.contoso2.com/bar/foo/index.htm?x=0");
1156                         Uri uri7 = new Uri ("http://www.contoso2.com/bar/foo/foobar.htm?z=0&y=5");
1157                         Uri uri8 = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1158
1159                         Assert.AreEqual ("foo/bar/index.htm", uri1.MakeRelative (uri2), "#1");
1160                         Assert.AreEqual ("../../index.htm", uri2.MakeRelative (uri1), "#2");
1161
1162                         Assert.AreEqual ("../../bar/foo/index.htm", uri2.MakeRelative (uri3), "#3");
1163                         Assert.AreEqual ("../../foo/bar/index.htm", uri3.MakeRelative (uri2), "#4");
1164
1165                         Assert.AreEqual ("../foo2/index.htm", uri3.MakeRelative (uri4), "#5");
1166                         Assert.AreEqual ("../foo/index.htm", uri4.MakeRelative (uri3), "#6");
1167
1168                         Assert.AreEqual ("https://www.contoso.com/bar/foo/index.htm?y=1", uri4.MakeRelative (uri5), "#7");
1169
1170                         Assert.AreEqual ("http://www.contoso2.com/bar/foo/index.htm?x=0", uri4.MakeRelative (uri6), "#8");
1171
1172                         Assert.AreEqual ("", uri6.MakeRelative (uri6), "#9");
1173                         Assert.AreEqual ("foobar.htm", uri6.MakeRelative (uri7), "#10");
1174
1175                         Uri uri10 = new Uri ("mailto:xxx@xxx.com");
1176                         Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
1177                         Assert.AreEqual ("", uri10.MakeRelative (uri11), "#11");
1178
1179                         Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
1180                         Assert.AreEqual ("mailto:xxx@mail.xxx.com?subject=hola", uri10.MakeRelative (uri12), "#12");
1181
1182                         Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
1183                         Assert.AreEqual ("/foo/bar", uri10.MakeRelative (uri13), "#13");
1184
1185                         Assert.AreEqual ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9, uri1.MakeRelative (uri8), "#14");
1186                 }
1187
1188                 [Test]
1189                 public void RelativeFragmentUri ()
1190                 {
1191                         Uri u = new Uri("http://localhost/../../../a");
1192                         Assert.AreEqual ("http://localhost/a", u.ToString ());
1193
1194                         u = new Uri ("http://localhost/../c/b/../a");
1195                         Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1196                 }
1197
1198                 [Test]
1199                 public void RelativeFragmentUri2 ()
1200                 {
1201                         Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1202                         if (isWin32) {
1203                                 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1204                                 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1205                                 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1206                                 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1207                         } else {
1208                                 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1209                                 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1210                                 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1211                                 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1212                         }
1213                         Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1214                 }
1215
1216                 [Test]
1217                 public void ToStringTest()
1218                 {
1219                         Uri uri = new Uri ("dummy://xxx");
1220                         Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1221                 }
1222
1223                 [Test]
1224                 public void CheckSchemeName ()
1225                 {
1226                         Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1227                         Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1228                         Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1229                         Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1230                         Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1231                         Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1232                         Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1233                         Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1234                         Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1235                         Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1236                         // 0x00E1 -> &atilde;
1237                         Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1238                 }
1239
1240                 [Test]
1241                 public void CheckSchemeName_FirstChar ()
1242                 {
1243                         for (int i = 0; i < 256; i++) {
1244                                 string s = String.Format ("#{0}", i);
1245                                 char c = (char) i;
1246                                 bool b = Uri.CheckSchemeName (c.ToString ());
1247                                 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1248                                 Assert.AreEqual (valid, b, s);
1249                         }
1250                 }
1251
1252                 [Test]
1253                 public void CheckSchemeName_AnyOtherChar ()
1254                 {
1255                         for (int i = 0; i < 256; i++) {
1256                                 string s = String.Format ("#{0}", i);
1257                                 char c = (char) i;
1258                                 string scheme = String.Format ("a+b-c.d{0}", c);
1259                                 bool b = Uri.CheckSchemeName (scheme);
1260                                 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1261                                 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1262                                 Assert.AreEqual (valid, b, s);
1263                         }
1264                 }
1265
1266                 [Test]
1267                 [ExpectedException (typeof (UriFormatException))]
1268                 public void NoHostname ()
1269                 {
1270                         Uri uri = new Uri ("http://");
1271                 }
1272
1273                 [Test]
1274                 public void NoHostname2 ()
1275                 {
1276                         // bug 75144
1277                         Uri uri = new Uri ("file://");
1278                         Assert.AreEqual (true, uri.IsFile, "#1");
1279                         Assert.AreEqual (false, uri.IsUnc, "#2");
1280                         Assert.AreEqual ("file", uri.Scheme, "#3");
1281                         Assert.AreEqual ("/", uri.LocalPath, "#4");
1282                         Assert.AreEqual (string.Empty, uri.Query, "#5");
1283                         Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1284                         Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1285                         Assert.AreEqual (string.Empty, uri.Authority, "#8");
1286                         Assert.AreEqual (string.Empty, uri.Host, "#9");
1287                         Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1288                         Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1289                         Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1290                         Assert.IsTrue (uri.IsLoopback, "#13");
1291                         Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1292                         Assert.AreEqual (false, uri.UserEscaped, "#15");
1293                         Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1294                         Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1295                         Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1296                         Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1297                 }
1298
1299                 [Test]
1300                 public void Segments1 ()
1301                 {
1302                         Uri uri = new Uri ("http://localhost/");
1303                         string [] segments = uri.Segments;
1304                         Assert.AreEqual (1, segments.Length, "#01");
1305                         Assert.AreEqual ("/", segments [0], "#02");
1306                 }
1307
1308                 [Test]
1309                 public void Segments2 ()
1310                 {
1311                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1312                         string [] segments = uri.Segments;
1313                         Assert.AreEqual (3, segments.Length, "#01");
1314                         Assert.AreEqual ("/", segments [0], "#02");
1315                         Assert.AreEqual ("dir/", segments [1], "#03");
1316                         Assert.AreEqual ("dummypage.html", segments [2], "#04");
1317                 }
1318
1319                 [Test]
1320                 public void CachingSegments ()
1321                 {
1322                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1323                         uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1324                         string [] segments = uri.Segments;
1325                         Assert.AreEqual (3, segments.Length, "#01");
1326                         Assert.AreEqual ("/", segments [0], "#02");
1327                         Assert.AreEqual ("dir/", segments [1], "#03");
1328                         Assert.AreEqual ("dummypage.html", segments [2], "#04");
1329                 }
1330
1331                 [Test]
1332                 public void Segments3 ()
1333                 {
1334                         Uri uri = new Uri ("http://localhost/dir/dummypage/");
1335                         string [] segments = uri.Segments;
1336                         Assert.AreEqual (3, segments.Length, "#01");
1337                         Assert.AreEqual ("/", segments [0], "#02");
1338                         Assert.AreEqual ("dir/", segments [1], "#03");
1339                         Assert.AreEqual ("dummypage/", segments [2], "#04");
1340                 }
1341
1342                 [Test]
1343                 public void Segments4 ()
1344                 {
1345                         Uri uri = new Uri ("file:///c:/hello");
1346
1347                         Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1348                         Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1349
1350                         string [] segments = uri.Segments;
1351                         Assert.AreEqual (3, segments.Length, "#01");
1352                         Assert.AreEqual ("/", segments [0], "#02");
1353                         Assert.AreEqual ("c:/", segments[1], "#03");
1354                         Assert.AreEqual ("hello", segments [2], "#04");
1355                 }
1356
1357                 [Test]
1358                 public void Segments5 ()
1359                 {
1360                         Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1361                         string [] segments = uri.Segments;
1362                         Assert.AreEqual (4, segments.Length, "#01");
1363                         Assert.AreEqual ("/", segments [0], "#02");
1364                         Assert.AreEqual ("bar/", segments [1], "#03");
1365                         Assert.AreEqual ("foo/", segments [2], "#04");
1366                         Assert.AreEqual ("foobar.htm", segments [3], "#05");
1367                 }
1368
1369                 [Test]
1370                 [ExpectedException (typeof (UriFormatException))]
1371                 public void UriStartingWithColon()
1372                 {
1373                         new Uri("://");
1374                 }
1375
1376                 [Test]
1377                 [ExpectedException (typeof (UriFormatException))]
1378                 public void EmptyScheme ()
1379                 {
1380                         new Uri ("hey");
1381                 }
1382
1383                 [Test]
1384                 public void SchemeWithDigits ()
1385                 {
1386                         Uri uri = new Uri ("net.p2p://foobar");
1387                         Assert.AreEqual ("net.p2p", uri.Scheme);
1388                 }
1389
1390                 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1391                 [ExpectedException (typeof (UriFormatException))]
1392                 [Test]
1393                 public void InvalidPort1 ()
1394                 {
1395                         Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1396                         Assert.AreEqual (65536, uri.Port);
1397                 }
1398
1399                 [ExpectedException (typeof (UriFormatException))]
1400                 [Test]
1401                 public void InvalidPort2 ()
1402                 {
1403                         // UInt32.MaxValue gives port == -1 !!!
1404                         Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1405                         Assert.AreEqual (-1, uri.Port);
1406                 }
1407
1408                 [ExpectedException (typeof (UriFormatException))]
1409                 [Test]
1410                 public void InvalidPort3 ()
1411                 {
1412                         // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1413                         Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1414                         Assert.AreEqual (-2147483648, uri.Port);
1415                 }
1416
1417                 [Test]
1418                 public void PortMax ()
1419                 {
1420                         // on .NET 2.0 a port number is limited to UInt16.MaxValue
1421                         Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1422                         Assert.AreEqual (65535, uri.Port);
1423                 }
1424
1425                 class UriEx2 : Uri
1426                 {
1427                         public UriEx2 (string s) : base (s)
1428                         {
1429                         }
1430
1431                         protected override void Parse ()
1432                         {
1433                         }
1434                 }
1435
1436                 // Parse method is no longer used on .NET 2.0
1437                 [ExpectedException (typeof (UriFormatException))]
1438                 [Test]
1439                 public void ParseOverride ()
1440                 {
1441                         // If this does not override base's Parse(), it will
1442                         // fail since this argument is not Absolute URI.
1443                         UriEx2 ex = new UriEx2 ("readme.txt");
1444                 }
1445
1446                 [Test]
1447                 public void UnixLocalPath ()
1448                 {
1449                         // This works--the location is not part of the absolute path
1450                         string path = "file://localhost/tmp/foo/bar";
1451                         Uri fileUri = new Uri( path );
1452                         Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1453
1454                         // Empty path == localhost, in theory
1455                         path = "file:///c:/tmp/foo/bar";
1456                         fileUri = new Uri( path );
1457                         Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1458                 }
1459
1460                 [Test]
1461                 public void WindowsLocalPath ()
1462                 {
1463                         new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1464                 }
1465
1466                 [Test]
1467                 public void TestEscapeDataString ()
1468                 {
1469                         StringBuilder sb = new StringBuilder ();
1470
1471                         for (int i = 0; i < 128; i++)
1472                                 sb.Append ((char) i);
1473
1474                         Assert.AreEqual (
1475                                 "%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",
1476                                 Uri.EscapeDataString (sb.ToString ()));
1477
1478                         Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1479                 }
1480                 [Test]
1481                 public void TestEscapeUriString ()
1482                 {
1483                         StringBuilder sb = new StringBuilder ();
1484                         for (int i = 0; i < 128; i++)
1485                                 sb.Append ((char) i);
1486
1487                         Assert.AreEqual (
1488                                 "%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",
1489                                 Uri.EscapeUriString (sb.ToString ()));
1490
1491                         Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1492                 }
1493
1494                 //bnc #363320
1495                 [Test]
1496                 public void TestUTF8Strings ()
1497                 {
1498                         string [] tests = {
1499                                 "file:///tmp/x (%232).jpg",
1500                                 "file:///tmp/ü (%232).jpg" };
1501
1502                         foreach (string test in tests) {
1503                                 Uri uri = new Uri (test);
1504                                 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1505                                 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1506                                 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1507                         }
1508                 }
1509
1510                 // This test doesn't work on Linux, and arguably shouldn't work.
1511                 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1512                 // on Linux, as anyone sane would expect.  It *doesn't* under .NET 1.1
1513                 // Apparently "tmp" is supposed to be a hostname (!)...
1514                 // Since "correct" behavior would confuse all Linux developers, and having
1515                 // an expected failure is evil, we'll just ignore this for now...
1516                 //
1517                 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1518                 //
1519                 // You are surrounded by conditional-compilation code, all alike.
1520                 // You are likely to be eaten by a Grue...
1521                 [Test]
1522                 public void UnixLocalPath_WTF ()
1523                 {
1524                         // Empty path == localhost, in theory
1525                         string path = "file:///tmp/foo/bar";
1526                         Uri fileUri = new Uri( path );
1527                         Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1528
1529                         // bug #76643
1530                         string path2 = "file:///foo%25bar";
1531                         fileUri = new Uri (path2);
1532                         Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1533                 }
1534
1535                 public static void Print (Uri uri)
1536                 {
1537                         Console.WriteLine ("ToString: " + uri.ToString ());
1538
1539                         Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1540                         Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1541                         Console.WriteLine ("Authority: " + uri.Authority);
1542                         Console.WriteLine ("Fragment: " + uri.Fragment);
1543                         Console.WriteLine ("Host: " + uri.Host);
1544                         Console.WriteLine ("HostNameType: " + uri.HostNameType);
1545                         Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1546                         Console.WriteLine ("IsFile: " + uri.IsFile);
1547                         Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1548                         Console.WriteLine ("IsUnc: " + uri.IsUnc);
1549                         Console.WriteLine ("LocalPath: " + uri.LocalPath);
1550                         Console.WriteLine ("PathAndQuery        : " + uri.PathAndQuery);
1551                         Console.WriteLine ("Port: " + uri.Port);
1552                         Console.WriteLine ("Query: " + uri.Query);
1553                         Console.WriteLine ("Scheme: " + uri.Scheme);
1554                         Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1555                         Console.WriteLine ("UserInfo: " + uri.UserInfo);
1556
1557                         Console.WriteLine ("Segments:");
1558                         string [] segments = uri.Segments;
1559                         if (segments == null)
1560                                 Console.WriteLine ("\tNo Segments");
1561                         else
1562                                 for (int i = 0; i < segments.Length; i++)
1563                                         Console.WriteLine ("\t" + segments[i]);
1564                         Console.WriteLine ("");
1565                 }
1566
1567                 [Test]
1568                 public void FtpRootPath ()
1569                 {
1570                         Uri u = new Uri ("ftp://a.b/%2fabc/def");
1571                         string p = u.PathAndQuery;
1572                         Assert.AreEqual ("/%2fabc/def", p);
1573                         p = Uri.UnescapeDataString (p).Substring (1);
1574                         Assert.AreEqual ("/abc/def", p);
1575                         u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1576                         Assert.AreEqual ("/abc/def", u.PathAndQuery);
1577                 }
1578
1579 //BNC#533572
1580                 [Test]
1581                 public void LocalPath_FileNameWithAtSign1 ()
1582                 {
1583                         string path = "/some/path/file_with_an_@_sign.mp3";
1584                         string fullpath = "http://thehost" + path;
1585                         Uri fileUri = new Uri (fullpath);
1586
1587                         Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1588                         Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1589                         Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1590                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1591                         Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1592
1593                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1594                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1595                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1596                         Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1597                 }
1598
1599                 [Test]
1600                 public void LocalPath_FileNameWithAtSign2 ()
1601                 {
1602                         string path = "/some/path/file_with_an_@_sign.mp3";
1603                         string fullpath = "http://user:password@thehost" + path;
1604                         Uri fileUri = new Uri (fullpath);
1605
1606                         Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1607                         Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1608                         Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1609                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1610                         Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1611
1612                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1613                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1614                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1615                         Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1616                 }
1617
1618                 [Test]
1619                 public void LocalPath_FileNameWithAtSign3 ()
1620                 {
1621                         string path = "/some/path/file_with_an_@_sign.mp3";
1622                         string fullpath = "file://" + path;
1623                         Uri fileUri = new Uri (fullpath);
1624
1625                         Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1626                         Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1627                         Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1628                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1629                         Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1630
1631                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1632                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1633                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1634                         Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1635                 }
1636
1637                 [Test]
1638                 public void LocalPath_FileNameWithAtSign4 ()
1639                 {
1640                         string path = "/some/path/file_with_an_@_sign.mp3";
1641                         string fullpath = "file://localhost" + path;
1642                         Uri fileUri = new Uri (fullpath);
1643
1644                         Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1645                         Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1646                         Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1647                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1648                         Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1649
1650                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1651                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1652                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1653                         //this test is marked as NotWorking below:
1654                         //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1655                 }
1656
1657                 [Test]
1658                 public void LocalPath_FileNameWithAtSign5 ()
1659                 {
1660                         string path = "/some/path/file_with_an_@_sign.mp3";
1661                         string fullpath = "file://localhost" + path;
1662                         Uri fileUri = new Uri (fullpath);
1663
1664                         string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1665                         Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1666                 }
1667
1668                 [Test]
1669                 [ExpectedException (typeof (UriFormatException))]
1670                 public void LocalPath_FileNameWithAtSign6 ()
1671                 {
1672                         string path = "/some/path/file_with_an_@_sign.mp3";
1673                         string fullpath = "file://user:password@localhost" + path;
1674                         Uri fileUri = new Uri (fullpath);
1675                 }
1676
1677                 [Test]
1678                 [Category ("NotDotNet")]
1679                 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1680                 {
1681                         Uri unixuri = new Uri ("/home/user/a@b");
1682                         Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1683                 }
1684
1685                 [Test]
1686                 [Category ("NotDotNet")]
1687                 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1688                 {
1689                         Uri unixuri = new Uri ("/home/user/a:b");
1690                         Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1691                 }
1692
1693                 [Test]
1694                 [Category ("NotDotNet")]
1695                 public void UnixAbsolutePath_ReplaceRelative ()
1696                 {
1697                         var u1 = new Uri ("/Users/demo/Projects/file.xml");
1698                         var u2 = new Uri (u1, "b.jpg");
1699
1700                         Assert.AreEqual ("file:///Users/demo/Projects/b.jpg", u2.ToString ());
1701                 }
1702
1703                 [Test]
1704                 public void RelativeUriWithColons ()
1705                 {
1706                         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
1707                         new Uri (s, UriKind.Relative);
1708                         new Uri (":", UriKind.Relative);
1709                         new Uri ("1:", UriKind.Relative);
1710                 }
1711
1712                 [Test]
1713                 public void ConsecutiveSlashes ()
1714                 {
1715                         Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1716                         Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1717                 }
1718
1719                 public class DerivedUri : Uri {
1720                         public DerivedUri (string uriString)
1721                                 : base (uriString)
1722                         {
1723                         }
1724
1725                         internal string TestUnescape (string path)
1726                         {
1727                                 return base.Unescape (path);
1728                         }
1729                 }
1730
1731                 [Test]
1732                 [ExpectedException (typeof (InvalidOperationException))]
1733                 public void GetComponents_Relative ()
1734                 {
1735                         Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1736                         rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1737                 }
1738
1739                 [Test]
1740                 public void GetComponents_AbsoluteUri ()
1741                 {
1742                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1743
1744                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1745
1746                         string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1747                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1748
1749                         string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1750                         Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1751
1752                         string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1753                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1754                 }
1755
1756                 [Test]
1757                 public void GetComponents_HttpRequestUrl ()
1758                 {
1759                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1760
1761                         string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1762                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1763
1764                         string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1765                         Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1766
1767                         string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1768                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1769                 }
1770
1771                 [Test]
1772                 public void GetComponents_KeepDelimiter ()
1773                 {
1774                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1775
1776                         string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1777                         Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1778
1779                         string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1780                         Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1781
1782                         string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1783                         Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1784                 }
1785
1786                 [Test]
1787                 public void GetComponents_StrongAuthority ()
1788                 {
1789                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1790
1791                         string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1792                         Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1793
1794                         string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1795                         Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1796
1797                         string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1798                         Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1799                 }
1800
1801                 [Test]
1802                 public void GetComponents_Path ()
1803                 {
1804                         Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1805                         Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1806
1807                         string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1808                         Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1809
1810                         string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1811                         Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1812
1813                         string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1814                         Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1815
1816                         // same result is unescaped original string
1817                         Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1818                         Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1819
1820                         safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1821                         Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1822
1823                         unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1824                         Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1825
1826                         escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1827                         Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1828                 }
1829
1830                 [Test]
1831                 public void GetComponents_PathAndQuery ()
1832                 {
1833                         Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1834
1835                         Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1836
1837                         string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1838                         Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1839
1840                         string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1841                         Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1842
1843                         string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1844                         Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1845                 }
1846
1847                 [Test]
1848                 public void GetComponents_Query ()
1849                 {
1850                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1851
1852                         Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1853                         
1854                         string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1855                         Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1856
1857                         string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1858                         Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1859
1860                         string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1861                         Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1862                 }
1863
1864                 [Test]
1865                 public void GetComponents_Fragment ()
1866                 {
1867                         Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1868
1869                         Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1870
1871                         string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1872                         Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1873
1874                         string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1875                         Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1876
1877                         string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1878                         Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1879                 }
1880
1881                 // When used, paths such as "/foo" are assumed relative.
1882                 static UriKind DotNetRelativeOrAbsolute = (Type.GetType ("Mono.Runtime") == null)? UriKind.RelativeOrAbsolute : (UriKind) 300;
1883
1884                 [Test]
1885                 public void DotNetRelativeOrAbsoluteTest ()
1886                 {
1887                         FieldInfo useDotNetRelativeOrAbsoluteField = null;
1888                         bool useDotNetRelativeOrAbsoluteOld = false;
1889
1890                         if (Type.GetType ("Mono.Runtime") != null) {
1891                                 useDotNetRelativeOrAbsoluteField = typeof (Uri).GetField ("useDotNetRelativeOrAbsolute",
1892                                         BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
1893                                 useDotNetRelativeOrAbsoluteOld = (bool) useDotNetRelativeOrAbsoluteField.GetValue (null);
1894                                 useDotNetRelativeOrAbsoluteField.SetValue (null, false);
1895                         }
1896
1897                         try {
1898                                 Uri uri;
1899
1900                                 uri = new Uri ("/foo", DotNetRelativeOrAbsolute);
1901                                 Assert.IsFalse (uri.IsAbsoluteUri, "#2");
1902                                 
1903                                 Assert.IsTrue (Uri.TryCreate("/foo", DotNetRelativeOrAbsolute, out uri), "#3");
1904                                 Assert.IsFalse (uri.IsAbsoluteUri, "#3a");
1905
1906                                 if (useDotNetRelativeOrAbsoluteField != null) {
1907                                         uri = new Uri ("/foo", UriKind.RelativeOrAbsolute);
1908                                         Assert.IsTrue (uri.IsAbsoluteUri, "#4");
1909
1910                                         Assert.IsTrue (Uri.TryCreate("/foo", UriKind.RelativeOrAbsolute, out uri), "#5");
1911                                         Assert.IsTrue (uri.IsAbsoluteUri, "#5a");
1912
1913                                         useDotNetRelativeOrAbsoluteField.SetValue (null, true);
1914                                 }
1915
1916                                 uri = new Uri ("/foo", UriKind.RelativeOrAbsolute);
1917                                 Assert.IsFalse (uri.IsAbsoluteUri, "#10");
1918
1919                                 Assert.IsTrue (Uri.TryCreate("/foo", UriKind.RelativeOrAbsolute, out uri), "#11");
1920                                 Assert.IsFalse (uri.IsAbsoluteUri, "#11a");
1921                         } finally {
1922                                 if (useDotNetRelativeOrAbsoluteField != null)
1923                                         useDotNetRelativeOrAbsoluteField.SetValue (null, useDotNetRelativeOrAbsoluteOld);
1924                         }
1925                 }
1926
1927                 [Test]
1928                 // Bug #12631
1929                 public void LocalPathWithBaseUrl ()
1930                 {
1931                         var mainUri = new Uri ("http://www.imdb.com");
1932                         var uriPath = "/title/tt0106521";
1933
1934                         Uri result;
1935                         Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1936                         Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
1937                 }
1938
1939                 [Test]
1940                 public void GetSerializationInfoStringOnRelativeUri ()
1941                 {
1942                         var uri = new Uri ("/relative/path", UriKind.Relative);
1943                         var result = uri.GetComponents (UriComponents.SerializationInfoString, UriFormat.UriEscaped);
1944
1945                         Assert.AreEqual (uri.OriginalString, result);
1946                 }
1947
1948                 [Test]
1949                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1950                 public void GetSerializationInfoStringException ()
1951                 {
1952                         var uri = new Uri ("/relative/path", UriKind.Relative);
1953                         uri.GetComponents (UriComponents.SerializationInfoString  | UriComponents.Host, UriFormat.UriEscaped);
1954                 }
1955
1956                 [Test]
1957                 public void UserInfo_EscapedLetter ()
1958                 {
1959                         var uri = new Uri ("https://first%61second@host");
1960                         Assert.AreEqual ("firstasecond", uri.UserInfo);
1961                 }
1962
1963                 [Test]
1964                 public void UserInfo_EscapedAt ()
1965                 {
1966                         var userinfo =  "first%40second";
1967                         var uri = new Uri ("https://" + userinfo + "@host");
1968                         Assert.AreEqual (userinfo, uri.UserInfo);
1969                 }
1970
1971                 [Test]
1972                 public void UserInfo_EscapedChars ()
1973                 {
1974                         for (var ch = (char) 1; ch < 128; ch++) {
1975                                 var userinfo = Uri.EscapeDataString (ch.ToString ());
1976                                 try {
1977                                         new Uri (string.Format("http://{0}@localhost:80/", userinfo));
1978                                 } catch (Exception e) {
1979                                         Assert.Fail (string.Format("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
1980                                 }
1981                         }
1982                 }
1983
1984                 [Test]
1985                 public void UserInfo_Spaces ()
1986                 {
1987                         const string userinfo = "test 1:pass 1";
1988                         const string expected = "test%201:pass%201";
1989
1990                         try {
1991                                 var uri = new Uri (string.Format ("rtmp://{0}@test.com:333/live", userinfo));
1992                                 Assert.AreEqual (expected, uri.UserInfo);
1993                         } catch (Exception e) {
1994                                 Assert.Fail (string.Format ("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
1995                         }
1996                 }
1997
1998                 // Covers #29864
1999                 [Test]
2000                 public void PathDotTrim ()
2001                 {
2002                         var baseUri = new Uri ("http://test.com", UriKind.Absolute);
2003                         var relUri = new Uri ("path/dot./", UriKind.Relative);
2004                         var uri = new Uri (baseUri, relUri);
2005                         Assert.AreEqual ("http://test.com/path/dot./", uri.ToString ());
2006                 }
2007
2008                 [Test]
2009                 public void GuardedIPv6Address ()
2010                 {
2011                         var x = new Uri ("asfd://[::1]:123/");
2012                         Assert.AreEqual ("[::1]", x.Host, "#1");
2013                 }
2014
2015                 [Test]
2016                 public void CombineWithUserSchema ()
2017                 {
2018                         var baseUri = new Uri ("zip:mem:///");
2019                         var relativeUrl = "zip:mem:///foo/bar.txt";
2020
2021                         var result = new Uri (baseUri, relativeUrl);
2022
2023                         Assert.AreEqual ("zip:mem:///foo/bar.txt", result.ToString ());
2024                 }
2025
2026                 [Test]
2027                 public void Scheme_msapp ()
2028                 {
2029                         var uri = new Uri ("ms-app://s-1-15-2-1613647288");
2030                         Assert.AreEqual ("ms-app", uri.Scheme);
2031                 }
2032
2033                 [Test]
2034                 public void CombineWithUnixAbsolutePath ()
2035                 {
2036                         var a = new Uri ("http://localhost/");
2037                         var b = new Uri ("/foo", UriKind.RelativeOrAbsolute);
2038                         var res = new Uri (a, b);
2039
2040                         Assert.AreEqual ("http://localhost/foo", res.ToString ());
2041                 }
2042
2043                 [Test]
2044                 public void ImplicitUnixFileWithUnicode ()
2045                 {
2046                         string value = "/Library/Frameworks/System.Runtim…ee";
2047                         Uri uri;
2048                         Assert.IsTrue (Uri.TryCreate (value, UriKind.Absolute, out uri));
2049                 }
2050
2051                 [Test]
2052                 public void UncValidPath ()
2053                 {
2054                         var uri = new Uri ("https://_foo/bar.html");
2055                         Assert.AreEqual ("https", uri.Scheme);
2056                 }
2057         }
2058 }