[System*] Add category for tests that use API that require BSD sockets.
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http / HttpClientTest.cs
index 7992ecafb20807263f94efc60b129375821c16d0..f1af864e214943a61321e2cf20cf93ff4a215cc7 100644 (file)
@@ -150,17 +150,41 @@ namespace MonoTests.System.Net.Http
                        }
                }
 
+               class ThrowOnlyProxy : IWebProxy
+               {
+                       public ICredentials Credentials {
+                               get {
+                                       throw new NotImplementedException ();
+                               }
+
+                               set {
+                                       throw new NotImplementedException ();
+                               }
+                       }
+
+                       public Uri GetProxy (Uri destination)
+                       {
+                               throw new NotImplementedException ();
+                       }
+
+                       public bool IsBypassed (Uri host)
+                       {
+                               throw new NotImplementedException ();
+                       }
+               }
+
                const int WaitTimeout = 5000;
 
-               string port, TestHost, LocalServer;
+               string TestHost, LocalServer;
+               int port;
 
                [SetUp]
                public void SetupFixture ()
                {
                        if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
-                               port = "810";
+                               port = 810;
                        } else {
-                               port = "8810";
+                               port = 8810;
                        }
 
                        TestHost = "localhost:" + port;
@@ -296,6 +320,26 @@ namespace MonoTests.System.Net.Http
                        }
                }
 
