Merge pull request #1508 from slluis/fix-20966
[mono.git] / mcs / class / System / Test / System.Net / HttpListenerRequestTest.cs
1 //
2 // HttpListenerRequestTest.cs - Unit tests for System.Net.HttpListenerRequest
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //      David Straw
7 //
8 // Copyright (C) 2007 Gert Driesen
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.IO;
32 using System.Net;
33 using System.Net.Sockets;
34 using System.Text;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Net
39 {
40         [TestFixture]
41         public class HttpListenerRequestTest
42         {
43                 [Test]
44                 [Category ("NotWorking")] // Bug #5742 
45                 public void HasEntityBody ()
46                 {
47                         HttpListenerContext ctx;
48                         HttpListenerRequest request;
49                         NetworkStream ns;
50
51                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
52                                 "http://127.0.0.1:9000/HasEntityBody/");
53
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%%%");
61
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%%%");
69
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%%%");
77
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%%%");
85
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%%%");
93
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%%%");
101
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%%%");
109
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");
116
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");
123
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");
130
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");
137
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");
144
145                         listener.Close ();
146                 }
147
148                 [Test]
149                 public void HttpMethod ()
150                 {
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);
158                         listener.Close ();
159                 }
160
161                 [Test]
162                 public void HttpBasicAuthScheme ()
163                 {
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");
171                         ns.Close ();
172                         listener.Close ();
173                 }
174
175                 [Test]
176                 public void HttpRequestUriIsNotDecoded ()
177                 {
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);
185                         listener.Close ();
186                 }
187         }
188 }