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.Threading;
33 using NUnit.Framework;
35 namespace MonoTests.System.Net {
38 [Ignore ("The class HttpListener is not implemented")]
40 public class HttpListenerTest {
43 public void DefaultProperties ()
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");
58 HttpListener listener = new HttpListener ();
65 HttpListener listener = new HttpListener ();
70 [ExpectedException (typeof (InvalidOperationException))]
71 public void GetContext1 ()
73 HttpListener listener = new HttpListener ();
74 // "Please call Start () before calling this method"
75 listener.GetContext ();
79 [ExpectedException (typeof (InvalidOperationException))]
80 public void GetContext2 ()
82 HttpListener listener = new HttpListener ();
84 // "Please call AddPrefix () before calling this method"
85 listener.GetContext ();
89 [ExpectedException (typeof (InvalidOperationException))]
90 public void BeginGetContext1 ()
92 HttpListener listener = new HttpListener ();
93 // "Please call Start () before calling this method"
94 listener.BeginGetContext (null, null);
98 public void BeginGetContext2 ()
100 HttpListener listener = new HttpListener ();
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);
110 public void TwoListeners_SameAddress ()
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/");
121 [ExpectedException (typeof (HttpListenerException))]
122 public void TwoListeners_SameURL ()
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/");
133 [ExpectedException (typeof (HttpListenerException))]
134 public void MultipleSlashes ()
136 HttpListener listener = new HttpListener ();
137 listener.Prefixes.Add ("http://localhost:7777/hola////");
138 // this one throws on Start(), not when adding it.
143 [ExpectedException (typeof (HttpListenerException))]
144 public void PercentSign ()
146 HttpListener listener = new HttpListener ();
147 listener.Prefixes.Add ("http://localhost:7777/hola%3E/");
148 // this one throws on Start(), not when adding it.
153 public void CloseBeforeStart ()
155 HttpListener listener = new HttpListener ();
160 public void CloseTwice ()
162 HttpListener listener = new HttpListener ();
163 listener.Prefixes.Add ("http://localhost:7777/hola/");
170 public void StartStopStart ()
172 HttpListener listener = new HttpListener ();
173 listener.Prefixes.Add ("http://localhost:7777/hola/");
181 public void StartStopDispose ()
183 using (HttpListener listener = new HttpListener ()){
184 listener.Prefixes.Add ("http://localhost:7777/hola/");
191 public void AbortBeforeStart ()
193 HttpListener listener = new HttpListener ();
198 public void AbortTwice ()
200 HttpListener listener = new HttpListener ();
201 listener.Prefixes.Add ("http://localhost:7777/hola/");
208 public void PropertiesWhenClosed1 ()
210 HttpListener listener = new HttpListener ();
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");
221 [ExpectedException (typeof (ObjectDisposedException))]
222 public void PropertiesWhenClosed2 ()
224 HttpListener listener = new HttpListener ();
226 HttpListenerPrefixCollection p = listener.Prefixes;
230 [ExpectedException (typeof (ObjectDisposedException))]
231 public void PropertiesWhenClosedSet1 ()
233 HttpListener listener = new HttpListener ();
235 listener.AuthenticationSchemes = AuthenticationSchemes.None;
239 [ExpectedException (typeof (ObjectDisposedException))]
240 public void PropertiesWhenClosedSet2 ()
242 HttpListener listener = new HttpListener ();
244 listener.AuthenticationSchemeSelectorDelegate = null;
248 [ExpectedException (typeof (ObjectDisposedException))]
249 public void PropertiesWhenClosedSet3 ()
251 HttpListener listener = new HttpListener ();
253 listener.IgnoreWriteExceptions = true;
257 [ExpectedException (typeof (ObjectDisposedException))]
258 public void PropertiesWhenClosedSet4 ()
260 HttpListener listener = new HttpListener ();
262 listener.Realm = "hola";
266 [ExpectedException (typeof (ObjectDisposedException))]
267 public void PropertiesWhenClosedSet5 ()
269 HttpListener listener = new HttpListener ();
271 listener.UnsafeConnectionNtlmAuthentication = true;
275 public void PropertiesWhenClosed3 ()
277 HttpListener listener = new HttpListener ();
279 Assert.IsFalse (listener.IsListening);
283 public void CloseWhileBegin ()
285 HttpListener listener = new HttpListener ();
286 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
288 CallMe cm = new CallMe ();
289 listener.BeginGetContext (cm.Callback, listener);
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");
299 public void AbortWhileBegin ()
301 HttpListener listener = new HttpListener ();
302 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
304 CallMe cm = new CallMe ();
305 listener.BeginGetContext (cm.Callback, listener);
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");
315 [ExpectedException (typeof (HttpListenerException))]
316 public void CloseWhileGet ()
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()
323 HttpListener listener = new HttpListener ();
324 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
326 RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
328 HttpListenerContext ctx = listener.GetContext ();
332 [ExpectedException (typeof (HttpListenerException))]
333 public void AbortWhileGet ()
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()
340 HttpListener listener = new HttpListener ();
341 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
343 RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
345 HttpListenerContext ctx = listener.GetContext ();
352 public object Result;
354 public RunMe (int delay_ms, Delegate d, object [] args)
356 this.delay_ms = delay_ms;
363 Thread th = new Thread (new ThreadStart (Run));
369 Thread.Sleep (delay_ms);
370 Result = d.DynamicInvoke (args);
375 public ManualResetEvent Event = new ManualResetEvent (false);
377 public HttpListenerContext Context;
378 public Exception Error;
388 public void Callback (IAsyncResult ares)
392 Error = new ArgumentNullException ("ares");
397 HttpListener listener = (HttpListener) ares.AsyncState;
398 Context = listener.EndGetContext (ares);
399 } catch (Exception e) {
405 public void Dispose ()