Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mcs / class / System / Test / System / UriTest.cs
index 508e1307993018782d889aeea5da971a371de287..beebf4dfc5b1b547974c42da95cc52ce84ca7126 100644 (file)
@@ -10,6 +10,7 @@
 // (C) 2003 Ben Maurer
 //
 
+using System.Reflection;
 using NUnit.Framework;
 using System;
 using System.IO;
@@ -21,11 +22,20 @@ namespace MonoTests.System
        public class UriTest
        {
                protected bool isWin32 = false;
+               public bool IriParsing;
 
                [TestFixtureSetUp]
                public void GetReady ()
                {
                        isWin32 = (Path.DirectorySeparatorChar == '\\');
+
+                       //Make sure Uri static constructor is called
+                       Uri.EscapeDataString ("");
+
+                       FieldInfo iriParsingField = typeof (Uri).GetField ("s_IriParsing",
+                               BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
+                       if (iriParsingField != null)
+                               IriParsing = (bool)iriParsingField.GetValue (null);
                }
 
                [Test]
@@ -166,10 +176,6 @@ namespace MonoTests.System
 
                        uri = new Uri (new Uri("http://www.xxx.com"), "?x=0");
                        Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel30");
-#if !NET_4_0
-                       uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "?x=0");
-                       Assert.AreEqual ("http://www.xxx.com/?x=0", uri.ToString(), "#rel31");
-#endif
                        uri = new Uri (new Uri("http://www.xxx.com/index.htm"), "#here");
                        Assert.AreEqual ("http://www.xxx.com/index.htm#here", uri.ToString(), "#rel32");
 
@@ -233,9 +239,6 @@ namespace MonoTests.System
                        Uri b = new Uri ("http://a/b/c/d;p?q");
                        Assert.AreEqual ("http://a/g", new Uri (b, "/g").ToString (), "#1");
                        Assert.AreEqual ("http://g/", new Uri (b, "//g").ToString (), "#2");
-#if !NET_4_0
-                       Assert.AreEqual ("http://a/b/c/?y", new Uri (b, "?y").ToString (), "#3");
-#endif
                        Assert.IsTrue (new Uri (b, "#s").ToString ().EndsWith ("#s"), "#4");
 
                        Uri u = new Uri (b, "/g?q=r");
@@ -557,12 +560,10 @@ namespace MonoTests.System
                        Assert.AreEqual ("/", uri.AbsolutePath, "#7e");
                        Assert.AreEqual ("/", uri.PathAndQuery, "#7f");
                        Assert.AreEqual ("file://one_file.txt/", uri.GetLeftPart (UriPartial.Path), "#7g");
-#if !TARGET_JVM
                        if (isWin32)
                                Assert.AreEqual ("\\\\one_file.txt\\", uri.LocalPath, "#7b");
                        else
                                Assert.AreEqual ("/", uri.LocalPath, "#7b");
-#endif
                        Assert.AreEqual ("file", uri.Scheme, "#7c");
                        Assert.AreEqual ("one_file.txt", uri.Host, "#7d");
                }
@@ -604,7 +605,11 @@ namespace MonoTests.System
                        Uri u1 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx");
                        Uri u2 = new Uri("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx");
 
-                       Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
+                       if (IriParsing)
+                               Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%2fSearchDoc%2fSearcher.aspx", u1.ToString (), "QE1");
+                       else
+                               Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=/SearchDoc/Searcher.aspx", u1.ToString (), "QE1");
+
                        Assert.AreEqual ("http://localhost:8080/test.aspx?ReturnUrl=%252fSearchDoc%252fSearcher.aspx", u2.ToString (), "QE2");
                }
 
