Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[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 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.Text;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Net
40 {
41         [TestFixture]
42 #if TARGET_JVM
43         [Ignore ("The class HttpListener is not supported")]
44 #endif
45         public class HttpListenerRequestTest
46         {
47                 [Test]
48                 public void HasEntityBody ()
49                 {
50                         HttpListenerContext ctx;
51                         HttpListenerRequest request;
52                         NetworkStream ns;
53
54                         HttpListener listener = HttpListener2Test.CreateAndStartListener (
55                                 "http://127.0.0.1:9000/HasEntityBody/");
56
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%%%");
64
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%%%");
72
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%%%");
80
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%%%");
88
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%%%");
96
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%%%");
104
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%%%");
112
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");
119
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");
126
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");
133
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");
140
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");
147
148                         listener.Close ();
149                 }
150
151                 [Test]
152                 public void HttpMethod ()
153                 {
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);
161                         listener.Close ();
162                 }
163                 
164                 [Test]
165                 public void HttpBasicAuthScheme()
166                 {
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");
174                         ns.Close();
175                         listener.Close();
176                 }
177         }
178 }
179
180 #endif