+               [Test]
+               [Category ("RequiresBSDSockets")]
+               public void Proxy_Disabled ()
+               {
+                       var pp = WebRequest.DefaultWebProxy;
+
+                       try {
+                               WebRequest.DefaultWebProxy = new ThrowOnlyProxy ();
+
+                               var request = new HttpClientHandler {
+                                       UseProxy = false
+                               };
+
+                               var client = new HttpClient (request);
+                               Assert.IsTrue (client.GetAsync ("http://google.com").Wait (5000), "needs internet access");
+                       } finally {
+                               WebRequest.DefaultWebProxy = pp;
+                       }
+               }
+
                [Test]
                public void Send ()
                {
@@ -354,6 +398,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_Default ()
                {
                        bool? failed = null;
@@ -398,6 +443,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_Version_1_0 ()
                {
                        bool? failed = null;
@@ -445,6 +491,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_ClientHandlerSettings ()
                {
                        bool? failed = null;
@@ -509,6 +556,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_CustomHeaders ()
                {
                        bool? failed = null;
@@ -573,6 +621,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_CustomHeaders_SpecialSeparators ()
                {
                        bool? failed = null;
@@ -607,6 +656,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_CustomHeaders_Host ()
                {
                        bool? failed = null;
@@ -640,6 +690,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Transfer_Encoding_Chunked ()
                {
                        bool? failed = null;
@@ -669,6 +720,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Transfer_Encoding_Custom ()
                {
                        bool? failed = null;
@@ -697,6 +749,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_Content ()
                {
                        var listener = CreateListener (l => {
@@ -724,6 +777,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_Content_MaxResponseContentBufferSize ()
                {
                        var listener = CreateListener (l => {
@@ -746,6 +800,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_Content_MaxResponseContentBufferSize_Error ()
                {
                        var listener = CreateListener (l => {
@@ -772,6 +827,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_NoContent ()
                {
                        foreach (var method in new HttpMethod[] { HttpMethod.Post, HttpMethod.Put, HttpMethod.Delete }) {
@@ -804,6 +860,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Complete_Error ()
                {
                        var listener = CreateListener (l => {
@@ -824,6 +881,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Content_Get ()
                {
                        var listener = CreateListener (l => {
@@ -843,6 +901,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Content_BomEncoding ()
                {
                        var listener = CreateListener (l => {
@@ -867,6 +926,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Content_Put ()
                {
                        bool passed = false;
@@ -892,6 +952,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void Send_Content_Put_CustomStream ()
                {
                        bool passed = false;
@@ -994,15 +1055,37 @@ namespace MonoTests.System.Net.Http
 
                [Test]
                [Category ("MobileNotWorking")] // Missing encoding
+               [Category ("RequiresBSDSockets")]
                public void GetString_Many ()
                {
-                       var client = new HttpClient ();
-                       var t1 = client.GetStringAsync ("http://example.org");
-                       var t2 = client.GetStringAsync ("http://example.org");
-                       Assert.IsTrue (Task.WaitAll (new [] { t1, t2 }, WaitTimeout));          
+                       Action<HttpListenerContext> context = (HttpListenerContext l) => {
+                               var response = l.Response;
+                               response.StatusCode = 200;
+                               response.OutputStream.WriteByte (0x68);
+                               response.OutputStream.WriteByte (0x65);
+                               response.OutputStream.WriteByte (0x6c);
+                               response.OutputStream.WriteByte (0x6c);
+                               response.OutputStream.WriteByte (0x6f);
+                       };
+
+                       var listener = CreateListener (context); // creates a default request handler
+                       AddListenerContext (listener, context);  // add another request handler for the second request
+
+                       try {
+                               var client = new HttpClient ();
+                               var t1 = client.GetStringAsync (LocalServer);
+                               var t2 = client.GetStringAsync (LocalServer);
+                               Assert.IsTrue (Task.WaitAll (new [] { t1, t2 }, WaitTimeout));
+                               Assert.AreEqual ("hello", t1.Result, "#1");
+                               Assert.AreEqual ("hello", t2.Result, "#2");
+                       } finally {
+                               listener.Abort ();
+                               listener.Close ();
+                       }
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void GetByteArray_ServerError ()
                {
                        var listener = CreateListener (l => {
@@ -1025,6 +1108,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void DisallowAutoRedirect ()
                {
                        var listener = CreateListener (l => {
@@ -1053,6 +1137,7 @@ namespace MonoTests.System.Net.Http
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                public void RequestUriAfterRedirect ()
                {
                        var listener = CreateListener (l => {
@@ -1060,9 +1145,20 @@ namespace MonoTests.System.Net.Http
                                var response = l.Response;
 
                                response.StatusCode = (int)HttpStatusCode.Moved;
-                               response.RedirectLocation = "http://xamarin.com/";
+                               response.RedirectLocation = "http://localhost:8811/";
                        });
 
+                       var listener2 = CreateListener (l => {
+                               var response = l.Response;
+
+                               response.StatusCode = (int)HttpStatusCode.OK;
+                               response.OutputStream.WriteByte (0x68);
+                               response.OutputStream.WriteByte (0x65);
+                               response.OutputStream.WriteByte (0x6c);
+                               response.OutputStream.WriteByte (0x6c);
+                               response.OutputStream.WriteByte (0x6f);
+                       }, 8811);
+
                        try {
                                var chandler = new HttpClientHandler ();
                                chandler.AllowAutoRedirect = true;
@@ -1071,14 +1167,18 @@ namespace MonoTests.System.Net.Http
                                var r = client.GetAsync (LocalServer);
                                Assert.IsTrue (r.Wait (WaitTimeout), "#1");
                                var resp = r.Result;
-                               Assert.AreEqual ("http://xamarin.com/", resp.RequestMessage.RequestUri.AbsoluteUri, "#2");
+                               Assert.AreEqual ("http://localhost:8811/", resp.RequestMessage.RequestUri.AbsoluteUri, "#2");
+                               Assert.AreEqual ("hello", resp.Content.ReadAsStringAsync ().Result, "#3");
                        } finally {
                                listener.Abort ();
                                listener.Close ();
+                               listener2.Abort ();
+                               listener2.Close ();
                        }
                }
 
                [Test]
+               [Category ("RequiresBSDSockets")]
                /*
                 * Properties may only be modified before sending the first request.
                 */
@@ -1131,10 +1231,22 @@ namespace MonoTests.System.Net.Http
                }
 
                HttpListener CreateListener (Action<HttpListenerContext> contextAssert)
+               {
+                       return CreateListener (contextAssert, port);
+               }
+
+               HttpListener CreateListener (Action<HttpListenerContext> contextAssert, int port)
                {
                        var l = new HttpListener ();
                        l.Prefixes.Add (string.Format ("http://+:{0}/", port));
                        l.Start ();
+                       AddListenerContext(l, contextAssert);
+
+                       return l;
+               }
+
+               HttpListener AddListenerContext (HttpListener l, Action<HttpListenerContext> contextAssert)
+               {
                        l.BeginGetContext (ar => {
                                var ctx = l.EndGetContext (ar);