@@ -688,10 +693,10 @@ namespace MonoTests.System
                {
                        Assert.AreEqual ("#", UriEx.UnescapeString ("file://localhost/c#", "%23"), "#1");
                        Assert.AreEqual ("c#", UriEx.UnescapeString ("file://localhost/c#", "c%23"), "#2");
-                       Assert.AreEqual ("\xA9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
                        Assert.AreEqual ("#", UriEx.UnescapeString ("http://localhost/c#", "%23"), "#1");
                        Assert.AreEqual ("c#", UriEx.UnescapeString ("http://localhost/c#", "c%23"), "#2");
-                       Assert.AreEqual ("\xA9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
+                       Assert.AreEqual ("%A9", UriEx.UnescapeString ("file://localhost/c#", "%A9"), "#3");
+                       Assert.AreEqual ("%A9", UriEx.UnescapeString ("http://localhost/c#", "%A9"), "#3");
                }
 
                [Test]
@@ -767,9 +772,7 @@ namespace MonoTests.System
                        // 2-byte escape sequence, 2 individual characters
                        uri = new Uri ("file:///foo/a%C2%F8b", true);
                        path = uri.LocalPath;
-                       Assert.AreEqual (9, path.Length, "#7");
-                       Assert.AreEqual (0xC2, path [6], "#8");
-                       Assert.AreEqual (0xF8, path [7], "#9");
+                       Assert.AreEqual ("/foo/a%C2%F8b", path, "#7");
                }
 
                [Test]
@@ -844,7 +847,12 @@ namespace MonoTests.System
                public void Fragment_Escape ()
                {
                        Uri u = new Uri("http://localhost/index.asp#main#start", false);
-                       Assert.AreEqual (u.Fragment, "#main%23start", "#1");
+
+#if NET_4_5
+                               Assert.AreEqual (u.Fragment, "#main#start", "#1");
+#else
+                               Assert.AreEqual (u.Fragment, "#main%23start", "#1");
+#endif
 
                        u = new Uri("http://localhost/index.asp#main#start", true);
                        Assert.AreEqual (u.Fragment, "#main#start", "#2");
@@ -853,7 +861,11 @@ namespace MonoTests.System
 
                        Uri b = new Uri ("http://www.gnome.org");
                        Uri n = new Uri (b, "blah#main#start");
-                       Assert.AreEqual (n.Fragment, "#main%23start", "#3");
+#if NET_4_5
+                               Assert.AreEqual (n.Fragment, "#main#start", "#3");
+#else
+                               Assert.AreEqual (n.Fragment, "#main%23start", "#3");
+#endif
 
                        n = new Uri (b, "blah#main#start", true);
                        Assert.AreEqual (n.Fragment, "#main#start", "#4");
@@ -1071,8 +1083,14 @@ namespace MonoTests.System
                        // IPv6 Address
                        Uri ftp = new Uri ("FTP://[::ffFF:169.32.14.5]/");
                        Assert.AreEqual ("ftp", ftp.Scheme, "#7");
+
+#if NET_4_5
+                       Assert.AreEqual ("[::ffff:169.32.14.5]", ftp.Host, "#8");
+                       Assert.AreEqual ("ftp://[::ffff:169.32.14.5]/", ftp.ToString (), "#9");
+#else
                        Assert.AreEqual ("[0000:0000:0000:0000:0000:FFFF:A920:0E05]", ftp.Host, "#8");
                        Assert.AreEqual ("ftp://[0000:0000:0000:0000:0000:FFFF:A920:0E05]/", ftp.ToString (), "#9");
+#endif
                }
 
                [Test]
@@ -1194,7 +1212,15 @@ namespace MonoTests.System
                }
 
                [Test]
+               [Category ("NotWorking")]
                public void RelativeUri ()
+               {
+                       var u = new Uri ("/foo/bar");
+                       Assert.IsFalse (u.IsAbsoluteUri, "#1");
+               }
+
+               [Test]
+               public void RelativeFragmentUri ()
                {
                        Uri u = new Uri("http://localhost/../../../a");
                        Assert.AreEqual ("http://localhost/a", u.ToString ());
@@ -1204,7 +1230,7 @@ namespace MonoTests.System
                }
 
                [Test]
-               public void RelativeUri2 ()
+               public void RelativeFragmentUri2 ()
                {
                        Assert.AreEqual ("hoge:ext", new Uri (new Uri ("hoge:foo:bar:baz"), "hoge:ext").ToString (), "#1");
                        if (isWin32) {
@@ -1388,6 +1414,13 @@ namespace MonoTests.System
                        new Uri ("hey");
                }
 
+               [Test]
+               public void SchemeWithDigits ()
+               {
+                       Uri uri = new Uri ("net.p2p://foobar");
+                       Assert.AreEqual ("net.p2p", uri.Scheme);
+               }
+
                // on .NET 2.0 a port number is limited to UInt16.MaxValue
                [ExpectedException (typeof (UriFormatException))]
                [Test]
@@ -1472,13 +1505,15 @@ namespace MonoTests.System
                        for (int i = 0; i < 128; i++)
                                sb.Append ((char) i);
 
+#if NET_4_5
                        Assert.AreEqual (
-#if NET_4_0
                                "%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",
+                               Uri.EscapeDataString (sb.ToString ()));
 #else
+                       Assert.AreEqual (
                                "%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",
+                               Uri.EscapeDataString (sb.ToString ()));
 #endif
-                                Uri.EscapeDataString (sb.ToString ()));
 
                        Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
                }
