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