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.Sockets;
35 using System.Collections.Generic;
37 using NUnit.Framework;
39 using MonoTests.Helpers;
41 namespace MonoTests.System.Net
44 public class HttpListenerRequestTest
47 [Category ("NotWorking")] // Bug #5742
48 public void HasEntityBody ()
50 HttpListenerContext ctx;
51 HttpListenerRequest request;
53 var port = NetworkHelpers.FindFreePort ();
54 HttpListener listener = HttpListener2Test.CreateAndStartListener (
55 "http://127.0.0.1:" + port + "/HasEntityBody/");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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");
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");
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");
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");
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");
152 public void HttpMethod ()
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);
166 public void HttpBasicAuthScheme ()
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");
181 public void HttpRequestUriIsNotDecoded ()
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);
195 public void HttpRequestIsLocal ()
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)
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");
215 public void HttpRequestUriUnescape ()
217 var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
218 var key = "Product/1";
220 var expectedUrl = prefix + key + "/";
221 var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
223 HttpListener listener = new HttpListener ();
224 listener.Prefixes.Add (prefix);
227 var contextTask = listener.GetContextAsync ();
229 var request = (HttpWebRequest) WebRequest.Create (rawUrl);
230 request.GetResponseAsync ();
232 if(!contextTask.Wait (1000))
233 Assert.Fail ("Timeout");
235 Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);