Merge pull request #3328 from BrzVlad/finalizer-thread-exited2
[mono.git] / mcs / class / System / Test / System.Net / WebRequestTest.cs
index c5b146bd6f16dfc21a5a02ab4eadc39dac626714..34ce1a370a1d4ed737bfe721ce2d4e03104c27b0 100644 (file)
 //\r
 \r
 using NUnit.Framework;\r
+using MonoTests.Helpers;
 using System;\r
 using System.Net;\r
+using System.Threading;
 using System.Collections;\r
 using System.Runtime.Serialization;\r
+using Socks = System.Net.Sockets;\r
+\r
 \r
 namespace MonoTests.System.Net {\r
 \r
@@ -44,16 +48,8 @@ namespace MonoTests.System.Net {
                [Test]\r
                public void SerializationConstructor ()\r
                {\r
-#if NET_2_0\r
                        NonAbstractWebRequest w = new NonAbstractWebRequest (null, new StreamingContext ());\r
                        Assert.IsNotNull (w);\r
-#else\r
-                       try {\r
-                               new NonAbstractWebRequest (null, new StreamingContext ());\r
-                               Assert.Fail ("#1");\r
-                       } catch (NotImplementedException) {\r
-                       }\r
-#endif\r
                }\r
 \r
                // properties (only test 'get'ter)\r
@@ -200,41 +196,39 @@ namespace MonoTests.System.Net {
        public void All ()\r
        {\r
                WebRequest req = WebRequest.Create ("http://www.contoso.com");\r
-               Assertion.Assert ("#1", req is HttpWebRequest);\r
+               Assert.IsTrue (req is HttpWebRequest, "#1");\r
                req = WebRequest.Create ("https://www.contoso.com");\r
-               Assertion.Assert ("#2", req is HttpWebRequest);\r
+               Assert.IsTrue (req is HttpWebRequest, "#2");\r
                req = WebRequest.Create ("file://www.contoso.com");\r
-               Assertion.Assert ("#3", req is FileWebRequest);\r
-#if NET_2_0\r
+               Assert.IsTrue (req is FileWebRequest, "#3");\r
                req = WebRequest.Create ("ftp://www.contoso.com");\r
-               Assertion.Assert ("#4", req is FtpWebRequest);\r
-#endif\r
+               Assert.IsTrue (req is FtpWebRequest, "#4");\r
                WebRequest.RegisterPrefix ("http://www.contoso.com", new TestWebRequestCreator ());\r
                bool ret = WebRequest.RegisterPrefix ("http://WWW.contoso.com", new TestWebRequestCreator ());\r
-               Assertion.AssertEquals ("#5a", false, ret);\r
+               Assert.AreEqual (false, ret, "#5a");\r
                ret = WebRequest.RegisterPrefix ("http://www.contoso.com/foo/bar", new TestWebRequestCreator2 ());\r
-               Assertion.AssertEquals ("#5b", true, ret);\r
+               Assert.AreEqual (true, ret, "#5b");\r
                ret = WebRequest.RegisterPrefix ("http://www", new TestWebRequestCreator3 ());\r
-               Assertion.AssertEquals ("#5c", true, ret);\r
+               Assert.AreEqual (true, ret, "#5c");\r
 \r
                req = WebRequest.Create ("http://WWW.contoso.com");\r
-               Assertion.Assert ("#6", req is TestWebRequest); \r
+               Assert.IsTrue (req is TestWebRequest, "#6"); \r
 \r
                req = WebRequest.Create ("http://WWW.contoso.com/foo/bar/index.html");\r
-               Assertion.Assert ("#7", req is TestWebRequest2); \r
+               Assert.IsTrue (req is TestWebRequest2, "#7"); \r
                \r
                req = WebRequest.Create ("http://WWW.x.com");\r
-               Assertion.Assert ("#8", req is TestWebRequest3); \r
+               Assert.IsTrue (req is TestWebRequest3, "#8"); \r
 \r
                req = WebRequest.Create ("http://WWW.c");\r
-               Assertion.Assert ("#9", req is TestWebRequest3); \r
+               Assert.IsTrue (req is TestWebRequest3, "#9"); \r
 \r
                req = WebRequest.CreateDefault (new Uri("http://WWW.contoso.com"));\r
-               Assertion.Assert ("#10", req is HttpWebRequest);\r
+               Assert.IsTrue (req is HttpWebRequest, "#10");\r
 \r
                try {\r
                        req = WebRequest.Create ("tcp://www.contoso.com");\r
-                       Assertion.Fail ("#11 should have failed with NotSupportedException");\r
+                       Assert.Fail ("#11 should have failed with NotSupportedException");\r
                } catch (NotSupportedException) {\r
                }\r
        }\r
@@ -269,7 +263,6 @@ namespace MonoTests.System.Net {
                }\r
        }\r
 \r
-#if NET_2_0\r
        [Test]\r
        public void DefaultWebProxy ()\r
        {\r
@@ -293,7 +286,6 @@ namespace MonoTests.System.Net {
                        new Uri ("http://www.mono-project.com"));\r
                Assert.IsNull (req.Proxy, "#D");\r
        }\r
-#endif\r
 \r
        [Test]\r
        public void RegisterPrefix_Creator_Null ()\r
@@ -325,6 +317,57 @@ namespace MonoTests.System.Net {
                }\r
        }\r
 \r
+       [Test] //BNC#323452\r
+       // Throws exception with Status == Timeout. The same code behaves as the test expects when run from a regular app.
+       // Might be an issue with the test suite. To investigate.
+       [Category("AndroidNotWorking")] 
+       public void TestFailedConnection ()\r
+       {\r
+               try {\r
+                       WebRequest.Create ("http://127.0.0.1:0/non-existant.txt").GetResponse ();\r
+                       Assert.Fail ("Should have raised an exception");\r
+               } catch (Exception e) {\r
+                       Assert.IsTrue (e is WebException, "Got " + e.GetType ().Name + ": " + e.Message);\r
+                       //#if NET_2_0 e.Message == "Unable to connect to the remote server"\r
+                       //#if NET_1_1 e.Message == "The underlying connection was closed: Unable to connect to the remote server."\r
+\r
+                       Assert.AreEqual (WebExceptionStatus.ConnectFailure, ((WebException)e).Status);\r
+\r
+                       //#if !NET_1_1 (this is not true in .NET 1.x)\r
+                       Assert.IsNotNull (e.InnerException);\r
+                       Assert.IsTrue (e.InnerException is Socks.SocketException, "InnerException should be SocketException");\r
+                       //e.Message == "The requested address is not valid in its context 127.0.0.1:0"\r
+                       //#endif\r
+               }\r
+       }\r
+\r
+       [Test] //BNC#323452\r
+       [Category ("AndroidNotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran
+       public void TestFailedResolution ()\r
+       {\r
+               try {\r
+                       var req = WebRequest.Create ("http://thisdomaindoesnotexist.monotestcase.x/non-existant.txt").GetResponse ();\r
+                       /*\r
+                        * Work around broken t-online.de DNS Server.\r
+                        * \r
+                        * T-Online's DNS Server for DSL Customers resolves\r
+                        * non-exisitng domain names to\r
+                        * http://navigationshilfe1.t-online.de/dnserror?url=....\r
+                        * instead of reporting an error.\r
+                        */\r
+                       if (req.ResponseUri.DnsSafeHost.Equals ("navigationshilfe1.t-online.de"))\r
+                               Assert.Ignore ("Misbehaving DNS server.");\r
+\r
+                       Assert.Fail ("Should have raised an exception");\r
+               } catch (Exception e) {\r
+                       Assert.IsTrue (e is WebException);\r
+                       //#if NET_2_0 e.Message == "The underlying connection was closed: The remote name could not be resolved."\r
+                       //#if NET_1_1 e.Message == "The remote name could not be resolved: 'thisdomaindoesnotexist.monotestcase.x'"\r
+                       Assert.AreEqual (((WebException)e).Status, WebExceptionStatus.NameResolutionFailure);\r
+                       Assert.IsNull (e.InnerException);\r
+               }\r
+       }\r
+\r
        internal class TestWebRequestCreator : IWebRequestCreate\r
        {\r
                internal TestWebRequestCreator () { }\r
@@ -369,6 +412,53 @@ namespace MonoTests.System.Net {
        {\r
                internal TestWebRequest3 () { }\r
        }\r
+
+       [Test] // Covers #41477
+       [Category ("RequiresBSDSockets")]
+       public void TestReceiveCancelation ()
+       {
+               var uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
+
+               HttpListener listener = new HttpListener ();
+               listener.Prefixes.Add (uri);
+               listener.Start ();
+
+               try {
+                       for (var i = 0; i < 10; i++) {
+                               var request = WebRequest.CreateHttp (uri);
+                               request.Method = "GET";
+
+                               var tokenSource = new CancellationTokenSource ();
+                               tokenSource.Token.Register(() => request.Abort ());
+
+                               var responseTask = request.GetResponseAsync ();
+
+                               var context = listener.GetContext ();
+                               byte[] outBuffer = new byte[8 * 1024];
+                               context.Response.OutputStream.WriteAsync (outBuffer, 0, outBuffer.Length);
+
+                               Assert.IsTrue (responseTask.Wait (1000), "Timeout #1");
+
+                               WebResponse response = responseTask.Result;
+                               var stream = response.GetResponseStream ();
+
+                               byte[] buffer = new byte[8 * 1024];
+                               var taskRead = stream.ReadAsync (buffer, 0, buffer.Length, tokenSource.Token);
+
+                               tokenSource.Cancel ();
+
+                               Assert.IsTrue (taskRead.Wait (1000), "Timeout #2");
+
+                               var byteRead = taskRead.Result;
+                       }
+               } catch (AggregateException ex) {
+                       var webEx = ex.InnerException as WebException;
+                       Assert.IsNotNull(webEx, "Inner exception is not a WebException");
+                       Assert.AreEqual (webEx.Status, WebExceptionStatus.RequestCanceled);
+               }
+
+               listener.Close ();
+       }
 }\r
 \r
 }\r