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