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;
36 using NUnit.Framework;
38 using MonoTests.Helpers;
40 namespace MonoTests.System.Net
43 public class HttpListenerRequestTest
46 [Category ("NotWorking")] // Bug #5742
47 public void HasEntityBody ()
49 HttpListenerContext ctx;
50 HttpListenerRequest request;
52 var port = NetworkHelpers.FindFreePort ();
53 HttpListener listener = HttpListener2Test.CreateAndStartListener (
54 "http://127.0.0.1:" + port + "/HasEntityBody/");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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%%%");
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");
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");
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");
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");
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");
151 public void HttpMethod ()
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);
165 public void HttpBasicAuthScheme ()
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");
180 public void HttpRequestUriIsNotDecoded ()
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);
194 public void HttpRequestUriUnescape ()
196 var prefix = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";
197 var key = "Product/1";
199 var expectedUrl = prefix + key + "/";
200 var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
202 HttpListener listener = new HttpListener ();
203 listener.Prefixes.Add (prefix);
206 var contextTask = listener.GetContextAsync ();
208 var request = (HttpWebRequest) WebRequest.Create (rawUrl);
209 request.GetResponseAsync ();
211 if(!contextTask.Wait (1000))
212 Assert.Fail ("Timeout");
214 Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);