Merge pull request #845 from meyerrj/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                 [Category ("NotWorking")]
1198                 public void RelativeUri ()
1199                 {
1200                         var u = new Uri ("/foo/bar");
1201                         Assert.IsFalse (u.IsAbsoluteUri, "#1");
1202                 }
1203
1204                 [Test]
1205                 public void RelativeFragmentUri ()
1206                 {
1207                         Uri u = new Uri("http://localhost/../../../a");
1208                         Assert.AreEqual ("http://localhost/a", u.ToString ());
1209
1210                         u = new Uri ("http://localhost/../c/b/../a");
1211                         Assert.AreEqual ("http://localhost/c/a", u.ToString ());
1212                 }
1213
1214                 [Test]
1215                 public void RelativeFragmentUri2 ()
1216                 {
1217                         Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
1218                         if (isWin32) {
1219                                 Assert.AreEqual ("file:///d:/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:///d:/myhost/ext").ToString (), "#2-w");
1220                                 Assert.AreEqual ("file:///c:/localhost/myhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:myhost/ext").ToString (), "#3-w");
1221                                 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c:/localhost/bar"), "uuid:ext").ToString (), "#4-w");
1222                                 Assert.AreEqual ("file:///c:/localhost/ext", new Uri (new Uri ("file:///c:/localhost/bar"), "file:./ext").ToString (), "#5-w");
1223                         } else {
1224                                 Assert.AreEqual ("file:///d/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:///d/myhost/ext").ToString (), "#2-u");
1225                                 Assert.AreEqual ("file:///c/localhost/myhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:myhost/ext").ToString (), "#3-u");
1226                                 Assert.AreEqual ("uuid:ext", new Uri (new Uri ("file:///c/localhost/bar"), "uuid:ext").ToString (), "#4-u");
1227                                 Assert.AreEqual ("file:///c/localhost/ext", new Uri (new Uri ("file:///c/localhost/bar"), "file:./ext").ToString (), "#5-u");
1228                         }
1229                         Assert.AreEqual ("http://localhost/ext", new Uri (new Uri ("http://localhost/bar"), "http:./ext").ToString (), "#6");
1230                 }
1231
1232                 [Test]
1233                 public void ToStringTest()
1234                 {
1235                         Uri uri = new Uri ("dummy://xxx");
1236                         Assert.AreEqual ("dummy://xxx/", uri.ToString (), "#1");
1237                 }
1238
1239                 [Test]
1240                 public void CheckSchemeName ()
1241                 {
1242                         Assert.AreEqual (false, Uri.CheckSchemeName (null), "#01");
1243                         Assert.AreEqual (false, Uri.CheckSchemeName (""), "#02");
1244                         Assert.AreEqual (true, Uri.CheckSchemeName ("http"), "#03");
1245                         Assert.AreEqual (true, Uri.CheckSchemeName ("http-"), "#04");
1246                         Assert.AreEqual (false, Uri.CheckSchemeName ("6http-"), "#05");
1247                         Assert.AreEqual (true, Uri.CheckSchemeName ("http6-"), "#06");
1248                         Assert.AreEqual (false, Uri.CheckSchemeName ("http6,"), "#07");
1249                         Assert.AreEqual (true, Uri.CheckSchemeName ("http6."), "#08");
1250                         Assert.AreEqual (false, Uri.CheckSchemeName ("+http"), "#09");
1251                         Assert.AreEqual (true, Uri.CheckSchemeName ("htt+p6"), "#10");
1252                         // 0x00E1 -> &atilde;
1253                         Assert.IsTrue (!Uri.CheckSchemeName ("htt\u00E1+p6"), "#11");
1254                 }
1255
1256                 [Test]
1257                 public void CheckSchemeName_FirstChar ()
1258                 {
1259                         for (int i = 0; i < 256; i++) {
1260                                 string s = String.Format ("#{0}", i);
1261                                 char c = (char) i;
1262                                 bool b = Uri.CheckSchemeName (c.ToString ());
1263                                 bool valid = (((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1264                                 Assert.AreEqual (valid, b, s);
1265                         }
1266                 }
1267
1268                 [Test]
1269                 public void CheckSchemeName_AnyOtherChar ()
1270                 {
1271                         for (int i = 0; i < 256; i++) {
1272                                 string s = String.Format ("#{0}", i);
1273                                 char c = (char) i;
1274                                 string scheme = String.Format ("a+b-c.d{0}", c);
1275                                 bool b = Uri.CheckSchemeName (scheme);
1276                                 bool common = Char.IsDigit (c) || (c == '+') || (c == '-') || (c == '.');
1277                                 bool valid = (common || ((i >= 0x41) && (i <= 0x5A)) || ((i >= 0x61) && (i <= 0x7A)));
1278                                 Assert.AreEqual (valid, b, s);
1279                         }
1280                 }
1281
1282                 [Test]
1283                 [ExpectedException (typeof (UriFormatException))]
1284                 public void NoHostname ()
1285                 {
1286                         Uri uri = new Uri ("http://");
1287                 }
1288
1289                 [Test]
1290                 public void NoHostname2 ()
1291                 {
1292                         // bug 75144
1293                         Uri uri = new Uri ("file://");
1294                         Assert.AreEqual (true, uri.IsFile, "#1");
1295                         Assert.AreEqual (false, uri.IsUnc, "#2");
1296                         Assert.AreEqual ("file", uri.Scheme, "#3");
1297                         Assert.AreEqual ("/", uri.LocalPath, "#4");
1298                         Assert.AreEqual (string.Empty, uri.Query, "#5");
1299                         Assert.AreEqual ("/", uri.AbsolutePath, "#6");
1300                         Assert.AreEqual ("file:///", uri.AbsoluteUri, "#7");
1301                         Assert.AreEqual (string.Empty, uri.Authority, "#8");
1302                         Assert.AreEqual (string.Empty, uri.Host, "#9");
1303                         Assert.AreEqual (UriHostNameType.Basic, uri.HostNameType, "#10");
1304                         Assert.AreEqual (string.Empty, uri.Fragment, "#11");
1305                         Assert.AreEqual (true, uri.IsDefaultPort, "#12");
1306                         Assert.IsTrue (uri.IsLoopback, "#13");
1307                         Assert.AreEqual ("/", uri.PathAndQuery, "#14");
1308                         Assert.AreEqual (false, uri.UserEscaped, "#15");
1309                         Assert.AreEqual (string.Empty, uri.UserInfo, "#16");
1310                         Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Authority), "#17");
1311                         Assert.AreEqual ("file:///", uri.GetLeftPart (UriPartial.Path), "#18");
1312                         Assert.AreEqual ("file://", uri.GetLeftPart (UriPartial.Scheme), "#19");
1313                 }
1314
1315                 [Test]
1316                 public void Segments1 ()
1317                 {
1318                         Uri uri = new Uri ("http://localhost/");
1319                         string [] segments = uri.Segments;
1320                         Assert.AreEqual (1, segments.Length, "#01");
1321                         Assert.AreEqual ("/", segments [0], "#02");
1322                 }
1323
1324                 [Test]
1325                 public void Segments2 ()
1326                 {
1327                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1328                         string [] segments = uri.Segments;
1329                         Assert.AreEqual (3, segments.Length, "#01");
1330                         Assert.AreEqual ("/", segments [0], "#02");
1331                         Assert.AreEqual ("dir/", segments [1], "#03");
1332                         Assert.AreEqual ("dummypage.html", segments [2], "#04");
1333                 }
1334
1335                 [Test]
1336                 public void CachingSegments ()
1337                 {
1338                         Uri uri = new Uri ("http://localhost/dir/dummypage.html");
1339                         uri.Segments [0] = uri.Segments [1] = uri.Segments [2] = "*";
1340                         string [] segments = uri.Segments;
1341                         Assert.AreEqual (3, segments.Length, "#01");
1342                         Assert.AreEqual ("/", segments [0], "#02");
1343                         Assert.AreEqual ("dir/", segments [1], "#03");
1344                         Assert.AreEqual ("dummypage.html", segments [2], "#04");
1345                 }
1346
1347                 [Test]
1348                 public void Segments3 ()
1349                 {
1350                         Uri uri = new Uri ("http://localhost/dir/dummypage/");
1351                         string [] segments = uri.Segments;
1352                         Assert.AreEqual (3, segments.Length, "#01");
1353                         Assert.AreEqual ("/", segments [0], "#02");
1354                         Assert.AreEqual ("dir/", segments [1], "#03");
1355                         Assert.AreEqual ("dummypage/", segments [2], "#04");
1356                 }
1357
1358                 [Test]
1359                 public void Segments4 ()
1360                 {
1361                         Uri uri = new Uri ("file:///c:/hello");
1362
1363                         Assert.AreEqual ("c:/hello", uri.AbsolutePath, "AbsolutePath");
1364                         Assert.AreEqual ("c:\\hello", uri.LocalPath, "LocalPath");
1365
1366                         string [] segments = uri.Segments;
1367                         Assert.AreEqual (3, segments.Length, "#01");
1368                         Assert.AreEqual ("/", segments [0], "#02");
1369                         Assert.AreEqual ("c:/", segments[1], "#03");
1370                         Assert.AreEqual ("hello", segments [2], "#04");
1371                 }
1372
1373                 [Test]
1374                 public void Segments5 ()
1375                 {
1376                         Uri uri = new Uri ("http://www.xxx.com/bar/foo/foobar.htm?z=0&y=5" + (char) 0xa9);
1377                         string [] segments = uri.Segments;
1378                         Assert.AreEqual (4, segments.Length, "#01");
1379                         Assert.AreEqual ("/", segments [0], "#02");
1380                         Assert.AreEqual ("bar/", segments [1], "#03");
1381                         Assert.AreEqual ("foo/", segments [2], "#04");
1382                         Assert.AreEqual ("foobar.htm", segments [3], "#05");
1383                 }
1384
1385                 [Test]
1386                 [ExpectedException (typeof (UriFormatException))]
1387                 public void UriStartingWithColon()
1388                 {
1389                         new Uri("://");
1390                 }
1391
1392                 [Test]
1393                 [ExpectedException (typeof (UriFormatException))]
1394                 public void EmptyScheme ()
1395                 {
1396                         new Uri ("hey");
1397                 }
1398
1399                 // on .NET 2.0 a port number is limited to UInt16.MaxValue
1400                 [ExpectedException (typeof (UriFormatException))]
1401                 [Test]
1402                 public void InvalidPort1 ()
1403                 {
1404                         Uri uri = new Uri ("http://www.contoso.com:65536/foo/bar/");
1405                         Assert.AreEqual (65536, uri.Port);
1406                 }
1407
1408                 [ExpectedException (typeof (UriFormatException))]
1409                 [Test]
1410                 public void InvalidPort2 ()
1411                 {
1412                         // UInt32.MaxValue gives port == -1 !!!
1413                         Uri uri = new Uri ("http://www.contoso.com:4294967295/foo/bar/");
1414                         Assert.AreEqual (-1, uri.Port);
1415                 }
1416
1417                 [ExpectedException (typeof (UriFormatException))]
1418                 [Test]
1419                 public void InvalidPort3 ()
1420                 {
1421                         // ((uint) Int32.MaxValue + (uint) 1) gives port == -2147483648 !!!
1422                         Uri uri = new Uri ("http://www.contoso.com:2147483648/foo/bar/");
1423                         Assert.AreEqual (-2147483648, uri.Port);
1424                 }
1425
1426                 [Test]
1427                 public void PortMax ()
1428                 {
1429                         // on .NET 2.0 a port number is limited to UInt16.MaxValue
1430                         Uri uri = new Uri ("http://www.contoso.com:65535/foo/bar/");
1431                         Assert.AreEqual (65535, uri.Port);
1432                 }
1433
1434                 class UriEx2 : Uri
1435                 {
1436                         public UriEx2 (string s) : base (s)
1437                         {
1438                         }
1439
1440                         protected override void Parse ()
1441                         {
1442                         }
1443                 }
1444
1445                 // Parse method is no longer used on .NET 2.0
1446                 [ExpectedException (typeof (UriFormatException))]
1447                 [Test]
1448                 public void ParseOverride ()
1449                 {
1450                         // If this does not override base's Parse(), it will
1451                         // fail since this argument is not Absolute URI.
1452                         UriEx2 ex = new UriEx2 ("readme.txt");
1453                 }
1454
1455                 [Test]
1456                 public void UnixLocalPath ()
1457                 {
1458                         // This works--the location is not part of the absolute path
1459                         string path = "file://localhost/tmp/foo/bar";
1460                         Uri fileUri = new Uri( path );
1461                         Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1462
1463                         // Empty path == localhost, in theory
1464                         path = "file:///c:/tmp/foo/bar";
1465                         fileUri = new Uri( path );
1466                         Assert.AreEqual ("c:/tmp/foo/bar", fileUri.AbsolutePath, path);
1467                 }
1468
1469                 [Test]
1470                 public void WindowsLocalPath ()
1471                 {
1472                         new Uri (@"file:///J:\Wrldwide\MSFin\Flash\FLASH.xls");
1473                 }
1474
1475                 [Test]
1476                 public void TestEscapeDataString ()
1477                 {
1478                         StringBuilder sb = new StringBuilder ();
1479
1480                         for (int i = 0; i < 128; i++)
1481                                 sb.Append ((char) i);
1482
1483                         Assert.AreEqual (
1484 #if NET_4_0
1485                                 "%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",
1486 #else
1487                                 "%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",
1488 #endif
1489                                  Uri.EscapeDataString (sb.ToString ()));
1490
1491                         Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1492                 }
1493                 [Test]
1494                 public void TestEscapeUriString ()
1495                 {
1496                         StringBuilder sb = new StringBuilder ();
1497                         for (int i = 0; i < 128; i++)
1498                                 sb.Append ((char) i);
1499
1500                         Assert.AreEqual (
1501 #if NET_4_0
1502                                 "%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",
1503 #else
1504                                 "%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",
1505 #endif
1506                                 Uri.EscapeUriString (sb.ToString ()));
1507
1508                         Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
1509                 }
1510
1511                 //bnc #363320
1512                 [Test]
1513                 public void TestUTF8Strings ()
1514                 {
1515                         string [] tests = {
1516                                 "file:///tmp/x (%232).jpg",
1517                                 "file:///tmp/ü (%232).jpg" };
1518
1519                         foreach (string test in tests) {
1520                                 Uri uri = new Uri (test);
1521                                 Assert.IsFalse (uri.IsWellFormedOriginalString (), "IsWellFormedOriginalString/" + test);
1522                                 Assert.AreEqual (test, uri.OriginalString, "OriginalString/" + test);
1523                                 Assert.AreEqual (test, uri.ToString (), "ToString/" + test);
1524                         }
1525                 }
1526
1527                 // This test doesn't work on Linux, and arguably shouldn't work.
1528                 // new Uri("file:///tmp/foo/bar").AbsolutePath returns "/tmp/foo/bar"
1529                 // on Linux, as anyone sane would expect.  It *doesn't* under .NET 1.1
1530                 // Apparently "tmp" is supposed to be a hostname (!)...
1531                 // Since "correct" behavior would confuse all Linux developers, and having
1532                 // an expected failure is evil, we'll just ignore this for now...
1533                 //
1534                 // Furthermore, Microsoft fixed this so it behaves sensibly in .NET 2.0.
1535                 //
1536                 // You are surrounded by conditional-compilation code, all alike.
1537                 // You are likely to be eaten by a Grue...
1538                 [Test]
1539                 public void UnixLocalPath_WTF ()
1540                 {
1541                         // Empty path == localhost, in theory
1542                         string path = "file:///tmp/foo/bar";
1543                         Uri fileUri = new Uri( path );
1544                         Assert.AreEqual ("/tmp/foo/bar", fileUri.AbsolutePath, path);
1545
1546                         // bug #76643
1547                         string path2 = "file:///foo%25bar";
1548                         fileUri = new Uri (path2);
1549                         Assert.AreEqual ("file:///foo%25bar", fileUri.ToString (), path2);
1550                 }
1551
1552                 public static void Print (Uri uri)
1553                 {
1554                         Console.WriteLine ("ToString: " + uri.ToString ());
1555
1556                         Console.WriteLine ("AbsolutePath: " + uri.AbsolutePath);
1557                         Console.WriteLine ("AbsoluteUri: " + uri.AbsoluteUri);
1558                         Console.WriteLine ("Authority: " + uri.Authority);
1559                         Console.WriteLine ("Fragment: " + uri.Fragment);
1560                         Console.WriteLine ("Host: " + uri.Host);
1561                         Console.WriteLine ("HostNameType: " + uri.HostNameType);
1562                         Console.WriteLine ("IsDefaultPort: " + uri.IsDefaultPort);
1563                         Console.WriteLine ("IsFile: " + uri.IsFile);
1564                         Console.WriteLine ("IsLoopback: " + uri.IsLoopback);
1565                         Console.WriteLine ("IsUnc: " + uri.IsUnc);
1566                         Console.WriteLine ("LocalPath: " + uri.LocalPath);
1567                         Console.WriteLine ("PathAndQuery        : " + uri.PathAndQuery);
1568                         Console.WriteLine ("Port: " + uri.Port);
1569                         Console.WriteLine ("Query: " + uri.Query);
1570                         Console.WriteLine ("Scheme: " + uri.Scheme);
1571                         Console.WriteLine ("UserEscaped: " + uri.UserEscaped);
1572                         Console.WriteLine ("UserInfo: " + uri.UserInfo);
1573
1574                         Console.WriteLine ("Segments:");
1575                         string [] segments = uri.Segments;
1576                         if (segments == null)
1577                                 Console.WriteLine ("\tNo Segments");
1578                         else
1579                                 for (int i = 0; i < segments.Length; i++)
1580                                         Console.WriteLine ("\t" + segments[i]);
1581                         Console.WriteLine ("");
1582                 }
1583
1584                 [Test]
1585                 public void FtpRootPath ()
1586                 {
1587                         Uri u = new Uri ("ftp://a.b/%2fabc/def");
1588                         string p = u.PathAndQuery;
1589                         Assert.AreEqual ("/%2fabc/def", p);
1590                         p = Uri.UnescapeDataString (p).Substring (1);
1591                         Assert.AreEqual ("/abc/def", p);
1592                         u = new Uri (new Uri ("ftp://a.b/c/d/e/f"), p);
1593                         Assert.AreEqual ("/abc/def", u.PathAndQuery);
1594                 }
1595
1596 //BNC#533572
1597                 [Test]
1598                 public void LocalPath_FileNameWithAtSign1 ()
1599                 {
1600                         string path = "/some/path/file_with_an_@_sign.mp3";
1601                         string fullpath = "http://thehost" + path;
1602                         Uri fileUri = new Uri (fullpath);
1603
1604                         Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1605                         Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1606                         Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1607                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1608                         Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1609
1610                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1611                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1612                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1613                         Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1614                 }
1615
1616                 [Test]
1617                 public void LocalPath_FileNameWithAtSign2 ()
1618                 {
1619                         string path = "/some/path/file_with_an_@_sign.mp3";
1620                         string fullpath = "http://user:password@thehost" + path;
1621                         Uri fileUri = new Uri (fullpath);
1622
1623                         Assert.AreEqual (fileUri.UserInfo, "user:password", "LocalPath_FileNameWithAtSign UserInfo");
1624                         Assert.AreEqual (fileUri.Host, "thehost", "LocalPath_FileNameWithAtSign Host");
1625                         Assert.IsFalse (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1626                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1627                         Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1628
1629                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1630                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1631                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1632                         Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1633                 }
1634
1635                 [Test]
1636                 public void LocalPath_FileNameWithAtSign3 ()
1637                 {
1638                         string path = "/some/path/file_with_an_@_sign.mp3";
1639                         string fullpath = "file://" + path;
1640                         Uri fileUri = new Uri (fullpath);
1641
1642                         Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1643                         Assert.AreEqual (fileUri.Host, String.Empty, "LocalPath_FileNameWithAtSign Host");
1644                         Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1645                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1646                         Assert.IsFalse (fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1647
1648                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1649                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1650                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1651                         Assert.AreEqual (path, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1652                 }
1653
1654                 [Test]
1655                 public void LocalPath_FileNameWithAtSign4 ()
1656                 {
1657                         string path = "/some/path/file_with_an_@_sign.mp3";
1658                         string fullpath = "file://localhost" + path;
1659                         Uri fileUri = new Uri (fullpath);
1660
1661                         Assert.AreEqual (fileUri.UserInfo, String.Empty, "LocalPath_FileNameWithAtSign UserInfo");
1662                         Assert.AreEqual (fileUri.Host, "localhost", "LocalPath_FileNameWithAtSign Host");
1663                         Assert.IsTrue (fileUri.IsFile, "LocalPath_FileNameWithAtSign IsFile");
1664                         Assert.IsTrue (fileUri.IsAbsoluteUri, "LocalPath_FileNameWithAtSign IsAbsUri");
1665                         Assert.AreEqual (isWin32, fileUri.IsUnc, "LocalPath_FileNameWithAtSign IsUnc");
1666
1667                         Assert.AreEqual (fullpath, fileUri.OriginalString, "LocalPath_FileNameWithAtSign OriginalString");
1668                         Assert.AreEqual (path, new DerivedUri (fullpath).TestUnescape (path), "LocalPath_FileNameWithAtSign ProtectedUnescape");
1669                         Assert.AreEqual (path, fileUri.AbsolutePath, "LocalPath_FileNameWithAtSign AbsPath");
1670                         //this test is marked as NotWorking below:
1671                         //Assert.AreEqual ("\\\\localhost" + path.Replace ("/", "\\"), fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1672                 }
1673
1674                 [Test]
1675                 public void LocalPath_FileNameWithAtSign5 ()
1676                 {
1677                         string path = "/some/path/file_with_an_@_sign.mp3";
1678                         string fullpath = "file://localhost" + path;
1679                         Uri fileUri = new Uri (fullpath);
1680
1681                         string expected = isWin32 ? "\\\\localhost" + path.Replace ("/", "\\") : "/some/path/file_with_an_@_sign.mp3";
1682                         Assert.AreEqual (expected, fileUri.LocalPath, "LocalPath_FileNameWithAtSign LocalPath");
1683                 }
1684
1685                 [Test]
1686                 [ExpectedException (typeof (UriFormatException))]
1687                 public void LocalPath_FileNameWithAtSign6 ()
1688                 {
1689                         string path = "/some/path/file_with_an_@_sign.mp3";
1690                         string fullpath = "file://user:password@localhost" + path;
1691                         Uri fileUri = new Uri (fullpath);
1692                 }
1693
1694                 [Test]
1695                 [Category ("NotDotNet")]
1696                 public void UnixAbsoluteFilePath_WithSpecialChars1 ()
1697                 {
1698                         Uri unixuri = new Uri ("/home/user/a@b");
1699                         Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #1");
1700                 }
1701
1702                 [Test]
1703                 [Category ("NotDotNet")]
1704                 public void UnixAbsoluteFilePath_WithSpecialChars2 ()
1705                 {
1706                         Uri unixuri = new Uri ("/home/user/a:b");
1707                         Assert.AreEqual ("file", unixuri.Scheme, "UnixAbsoluteFilePath_WithSpecialChars #2");
1708                 }
1709
1710                 [Test]
1711                 public void RelativeUriWithColons ()
1712                 {
1713                         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
1714                         new Uri (s, UriKind.Relative);
1715                         new Uri (":", UriKind.Relative);
1716                         new Uri ("1:", UriKind.Relative);
1717                 }
1718
1719                 [Test]
1720                 public void ConsecutiveSlashes ()
1721                 {
1722                         Uri uri = new Uri ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt");
1723                         Assert.AreEqual ("http://media.libsyn.com/bounce/http://cdn4.libsyn.com/nerdist/somestuff.txt", uri.ToString ());
1724                 }
1725
1726                 public class DerivedUri : Uri {
1727                         public DerivedUri (string uriString)
1728                                 : base (uriString)
1729                         {
1730                         }
1731
1732                         internal string TestUnescape (string path)
1733                         {
1734                                 return base.Unescape (path);
1735                         }
1736                 }
1737
1738                 [Test]
1739                 [ExpectedException (typeof (InvalidOperationException))]
1740                 public void GetComponents_Relative ()
1741                 {
1742                         Uri rel = new Uri ("/relative/path/with?query", UriKind.Relative);
1743                         rel.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1744                 }
1745
1746                 [Test]
1747                 public void GetComponents_AbsoluteUri ()
1748                 {
1749                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1750
1751                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", uri.AbsoluteUri, "AbsoluteUri");
1752
1753                         string safe = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.SafeUnescaped);
1754                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", safe, "SafeUnescaped");
1755
1756                         string unescaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.Unescaped);
1757                         Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc#fragment&3", unescaped, "Unescaped");
1758
1759                         string escaped = uri.GetComponents (UriComponents.AbsoluteUri, UriFormat.UriEscaped);
1760                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263", escaped, "UriEscaped");
1761                 }
1762
1763                 [Test]
1764                 public void GetComponents_HttpRequestUrl ()
1765                 {
1766                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1767
1768                         string safe = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped);
1769                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", safe, "SafeUnescaped");
1770
1771                         string unescaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.Unescaped);
1772                         Assert.AreEqual ("http://mono-project.com/list?id=1&2&sort=asc", unescaped, "Unescaped");
1773
1774                         string escaped = uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped);
1775                         Assert.AreEqual ("http://mono-project.com/list?id=1%262&sort=asc", escaped, "UriEscaped");
1776                 }
1777
1778                 [Test]
1779                 public void GetComponents_KeepDelimiter ()
1780                 {
1781                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1782
1783                         string safe = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.SafeUnescaped);
1784                         Assert.AreEqual (String.Empty, safe, "SafeUnescaped");
1785
1786                         string unescaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.Unescaped);
1787                         Assert.AreEqual (String.Empty, unescaped, "Unescaped");
1788
1789                         string escaped = uri.GetComponents (UriComponents.KeepDelimiter, UriFormat.UriEscaped);
1790                         Assert.AreEqual (String.Empty, escaped, "UriEscaped");
1791                 }
1792
1793                 [Test]
1794                 public void GetComponents_StrongAuthority ()
1795                 {
1796                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc#fragment%263");
1797
1798                         string safe = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.SafeUnescaped);
1799                         Assert.AreEqual ("mono-project.com:80", safe, "SafeUnescaped");
1800
1801                         string unescaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.Unescaped);
1802                         Assert.AreEqual ("mono-project.com:80", unescaped, "Unescaped");
1803
1804                         string escaped = uri.GetComponents (UriComponents.StrongAuthority, UriFormat.UriEscaped);
1805                         Assert.AreEqual ("mono-project.com:80", escaped, "UriEscaped");
1806                 }
1807
1808                 [Test]
1809                 public void GetComponents_Path ()
1810                 {
1811                         Uri uri1 = new Uri ("http://mono-project.com/Main%20Page");
1812                         Assert.AreEqual ("/Main Page", uri1.LocalPath, "Path1");
1813
1814                         string safe = uri1.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1815                         Assert.AreEqual ("Main Page", safe, "SafeUnescaped1");
1816
1817                         string unescaped = uri1.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1818                         Assert.AreEqual ("Main Page", unescaped, "Unescaped1");
1819
1820                         string escaped = uri1.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1821                         Assert.AreEqual ("Main%20Page", escaped, "UriEscaped1");
1822
1823                         // same result is unescaped original string
1824                         Uri uri2 = new Uri ("http://mono-project.com/Main Page");
1825                         Assert.AreEqual ("/Main Page", uri2.LocalPath, "Path2");
1826
1827                         safe = uri2.GetComponents (UriComponents.Path, UriFormat.SafeUnescaped);
1828                         Assert.AreEqual ("Main Page", safe, "SafeUnescaped2");
1829
1830                         unescaped = uri2.GetComponents (UriComponents.Path, UriFormat.Unescaped);
1831                         Assert.AreEqual ("Main Page", unescaped, "Unescaped2");
1832
1833                         escaped = uri2.GetComponents (UriComponents.Path, UriFormat.UriEscaped);
1834                         Assert.AreEqual ("Main%20Page", escaped, "UriEscaped2");
1835                 }
1836
1837                 [Test]
1838                 public void GetComponents_PathAndQuery ()
1839                 {
1840                         Uri uri = new Uri ("http://mono-project.com/MåÏn Påge?id=1%262&sort=asc");
1841
1842                         Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", uri.PathAndQuery, "PathAndQuery");
1843
1844                         string safe = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.SafeUnescaped);
1845                         Assert.AreEqual ("/MåÏn Påge?id=1%262&sort=asc", safe, "SafeUnescaped");
1846
1847                         string unescaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.Unescaped);
1848                         Assert.AreEqual ("/MåÏn Påge?id=1&2&sort=asc", unescaped, "Unescaped");
1849
1850                         string escaped = uri.GetComponents (UriComponents.PathAndQuery, UriFormat.UriEscaped);
1851                         Assert.AreEqual ("/M%C3%A5%C3%8Fn%20P%C3%A5ge?id=1%262&sort=asc", escaped, "UriEscaped");
1852                 }
1853
1854                 [Test]
1855                 public void GetComponents_Query ()
1856                 {
1857                         Uri uri = new Uri ("http://mono-project.com/list?id=1%262&sort=asc");
1858
1859                         Assert.AreEqual ("?id=1%262&sort=asc", uri.Query, "Query");
1860                         
1861                         string safe = uri.GetComponents (UriComponents.Query, UriFormat.SafeUnescaped);
1862                         Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1863
1864                         string unescaped = uri.GetComponents (UriComponents.Query, UriFormat.Unescaped);
1865                         Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1866
1867                         string escaped = uri.GetComponents (UriComponents.Query, UriFormat.UriEscaped);
1868                         Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1869                 }
1870
1871                 [Test]
1872                 public void GetComponents_Fragment ()
1873                 {
1874                         Uri uri = new Uri ("http://mono-project.com/list#id=1%262&sort=asc");
1875
1876                         Assert.AreEqual ("#id=1%262&sort=asc", uri.Fragment, "Fragment");
1877
1878                         string safe = uri.GetComponents (UriComponents.Fragment, UriFormat.SafeUnescaped);
1879                         Assert.AreEqual ("id=1%262&sort=asc", safe, "SafeUnescaped");
1880
1881                         string unescaped = uri.GetComponents (UriComponents.Fragment, UriFormat.Unescaped);
1882                         Assert.AreEqual ("id=1&2&sort=asc", unescaped, "Unescaped");
1883
1884                         string escaped = uri.GetComponents (UriComponents.Fragment, UriFormat.UriEscaped);
1885                         Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
1886                 }
1887
1888                 [Test]
1889                 // Bug #12631
1890                 public void LocalPathWithBaseUrl ()
1891                 {
1892                         var mainUri = new Uri ("http://www.imdb.com");
1893                         var uriPath = "/title/tt0106521";
1894
1895                         Uri result;
1896                         Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
1897                         Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
1898                 }
1899         }
1900 }