[System] Test HttpListenerRequest.IsLocal.
authorMarcos Henrich <marcos.henrich@xamarin.com>
Mon, 22 Feb 2016 15:43:00 +0000 (15:43 +0000)
committerMarcos Henrich <marcos.henrich@xamarin.com>
Mon, 22 Feb 2016 15:43:00 +0000 (15:43 +0000)
Covers #38322.

mcs/class/System/Test/System.Net/HttpListener2Test.cs
mcs/class/System/Test/System.Net/HttpListenerRequestTest.cs

index 6e459b9d07896f0cf07b83e37b9dfdd9d2848656..3fd408510e4972836100346a870cead721a8e793 100644 (file)
@@ -79,13 +79,23 @@ namespace MonoTests.System.Net {
 
                public static MyNetworkStream CreateNS (int port)
                {
-                       return CreateNS (port, 5000);
+                       return CreateNS (IPAddress.Loopback, port, 5000);
                }
 
                public static MyNetworkStream CreateNS (int port, int timeout_ms)
+               {
+                       return CreateNS (IPAddress.Loopback, port, timeout_ms);
+               }
+
+               public static MyNetworkStream CreateNS (IPAddress ip, int port)
+               {
+                       return CreateNS (ip, port, 5000);
+               }
+
+               public static MyNetworkStream CreateNS (IPAddress ip, int port, int timeout_ms)
                {
                        Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-                       sock.Connect (new IPEndPoint (IPAddress.Loopback, port));
+                       sock.Connect (new IPEndPoint (ip, port));
                        sock.SendTimeout = timeout_ms;
                        sock.ReceiveTimeout = timeout_ms;
                        return new MyNetworkStream (sock);
index bc2bb3fa23d743ae75a104d6813567b2b4c8fbdb..260d85f908313de60f7d8b6af403f10bd1b66156 100644 (file)
@@ -32,6 +32,7 @@ using System.IO;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
+using System.Collections.Generic;
 
 using NUnit.Framework;
 
@@ -185,6 +186,26 @@ namespace MonoTests.System.Net
                        listener.Close ();
                }
 
+               [Test]
+               public void HttpRequestIsLocal ()
+               {
+                       var ips = new List<IPAddress> (Dns.GetHostAddresses (Dns.GetHostName ()));
+                       ips.Add (IPAddress.Loopback);
+                       foreach (var ip in ips) {
+                               if (ip.AddressFamily != AddressFamily.InterNetwork)
+                                       continue;
+
+                               HttpListener listener = HttpListener2Test.CreateAndStartListener (
+                                       "http://" + ip + ":9000/HttpRequestIsLocal/");
+                               NetworkStream ns = HttpListener2Test.CreateNS (ip, 9000);
+                               HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
+                               HttpListenerContext ctx = listener.GetContext ();
+                               HttpListenerRequest request = ctx.Request;
+                               Assert.AreEqual (true, request.IsLocal, "IP " + ip + " is not local");
+                               listener.Close ();
+                       }
+               }
+
                [Test] // #29927
                public void HttpRequestUriUnescape ()
                {