Merge pull request #301 from directhex/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                 public void HasEntityBody ()
47                 {
48                         HttpListenerContext ctx;
49                         HttpListenerRequest request;
50                         NetworkStream ns;
51
52                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
53                                 "http://127.0.0.1:9000/HasEntityBody/");
54
55                         // POST with non-zero Content-Lenth
56                         ns = HttpListener2Test.CreateNS (9000);
57                         HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
58                         ctx = listener.GetContext ();
59                         request = ctx.Request;
60                         Assert.IsTrue (request.HasEntityBody, "#A");
61                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
62
63                         // POST with zero Content-Lenth
64                         ns = HttpListener2Test.CreateNS (9000);
65                         HttpListener2Test.Send (ns, "POST /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
66                         ctx = listener.GetContext ();
67                         request = ctx.Request;
68                         Assert.IsFalse (request.HasEntityBody, "#B");
69                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
70
71                         // POST with chunked encoding
72                         ns = HttpListener2Test.CreateNS (9000);
73                         HttpListener2Test.Send (ns, "POST /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
74                         ctx = listener.GetContext ();
75                         request = ctx.Request;
76                         Assert.IsTrue (request.HasEntityBody, "#C");
77                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
78
79                         // GET with no Content-Length
80                         ns = HttpListener2Test.CreateNS (9000);
81                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
82                         ctx = listener.GetContext ();
83                         request = ctx.Request;
84                         Assert.IsFalse (request.HasEntityBody, "#D");
85                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
86
87                         // GET with non-zero Content-Length
88                         ns = HttpListener2Test.CreateNS (9000);
89                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n");
90                         ctx = listener.GetContext ();
91                         request = ctx.Request;
92                         Assert.IsTrue (request.HasEntityBody, "#E");
93                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
94
95                         // GET with zero Content-Length
96                         ns = HttpListener2Test.CreateNS (9000);
97                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n");
98                         ctx = listener.GetContext ();
99                         request = ctx.Request;
100                         Assert.IsFalse (request.HasEntityBody, "#F");
101                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
102
103                         // GET with chunked encoding
104                         ns = HttpListener2Test.CreateNS (9000);
105                         HttpListener2Test.Send (ns, "GET /HasEntityBody HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
106                         ctx = listener.GetContext ();
107                         request = ctx.Request;
108                         Assert.IsTrue (request.HasEntityBody, "#G");
109                         HttpListener2Test.Send (ctx.Response.OutputStream, "%%%OK%%%");
110
111                         // PUT with non-zero Content-Lenth
112                         ns = HttpListener2Test.CreateNS (9000);
113                         HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
114                         ctx = listener.GetContext ();
115                         request = ctx.Request;
116                         Assert.IsTrue (request.HasEntityBody, "#H");
117
118                         // PUT with zero Content-Lenth
119                         ns = HttpListener2Test.CreateNS (9000);
120                         HttpListener2Test.Send (ns, "PUT /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
121                         ctx = listener.GetContext ();
122                         request = ctx.Request;
123                         Assert.IsFalse (request.HasEntityBody, "#I");
124
125                         // INVALID with non-zero Content-Lenth
126                         ns = HttpListener2Test.CreateNS (9000);
127                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
128                         ctx = listener.GetContext ();
129                         request = ctx.Request;
130                         Assert.IsTrue (request.HasEntityBody, "#J");
131
132                         // INVALID with zero Content-Lenth
133                         ns = HttpListener2Test.CreateNS (9000);
134                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 0\r\n\r\n123");
135                         ctx = listener.GetContext ();
136                         request = ctx.Request;
137                         Assert.IsFalse (request.HasEntityBody, "#K");
138
139                         // INVALID with chunked encoding
140                         ns = HttpListener2Test.CreateNS (9000);
141                         HttpListener2Test.Send (ns, "INVALID /HasEntityBody/ HTTP/1.1\r\nHost: 127.0.0.1\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n");
142                         ctx = listener.GetContext ();
143                         request = ctx.Request;
144                         Assert.IsTrue (request.HasEntityBody, "#L");
145
146                         listener.Close ();
147                 }
148
149                 [Test]
150                 public void HttpMethod ()
151                 {
152                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
153                                 "http://127.0.0.1:9000/HttpMethod/");
154                         NetworkStream ns = HttpListener2Test.CreateNS (9000);
155                         HttpListener2Test.Send (ns, "pOsT /HttpMethod/ HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 3\r\n\r\n123");
156                         HttpListenerContext ctx = listener.GetContext ();
157                         HttpListenerRequest request = ctx.Request;
158                         Assert.AreEqual ("pOsT", request.HttpMethod);
159                         listener.Close ();
160                 }
161
162                 [Test]
163                 public void HttpBasicAuthScheme ()
164                 {
165                         HttpListener listener = HttpListener2Test.CreateAndStartListener ("http://*:9000/authTest/", AuthenticationSchemes.Basic);
166                         //dummy-wait for context
167                         listener.BeginGetContext (null, listener);
168                         NetworkStream ns = HttpListener2Test.CreateNS (9000);
169                         HttpListener2Test.Send (ns, "GET /authTest/ HTTP/1.0\r\n\r\n");
170                         String response = HttpListener2Test.Receive (ns, 512);
171                         Assert.IsTrue (response.Contains ("WWW-Authenticate: Basic realm"), "#A");
172                         ns.Close ();
173                         listener.Close ();
174                 }
175         }
176 }