Merge branch 'master' into mono4-continuations_fix
[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                 public void HttpMethod ()
154                 {
155                         var port = NetworkHelpers.FindFreePort ();
156                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
157                                 "http://127.0.0.1:" + port + "/HttpMethod/");
158                         NetworkStream ns = HttpListener2Test.CreateNS (port);
159                         HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
160                         HttpListenerContext ctx = listener.GetContext ();
161                         HttpListenerRequest request = ctx.Request;
162                         Assert.AreEqual ("pOsT", request.HttpMethod);
163                         listener.Close ();
164                 }
165
166                 [Test]
167                 public void HttpBasicAuthScheme ()
168                 {
169                         var port = NetworkHelpers.FindFreePort ();                      
170                         HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:" + port + "/authTest/", AuthenticationSchemes.Basic);
171                         //dummy-wait for context
172                         listener.BeginGetContext (null, listener);
173                         NetworkStream ns = HttpListener2Test.CreateNS (port);
174                         HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
175                         String response = HttpListener2Test.Receive (ns, 512);
176                         Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
177                         ns.Close ();
178                         listener.Close ();
179                 }
180
181                 [Test]
182                 public void HttpRequestUriIsNotDecoded ()
183                 {
184                         var port = NetworkHelpers.FindFreePort ();
185                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
186                                 "http://127.0.0.1:" + port + "/RequestUriDecodeTest/");
187                         NetworkStream ns = HttpListener2Test.CreateNS (port);
188                         HttpListener2Test.Send (ns, "GET /RequestUriDecodeTest/?a=b&c=d%26e HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
189                         HttpListenerContext ctx = listener.GetContext ();
190                         HttpListenerRequest request = ctx.Request;
191                         Assert.AreEqual ("/RequestUriDecodeTest/?a=b&c=d%26e", request.Url.PathAndQuery);
192                         listener.Close ();
193                 }
194
195                 [Test]
196                 public void HttpRequestIsLocal ()
197                 {
198                         var port = NetworkHelpers.FindFreePort ();
199                         var ips = new List<IPAddress> ();
200                         ips.Add (IPAddress.Loopback);
201                         foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces ()) {
202                                 foreach (var ip in adapter.GetIPProperties ().UnicastAddresses) {
203                                         ips.Add (ip.Address);
204                                 }
205                         }
206
207                         foreach (var ip in ips) {
208                                 if (ip.AddressFamily != AddressFamily.InterNetwork)
209                                         continue;
210
211                                 HttpListener listener = HttpListener2Test.CreateAndStartListener (
212                                         "http://" + ip + ":" + port + "/HttpRequestIsLocal/");
213                                 NetworkStream ns = HttpListener2Test.CreateNS (ip, port);
214                                 HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
215                                 HttpListenerContext ctx = listener.GetContext ();
216                                 HttpListenerRequest request = ctx.Request;
217                                 Assert.AreEqual (true, request.IsLocal, "IP " + ip + " is not local");
218                                 listener.Close ();
219                         }
220                 }
221
222                 [Test] // #29927
223                 public void HttpRequestUriUnescape ()
224                 {
225                         var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
226                         var key = "Product/1";
227
228                         var expectedUrl = prefix + key + "/";
229                         var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
230
231                         HttpListener listener = new HttpListener ();
232                         listener.Prefixes.Add (prefix);
233                         listener.Start ();
234
235                         var contextTask = listener.GetContextAsync ();
236
237                         var request = (HttpWebRequest) WebRequest.Create (rawUrl);
238                         request.GetResponseAsync ();
239
240                         if(!contextTask.Wait (1000))
241                                 Assert.Fail ("Timeout");
242
243                         Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);
244
245                         listener.Close ();
246                 }
247         }
248 }