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