2 // HttpListenerRequestTest.cs - Unit tests for System.Net.HttpListenerRequest
5 // Gert Driesen (drieseng@users.sourceforge.net)
8 // Copyright (C) 2007 Gert Driesen
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 using System.Net.NetworkInformation;
34 using System.Net.Sockets;
36 using System.Collections.Generic;
38 using NUnit.Framework;
40 using MonoTests.Helpers;
42 namespace MonoTests.System.Net
45 public class HttpListenerRequestTest
48 [Category ("NotWorking")] // Bug #5742
49 public void HasEntityBody ()
51 HttpListenerContext ctx;
52 HttpListenerRequest request;
54 var port = NetworkHelpers.FindFreePort ();
55 HttpListener listener = HttpListener2Test.CreateAndStartListener (
56 "http://127.0.0.1:" + port + "/HasEntityBody/");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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");
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");
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");
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");
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");
153 #if FEATURE_NO_BSD_SOCKETS
154 [ExpectedException (typeof (PlatformNotSupportedException))]
156 public void HttpMethod ()
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);
170 #if FEATURE_NO_BSD_SOCKETS
171 [ExpectedException (typeof (PlatformNotSupportedException))]
173 public void HttpBasicAuthScheme ()
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");
188 #if FEATURE_NO_BSD_SOCKETS
189 [ExpectedException (typeof (PlatformNotSupportedException))]
191 public void HttpRequestUriIsNotDecoded ()
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);
205 #if FEATURE_NO_BSD_SOCKETS
206 [ExpectedException (typeof (PlatformNotSupportedException))]
208 public void HttpRequestIsLocal ()
210 var port = NetworkHelpers.FindFreePort ();
211 var ips = new List<IPAddress> ();
212 ips.Add (IPAddress.Loopback);
213 foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces ()) {
214 if (adapter.OperationalStatus != OperationalStatus.Up)
216 foreach (var ip in adapter.GetIPProperties ().UnicastAddresses) {
217 ips.Add (ip.Address);
221 foreach (var ip in ips) {
222 if (ip.AddressFamily != AddressFamily.InterNetwork)
225 HttpListener listener = HttpListener2Test.CreateAndStartListener (
226 "http://" + ip + ":" + port + "/HttpRequestIsLocal/");
227 NetworkStream ns = HttpListener2Test.CreateNS (ip, port);
228 HttpListener2Test.Send (ns, "GET /HttpRequestIsLocal/ HTTP/1.0\r\n\r\n");
229 HttpListenerContext ctx = listener.GetContext ();
230 HttpListenerRequest request = ctx.Request;
231 Assert.AreEqual (true, request.IsLocal, "IP " + ip + " is not local");
237 #if FEATURE_NO_BSD_SOCKETS
238 [ExpectedException (typeof (PlatformNotSupportedException))]
240 public void HttpRequestUriUnescape ()
242 var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
243 var key = "Product/1";
245 var expectedUrl = prefix + key + "/";
246 var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
248 HttpListener listener = new HttpListener ();
249 listener.Prefixes.Add (prefix);
252 var contextTask = listener.GetContextAsync ();
254 var request = (HttpWebRequest) WebRequest.Create (rawUrl);
255 request.GetResponseAsync ();
257 if(!contextTask.Wait (1000))
258 Assert.Fail ("Timeout");
260 Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);