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 {
38 public class HttpListenerTest {
40 public void DefaultProperties ()
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");
55 HttpListener listener = new HttpListener ();
62 HttpListener listener = new HttpListener ();
67 [ExpectedException (typeof (InvalidOperationException))]
68 public void GetContext1 ()
70 HttpListener listener = new HttpListener ();
71 // "Please call Start () before calling this method"
72 listener.GetContext ();
76 [ExpectedException (typeof (InvalidOperationException))]
77 public void GetContext2 ()
79 HttpListener listener = new HttpListener ();
81 // "Please call AddPrefix () before calling this method"
82 listener.GetContext ();
86 [ExpectedException (typeof (InvalidOperationException))]
87 public void BeginGetContext1 ()
89 HttpListener listener = new HttpListener ();
90 // "Please call Start () before calling this method"
91 listener.BeginGetContext (null, null);
95 public void BeginGetContext2 ()
97 HttpListener listener = new HttpListener ();
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);
106 private bool CanOpenPort(int port)
110 using(Socket socket = new Socket (AddressFamily.InterNetwork,
114 socket.Bind (new IPEndPoint (IPAddress.Loopback, port));
118 catch(Exception ex) {
119 //Can be AccessDeniedException(ports 80/443 need root access) or
120 //SocketException because other application is listening
127 public void DefaultHttpPort ()
129 if (!CanOpenPort (80))
130 Assert.Ignore ("Can not open port 80 skipping test.");
131 using(HttpListener listener = new HttpListener ())
133 listener.Prefixes.Add ("http://127.0.0.1/");
135 Assert.IsFalse (CanOpenPort (80), "HttpListener is not listening on port 80.");
140 public void DefaultHttpsPort ()
142 if (!CanOpenPort (443))
143 Assert.Ignore ("Can not open port 443 skipping test.");
144 using(HttpListener listener = new HttpListener ())
146 listener.Prefixes.Add ("https://127.0.0.1/");
148 Assert.IsFalse (CanOpenPort (443), "HttpListener is not listening on port 443.");
153 public void TwoListeners_SameAddress ()
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/");
164 [ExpectedException (typeof (HttpListenerException))]
165 public void TwoListeners_SameURL ()
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/");
176 [ExpectedException (typeof (HttpListenerException))]
177 public void MultipleSlashes ()
179 HttpListener listener = new HttpListener ();
180 listener.Prefixes.Add ("http://localhost:7777/hola////");
181 // this one throws on Start(), not when adding it.
186 [ExpectedException (typeof (HttpListenerException))]
187 public void PercentSign ()
189 HttpListener listener = new HttpListener ();
190 listener.Prefixes.Add ("http://localhost:7777/hola%3E/");
191 // this one throws on Start(), not when adding it.
196 public void CloseBeforeStart ()
198 HttpListener listener = new HttpListener ();
203 public void CloseTwice ()
205 HttpListener listener = new HttpListener ();
206 listener.Prefixes.Add ("http://localhost:7777/hola/");
213 public void StartStopStart ()
215 HttpListener listener = new HttpListener ();
216 listener.Prefixes.Add ("http://localhost:7777/hola/");
224 public void StartStopDispose ()
226 using (HttpListener listener = new HttpListener ()){
227 listener.Prefixes.Add ("http://localhost:7777/hola/");
234 public void AbortBeforeStart ()
236 HttpListener listener = new HttpListener ();
241 public void AbortTwice ()
243 HttpListener listener = new HttpListener ();
244 listener.Prefixes.Add ("http://localhost:7777/hola/");
251 public void PropertiesWhenClosed1 ()
253 HttpListener listener = new HttpListener ();
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");
264 [ExpectedException (typeof (ObjectDisposedException))]
265 public void PropertiesWhenClosed2 ()
267 HttpListener listener = new HttpListener ();
269 HttpListenerPrefixCollection p = listener.Prefixes;
273 [ExpectedException (typeof (ObjectDisposedException))]
274 public void PropertiesWhenClosedSet1 ()
276 HttpListener listener = new HttpListener ();
278 listener.AuthenticationSchemes = AuthenticationSchemes.None;
282 [ExpectedException (typeof (ObjectDisposedException))]
283 public void PropertiesWhenClosedSet2 ()
285 HttpListener listener = new HttpListener ();
287 listener.AuthenticationSchemeSelectorDelegate = null;
291 [ExpectedException (typeof (ObjectDisposedException))]
292 public void PropertiesWhenClosedSet3 ()
294 HttpListener listener = new HttpListener ();
296 listener.IgnoreWriteExceptions = true;
300 [ExpectedException (typeof (ObjectDisposedException))]
301 public void PropertiesWhenClosedSet4 ()
303 HttpListener listener = new HttpListener ();
305 listener.Realm = "hola";
309 [ExpectedException (typeof (ObjectDisposedException))]
310 public void PropertiesWhenClosedSet5 ()
312 HttpListener listener = new HttpListener ();
314 listener.UnsafeConnectionNtlmAuthentication = true;
318 public void PropertiesWhenClosed3 ()
320 HttpListener listener = new HttpListener ();
322 Assert.IsFalse (listener.IsListening);
326 public void CloseWhileBegin ()
328 HttpListener listener = new HttpListener ();
329 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhilebegin/");
331 CallMe cm = new CallMe ();
332 listener.BeginGetContext (cm.Callback, listener);
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");
342 public void AbortWhileBegin ()
344 HttpListener listener = new HttpListener ();
345 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhilebegin/");
347 CallMe cm = new CallMe ();
348 listener.BeginGetContext (cm.Callback, listener);
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");
358 [ExpectedException (typeof (HttpListenerException))]
359 public void CloseWhileGet ()
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()
366 HttpListener listener = new HttpListener ();
367 listener.Prefixes.Add ("http://127.0.0.1:9001/closewhileget/");
369 RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
371 HttpListenerContext ctx = listener.GetContext ();
375 [ExpectedException (typeof (HttpListenerException))]
376 public void AbortWhileGet ()
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()
383 HttpListener listener = new HttpListener ();
384 listener.Prefixes.Add ("http://127.0.0.1:9001/abortwhileget/");
386 RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
388 HttpListenerContext ctx = listener.GetContext ();
395 public object Result;
397 public RunMe (int delay_ms, Delegate d, object [] args)
399 this.delay_ms = delay_ms;
406 Thread th = new Thread (new ThreadStart (Run));
412 Thread.Sleep (delay_ms);
413 Result = d.DynamicInvoke (args);
418 public ManualResetEvent Event = new ManualResetEvent (false);
420 public HttpListenerContext Context;
421 public Exception Error;
431 public void Callback (IAsyncResult ares)
435 Error = new ArgumentNullException ("ares");
440 HttpListener listener = (HttpListener) ares.AsyncState;
441 Context = listener.EndGetContext (ares);
442 } catch (Exception e) {
448 public void Dispose ()
455 public void ConnectionReuse ()
457 var uri = "http://localhost:1338/";
459 HttpListener listener = new HttpListener ();
460 listener.Prefixes.Add (uri);
463 IPEndPoint expectedIpEndPoint = CreateListenerRequest (listener, uri);
465 Assert.AreEqual (expectedIpEndPoint, CreateListenerRequest (listener, uri), "reuse1");
466 Assert.AreEqual (expectedIpEndPoint, CreateListenerRequest (listener, uri), "reuse2");
469 public IPEndPoint CreateListenerRequest (HttpListener listener, string uri)
471 IPEndPoint ipEndPoint = null;
472 listener.BeginGetContext ((result) => ipEndPoint = ListenerCallback (result), listener);
474 var request = (HttpWebRequest) WebRequest.Create (uri);
475 request.Method = "POST";
477 // We need to write something
478 request.GetRequestStream ().Write (new byte [] {(byte)'a'}, 0, 1);
479 request.GetRequestStream ().Dispose ();
481 // Send request, socket is created or reused.
482 var response = request.GetResponse ();
484 // Close response so socket can be reused.
490 public static IPEndPoint ListenerCallback (IAsyncResult result)
492 var listener = (HttpListener) result.AsyncState;
493 var context = listener.EndGetContext (result);
494 var clientEndPoint = context.Request.RemoteEndPoint;
496 // Disposing InputStream should not avoid socket reuse
497 context.Request.InputStream.Dispose ();
499 // Close OutputStream to send response
500 context.Response.OutputStream.Close ();
502 return clientEndPoint;