3 // - Unit tests for System.Net.HttpListener
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Net.Sockets;
33 using System.Threading;
34 using NUnit.Framework;
36 namespace MonoTests.System.Net {
39 [Ignore ("The class HttpListener is not implemented")]
41 public class HttpListenerTest {
44 public void DefaultProperties ()
46 HttpListener listener = new HttpListener ();
47 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
48 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
49 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
50 Assert.AreEqual (false, listener.IsListening, "#03");
51 Assert.AreEqual (0, listener.Prefixes.Count, "#04");
52 Assert.AreEqual (null, listener.Realm, "#05");
53 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
59 HttpListener listener = new HttpListener ();
66 HttpListener listener = new HttpListener ();
71 [ExpectedException (typeof (InvalidOperationException))]
72 public void GetContext1 ()
74 HttpListener listener = new HttpListener ();
75 // "Please call Start () before calling this method"
76 listener.GetContext ();
80 [ExpectedException (typeof (InvalidOperationException))]
81 public void GetContext2 ()
83 HttpListener listener = new HttpListener ();
85 // "Please call AddPrefix () before calling this method"
86 listener.GetContext ();
90 [ExpectedException (typeof (InvalidOperationException))]
91 public void BeginGetContext1 ()
93 HttpListener listener = new HttpListener ();
94 // "Please call Start () before calling this method"
95 listener.BeginGetContext (null, null);
99 public void BeginGetContext2 ()
101 HttpListener listener = new HttpListener ();
103 // One would expect this to fail as BeginGetContext1 does not fail and
104 // calling EndGetContext will wait forever.
105 // Lame. They should check that we have no prefixes.
106 IAsyncResult ares = listener.BeginGetContext (null, null);
107 Assert.IsFalse (ares.IsCompleted);
110 private bool CanOpenPort(int port)
114 using(Socket socket = new Socket (AddressFamily.InterNetwork,
118 socket.Bind (new IPEndPoint (IPAddress.Loopback, port));
122 catch(Exception ex) {
123 //Can be AccessDeniedException(ports 80/443 need root access) or
124 //SocketException because other application is listening
131 public void DefaultHttpPort ()
133 if (!CanOpenPort (80))
134 Assert.Ignore ("Can not open port 80 skipping test.");
135 using(HttpListener listener = new HttpListener ())
137 listener.Prefixes.Add ("http://127.0.0.1/");
139 Assert.IsFalse (CanOpenPort (80), "HttpListener is not listening on port 80.");
144 public void DefaultHttpsPort ()
146 if (!CanOpenPort (443))
147 Assert.Ignore ("Can not open port 443 skipping test.");
148 using(HttpListener listener = new HttpListener ())
150 listener.Prefixes.Add ("https://127.0.0.1/");
152 Assert.IsFalse (CanOpenPort (443), "HttpListener is not listening on port 443.");
157 public void TwoListeners_SameAddress ()
159 HttpListener listener1 = new HttpListener ();
160 listener1.Prefixes.Add ("http://127.0.0.1:7777/");
161 HttpListener listener2 = new HttpListener ();
162 listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
168 [ExpectedException (typeof (HttpListenerException))]
169 public void TwoListeners_SameURL ()
171 HttpListener listener1 = new HttpListener ();
172 listener1.Prefixes.Add ("http://127.0.0.1:7777/hola/");
173 HttpListener listener2 = new HttpListener ();
174 listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
180 [ExpectedException (typeof (HttpListenerException))]
181 public void MultipleSlashes ()
183 HttpListener listener = new HttpListener ();
184 listener.Prefixes.Add ("http://localhost:7777/hola////");
185 // this one throws on Start(), not when adding it.
190 [ExpectedException (typeof (HttpListenerException))]
191 public void PercentSign ()
193 HttpListener listener = new HttpListener ();
194 listener.Prefixes.Add ("http://localhost:7777/hola%3E/");
195 // this one throws on Start(), not when adding it.
200 public void CloseBeforeStart ()
202 HttpListener listener = new HttpListener ();
207 public void CloseTwice ()
209 HttpListener listener = new HttpListener ();
210 listener.Prefixes.Add ("http://localhost:7777/hola/");
217 public void StartStopStart ()
219 HttpListener listener = new HttpListener ();
220 listener.Prefixes.Add ("http://localhost:7777/hola/");
228 public void StartStopDispose ()
230 using (HttpListener listener = new HttpListener ()){
231 listener.Prefixes.Add ("http://localhost:7777/hola/");
238 public void AbortBeforeStart ()
240 HttpListener listener = new HttpListener ();
245 public void AbortTwice ()
247 HttpListener listener = new HttpListener ();
248 listener.Prefixes.Add ("http://localhost:7777/hola/");
255 public void PropertiesWhenClosed1 ()
257 HttpListener listener = new HttpListener ();
259 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
260 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
261 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
262 Assert.AreEqual (false, listener.IsListening, "#03");
263 Assert.AreEqual (null, listener.Realm, "#05");
264 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
268 [ExpectedException (typeof (ObjectDisposedException))]
269 public void PropertiesWhenClosed2 ()
271 HttpListener listener = new HttpListener ();
273 HttpListenerPrefixCollection p = listener.Prefixes;
277 [ExpectedException (typeof (ObjectDisposedException))]
278 public void PropertiesWhenClosedSet1 ()
280 HttpListener listener = new HttpListener ();
282 listener.AuthenticationSchemes = AuthenticationSchemes.None;
286 [ExpectedException (typeof (ObjectDisposedException))]
287 public void PropertiesWhenClosedSet2 ()
289 HttpListener listener = new HttpListener ();
291 listener.AuthenticationSchemeSelectorDelegate = null;
295 [ExpectedException (typeof (ObjectDisposedException))]
296 public void PropertiesWhenClosedSet3 ()
298 HttpListener listener = new HttpListener ();
300 listener.IgnoreWriteExceptions = true;
304 [ExpectedException (typeof (ObjectDisposedException))]
305 public void PropertiesWhenClosedSet4 ()
307 HttpListener listener = new HttpListener ();
309 listener.Realm = "hola";
313 [ExpectedException (typeof (ObjectDisposedException))]
314 public void PropertiesWhenClosedSet5 ()
316 HttpListener listener = new HttpListener ();
318 listener.UnsafeConnectionNtlmAuthentication = true;
322 public void PropertiesWhenClosed3 ()
324 HttpListener listener = new HttpListener ();
326 Assert.IsFalse (listener.IsListening);
330 public void CloseWhileBegin ()
332 HttpListener listener = new HttpListener ();
333 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
335 CallMe cm = new CallMe ();
336 listener.BeginGetContext (cm.Callback, listener);
338 if (false == cm.Event.WaitOne (3000, false))
339 Assert.Fail ("This should not time out.");
340 Assert.IsNotNull (cm.Error);
341 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
346 public void AbortWhileBegin ()
348 HttpListener listener = new HttpListener ();
349 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
351 CallMe cm = new CallMe ();
352 listener.BeginGetContext (cm.Callback, listener);
354 if (false == cm.Event.WaitOne (3000, false))
355 Assert.Fail ("This should not time out.");
356 Assert.IsNotNull (cm.Error);
357 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
362 [ExpectedException (typeof (HttpListenerException))]
363 public void CloseWhileGet ()
365 // "System.Net.HttpListener Exception : The I/O operation has been aborted
366 // because of either a thread exit or an application request
367 // at System.Net.HttpListener.GetContext()
368 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
370 HttpListener listener = new HttpListener ();
371 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
373 RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
375 HttpListenerContext ctx = listener.GetContext ();
379 [ExpectedException (typeof (HttpListenerException))]
380 public void AbortWhileGet ()
382 // "System.Net.HttpListener Exception : The I/O operation has been aborted
383 // because of either a thread exit or an application request
384 // at System.Net.HttpListener.GetContext()
385 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
387 HttpListener listener = new HttpListener ();
388 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
390 RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
392 HttpListenerContext ctx = listener.GetContext ();
399 public object Result;
401 public RunMe (int delay_ms, Delegate d, object [] args)
403 this.delay_ms = delay_ms;
410 Thread th = new Thread (new ThreadStart (Run));
416 Thread.Sleep (delay_ms);
417 Result = d.DynamicInvoke (args);
422 public ManualResetEvent Event = new ManualResetEvent (false);
424 public HttpListenerContext Context;
425 public Exception Error;
435 public void Callback (IAsyncResult ares)
439 Error = new ArgumentNullException ("ares");
444 HttpListener listener = (HttpListener) ares.AsyncState;
445 Context = listener.EndGetContext (ares);
446 } catch (Exception e) {
452 public void Dispose ()