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 namespace MonoTests.System.Net
42 public class HttpListenerRequestTest
45 [Category ("NotWorking")] // Bug #5742
46 public void HasEntityBody ()
48 HttpListenerContext ctx;
49 HttpListenerRequest request;
52 HttpListener listener = HttpListener2Test.CreateAndStartListener (
53 "http://127.0.0.1:9000/HasEntityBody/");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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");
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");
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");
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");
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");
150 public void HttpMethod ()
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);
163 public void HttpBasicAuthScheme ()
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");
177 public void HttpRequestUriIsNotDecoded ()
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);
190 public void HttpRequestIsLocal ()
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)
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");
210 public void HttpRequestUriUnescape ()
212 var prefix = "http://localhost:12345/";
213 var key = "Product/1";
215 var expectedUrl = prefix + key + "/";
216 var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
218 HttpListener listener = new HttpListener ();
219 listener.Prefixes.Add (prefix);
222 var contextTask = listener.GetContextAsync ();
224 var request = (HttpWebRequest) WebRequest.Create (rawUrl);
225 request.GetResponseAsync ();
227 if(!contextTask.Wait (1000))
228 Assert.Fail ("Timeout");
230 Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);