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 AbortBeforeStart ()
172 HttpListener listener = new HttpListener ();
177 public void AbortTwice ()
179 HttpListener listener = new HttpListener ();
180 listener.Prefixes.Add ("http://localhost:7777/hola/");
187 public void PropertiesWhenClosed1 ()
189 HttpListener listener = new HttpListener ();
191 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
192 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
193 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
194 Assert.AreEqual (false, listener.IsListening, "#03");
195 Assert.AreEqual (null, listener.Realm, "#05");
196 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
200 [ExpectedException (typeof (ObjectDisposedException))]
201 public void PropertiesWhenClosed2 ()
203 HttpListener listener = new HttpListener ();
205 HttpListenerPrefixCollection p = listener.Prefixes;
209 [ExpectedException (typeof (ObjectDisposedException))]
210 public void PropertiesWhenClosedSet1 ()
212 HttpListener listener = new HttpListener ();
214 listener.AuthenticationSchemes = AuthenticationSchemes.None;
218 [ExpectedException (typeof (ObjectDisposedException))]
219 public void PropertiesWhenClosedSet2 ()
221 HttpListener listener = new HttpListener ();
223 listener.AuthenticationSchemeSelectorDelegate = null;
227 [ExpectedException (typeof (ObjectDisposedException))]
228 public void PropertiesWhenClosedSet3 ()
230 HttpListener listener = new HttpListener ();
232 listener.IgnoreWriteExceptions = true;
236 [ExpectedException (typeof (ObjectDisposedException))]
237 public void PropertiesWhenClosedSet4 ()
239 HttpListener listener = new HttpListener ();
241 listener.Realm = "hola";
245 [ExpectedException (typeof (ObjectDisposedException))]
246 public void PropertiesWhenClosedSet5 ()
248 HttpListener listener = new HttpListener ();
250 listener.UnsafeConnectionNtlmAuthentication = true;
254 public void PropertiesWhenClosed3 ()
256 HttpListener listener = new HttpListener ();
258 Assert.IsFalse (listener.IsListening);
262 [Category ("NotWorking")] // we throw a HttpListenerException
263 public void CloseWhileBegin ()
265 HttpListener listener = new HttpListener ();
266 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
268 CallMe cm = new CallMe ();
269 listener.BeginGetContext (cm.Callback, listener);
271 if (false == cm.Event.WaitOne (3000, false))
272 Assert.Fail ("This should not time out.");
273 Assert.IsNotNull (cm.Error);
274 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
279 [Category ("NotWorking")] // we throw a HttpListenerException
280 public void AbortWhileBegin ()
282 HttpListener listener = new HttpListener ();
283 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
285 CallMe cm = new CallMe ();
286 listener.BeginGetContext (cm.Callback, listener);
288 if (false == cm.Event.WaitOne (3000, false))
289 Assert.Fail ("This should not time out.");
290 Assert.IsNotNull (cm.Error);
291 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
296 [ExpectedException (typeof (HttpListenerException))]
297 public void CloseWhileGet ()
299 // "System.Net.HttpListener Exception : The I/O operation has been aborted
300 // because of either a thread exit or an application request
301 // at System.Net.HttpListener.GetContext()
302 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
304 HttpListener listener = new HttpListener ();
305 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
307 RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
309 HttpListenerContext ctx = listener.GetContext ();
313 [ExpectedException (typeof (HttpListenerException))]
314 public void AbortWhileGet ()
316 // "System.Net.HttpListener Exception : The I/O operation has been aborted
317 // because of either a thread exit or an application request
318 // at System.Net.HttpListener.GetContext()
319 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
321 HttpListener listener = new HttpListener ();
322 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
324 RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
326 HttpListenerContext ctx = listener.GetContext ();
333 public object Result;
335 public RunMe (int delay_ms, Delegate d, object [] args)
337 this.delay_ms = delay_ms;
344 Thread th = new Thread (new ThreadStart (Run));
350 Thread.Sleep (delay_ms);
351 Result = d.DynamicInvoke (args);
356 public ManualResetEvent Event = new ManualResetEvent (false);
358 public HttpListenerContext Context;
359 public Exception Error;
369 public void Callback (IAsyncResult ares)
373 Error = new ArgumentNullException ("ares");
378 HttpListener listener = (HttpListener) ares.AsyncState;
379 Context = listener.EndGetContext (ares);
380 } catch (Exception e) {
386 public void Dispose ()