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