Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / System / Test / System.Net / HttpListenerTest.cs
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.
28 //
29 #if NET_2_0
30 using System;
31 using System.Net;
32 using System.Threading;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Net {
36         [TestFixture]
37 #if TARGET_JVM
38         [Ignore ("The class HttpListener is not implemented")]
39 #endif
40         public class HttpListenerTest {
41 #if !TARGET_JVM
42                 [Test]
43                 public void DefaultProperties ()
44                 {
45                         HttpListener listener = new HttpListener ();
46                         Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
47                         Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
48                         Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
49                         Assert.AreEqual (false, listener.IsListening, "#03");
50                         Assert.AreEqual (0, listener.Prefixes.Count, "#04");
51                         Assert.AreEqual (null, listener.Realm, "#05");
52                         Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
53                 }
54
55                 [Test]
56                 public void Start1 ()
57                 {
58                         HttpListener listener = new HttpListener ();
59                         listener.Start ();
60                 }
61
62                 [Test]
63                 public void Stop1 ()
64                 {
65                         HttpListener listener = new HttpListener ();
66                         listener.Stop ();
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (InvalidOperationException))]
71                 public void GetContext1 ()
72                 {
73                         HttpListener listener = new HttpListener ();
74                         // "Please call Start () before calling this method"
75                         listener.GetContext ();
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (InvalidOperationException))]
80                 public void GetContext2 ()
81                 {
82                         HttpListener listener = new HttpListener ();
83                         listener.Start ();
84                         // "Please call AddPrefix () before calling this method"
85                         listener.GetContext ();
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (InvalidOperationException))]
90                 public void BeginGetContext1 ()
91                 {
92                         HttpListener listener = new HttpListener ();
93                         // "Please call Start () before calling this method"
94                         listener.BeginGetContext (null, null);
95                 }
96
97                 [Test]
98                 public void BeginGetContext2 ()
99                 {
100                         HttpListener listener = new HttpListener ();
101                         listener.Start ();
102                         // One would expect this to fail as BeginGetContext1 does not fail and
103                         // calling EndGetContext will wait forever.
104                         // Lame. They should check that we have no prefixes.
105                         IAsyncResult ares = listener.BeginGetContext (null, null);
106                         Assert.IsFalse (ares.IsCompleted);
107                 }
108
109                 [Test]
110                 public void TwoListeners_SameAddress ()
111                 {
112                         HttpListener listener1 = new HttpListener ();
113                         listener1.Prefixes.Add ("http://127.0.0.1:7777/");
114                         HttpListener listener2 = new HttpListener ();
115                         listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
116                         listener1.Start ();
117                         listener2.Start ();
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (HttpListenerException))]
122                 public void TwoListeners_SameURL ()
123                 {
124                         HttpListener listener1 = new HttpListener ();
125                         listener1.Prefixes.Add ("http://127.0.0.1:7777/hola/");
126                         HttpListener listener2 = new HttpListener ();
127                         listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
128                         listener1.Start ();
129                         listener2.Start ();
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (HttpListenerException))]
134                 public void MultipleSlashes ()
135                 {
136                         HttpListener listener = new HttpListener ();
137                         listener.Prefixes.Add ("http://localhost:7777/hola////");
138                         // this one throws on Start(), not when adding it.
139                         listener.Start ();
140                 }
141
142                 [Test]
143                 [ExpectedException (typeof (HttpListenerException))]
144                 public void PercentSign ()
145                 {
146                         HttpListener listener = new HttpListener ();
147                         listener.Prefixes.Add ("http://localhost:7777/hola%3E/");
148                         // this one throws on Start(), not when adding it.
149                         listener.Start ();
150                 }
151
152                 [Test]
153                 public void CloseBeforeStart ()
154                 {
155                         HttpListener listener = new HttpListener ();
156                         listener.Close ();
157                 }
158
159                 [Test]
160                 public void CloseTwice ()
161                 {
162                         HttpListener listener = new HttpListener ();
163                         listener.Prefixes.Add ("http://localhost:7777/hola/");
164                         listener.Start ();
165                         listener.Close ();
166                         listener.Close ();
167                 }
168
169                 [Test]
170                 public void StartStopStart ()
171                 {
172                         HttpListener listener = new HttpListener ();
173                         listener.Prefixes.Add ("http://localhost:7777/hola/");
174                         listener.Start ();
175                         listener.Stop ();
176                         listener.Start ();
177                         listener.Close ();
178                 }
179
180                 [Test]
181                 public void StartStopDispose ()
182                 {
183                         using (HttpListener listener = new HttpListener ()){
184                                 listener.Prefixes.Add ("http://localhost:7777/hola/");
185                                 listener.Start ();
186                                 listener.Stop ();
187                         }
188                 }
189                 
190                 [Test]
191                 public void AbortBeforeStart ()
192                 {
193                         HttpListener listener = new HttpListener ();
194                         listener.Abort ();
195                 }
196
197                 [Test]
198                 public void AbortTwice ()
199                 {
200                         HttpListener listener = new HttpListener ();
201                         listener.Prefixes.Add ("http://localhost:7777/hola/");
202                         listener.Start ();
203                         listener.Abort ();
204                         listener.Abort ();
205                 }
206
207                 [Test]
208                 public void PropertiesWhenClosed1 ()
209                 {
210                         HttpListener listener = new HttpListener ();
211                         listener.Close ();
212                         Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
213                         Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
214                         Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
215                         Assert.AreEqual (false, listener.IsListening, "#03");
216                         Assert.AreEqual (null, listener.Realm, "#05");
217                         Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (ObjectDisposedException))]
222                 public void PropertiesWhenClosed2 ()
223                 {
224                         HttpListener listener = new HttpListener ();
225                         listener.Close ();
226                         HttpListenerPrefixCollection p = listener.Prefixes;
227                 }
228
229                 [Test]
230                 [ExpectedException (typeof (ObjectDisposedException))]
231                 public void PropertiesWhenClosedSet1 ()
232                 {
233                         HttpListener listener = new HttpListener ();
234                         listener.Close ();
235                         listener.AuthenticationSchemes = AuthenticationSchemes.None;
236                 }
237
238                 [Test]
239                 [ExpectedException (typeof (ObjectDisposedException))]
240                 public void PropertiesWhenClosedSet2 ()
241                 {
242                         HttpListener listener = new HttpListener ();
243                         listener.Close ();
244                         listener.AuthenticationSchemeSelectorDelegate = null;
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (ObjectDisposedException))]
249                 public void PropertiesWhenClosedSet3 ()
250                 {
251                         HttpListener listener = new HttpListener ();
252                         listener.Close ();
253                         listener.IgnoreWriteExceptions = true;
254                 }
255
256                 [Test]
257                 [ExpectedException (typeof (ObjectDisposedException))]
258                 public void PropertiesWhenClosedSet4 ()
259                 {
260                         HttpListener listener = new HttpListener ();
261                         listener.Close ();
262                         listener.Realm = "hola";
263                 }
264
265                 [Test]
266                 [ExpectedException (typeof (ObjectDisposedException))]
267                 public void PropertiesWhenClosedSet5 ()
268                 {
269                         HttpListener listener = new HttpListener ();
270                         listener.Close ();
271                         listener.UnsafeConnectionNtlmAuthentication = true;
272                 }
273
274                 [Test]
275                 public void PropertiesWhenClosed3 ()
276                 {
277                         HttpListener listener = new HttpListener ();
278                         listener.Close ();
279                         Assert.IsFalse (listener.IsListening);
280                 }
281
282                 [Test]
283                 public void CloseWhileBegin ()
284                 {
285                         HttpListener listener = new HttpListener ();
286                         listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
287                         listener.Start ();
288                         CallMe cm = new CallMe ();
289                         listener.BeginGetContext (cm.Callback, listener);
290                         listener.Close ();
291                         if (false == cm.Event.WaitOne (3000, false))
292                                 Assert.Fail ("This should not time out.");
293                         Assert.IsNotNull (cm.Error);
294                         Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
295                         cm.Dispose ();
296                 }
297
298                 [Test]
299                 public void AbortWhileBegin ()
300                 {
301                         HttpListener listener = new HttpListener ();
302                         listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
303                         listener.Start ();
304                         CallMe cm = new CallMe ();
305                         listener.BeginGetContext (cm.Callback, listener);
306                         listener.Abort ();
307                         if (false == cm.Event.WaitOne (3000, false))
308                                 Assert.Fail ("This should not time out.");
309                         Assert.IsNotNull (cm.Error);
310                         Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
311                         cm.Dispose ();
312                 }
313
314                 [Test]
315                 [ExpectedException (typeof (HttpListenerException))]
316                 public void CloseWhileGet ()
317                 {
318                         // "System.Net.HttpListener Exception : The I/O operation has been aborted
319                         // because of either a thread exit or an application request
320                         //   at System.Net.HttpListener.GetContext()
321                         //   at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
322
323                         HttpListener listener = new HttpListener ();
324                         listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
325                         listener.Start ();
326                         RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
327                         rm.Start ();
328                         HttpListenerContext ctx = listener.GetContext ();
329                 }
330
331                 [Test]
332                 [ExpectedException (typeof (HttpListenerException))]
333                 public void AbortWhileGet ()
334                 {
335                         // "System.Net.HttpListener Exception : The I/O operation has been aborted
336                         // because of either a thread exit or an application request
337                         //   at System.Net.HttpListener.GetContext()
338                         //   at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
339
340                         HttpListener listener = new HttpListener ();
341                         listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
342                         listener.Start ();
343                         RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
344                         rm.Start ();
345                         HttpListenerContext ctx = listener.GetContext ();
346                 }
347
348                 class RunMe {
349                         Delegate d;
350                         int delay_ms;
351                         object [] args;
352                         public object Result;
353
354                         public RunMe (int delay_ms, Delegate d, object [] args)
355                         {
356                                 this.delay_ms = delay_ms;
357                                 this.d = d;
358                                 this.args = args;
359                         }
360
361                         public void Start ()
362                         {
363                                 Thread th = new Thread (new ThreadStart (Run));
364                                 th.Start ();
365                         }
366
367                         void Run ()
368                         {
369                                 Thread.Sleep (delay_ms);
370                                 Result = d.DynamicInvoke (args);
371                         }
372                 }
373
374                 class CallMe {
375                         public ManualResetEvent Event = new ManualResetEvent (false);
376                         public bool Called;
377                         public HttpListenerContext Context;
378                         public Exception Error;
379
380                         public void Reset ()
381                         {
382                                 Called = false;
383                                 Context = null;
384                                 Error = null;
385                                 Event.Reset ();
386                         }
387
388                         public void Callback (IAsyncResult ares)
389                         {
390                                 Called = true;
391                                 if (ares == null) {
392                                         Error = new ArgumentNullException ("ares");
393                                         return;
394                                 }
395                                 
396                                 try {
397                                         HttpListener listener = (HttpListener) ares.AsyncState;
398                                         Context = listener.EndGetContext (ares);
399                                 } catch (Exception e) {
400                                         Error = e;
401                                 }
402                                 Event.Set ();
403                         }
404
405                         public void Dispose ()
406                         {
407                                 Event.Close ();
408                         }
409                 }
410 #endif
411         }
412 }
413 #endif
414