Merge pull request #2698 from esdrubal/iosxmlarray
[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.Sockets;
34 using System.Text;
35 using System.Collections.Generic;
36
37 using NUnit.Framework;
38
39 using MonoTests.Helpers;
40
41 namespace MonoTests.System.Net
42 {
43         [TestFixture]
44         public class HttpListenerRequestTest
45         {
46                 [Test]
47                 [Category ("NotWorking")] // Bug #5742 
48                 public void HasEntityBody ()
49                 {
50                         HttpListenerContext ctx;
51                         HttpListenerRequest request;
52                         NetworkStream ns;
53                         var port = NetworkHelpers.FindFreePort ();
54                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
55                                 "http://127.0.0.1:" + port + "/HasEntityBody/");
56
57                         // POST with non-zero Content-Lenth
58                         ns = HttpListener2Test.CreateNS (port);
59                         HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
60                         ctx = listener.GetContext ();
61                         request = ctx.Request;
62                         Assert.IsTrue (request.HasEntityBody, "#A");
63                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
64
65                         // POST with zero Content-Lenth
66                         ns = HttpListener2Test.CreateNS (port);
67                         HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
68                         ctx = listener.GetContext ();
69                         request = ctx.Request;
70                         Assert.IsFalse (request.HasEntityBody, "#B");
71                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
72
73                         // POST with chunked encoding
74                         ns = HttpListener2Test.CreateNS (port);
75                         HttpListener2Test.Send (ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
76                         ctx = listener.GetContext ();
77                         request = ctx.Request;
78                         Assert.IsTrue (request.HasEntityBody, "#C");
79                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
80
81                         // GET with no Content-Length
82                         ns = HttpListener2Test.CreateNS (port);
83                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
84                         ctx = listener.GetContext ();
85                         request = ctx.Request;
86                         Assert.IsFalse (request.HasEntityBody, "#D");
87                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
88
89                         // GET with non-zero Content-Length
90                         ns = HttpListener2Test.CreateNS (port);
91                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n");
92                         ctx = listener.GetContext ();
93                         request = ctx.Request;
94                         Assert.IsTrue (request.HasEntityBody, "#E");
95                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
96
97                         // GET with zero Content-Length
98                         ns = HttpListener2Test.CreateNS (port);
99                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n");
100                         ctx = listener.GetContext ();
101                         request = ctx.Request;
102                         Assert.IsFalse (request.HasEntityBody, "#F");
103                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
104
105                         // GET with chunked encoding
106                         ns = HttpListener2Test.CreateNS (port);
107                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
108                         ctx = listener.GetContext ();
109                         request = ctx.Request;
110                         Assert.IsTrue (request.HasEntityBody, "#G");
111                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
112
113                         // PUT with non-zero Content-Lenth
114                         ns = HttpListener2Test.CreateNS (port);
115                         HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
116                         ctx = listener.GetContext ();
117                         request = ctx.Request;
118                         Assert.IsTrue (request.HasEntityBody, "#H");
119
120                         // PUT with zero Content-Lenth
121                         ns = HttpListener2Test.CreateNS (port);
122                         HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
123                         ctx = listener.GetContext ();
124                         request = ctx.Request;
125                         Assert.IsFalse (request.HasEntityBody, "#I");
126
127                         // INVALID with non-zero Content-Lenth
128                         ns = HttpListener2Test.CreateNS (port);
129                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
130                         ctx = listener.GetContext ();
131                         request = ctx.Request;
132                         Assert.IsTrue (request.HasEntityBody, "#J");
133
134                         // INVALID with zero Content-Lenth
135                         ns = HttpListener2Test.CreateNS (port);
136                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
137                         ctx = listener.GetContext ();
138                         request = ctx.Request;
139                         Assert.IsFalse (request.HasEntityBody, "#K");
140
141                         // INVALID with chunked encoding
142                         ns = HttpListener2Test.CreateNS (port);
143                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
144                         ctx = listener.GetContext ();
145                         request = ctx.Request;
146                         Assert.IsTrue (request.HasEntityBody, "#L");
147
148                         listener.Close ();
149                 }
150
151                 [Test]
152                 public void HttpMethod ()
153                 {
154                         var port = NetworkHelpers.FindFreePort ();
155                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
156                                 "http://127.0.0.1:" + port + "/HttpMethod/");
157                         NetworkStream ns = HttpListener2Test.CreateNS (port);
158                         HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
159                         HttpListenerContext ctx = listener.GetContext ();
160                         HttpListenerRequest request = ctx.Request;
161                         Assert.AreEqual ("pOsT", request.HttpMethod);
162                         listener.Close ();
163                 }
164
165                 [Test]
166                 public void HttpBasicAuthScheme ()
167                 {
168                         var port = NetworkHelpers.FindFreePort ();                      
169                         HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:" + port + "/authTest/", AuthenticationSchemes.Basic);
170                         //dummy-wait for context
171                         listener.BeginGetContext (null, listener);
172                         NetworkStream ns = HttpListener2Test.CreateNS (port);
173                         HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
174                         String response = HttpListener2Test.Receive (ns, 512);
175                         Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
176                         ns.Close ();
177                         listener.Close ();
178                 }
179
180                 [Test]
181                 public void HttpRequestUriIsNotDecoded ()
182                 {
183                         var port = NetworkHelpers.FindFreePort ();
184                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
185                                 "http://127.0.0.1:" + port + "/RequestUriDecodeTest/");
186                         NetworkStream ns = HttpListener2Test.CreateNS (port);
187                         HttpListener2Test.Send (ns, "GET /RequestUriDecodeTest/?a=b&c=d%26e HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
188                         HttpListenerContext ctx = listener.GetContext ();
189                         HttpListenerRequest request = ctx.Request;
190                         Assert.AreEqual ("/RequestUriDecodeTest/?a=b&c=d%26e", request.Url.PathAndQuery);
191                         listener.Close ();
192                 }
193
194                 [Test]
195                 public void HttpRequestIsLocal ()
196                 {
197                         var ips = new List<IPAddress> (Dns.GetHostAddresses (Dns.GetHostName ()));
198                         ips.Add (IPAddress.Loopback);
199                         foreach (var ip in ips) {
200                                 if (ip.AddressFamily != AddressFamily.InterNetwork)
201                                         continue;
202
203                                 HttpListener listener = HttpListener2Test.CreateAndStartListener (
204                                         "http://" + ip + ":9000/HttpRequestIsLocal/");
205                                 NetworkStream ns = HttpListener2Test.CreateNS (ip, 9000);
206                                 HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
207                                 HttpListenerContext ctx = listener.GetContext ();
208                                 HttpListenerRequest request = ctx.Request;
209                                 Assert.AreEqual (true, request.IsLocal, "IP " + ip + " is not local");
210                                 listener.Close ();
211                         }
212                 }
213
214                 [Test] // #29927
215                 public void HttpRequestUriUnescape ()
216                 {
217                         var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
218                         var key = "Product/1";
219
220                         var expectedUrl = prefix + key + "/";
221                         var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
222
223                         HttpListener listener = new HttpListener ();
224                         listener.Prefixes.Add (prefix);
225                         listener.Start ();
226
227                         var contextTask = listener.GetContextAsync ();
228
229                         var request = (HttpWebRequest) WebRequest.Create (rawUrl);
230                         request.GetResponseAsync ();
231
232                         if(!contextTask.Wait (1000))
233                                 Assert.Fail ("Timeout");
234
235                         Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);
236
237                         listener.Close ();
238                 }
239         }
240 }