Merge pull request #1222 from LogosBible/uri-trycreate
[mono.git] / mcs / class / System / Test / System / UriTest3.cs
index 98cf9ec63e6fa739aa09b90cf988f8db393538b2..7ff8924d0daaed4c9091dbdcbba456fa6b3a0323 100644 (file)
@@ -26,8 +26,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using NUnit.Framework;
 
 using System;
@@ -183,6 +181,15 @@ namespace MonoTests.System
                        uri = new Uri ("http://dummy.com");
                        Assert.IsTrue (Uri.TryCreate (relative, UriKind.Relative, out uri), "relative-Relative");
                        Assert.AreEqual (relative, uri.OriginalString, "relative-RelativeOrAbsolute-OriginalString");
+
+                       Assert.IsTrue (Uri.TryCreate ("http://mono-project.com/☕", UriKind.Absolute, out uri), "highunicode-Absolute");
+                       Assert.AreEqual("http://mono-project.com/%E2%98%95", uri.AbsoluteUri, "highunicode-Absolute-AbsoluteUri");
+
+                       string mixedCaseUri = "http://mOnO-proJECT.com";
+                       uri = new Uri (mixedCaseUri);
+                       Uri uri2;
+                       Assert.IsTrue (Uri.TryCreate (mixedCaseUri, UriKind.Absolute, out uri2), "mixedcasehost-absolute");
+                       Assert.AreEqual (uri.AbsoluteUri, uri2.AbsoluteUri, "mixedcasehost-absoluteuri-absoluteuri");
                }
 
                [Test] // TryCreate (String, UriKind, Uri)
@@ -213,7 +220,6 @@ namespace MonoTests.System
                }
 
                [Test] // TryCreate (Uri, String, Uri)
-               [Category ("NotWorking")]
                public void TryCreate2 ()
                {
                        Uri baseUri = new Uri (absolute);
@@ -253,7 +259,6 @@ namespace MonoTests.System
                }
 
                [Test] // TryCreate (Uri, Uri, Uri)
-               [Category ("NotWorking")]
                public void TryCreate3 ()
                {
                        Uri baseUri = new Uri (absolute);
@@ -292,8 +297,16 @@ namespace MonoTests.System
                        Uri baseUri = new Uri (absolute);
                        try {
                                Uri.TryCreate (baseUri, (Uri) null, out uri);
-                               Assert.Fail ();
-                       } catch (NullReferenceException) {
+#if NET_4_0
+                               Assert.IsNull (uri);
+#else
+                               Assert.Fail ("throw NRE under FX 2.0");
+#endif
+                       }
+                       catch (NullReferenceException) {
+#if NET_4_0
+                               Assert.Fail ("does not throw NRE under FX 4.0");
+#endif
                        }
                }
 
@@ -333,6 +346,13 @@ namespace MonoTests.System
                        Uri u2 = null;
                        Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "null-null");
 
+                       u1 = new Uri ("http://www.go-mono.com");
+                       Assert.AreEqual (1, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "non-null - null");
+
+                       u1 = null;
+                       u2 = new Uri ("http://www.go-mono.com");
+                       Assert.AreEqual (-1, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.UriEscaped, StringComparison.CurrentCulture), "null - non-null");
+
                        u1 = new Uri ("http://www.go-mono.com/Main Page");
                        u2 = new Uri ("http://www.go-mono.com/Main%20Page");
                        Assert.AreEqual (0, Uri.Compare (u1, u2, UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.CurrentCulture), "http/space-http/%20-unescaped");
@@ -410,7 +430,8 @@ namespace MonoTests.System
                        try {
                                http.IsBaseOf (null);
                                Assert.Fail ();
-                       } catch (NullReferenceException) {
+                       }
+                       catch (ArgumentNullException) {
                        }
                }
 
@@ -429,6 +450,9 @@ namespace MonoTests.System
                        Uri uri11 = new Uri ("mailto:xxx@xxx.com?subject=hola");
                        Uri uri12 = new Uri ("mailto:xxx@mail.xxx.com?subject=hola");
                        Uri uri13 = new Uri ("mailto:xxx@xxx.com/foo/bar");
+                       Uri uri14 = new Uri ("http://www.contoso.com/test1/");
+                       Uri uri15 = new Uri ("http://www.contoso.com/");
+                       Uri uri16 = new Uri ("http://www.contoso.com/test");
 
                        AssertRelativeUri ("foo/bar/index.htm#fragment", uri1, uri2, "#A");
                        AssertRelativeUri ("../../index.htm?x=2", uri2, uri1, "#B");
@@ -460,35 +484,84 @@ namespace MonoTests.System
                        Assert.IsTrue (relativeUri.IsAbsoluteUri, "#N1");
                        Assert.AreEqual (uri5.ToString (), relativeUri.ToString (), "#N2");
                        Assert.AreEqual (uri5.OriginalString, relativeUri.OriginalString, "#N3");
