[Cleanup] Removed TARGET_JVM
[mono-project.git] / mcs / class / System / Test / System.Net / HttpListenerTest.cs
blob25f7b96a9b935a7cabf64a58906476934aa19824
1 //
2 // HttpListenerTest.cs
3 // - Unit tests for System.Net.HttpListener
4 //
5 // Author:
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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.
29 #if NET_2_0
30 using System;
31 using System.Net;
32 using System.Net.Sockets;
33 using System.Threading;
34 using NUnit.Framework;
36 namespace MonoTests.System.Net {
37 [TestFixture]
38 public class HttpListenerTest {
39 [Test]
40 public void DefaultProperties ()
42 HttpListener listener = new HttpListener ();
43 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
44 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
45 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
46 Assert.AreEqual (false, listener.IsListening, "#03");
47 Assert.AreEqual (0, listener.Prefixes.Count, "#04");
48 Assert.AreEqual (null, listener.Realm, "#05");
49 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
52 [Test]
53 public void Start1 ()
55 HttpListener listener = new HttpListener ();
56 listener.Start ();
59 [Test]
60 public void Stop1 ()
62 HttpListener listener = new HttpListener ();
63 listener.Stop ();
66 [Test]
67 [ExpectedException (typeof (InvalidOperationException))]
68 public void GetContext1 ()
70 HttpListener listener = new HttpListener ();
71 // "Please call Start () before calling this method"
72 listener.GetContext ();
75 [Test]
76 [ExpectedException (typeof (InvalidOperationException))]
77 public void GetContext2 ()
79 HttpListener listener = new HttpListener ();
80 listener.Start ();
81 // "Please call AddPrefix () before calling this method"
82 listener.GetContext ();
85 [Test]
86 [ExpectedException (typeof (InvalidOperationException))]
87 public void BeginGetContext1 ()
89 HttpListener listener = new HttpListener ();
90 // "Please call Start () before calling this method"
91 listener.BeginGetContext (null, null);
94 [Test]
95 public void BeginGetContext2 ()
97 HttpListener listener = new HttpListener ();
98 listener.Start ();
99 // One would expect this to fail as BeginGetContext1 does not fail and
100 // calling EndGetContext will wait forever.
101 // Lame. They should check that we have no prefixes.
102 IAsyncResult ares = listener.BeginGetContext (null, null);
103 Assert.IsFalse (ares.IsCompleted);
106 private bool CanOpenPort(int port)
110 using(Socket socket = new Socket (AddressFamily.InterNetwork,
111 SocketType.Stream,
112 ProtocolType.Tcp))
114 socket.Bind (new IPEndPoint (IPAddress.Loopback, port));
115 socket.Listen(1);
118 catch(Exception ex) {
119 //Can be AccessDeniedException(ports 80/443 need root access) or
120 //SocketException because other application is listening
121 return false;
123 return true;
126 [Test]
127 public void DefaultHttpPort ()
129 if (!CanOpenPort (80))
130 Assert.Ignore ("Can not open port 80 skipping test.");
131 using(HttpListener listener = new HttpListener ())
133 listener.Prefixes.Add ("http://127.0.0.1/");
134 listener.Start ();
135 Assert.IsFalse (CanOpenPort (80), "HttpListener is not listening on port 80.");
139 [Test]
140 public void DefaultHttpsPort ()
142 if (!CanOpenPort (443))
143 Assert.Ignore ("Can not open port 443 skipping test.");
144 using(HttpListener listener = new HttpListener ())
146 listener.Prefixes.Add ("https://127.0.0.1/");
147 listener.Start ();
148 Assert.IsFalse (CanOpenPort (443), "HttpListener is not listening on port 443.");
152 [Test]
153 public void TwoListeners_SameAddress ()
155 HttpListener listener1 = new HttpListener ();
156 listener1.Prefixes.Add ("http://127.0.0.1:7777/");
157 HttpListener listener2 = new HttpListener ();
158 listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
159 listener1.Start ();
160 listener2.Start ();
163 [Test]
164 [ExpectedException (typeof (HttpListenerException))]
165 public void TwoListeners_SameURL ()
167 HttpListener listener1 = new HttpListener ();
168 listener1.Prefixes.Add ("http://127.0.0.1:7777/hola/");
169 HttpListener listener2 = new HttpListener ();
170 listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
171 listener1.Start ();
172 listener2.Start ();
175 [Test]
176 [ExpectedException (typeof (HttpListenerException))]
177 public void MultipleSlashes ()
179 HttpListener listener = new HttpListener ();
180 listener.Prefixes.Add ("http://localhost:7777/hola////");
181 // this one throws on Start(), not when adding it.
182 listener.Start ();
185 [Test]
186 [ExpectedException (typeof (HttpListenerException))]
187 public void PercentSign ()
189 HttpListener listener = new HttpListener ();
190 listener.Prefixes.Add ("http://localhost:7777/hola%3E/");
191 // this one throws on Start(), not when adding it.
192 listener.Start ();
195 [Test]
196 public void CloseBeforeStart ()
198 HttpListener listener = new HttpListener ();
199 listener.Close ();
202 [Test]
203 public void CloseTwice ()
205 HttpListener listener = new HttpListener ();
206 listener.Prefixes.Add ("http://localhost:7777/hola/");
207 listener.Start ();
208 listener.Close ();
209 listener.Close ();
212 [Test]
213 public void StartStopStart ()
215 HttpListener listener = new HttpListener ();
216 listener.Prefixes.Add ("http://localhost:7777/hola/");
217 listener.Start ();
218 listener.Stop ();
219 listener.Start ();
220 listener.Close ();
223 [Test]
224 public void StartStopDispose ()
226 using (HttpListener listener = new HttpListener ()){
227 listener.Prefixes.Add ("http://localhost:7777/hola/");
228 listener.Start ();
229 listener.Stop ();
233 [Test]
234 public void AbortBeforeStart ()
236 HttpListener listener = new HttpListener ();
237 listener.Abort ();
240 [Test]
241 public void AbortTwice ()
243 HttpListener listener = new HttpListener ();
244 listener.Prefixes.Add ("http://localhost:7777/hola/");
245 listener.Start ();
246 listener.Abort ();
247 listener.Abort ();
250 [Test]
251 public void PropertiesWhenClosed1 ()
253 HttpListener listener = new HttpListener ();
254 listener.Close ();
255 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
256 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
257 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
258 Assert.AreEqual (false, listener.IsListening, "#03");
259 Assert.AreEqual (null, listener.Realm, "#05");
260 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
263 [Test]
264 [ExpectedException (typeof (ObjectDisposedException))]
265 public void PropertiesWhenClosed2 ()
267 HttpListener listener = new HttpListener ();
268 listener.Close ();
269 HttpListenerPrefixCollection p = listener.Prefixes;
272 [Test]
273 [ExpectedException (typeof (ObjectDisposedException))]
274 public void PropertiesWhenClosedSet1 ()
276 HttpListener listener = new HttpListener ();
277 listener.Close ();
278 listener.AuthenticationSchemes = AuthenticationSchemes.None;
281 [Test]
282 [ExpectedException (typeof (ObjectDisposedException))]
283 public void PropertiesWhenClosedSet2 ()
285 HttpListener listener = new HttpListener ();
286 listener.Close ();
287 listener.AuthenticationSchemeSelectorDelegate = null;
290 [Test]
291 [ExpectedException (typeof (ObjectDisposedException))]
292 public void PropertiesWhenClosedSet3 ()
294 HttpListener listener = new HttpListener ();
295 listener.Close ();
296 listener.IgnoreWriteExceptions = true;
299 [Test]
300 [ExpectedException (typeof (ObjectDisposedException))]
301 public void PropertiesWhenClosedSet4 ()
303 HttpListener listener = new HttpListener ();
304 listener.Close ();
305 listener.Realm = "hola";
308 [Test]
309 [ExpectedException (typeof (ObjectDisposedException))]
310 public void PropertiesWhenClosedSet5 ()
312 HttpListener listener = new HttpListener ();
313 listener.Close ();
314 listener.UnsafeConnectionNtlmAuthentication = true;
317 [Test]
318 public void PropertiesWhenClosed3 ()
320 HttpListener listener = new HttpListener ();
321 listener.Close ();
322 Assert.IsFalse (listener.IsListening);
325 [Test]
326 public void CloseWhileBegin ()
328 HttpListener listener = new HttpListener ();
329 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
330 listener.Start ();
331 CallMe cm = new CallMe ();
332 listener.BeginGetContext (cm.Callback, listener);
333 listener.Close ();
334 if (false == cm.Event.WaitOne (3000, false))
335 Assert.Fail ("This should not time out.");
336 Assert.IsNotNull (cm.Error);
337 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
338 cm.Dispose ();
341 [Test]
342 public void AbortWhileBegin ()
344 HttpListener listener = new HttpListener ();
345 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
346 listener.Start ();
347 CallMe cm = new CallMe ();
348 listener.BeginGetContext (cm.Callback, listener);
349 listener.Abort ();
350 if (false == cm.Event.WaitOne (3000, false))
351 Assert.Fail ("This should not time out.");
352 Assert.IsNotNull (cm.Error);
353 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
354 cm.Dispose ();
357 [Test]
358 [ExpectedException (typeof (HttpListenerException))]
359 public void CloseWhileGet ()
361 // "System.Net.HttpListener Exception : The I/O operation has been aborted
362 // because of either a thread exit or an application request
363 // at System.Net.HttpListener.GetContext()
364 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
366 HttpListener listener = new HttpListener ();
367 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
368 listener.Start ();
369 RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
370 rm.Start ();
371 HttpListenerContext ctx = listener.GetContext ();
374 [Test]
375 [ExpectedException (typeof (HttpListenerException))]
376 public void AbortWhileGet ()
378 // "System.Net.HttpListener Exception : The I/O operation has been aborted
379 // because of either a thread exit or an application request
380 // at System.Net.HttpListener.GetContext()
381 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
383 HttpListener listener = new HttpListener ();
384 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
385 listener.Start ();
386 RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
387 rm.Start ();
388 HttpListenerContext ctx = listener.GetContext ();
391 class RunMe {
392 Delegate d;
393 int delay_ms;
394 object [] args;
395 public object Result;
397 public RunMe (int delay_ms, Delegate d, object [] args)
399 this.delay_ms = delay_ms;
400 this.d = d;
401 this.args = args;
404 public void Start ()
406 Thread th = new Thread (new ThreadStart (Run));
407 th.Start ();
410 void Run ()
412 Thread.Sleep (delay_ms);
413 Result = d.DynamicInvoke (args);
417 class CallMe {
418 public ManualResetEvent Event = new ManualResetEvent (false);
419 public bool Called;
420 public HttpListenerContext Context;
421 public Exception Error;
423 public void Reset ()
425 Called = false;
426 Context = null;
427 Error = null;
428 Event.Reset ();
431 public void Callback (IAsyncResult ares)
433 Called = true;
434 if (ares == null) {
435 Error = new ArgumentNullException ("ares");
436 return;
439 try {
440 HttpListener listener = (HttpListener) ares.AsyncState;
441 Context = listener.EndGetContext (ares);
442 } catch (Exception e) {
443 Error = e;
445 Event.Set ();
448 public void Dispose ()
450 Event.Close ();
455 #endif