Remove excessive shortcut key matching in ToolStrip
[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.Net.Sockets;
33 using System.Threading;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Net {
37         [TestFixture]
38         public class HttpListenerTest {
39                 [Test]
40                 public void DefaultProperties ()
41                 {
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");
50                 }
51
52                 [Test]
53                 public void Start1 ()
54                 {
55                         HttpListener listener = new HttpListener ();
56                         listener.Start ();
57                 }
58
59                 [Test]
60                 public void Stop1 ()
61                 {
62                         HttpListener listener = new HttpListener ();
63                         listener.Stop ();
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (InvalidOperationException))]
68                 public void GetContext1 ()
69                 {
70                         HttpListener listener = new HttpListener ();
71                         // "Please call Start () before calling this method"
72                         listener.GetContext ();
73                 }
74
75                 [Test]
76                 [ExpectedException (typeof (InvalidOperationException))]
77                 public void GetContext2 ()
78                 {
79                         HttpListener listener = new HttpListener ();
80                         listener.Start ();
81                         // "Please call AddPrefix () before calling this method"
82                         listener.GetContext ();
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (InvalidOperationException))]
87                 public void BeginGetContext1 ()
88                 {
89                         HttpListener listener = new HttpListener ();
90                         // "Please call Start () before calling this method"
91                         listener.BeginGetContext (null, null);
92                 }
93
94                 [Test]
95                 public void BeginGetContext2 ()
96                 {
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);
104                 }
105
106                 private bool CanOpenPort(int port)
107                 {
108                         try
109                         {
110                                 using(Socket socket = new Socket (AddressFamily.InterNetwork,
111                                         SocketType.Stream,
112                                         ProtocolType.Tcp))
113                                 {
114                                         socket.Bind (new IPEndPoint (IPAddress.Loopback, port));
115                                         socket.Listen(1);
116                                 }
117                         }
118                         catch(Exception) {
119                                 //Can be AccessDeniedException(ports 80/443 need root access) or
120                                 //SocketException because other application is listening
121                                 return false;
122                         }
123                         return true;
124                 }
125
126                 [Test]
127                 public void DefaultHttpPort ()
128                 {
129                         if (!CanOpenPort (80))
130                                 Assert.Ignore ("Can not open port 80 skipping test.");
131                         using(HttpListener listener = new HttpListener ())
132                         {
133                                 listener.Prefixes.Add ("http://127.0.0.1/");
134                                 listener.Start ();
135                                 Assert.IsFalse (CanOpenPort (80), "HttpListener is not listening on port 80.");
136                         }
137                 }
138
139                 [Test]
140                 public void DefaultHttpsPort ()
141                 {
142                         if (!CanOpenPort (443))
143                                 Assert.Ignore ("Can not open port 443 skipping test.");
144                         using(HttpListener listener = new HttpListener ())
145                         {
146                                 listener.Prefixes.Add ("https://127.0.0.1/");
147                                 listener.Start ();
148                                 Assert.IsFalse (CanOpenPort (443), "HttpListener is not listening on port 443.");
149                         }
150                 }
151
152                 [Test]
153                 public void TwoListeners_SameAddress ()
154                 {
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 ();
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof (HttpListenerException))]
165                 public void TwoListeners_SameURL ()
166                 {
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 ();
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (HttpListenerException))]
177                 public void MultipleSlashes ()
178                 {
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 ();
183                 }
184
185                 [Test]
186                 [ExpectedException (typeof (HttpListenerException))]
187                 public void PercentSign ()
188                 {
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 ();
193                 }
194
195                 [Test]
196                 public void CloseBeforeStart ()
197                 {
198                         HttpListener listener = new HttpListener ();
199                         listener.Close ();
200                 }
201
202                 [Test]
203                 public void CloseTwice ()
204                 {
205                         HttpListener listener = new HttpListener ();
206                         listener.Prefixes.Add ("http://localhost:7777/hola/");
207                         listener.Start ();
208                         listener.Close ();
209                         listener.Close ();
210                 }
211
212                 [Test]
213                 public void StartStopStart ()
214                 {
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 ();
221                 }
222
223                 [Test]
224                 public void StartStopDispose ()
225                 {
226                         using (HttpListener listener = new HttpListener ()){
227                                 listener.Prefixes.Add ("http://localhost:7777/hola/");
228                                 listener.Start ();
229                                 listener.Stop ();
230                         }
231                 }
232                 
233                 [Test]
234                 public void AbortBeforeStart ()
235                 {
236                         HttpListener listener = new HttpListener ();
237                         listener.Abort ();
238                 }
239
240                 [Test]
241                 public void AbortTwice ()
242                 {
243                         HttpListener listener = new HttpListener ();
244                         listener.Prefixes.Add ("http://localhost:7777/hola/");
245                         listener.Start ();
246                         listener.Abort ();
247                         listener.Abort ();
248                 }
249
250                 [Test]
251                 public void PropertiesWhenClosed1 ()
252                 {
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");
261                 }
262
263                 [Test]
264                 [ExpectedException (typeof (ObjectDisposedException))]
265                 public void PropertiesWhenClosed2 ()
266                 {
267                         HttpListener listener = new HttpListener ();
268                         listener.Close ();
269                         HttpListenerPrefixCollection p = listener.Prefixes;
270                 }
271
272                 [Test]
273                 [ExpectedException (typeof (ObjectDisposedException))]
274                 public void PropertiesWhenClosedSet1 ()
275                 {
276                         HttpListener listener = new HttpListener ();
277                         listener.Close ();
278                         listener.AuthenticationSchemes = AuthenticationSchemes.None;
279                 }
280
281                 [Test]
282                 [ExpectedException (typeof (ObjectDisposedException))]
283                 public void PropertiesWhenClosedSet2 ()
284                 {
285                         HttpListener listener = new HttpListener ();
286                         listener.Close ();
287                         listener.AuthenticationSchemeSelectorDelegate = null;
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (ObjectDisposedException))]
292                 public void PropertiesWhenClosedSet3 ()
293                 {
294                         HttpListener listener = new HttpListener ();
295                         listener.Close ();
296                         listener.IgnoreWriteExceptions = true;
297                 }
298
299                 [Test]
300                 [ExpectedException (typeof (ObjectDisposedException))]
301                 public void PropertiesWhenClosedSet4 ()
302                 {
303                         HttpListener listener = new HttpListener ();
304                         listener.Close ();
305                         listener.Realm = "hola";
306                 }
307
308                 [Test]
309                 [ExpectedException (typeof (ObjectDisposedException))]
310                 public void PropertiesWhenClosedSet5 ()
311                 {
312                         HttpListener listener = new HttpListener ();
313                         listener.Close ();
314                         listener.UnsafeConnectionNtlmAuthentication = true;
315                 }
316
317                 [Test]
318                 public void PropertiesWhenClosed3 ()
319                 {
320                         HttpListener listener = new HttpListener ();
321                         listener.Close ();
322                         Assert.IsFalse (listener.IsListening);
323                 }
324
325                 [Test]
326                 public void CloseWhileBegin ()
327                 {
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 ();
339                 }
340
341                 [Test]
342                 public void AbortWhileBegin ()
343                 {
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 ();
355                 }
356
357                 [Test]
358                 [ExpectedException (typeof (HttpListenerException))]
359                 public void CloseWhileGet ()
360                 {
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()
365
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 ();
372                 }
373
374                 [Test]
375                 [ExpectedException (typeof (HttpListenerException))]
376                 public void AbortWhileGet ()
377                 {
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()
382
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 ();
389                 }
390
391                 class RunMe {
392                         Delegate d;
393                         int delay_ms;
394                         object [] args;
395                         public object Result;
396
397                         public RunMe (int delay_ms, Delegate d, object [] args)
398                         {
399                                 this.delay_ms = delay_ms;
400                                 this.d = d;
401                                 this.args = args;
402                         }
403
404                         public void Start ()
405                         {
406                                 Thread th = new Thread (new ThreadStart (Run));
407                                 th.Start ();
408                         }
409
410                         void Run ()
411                         {
412                                 Thread.Sleep (delay_ms);
413                                 Result = d.DynamicInvoke (args);
414                         }
415                 }
416
417                 class CallMe {
418                         public ManualResetEvent Event = new ManualResetEvent (false);
419                         public bool Called;
420                         public HttpListenerContext Context;
421                         public Exception Error;
422
423                         public void Reset ()
424                         {
425                                 Called = false;
426                                 Context = null;
427                                 Error = null;
428                                 Event.Reset ();
429                         }
430
431                         public void Callback (IAsyncResult ares)
432                         {
433                                 Called = true;
434                                 if (ares == null) {
435                                         Error = new ArgumentNullException ("ares");
436                                         return;
437                                 }
438                                 
439                                 try {
440                                         HttpListener listener = (HttpListener) ares.AsyncState;
441                                         Context = listener.EndGetContext (ares);
442                                 } catch (Exception e) {
443                                         Error = e;
444                                 }
445                                 Event.Set ();
446                         }
447
448                         public void Dispose ()
449                         {
450                                 Event.Close ();
451                         }
452                 }
453         }
454 }
455 #endif
456