[System*] Throw a PlatformNotSupported exception when using the networking stack...
[mono.git] / mcs / class / System / Test / System.Net / HttpListenerRequestTest.cs
1 //
2 // HttpListenerRequestTest.cs - Unit tests for System.Net.HttpListenerRequest
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //      David Straw
7 //
8 // Copyright (C) 2007 Gert Driesen
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.IO;
32 using System.Net;
33 using System.Net.NetworkInformation;
34 using System.Net.Sockets;
35 using System.Text;
36 using System.Collections.Generic;
37
38 using NUnit.Framework;
39
40 using MonoTests.Helpers;
41
42 namespace MonoTests.System.Net
43 {
44         [TestFixture]
45         public class HttpListenerRequestTest
46         {
47                 [Test]
48                 [Category ("NotWorking")] // Bug #5742 
49                 public void HasEntityBody ()
50                 {
51                         HttpListenerContext ctx;
52                         HttpListenerRequest request;
53                         NetworkStream ns;
54                         var port = NetworkHelpers.FindFreePort ();
55                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
56                                 "http://127.0.0.1:" + port + "/HasEntityBody/");
57
58                         // POST with non-zero Content-Lenth
59                         ns = HttpListener2Test.CreateNS (port);
60                         HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
61                         ctx = listener.GetContext ();
62                         request = ctx.Request;
63                         Assert.IsTrue (request.HasEntityBody, "#A");
64                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
65
66                         // POST with zero Content-Lenth
67                         ns = HttpListener2Test.CreateNS (port);
68                         HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
69                         ctx = listener.GetContext ();
70                         request = ctx.Request;
71                         Assert.IsFalse (request.HasEntityBody, "#B");
72                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
73
74                         // POST with chunked encoding
75                         ns = HttpListener2Test.CreateNS (port);
76                         HttpListener2Test.Send (ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
77                         ctx = listener.GetContext ();
78                         request = ctx.Request;
79                         Assert.IsTrue (request.HasEntityBody, "#C");
80                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
81
82                         // GET with no Content-Length
83                         ns = HttpListener2Test.CreateNS (port);
84                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
85                         ctx = listener.GetContext ();
86                         request = ctx.Request;
87                         Assert.IsFalse (request.HasEntityBody, "#D");
88                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
89
90                         // GET with non-zero Content-Length
91                         ns = HttpListener2Test.CreateNS (port);
92                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n");
93                         ctx = listener.GetContext ();
94                         request = ctx.Request;
95                         Assert.IsTrue (request.HasEntityBody, "#E");
96                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
97
98                         // GET with zero Content-Length
99                         ns = HttpListener2Test.CreateNS (port);
100                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n");
101                         ctx = listener.GetContext ();
102                         request = ctx.Request;
103                         Assert.IsFalse (request.HasEntityBody, "#F");
104                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
105
106                         // GET with chunked encoding
107                         ns = HttpListener2Test.CreateNS (port);
108                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
109                         ctx = listener.GetContext ();
110                         request = ctx.Request;
111                         Assert.IsTrue (request.HasEntityBody, "#G");
112                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
113
114                         // PUT with non-zero Content-Lenth
115                         ns = HttpListener2Test.CreateNS (port);
116                         HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
117                         ctx = listener.GetContext ();
118                         request = ctx.Request;
119                         Assert.IsTrue (request.HasEntityBody, "#H");
120
121                         // PUT with zero Content-Lenth
122                         ns = HttpListener2Test.CreateNS (port);
123                         HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
124                         ctx = listener.GetContext ();
125                         request = ctx.Request;
126                         Assert.IsFalse (request.HasEntityBody, "#I");
127
128                         // INVALID with non-zero Content-Lenth
129                         ns = HttpListener2Test.CreateNS (port);
130                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
131                         ctx = listener.GetContext ();
132                         request = ctx.Request;
133                         Assert.IsTrue (request.HasEntityBody, "#J");
134
135                         // INVALID with zero Content-Lenth
136                         ns = HttpListener2Test.CreateNS (port);
137                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
138                         ctx = listener.GetContext ();
139                         request = ctx.Request;
140                         Assert.IsFalse (request.HasEntityBody, "#K");
141
142                         // INVALID with chunked encoding
143                         ns = HttpListener2Test.CreateNS (port);
144                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
145                         ctx = listener.GetContext ();
146                         request = ctx.Request;
147                         Assert.IsTrue (request.HasEntityBody, "#L");
148
149                         listener.Close ();
150                 }
151
152                 [Test]
153 #if FEATURE_NO_BSD_SOCKETS
154                 [ExpectedException (typeof (PlatformNotSupportedException))]
155 #endif
156                 public void HttpMethod ()
157                 {
158                         var port = NetworkHelpers.FindFreePort ();
159                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
160                                 "http://127.0.0.1:" + port + "/HttpMethod/");
161                         NetworkStream ns = HttpListener2Test.CreateNS (port);
162                         HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
163                         HttpListenerContext ctx = listener.GetContext ();
164                         HttpListenerRequest request = ctx.Request;
165                         Assert.AreEqual ("pOsT", request.HttpMethod);
166                         listener.Close ();
167                 }
168
169                 [Test]
170 #if FEATURE_NO_BSD_SOCKETS
171                 [ExpectedException (typeof (PlatformNotSupportedException))]
172 #endif
173                 public void HttpBasicAuthScheme ()
174                 {
175                         var port = NetworkHelpers.FindFreePort ();                      
176                         HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:" + port + "/authTest/", AuthenticationSchemes.Basic);
177                         //dummy-wait for context
178                         listener.BeginGetContext (null, listener);
179                         NetworkStream ns = HttpListener2Test.CreateNS (port);
180                         HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
181                         String response = HttpListener2Test.Receive (ns, 512);
182                         Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
183                         ns.Close ();
184                         listener.Close ();
185                 }
186
187                 [Test]
188 #if FEATURE_NO_BSD_SOCKETS
189                 [ExpectedException (typeof (PlatformNotSupportedException))]
190 #endif
191                 public void HttpRequestUriIsNotDecoded ()
192                 {
193                         var port = NetworkHelpers.FindFreePort ();
194                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
195                                 "http://127.0.0.1:" + port + "/RequestUriDecodeTest/");
196                         NetworkStream ns = HttpListener2Test.CreateNS (port);
197                         HttpListener2Test.Send (ns, "GET /RequestUriDecodeTest/?a=b&c=d%26e HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
198                         HttpListenerContext ctx = listener.GetContext ();
199                         HttpListenerRequest request = ctx.Request;
200                         Assert.AreEqual ("/RequestUriDecodeTest/?a=b&c=d%26e", request.Url.PathAndQuery);
201                         listener.Close ();
202                 }
203
204                 [Test]
205 #if FEATURE_NO_BSD_SOCKETS
206                 [ExpectedException (typeof (PlatformNotSupportedException))]
207 #endif
208                 public void HttpRequestIsLocal ()
209                 {
210                         var port = NetworkHelpers.FindFreePort ();
211                         var ips = new List<IPAddress> ();
212                         ips.Add (IPAddress.Loopback);
213                         foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces ()) {
214                                 foreach (var ip in adapter.GetIPProperties ().UnicastAddresses) {
215                                         ips.Add (ip.Address);
216                                 }
217                         }
218
219                         foreach (var ip in ips) {
220                                 if (ip.AddressFamily != AddressFamily.InterNetwork)
221                                         continue;
222
223                                 HttpListener listener = HttpListener2Test.CreateAndStartListener (
224                                         "http://" + ip + ":" + port + "/HttpRequestIsLocal/");
225                                 NetworkStream ns = HttpListener2Test.CreateNS (ip, port);
226                                 HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
227                                 HttpListenerContext ctx = listener.GetContext ();
228                                 HttpListenerRequest request = ctx.Request;
229                                 Assert.AreEqual (true, request.IsLocal, "IP " + ip + " is not local");
230                                 listener.Close ();
231                         }
232                 }
233
234                 [Test] // #29927
235 #if FEATURE_NO_BSD_SOCKETS
236                 [ExpectedException (typeof (PlatformNotSupportedException))]
237 #endif
238                 public void HttpRequestUriUnescape ()
239                 {
240                         var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
241                         var key = "Product/1";
242
243                         var expectedUrl = prefix + key + "/";
244                         var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
245
246                         HttpListener listener = new HttpListener ();
247                         listener.Prefixes.Add (prefix);
248                         listener.Start ();
249
250                         var contextTask = listener.GetContextAsync ();
251
252                         var request = (HttpWebRequest) WebRequest.Create (rawUrl);
253                         request.GetResponseAsync ();
254
255                         if(!contextTask.Wait (1000))
256                                 Assert.Fail ("Timeout");
257
258                         Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);
259
260                         listener.Close ();
261                 }
262         }
263 }