2 // HttpListenerRequestTest.cs - Unit tests for System.Net.HttpListenerRequest
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2007 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Net.Sockets;
37 using NUnit.Framework;
39 namespace MonoTests.System.Net
43 [Ignore ("The class HttpListener is not supported")]
45 public class HttpListenerRequestTest
48 public void HasEntityBody ()
50 HttpListenerContext ctx;
51 HttpListenerRequest request;
54 HttpListener listener = HttpListener2Test.CreateAndStartListener (
55 "http://127.0.0.1:9000/HasEntityBody/");
57 // POST with non-zero Content-Lenth
58 ns = HttpListener2Test.CreateNS (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 (9000);
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 HttpListener listener = HttpListener2Test.CreateAndStartListener (
155 "http://127.0.0.1:9000/HttpMethod/");
156 NetworkStream ns = HttpListener2Test.CreateNS (9000);
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 HttpListener listener = HttpListener2Test.CreateAndStartListener("http://*:9000/authTest/", AuthenticationSchemes.Basic);
168 //dummy-wait for context
169 listener.BeginGetContext(null, listener);
170 NetworkStream ns = HttpListener2Test.CreateNS(9000);
171 HttpListener2Test.Send(ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
172 String response = HttpListener2Test.Receive(ns, 512);
173 Assert.IsTrue(response.Contains("WWW-Authenticate: Basic realm"), "#A");