+
+                       AssertRelativeUri ("../", uri14, uri15, "#O");
+                       AssertRelativeUri ("./", uri16, uri15, "#P");
+
+                       Uri a1 = new Uri ("http://something/something2/test/");
+                       Uri a2 = new Uri ("http://something/something2/");
+                       Uri a3 = new Uri ("http://something/something2/test");
+                       Uri a4 = new Uri ("http://something/something2");
+
+                       AssertRelativeUri ("../", a1, a2, "Q1");
+                       AssertRelativeUri ("../../something2", a1, a4, "Q2");
+                       AssertRelativeUri ("./", a3, a2, "Q3");
+                       AssertRelativeUri ("../something2", a3, a4, "Q4");
+
+                       Uri b1 = new Uri ("http://something/test/");
+                       Uri b2 = new Uri ("http://something/");
+                       Uri b3 = new Uri ("http://something/test");
+                       Uri b4 = new Uri ("http://something");
+                       
+                       AssertRelativeUri ("../", b1, b2, "R1");
+                       AssertRelativeUri ("../", b1, b4, "R2");
+                       AssertRelativeUri ("./", b3, b2, "R3");
+                       AssertRelativeUri ("./", b3, b4, "R4");
+
+                       Uri c1 = new Uri ("C:\\something\\something2\\test\\");
+                       Uri c2 = new Uri ("C:\\something\\something2\\");
+                       Uri c3 = new Uri ("C:\\something\\something2\\test");
+                       Uri c4 = new Uri ("C:\\something\\something2");
+                       
+                       AssertRelativeUri ("../", c1, c2, "S1");
+                       AssertRelativeUri ("../../something2", c1, c4, "S2");
+                       AssertRelativeUri ("./", c3, c2, "S3");
+                       AssertRelativeUri ("../something2", c3, c4, "S4");
+
+                       Uri d1 = new Uri ("C:\\something\\test\\");
+                       Uri d2 = new Uri ("C:\\something\\");
+                       Uri d3 = new Uri ("C:\\something\\test");
+                       Uri d4 = new Uri ("C:\\something");
+                       
+                       AssertRelativeUri ("../", d1, d2, "T1");
+                       AssertRelativeUri ("../../something", d1, d4, "T2");
+                       AssertRelativeUri ("./", d3, d2, "T3");
+                       AssertRelativeUri ("../something", d3, d4, "T4");
+
+                       Uri e1 = new Uri ("C:\\something\\");
+                       Uri e2 = new Uri ("C:\\");
+                       Uri e3 = new Uri ("C:\\something");
+                       
+                       AssertRelativeUri ("../", e1, e2, "U1");
+                       AssertRelativeUri ("./", e3, e2, "U2");
+                       AssertRelativeUri ("", e1, e1, "U3");
+                       AssertRelativeUri ("", e3, e3, "U4");
+                       AssertRelativeUri ("../something", e1, e3, "U5");
+                       AssertRelativeUri ("something/", e3, e1, "U6");
+                       AssertRelativeUri ("something", e2, e3, "U7");
                }
 
                [Test]
-               [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=299942
-               public void MakeRelativeUri_Uri_Null_Mono ()
+               public void MakeRelativeUri_Uri_Null ()
                {
                        Uri uri = new Uri ("http://test.com");
                        try {
                                uri.MakeRelativeUri ((Uri) null);
                                Assert.Fail ("#1");
-                       } catch (ArgumentNullException ex) {
+                       }
+#if NET_4_0
+                       catch (ArgumentNullException ex) {
                                Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
                                Assert.IsNull (ex.InnerException, "#3");
                                Assert.IsNotNull (ex.Message, "#4");
                                Assert.IsNotNull (ex.ParamName, "#5");
                                Assert.AreEqual ("uri", ex.ParamName, "#6");
                        }
-               }
-
-               [Test]
-               [Category ("NotWorking")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=299942
-               public void MakeRelativeUri_Uri_Null_MS ()
-               {
-                       Uri uri = new Uri ("http://test.com");
-                       try {
-                               uri.MakeRelativeUri ((Uri) null);
-                               Assert.Fail ("#1");
-                       } catch (NullReferenceException) {
+#else
+                       catch (NullReferenceException) {
+                               // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=299942
                        }
+#endif
                }
 
                [Test] // LAMESPEC: see bug #321113
@@ -500,7 +573,7 @@ namespace MonoTests.System
 
                        Uri a = new Uri ("http://www.mono-project.com:808/foo");
                        Uri b = new Uri (a, "../docs?queryyy#% %20%23%25bar");
-                       //Assert.AreEqual ("http://www.mono-project.com:808/docs?queryyy#% %20%23%25bar", b.OriginalString, "#2");
+                       Assert.AreEqual ("http://www.mono-project.com:808/docs?queryyy#% %20%23%25bar", b.OriginalString, "#2");
 
                        Uri c = new Uri ("http://www.mono-project.com:808/foo");
                        Uri d = new Uri (a, "../docs?queryyy#%20%23%25bar");
@@ -620,7 +693,19 @@ namespace MonoTests.System
                        {
                        }
                }
+
+               [Test]
+               public void DomainLabelLength ()
+               {
+                       UriHostNameType type = Uri.CheckHostName ("3.141592653589793238462643383279502884197169399375105820974944592.com");
+                       Assert.AreEqual (UriHostNameType.Dns, type, "DomainLabelLength#1");
+                       type = Uri.CheckHostName ("3141592653589793238462643383279502884197169399375105820974944592.com");
+                       Assert.AreEqual (UriHostNameType.Unknown, type, "DomainLabelLength#2");
+                       type = Uri.CheckHostName ("3.1415926535897932384626433832795028841971693993751058209749445923.com");
+                       Assert.AreEqual (UriHostNameType.Unknown, type, "DomainLabelLength#2");
+                       type = Uri.CheckHostName ("3.141592653589793238462643383279502884197169399375105820974944592._om");
+                       Assert.AreEqual (UriHostNameType.Unknown, type, "DomainLabelLength#3");
+               }
        }
 }
 
-#endif