Merge pull request #323 from crazyjncsu/master
[mono.git] / mcs / class / System / Test / System.Net / HttpListenerRequestTest.cs
1 //
2 // HttpListenerRequestTest.cs - Unit tests for System.Net.HttpListenerRequest
3 //
4 // Author:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2007 Gert Driesen
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.IO;
31 using System.Net;
32 using System.Net.Sockets;
33 using System.Text;
34
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Net
38 {
39         [TestFixture]
40 #if TARGET_JVM
41         [Ignore ("The class HttpListener is not supported")]
42 #endif
43         public class HttpListenerRequestTest
44         {
45                 [Test]
46                 [Category ("NotWorking")] // Bug #5742 
47                 public void HasEntityBody ()
48                 {
49                         HttpListenerContext ctx;
50                         HttpListenerRequest request;
51                         NetworkStream ns;
52
53                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
54                                 "http://127.0.0.1:9000/HasEntityBody/");
55
56                         // POST with non-zero Content-Lenth
57                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
63
64                         // POST with zero Content-Lenth
65                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
71
72                         // POST with chunked encoding
73                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
79
80                         // GET with no Content-Length
81                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
87
88                         // GET with non-zero Content-Length
89                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
95
96                         // GET with zero Content-Length
97                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
103
104                         // GET with chunked encoding
105                         ns = HttpListener2Test.CreateNS (9000);
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%%%");
111
112                         // PUT with non-zero Content-Lenth
113                         ns = HttpListener2Test.CreateNS (9000);
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");
118
119                         // PUT with zero Content-Lenth
120                         ns = HttpListener2Test.CreateNS (9000);
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");
125
126                         // INVALID with non-zero Content-Lenth
127                         ns = HttpListener2Test.CreateNS (9000);
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");
132
133                         // INVALID with zero Content-Lenth
134                         ns = HttpListener2Test.CreateNS (9000);
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");
139
140                         // INVALID with chunked encoding
141                         ns = HttpListener2Test.CreateNS (9000);
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");
146
147                         listener.Close ();
148                 }
149
150                 [Test]
151                 public void HttpMethod ()
152                 {
153                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
154                                 "http://127.0.0.1:9000/HttpMethod/");
155                         NetworkStream ns = HttpListener2Test.CreateNS (9000);
156                         HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
157                         HttpListenerContext ctx = listener.GetContext ();
158                         HttpListenerRequest request = ctx.Request;
159                         Assert.AreEqual ("pOsT", request.HttpMethod);
160                         listener.Close ();
161                 }
162
163                 [Test]
164                 public void HttpBasicAuthScheme ()
165                 {
166                         HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:9000/authTest/", AuthenticationSchemes.Basic);
167                         //dummy-wait for context
168                         listener.BeginGetContext (null, listener);
169                         NetworkStream ns = HttpListener2Test.CreateNS (9000);
170                         HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
171                         String response = HttpListener2Test.Receive (ns, 512);
172                         Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
173                         ns.Close ();
174                         listener.Close ();
175                 }
176         }
177 }