Merge pull request #2663 from esdrubal/islocal
[mono.git] / mcs / class / System / Test / System.Net / HttpListenerRequestTest.cs
index 260d85f908313de60f7d8b6af403f10bd1b66156..c088e4e14e2ddeb71509bbda356c4d7761d12c66 100644 (file)
@@ -36,6 +36,8 @@ using System.Collections.Generic;
 
 using NUnit.Framework;
 
+using MonoTests.Helpers;
+
 namespace MonoTests.System.Net
 {
        [TestFixture]
@@ -48,12 +50,12 @@ namespace MonoTests.System.Net
                        HttpListenerContext ctx;
                        HttpListenerRequest request;
                        NetworkStream ns;
-
+                       var port = NetworkHelpers.FindFreePort ();
                        HttpListener listener = HttpListener2Test.CreateAndStartListener (
-                               "http://127.0.0.1:9000/HasEntityBody/");
+                               "http://127.0.0.1:" + port + "/HasEntityBody/");
 
                        // POST with non-zero Content-Lenth
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -61,7 +63,7 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // POST with zero Content-Lenth
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -69,7 +71,7 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // POST with chunked encoding
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -77,7 +79,7 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // GET with no Content-Length
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -85,7 +87,7 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // GET with non-zero Content-Length
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -93,7 +95,7 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // GET with zero Content-Length
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -101,7 +103,7 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // GET with chunked encoding
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -109,35 +111,35 @@ namespace MonoTests.System.Net
                        HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
 
                        // PUT with non-zero Content-Lenth
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
                        Assert.IsTrue (request.HasEntityBody, "#H");
 
                        // PUT with zero Content-Lenth
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
                        Assert.IsFalse (request.HasEntityBody, "#I");
 
                        // INVALID with non-zero Content-Lenth
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
                        Assert.IsTrue (request.HasEntityBody, "#J");
 
                        // INVALID with zero Content-Lenth
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
                        Assert.IsFalse (request.HasEntityBody, "#K");
 
                        // INVALID with chunked encoding
-                       ns = HttpListener2Test.CreateNS (9000);
+                       ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
                        ctx = listener.GetContext ();
                        request = ctx.Request;
@@ -149,9 +151,10 @@ namespace MonoTests.System.Net
                [Test]
                public void HttpMethod ()
                {
+                       var port = NetworkHelpers.FindFreePort ();
                        HttpListener listener = HttpListener2Test.CreateAndStartListener (
-                               "http://127.0.0.1:9000/HttpMethod/");
-                       NetworkStream ns = HttpListener2Test.CreateNS (9000);
+                               "http://127.0.0.1:" + port + "/HttpMethod/");
+                       NetworkStream ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
                        HttpListenerContext ctx = listener.GetContext ();
                        HttpListenerRequest request = ctx.Request;
@@ -162,10 +165,11 @@ namespace MonoTests.System.Net
                [Test]
                public void HttpBasicAuthScheme ()
                {
-                       HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:9000/authTest/", AuthenticationSchemes.Basic);
+                       var port = NetworkHelpers.FindFreePort ();                      
+                       HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:" + port + "/authTest/", AuthenticationSchemes.Basic);
                        //dummy-wait for context
                        listener.BeginGetContext (null, listener);
-                       NetworkStream ns = HttpListener2Test.CreateNS (9000);
+                       NetworkStream ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
                        String response = HttpListener2Test.Receive (ns, 512);
                        Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
@@ -176,9 +180,10 @@ namespace MonoTests.System.Net
                [Test]
                public void HttpRequestUriIsNotDecoded ()
                {
+                       var port = NetworkHelpers.FindFreePort ();
                        HttpListener listener = HttpListener2Test.CreateAndStartListener (
-                               "http://127.0.0.1:9000/RequestUriDecodeTest/");
-                       NetworkStream ns = HttpListener2Test.CreateNS (9000);
+                               "http://127.0.0.1:" + port + "/RequestUriDecodeTest/");
+                       NetworkStream ns = HttpListener2Test.CreateNS (port);
                        HttpListener2Test.Send (ns, "GET /RequestUriDecodeTest/?a=b&c=d%26e HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
                        HttpListenerContext ctx = listener.GetContext ();
                        HttpListenerRequest request = ctx.Request;
@@ -209,7 +214,7 @@ namespace MonoTests.System.Net
                [Test] // #29927
                public void HttpRequestUriUnescape ()
                {
-                       var prefix = "http://localhost:12345/";
+                       var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
                        var key = "Product/1";
 
                        var expectedUrl = prefix + key + "/";