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