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 namespace MonoTests.System.Net
41 public class HttpListenerRequestTest
44 [Category ("NotWorking")] // Bug #5742
45 public void HasEntityBody ()
47 HttpListenerContext ctx;
48 HttpListenerRequest request;
51 HttpListener listener = HttpListener2Test.CreateAndStartListener (
52 "http://127.0.0.1:9000/HasEntityBody/");
54 // POST with non-zero Content-Lenth
55 ns = HttpListener2Test.CreateNS (9000);
56 HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
57 ctx = listener.GetContext ();
58 request = ctx.Request;
59 Assert.IsTrue (request.HasEntityBody, "#A");
60 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
62 // POST with zero Content-Lenth
63 ns = HttpListener2Test.CreateNS (9000);
64 HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
65 ctx = listener.GetContext ();
66 request = ctx.Request;
67 Assert.IsFalse (request.HasEntityBody, "#B");
68 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
70 // POST with chunked encoding
71 ns = HttpListener2Test.CreateNS (9000);
72 HttpListener2Test.Send (ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
73 ctx = listener.GetContext ();
74 request = ctx.Request;
75 Assert.IsTrue (request.HasEntityBody, "#C");
76 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
78 // GET with no Content-Length
79 ns = HttpListener2Test.CreateNS (9000);
80 HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
81 ctx = listener.GetContext ();
82 request = ctx.Request;
83 Assert.IsFalse (request.HasEntityBody, "#D");
84 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
86 // GET with non-zero Content-Length
87 ns = HttpListener2Test.CreateNS (9000);
88 HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n");
89 ctx = listener.GetContext ();
90 request = ctx.Request;
91 Assert.IsTrue (request.HasEntityBody, "#E");
92 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
94 // GET with zero Content-Length
95 ns = HttpListener2Test.CreateNS (9000);
96 HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n");
97 ctx = listener.GetContext ();
98 request = ctx.Request;
99 Assert.IsFalse (request.HasEntityBody, "#F");
100 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
102 // GET with chunked encoding
103 ns = HttpListener2Test.CreateNS (9000);
104 HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
105 ctx = listener.GetContext ();
106 request = ctx.Request;
107 Assert.IsTrue (request.HasEntityBody, "#G");
108 HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
110 // PUT with non-zero Content-Lenth
111 ns = HttpListener2Test.CreateNS (9000);
112 HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
113 ctx = listener.GetContext ();
114 request = ctx.Request;
115 Assert.IsTrue (request.HasEntityBody, "#H");
117 // PUT with zero Content-Lenth
118 ns = HttpListener2Test.CreateNS (9000);
119 HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
120 ctx = listener.GetContext ();
121 request = ctx.Request;
122 Assert.IsFalse (request.HasEntityBody, "#I");
124 // INVALID with non-zero Content-Lenth
125 ns = HttpListener2Test.CreateNS (9000);
126 HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
127 ctx = listener.GetContext ();
128 request = ctx.Request;
129 Assert.IsTrue (request.HasEntityBody, "#J");
131 // INVALID with zero Content-Lenth
132 ns = HttpListener2Test.CreateNS (9000);
133 HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
134 ctx = listener.GetContext ();
135 request = ctx.Request;
136 Assert.IsFalse (request.HasEntityBody, "#K");
138 // INVALID with chunked encoding
139 ns = HttpListener2Test.CreateNS (9000);
140 HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
141 ctx = listener.GetContext ();
142 request = ctx.Request;
143 Assert.IsTrue (request.HasEntityBody, "#L");
149 public void HttpMethod ()
151 HttpListener listener = HttpListener2Test.CreateAndStartListener (
152 "http://127.0.0.1:9000/HttpMethod/");
153 NetworkStream ns = HttpListener2Test.CreateNS (9000);
154 HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
155 HttpListenerContext ctx = listener.GetContext ();
156 HttpListenerRequest request = ctx.Request;
157 Assert.AreEqual ("pOsT", request.HttpMethod);
162 public void HttpBasicAuthScheme ()
164 HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:9000/authTest/", AuthenticationSchemes.Basic);
165 //dummy-wait for context
166 listener.BeginGetContext (null, listener);
167 NetworkStream ns = HttpListener2Test.CreateNS (9000);
168 HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
169 String response = HttpListener2Test.Receive (ns, 512);
170 Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
176 public void HttpRequestUriIsNotDecoded ()
178 HttpListener listener = HttpListener2Test.CreateAndStartListener (
179 "http://127.0.0.1:9000/RequestUriDecodeTest/");
180 NetworkStream ns = HttpListener2Test.CreateNS (9000);
181 HttpListener2Test.Send (ns, "GET /RequestUriDecodeTest/?a=b&c=d%26e HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
182 HttpListenerContext ctx = listener.GetContext ();
183 HttpListenerRequest request = ctx.Request;
184 Assert.AreEqual ("/RequestUriDecodeTest/?a=b&c=d%26e", request.Url.PathAndQuery);
189 public void HttpRequestUriUnescape ()
191 var prefix = "http://localhost:12345/";
192 var key = "Product/1";
194 var expectedUrl = prefix + key + "/";
195 var rawUrl = prefix + Uri.EscapeDataString (key) + "/";
197 HttpListener listener = new HttpListener ();
198 listener.Prefixes.Add (prefix);
201 var contextTask = listener.GetContextAsync ();
203 var request = (HttpWebRequest) WebRequest.Create (rawUrl);
204 request.GetResponseAsync ();
206 if(!contextTask.Wait (1000))
207 Assert.Fail ("Timeout");
209 Assert.AreEqual (expectedUrl, contextTask.Result.Request.Url.AbsoluteUri);