@@ -1489,13 +1524,15 @@ namespace MonoTests.System
                        for (int i = 0; i < 128; i++)
                                sb.Append ((char) i);
 
+#if NET_4_5
                        Assert.AreEqual (
-#if NET_4_0
                                "%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",
+                               Uri.EscapeUriString (sb.ToString ()));
 #else
+                       Assert.AreEqual (
                                "%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",
-#endif
                                Uri.EscapeUriString (sb.ToString ()));
+#endif
 
                        Assert.AreEqual ("%C3%A1", Uri.EscapeDataString ("á"));
                }
@@ -1877,6 +1914,20 @@ namespace MonoTests.System
                        Assert.AreEqual ("id=1%262&sort=asc", escaped, "UriEscaped");
                }
 
+               // When used, paths such as "/foo" are assumed relative.
+               static UriKind DotNetRelativeOrAbsolute = (Type.GetType ("Mono.Runtime") == null)? UriKind.RelativeOrAbsolute : (UriKind) 300;
+
+               [Test]
+               public void DotNetRelativeOrAbsoluteTest ()
+               {
+                       var uri1 = new Uri ("/foo", DotNetRelativeOrAbsolute);
+                       Assert.IsFalse (uri1.IsAbsoluteUri);
+                       
+                       Uri uri2;
+                       Uri.TryCreate("/foo", DotNetRelativeOrAbsolute, out uri2);
+                       Assert.IsFalse (uri2.IsAbsoluteUri);
+               }
+
                [Test]
                // Bug #12631
                public void LocalPathWithBaseUrl ()
@@ -1888,5 +1939,82 @@ namespace MonoTests.System
                        Assert.IsTrue (Uri.TryCreate (mainUri, uriPath, out result), "#1");
                        Assert.AreEqual ("http://www.imdb.com/title/tt0106521", result.ToString (), "#2");
                }
+
+               [Test]
+               public void GetSerializationInfoStringOnRelativeUri ()
+               {
+                       var uri = new Uri ("/relative/path", UriKind.Relative);
+                       var result = uri.GetComponents (UriComponents.SerializationInfoString, UriFormat.UriEscaped);
+
+                       Assert.AreEqual (uri.OriginalString, result);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void GetSerializationInfoStringException ()
+               {
+                       var uri = new Uri ("/relative/path", UriKind.Relative);
+                       uri.GetComponents (UriComponents.SerializationInfoString  | UriComponents.Host, UriFormat.UriEscaped);
+               }
+
+               [Test]
+               public void UserInfo_EscapedLetter ()
+               {
+                       var uri = new Uri ("https://first%61second@host");
+                       Assert.AreEqual ("firstasecond", uri.UserInfo);
+               }
+
+               [Test]
+               public void UserInfo_EscapedAt ()
+               {
+                       var userinfo =  "first%40second";
+                       var uri = new Uri ("https://" + userinfo + "@host");
+                       Assert.AreEqual (userinfo, uri.UserInfo);
+               }
+
+               [Test]
+               public void UserInfo_EscapedChars ()
+               {
+                       for (var ch = (char) 1; ch < 128; ch++) {
+                               var userinfo = Uri.EscapeDataString (ch.ToString ());
+                               try {
+                                       new Uri (string.Format("http://{0}@localhost:80/", userinfo));
+                               } catch (Exception e) {
+                                       Assert.Fail (string.Format("Unexpected {0} while building URI with username {1}", e.GetType ().Name, userinfo));
+                               }
+                       }
+               }
+       }
+
+       // Tests non default IriParsing
+       [TestFixture]
+       public class UriTestAux : UriTest
+       {
+               private FieldInfo iriParsingField;
+               private bool originalIriParsing;
+
+               [TestFixtureSetUp]
+               public void GetReady2 ()
+               {
+                       isWin32 = (Path.DirectorySeparatorChar == '\\');
+
+                       //Make sure Uri static constructor is called
+                       Uri.EscapeDataString ("");
+
+                       iriParsingField = typeof (Uri).GetField ("s_IriParsing",
+                               BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
+
+                       originalIriParsing = (bool) iriParsingField.GetValue (null);
+
+                       IriParsing = !originalIriParsing;
+
+                       iriParsingField.SetValue (null, IriParsing);
+               }
+
+               [TestFixtureTearDown]
+               public void TearDown ()
+               {
+                       iriParsingField.SetValue (null, originalIriParsing);
+               }
        }
 }