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 {
37 public class HttpListenerTest {
39 public void DefaultProperties ()
41 HttpListener listener = new HttpListener ();
42 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
43 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
44 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
45 Assert.AreEqual (false, listener.IsListening, "#03");
46 Assert.AreEqual (0, listener.Prefixes.Count, "#04");
47 Assert.AreEqual (null, listener.Realm, "#05");
48 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
54 HttpListener listener = new HttpListener ();
61 HttpListener listener = new HttpListener ();
66 [ExpectedException (typeof (InvalidOperationException))]
67 public void GetContext1 ()
69 HttpListener listener = new HttpListener ();
70 // "Please call Start () before calling this method"
71 listener.GetContext ();
75 [ExpectedException (typeof (InvalidOperationException))]
76 public void GetContext2 ()
78 HttpListener listener = new HttpListener ();
80 // "Please call AddPrefix () before calling this method"
81 listener.GetContext ();
85 [ExpectedException (typeof (InvalidOperationException))]
86 public void BeginGetContext1 ()
88 HttpListener listener = new HttpListener ();
89 // "Please call Start () before calling this method"
90 listener.BeginGetContext (null, null);
94 public void BeginGetContext2 ()
96 HttpListener listener = new HttpListener ();
98 // One would expect this to fail as BeginGetContext1 does not fail and
99 // calling EndGetContext will wait forever.
100 // Lame. They should check that we have no prefixes.
101 IAsyncResult ares = listener.BeginGetContext (null, null);
102 Assert.IsFalse (ares.IsCompleted);
106 public void TwoListeners_SameAddress ()
108 HttpListener listener1 = new HttpListener ();
109 listener1.Prefixes.Add ("http://127.0.0.1:7777/");
110 HttpListener listener2 = new HttpListener ();
111 listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
117 [ExpectedException (typeof (HttpListenerException))]
118 public void TwoListeners_SameURL ()
120 HttpListener listener1 = new HttpListener ();
121 listener1.Prefixes.Add ("http://127.0.0.1:7777/hola/");
122 HttpListener listener2 = new HttpListener ();
123 listener2.Prefixes.Add ("http://127.0.0.1:7777/hola/");
129 [ExpectedException (typeof (HttpListenerException))]
130 public void MultipleSlashes ()
132 HttpListener listener = new HttpListener ();
133 listener.Prefixes.Add ("http://localhost:7777/hola////");
134 // this one throws on Start(), not when adding it.
139 [ExpectedException (typeof (HttpListenerException))]
140 public void PercentSign ()
142 HttpListener listener = new HttpListener ();
143 listener.Prefixes.Add ("http://localhost:7777/hola%3E/");
144 // this one throws on Start(), not when adding it.
149 public void CloseBeforeStart ()
151 HttpListener listener = new HttpListener ();
156 public void CloseTwice ()
158 HttpListener listener = new HttpListener ();
159 listener.Prefixes.Add ("http://localhost:7777/hola/");
166 public void AbortBeforeStart ()
168 HttpListener listener = new HttpListener ();
173 public void AbortTwice ()
175 HttpListener listener = new HttpListener ();
176 listener.Prefixes.Add ("http://localhost:7777/hola/");
183 public void PropertiesWhenClosed1 ()
185 HttpListener listener = new HttpListener ();
187 Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
188 Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
189 Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
190 Assert.AreEqual (false, listener.IsListening, "#03");
191 Assert.AreEqual (null, listener.Realm, "#05");
192 Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
196 [ExpectedException (typeof (ObjectDisposedException))]
197 public void PropertiesWhenClosed2 ()
199 HttpListener listener = new HttpListener ();
201 HttpListenerPrefixCollection p = listener.Prefixes;
205 [ExpectedException (typeof (ObjectDisposedException))]
206 public void PropertiesWhenClosedSet1 ()
208 HttpListener listener = new HttpListener ();
210 listener.AuthenticationSchemes = AuthenticationSchemes.None;
214 [ExpectedException (typeof (ObjectDisposedException))]
215 public void PropertiesWhenClosedSet2 ()
217 HttpListener listener = new HttpListener ();
219 listener.AuthenticationSchemeSelectorDelegate = null;
223 [ExpectedException (typeof (ObjectDisposedException))]
224 public void PropertiesWhenClosedSet3 ()
226 HttpListener listener = new HttpListener ();
228 listener.IgnoreWriteExceptions = true;
232 [ExpectedException (typeof (ObjectDisposedException))]
233 public void PropertiesWhenClosedSet4 ()
235 HttpListener listener = new HttpListener ();
237 listener.Realm = "hola";
241 [ExpectedException (typeof (ObjectDisposedException))]
242 public void PropertiesWhenClosedSet5 ()
244 HttpListener listener = new HttpListener ();
246 listener.UnsafeConnectionNtlmAuthentication = true;
250 public void PropertiesWhenClosed3 ()
252 HttpListener listener = new HttpListener ();
254 Assert.IsFalse (listener.IsListening);
258 [Category ("NotWorking")] // we throw a HttpListenerException
259 public void CloseWhileBegin ()
261 HttpListener listener = new HttpListener ();
262 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
264 CallMe cm = new CallMe ();
265 listener.BeginGetContext (cm.Callback, listener);
267 if (false == cm.Event.WaitOne (3000, false))
268 Assert.Fail ("This should not time out.");
269 Assert.IsNotNull (cm.Error);
270 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
275 [Category ("NotWorking")] // we throw a HttpListenerException
276 public void AbortWhileBegin ()
278 HttpListener listener = new HttpListener ();
279 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
281 CallMe cm = new CallMe ();
282 listener.BeginGetContext (cm.Callback, listener);
284 if (false == cm.Event.WaitOne (3000, false))
285 Assert.Fail ("This should not time out.");
286 Assert.IsNotNull (cm.Error);
287 Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
292 [ExpectedException (typeof (HttpListenerException))]
293 public void CloseWhileGet ()
295 // "System.Net.HttpListener Exception : The I/O operation has been aborted
296 // because of either a thread exit or an application request
297 // at System.Net.HttpListener.GetContext()
298 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
300 HttpListener listener = new HttpListener ();
301 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
303 RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
305 HttpListenerContext ctx = listener.GetContext ();
309 [ExpectedException (typeof (HttpListenerException))]
310 public void AbortWhileGet ()
312 // "System.Net.HttpListener Exception : The I/O operation has been aborted
313 // because of either a thread exit or an application request
314 // at System.Net.HttpListener.GetContext()
315 // at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()
317 HttpListener listener = new HttpListener ();
318 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
320 RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
322 HttpListenerContext ctx = listener.GetContext ();
329 public object Result;
331 public RunMe (int delay_ms, Delegate d, object [] args)
333 this.delay_ms = delay_ms;
340 Thread th = new Thread (new ThreadStart (Run));
346 Thread.Sleep (delay_ms);
347 Result = d.DynamicInvoke (args);
352 public ManualResetEvent Event = new ManualResetEvent (false);
354 public HttpListenerContext Context;
355 public Exception Error;
365 public void Callback (IAsyncResult ares)
369 Error = new ArgumentNullException ("ares");
374 HttpListener listener = (HttpListener) ares.AsyncState;
375 Context = listener.EndGetContext (ares);
376 } catch (Exception e) {
382 public void Dispose ()