File: HttpListenerTest.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (585 lines) | stat: -rw-r--r-- 16,598 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//
// HttpListenerTest.cs
//	- Unit tests for System.Net.HttpListener
//
// Author:
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using MonoTests.Helpers;

namespace MonoTests.System.Net {
	[TestFixture]
	public class HttpListenerTest {

		int port;

		[SetUp]
		public void SetUp () {
			port = NetworkHelpers.FindFreePort ();
		}

		[Test]
		public void DefaultProperties ()
		{
			HttpListener listener = new HttpListener ();
			Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
			Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
			Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
			Assert.AreEqual (false, listener.IsListening, "#03");
			Assert.AreEqual (0, listener.Prefixes.Count, "#04");
			Assert.AreEqual (null, listener.Realm, "#05");
			Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
		}

		[Test]
		public void Start1 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Start ();
		}

		[Test]
		public void Stop1 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Stop ();
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void GetContext1 ()
		{
			HttpListener listener = new HttpListener ();
			// "Please call Start () before calling this method"
			listener.GetContext ();
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void GetContext2 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Start ();
			// "Please call AddPrefix () before calling this method"
			listener.GetContext ();
		}

		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void BeginGetContext1 ()
		{
			HttpListener listener = new HttpListener ();
			// "Please call Start () before calling this method"
			listener.BeginGetContext (null, null);
		}

		[Test]
		public void BeginGetContext2 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Start ();
			// One would expect this to fail as BeginGetContext1 does not fail and
			// calling EndGetContext will wait forever.
			// Lame. They should check that we have no prefixes.
			IAsyncResult ares = listener.BeginGetContext (null, null);
			Assert.IsFalse (ares.IsCompleted);
		}

		private bool CanOpenPort(int port)
		{
			try
			{
				using(Socket socket = new Socket (AddressFamily.InterNetwork,
					SocketType.Stream,
					ProtocolType.Tcp))
				{
					socket.Bind (new IPEndPoint (IPAddress.Loopback, port));
					socket.Listen(1);
				}
			}
			catch(Exception) {
				//Can be AccessDeniedException(ports 80/443 need root access) or
				//SocketException because other application is listening
				return false;
			}
			return true;
		}

		[Test]
		public void DefaultHttpPort ()
		{
			if (!CanOpenPort (80))
				Assert.Ignore ("Can not open port 80 skipping test.");
			using(HttpListener listener = new HttpListener ())
			{
				listener.Prefixes.Add ("http://127.0.0.1/");
				listener.Start ();
				Assert.IsFalse (CanOpenPort (80), "HttpListener is not listening on port 80.");
			}
		}

		[Test]
		public void DefaultHttpsPort ()
		{
			if (!CanOpenPort (443))
				Assert.Ignore ("Can not open port 443 skipping test.");
			using(HttpListener listener = new HttpListener ())
			{
				listener.Prefixes.Add ("https://127.0.0.1/");
				listener.Start ();
				Assert.IsFalse (CanOpenPort (443), "HttpListener is not listening on port 443.");
			}
		}

		[Test]
		public void TwoListeners_SameAddress ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener1 = new HttpListener ();
			listener1.Prefixes.Add ("http://127.0.0.1:" + port + "/");
			HttpListener listener2 = new HttpListener ();
			listener2.Prefixes.Add ("http://127.0.0.1:" + port + "/hola/");
			listener1.Start ();
			listener2.Start ();
		}

		[Test]
		[ExpectedException (typeof (HttpListenerException))]
		public void TwoListeners_SameURL ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener1 = new HttpListener ();
			listener1.Prefixes.Add ("http://127.0.0.1:" + port + "/hola/");
			HttpListener listener2 = new HttpListener ();
			listener2.Prefixes.Add ("http://127.0.0.1:" + port + "/hola/");
			listener1.Start ();
			listener2.Start ();
		}

		[Test]
		[ExpectedException (typeof (HttpListenerException))]
		public void MultipleSlashes ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://localhost:" + port + "/hola////");
			// this one throws on Start(), not when adding it.
			listener.Start ();
		}

		[Test]
		[ExpectedException (typeof (HttpListenerException))]
		public void PercentSign ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://localhost:" + port + "/hola%3E/");
			// this one throws on Start(), not when adding it.
			listener.Start ();
		}

		[Test]
		public void CloseBeforeStart ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
		}

		[Test]
		public void CloseTwice ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://localhost:" + port + "/hola/");
			listener.Start ();
			listener.Close ();
			listener.Close ();
		}

		[Test]
		public void StartStopStart ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://localhost:" + port + "/hola/");
			listener.Start ();
			listener.Stop ();
			listener.Start ();
			listener.Close ();
		}

		[Test]
		public void StartStopDispose ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			using (HttpListener listener = new HttpListener ()){
				listener.Prefixes.Add ("http://localhost:" + port + "/hola/");
				listener.Start ();
				listener.Stop ();
			}
		}
		
		[Test]
		public void AbortBeforeStart ()
		{
			HttpListener listener = new HttpListener ();
			listener.Abort ();
		}

		[Test]
		public void AbortTwice ()
		{
			if (!CanOpenPort (port))
				Assert.Ignore ("port");
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://localhost:" + port + "/hola/");
			listener.Start ();
			listener.Abort ();
			listener.Abort ();
		}

		[Test]
		public void PropertiesWhenClosed1 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			Assert.AreEqual (AuthenticationSchemes.Anonymous, listener.AuthenticationSchemes, "#01");
			Assert.AreEqual (null, listener.AuthenticationSchemeSelectorDelegate, "#02");
			Assert.AreEqual (false, listener.IgnoreWriteExceptions, "#03");
			Assert.AreEqual (false, listener.IsListening, "#03");
			Assert.AreEqual (null, listener.Realm, "#05");
			Assert.AreEqual (false, listener.UnsafeConnectionNtlmAuthentication, "#06");
		}

		[Test]
		[ExpectedException (typeof (ObjectDisposedException))]
		public void PropertiesWhenClosed2 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			HttpListenerPrefixCollection p = listener.Prefixes;
		}

		[Test]
		[ExpectedException (typeof (ObjectDisposedException))]
		public void PropertiesWhenClosedSet1 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			listener.AuthenticationSchemes = AuthenticationSchemes.None;
		}

		[Test]
		[ExpectedException (typeof (ObjectDisposedException))]
		public void PropertiesWhenClosedSet2 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			listener.AuthenticationSchemeSelectorDelegate = null;
		}

		[Test]
		[ExpectedException (typeof (ObjectDisposedException))]
		public void PropertiesWhenClosedSet3 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			listener.IgnoreWriteExceptions = true;
		}

		[Test]
		[ExpectedException (typeof (ObjectDisposedException))]
		public void PropertiesWhenClosedSet4 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			listener.Realm = "hola";
		}

		[Test]
		[ExpectedException (typeof (ObjectDisposedException))]
		public void PropertiesWhenClosedSet5 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			listener.UnsafeConnectionNtlmAuthentication = true;
		}

		[Test]
		public void PropertiesWhenClosed3 ()
		{
			HttpListener listener = new HttpListener ();
			listener.Close ();
			Assert.IsFalse (listener.IsListening);
		}

		[Test]
		public void CloseWhileBegin ()
		{
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/closewhilebegin/");
			listener.Start ();
			CallMe cm = new CallMe ();
			listener.BeginGetContext (cm.Callback, listener);
			listener.Close ();
			if (false == cm.Event.WaitOne (3000, false))
				Assert.Fail ("This should not time out.");
			Assert.IsNotNull (cm.Error);
			Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
			cm.Dispose ();
		}

		[Test]
		public void AbortWhileBegin ()
		{
			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/abortwhilebegin/");
			listener.Start ();
			CallMe cm = new CallMe ();
			listener.BeginGetContext (cm.Callback, listener);
			listener.Abort ();
			if (false == cm.Event.WaitOne (3000, false))
				Assert.Fail ("This should not time out.");
			Assert.IsNotNull (cm.Error);
			Assert.AreEqual (typeof (ObjectDisposedException), cm.Error.GetType (), "Exception type");
			cm.Dispose ();
		}

		[Test]
		[ExpectedException (typeof (HttpListenerException))]
		public void CloseWhileGet ()
		{
			// "System.Net.HttpListener Exception : The I/O operation has been aborted
			// because of either a thread exit or an application request
			//   at System.Net.HttpListener.GetContext()
			//   at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()

			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/closewhileget/");
			listener.Start ();
			RunMe rm = new RunMe (1000, new ThreadStart (listener.Close), new object [0]);
			rm.Start ();
			HttpListenerContext ctx = listener.GetContext ();
		}

		[Test]
		[ExpectedException (typeof (HttpListenerException))]
		public void AbortWhileGet ()
		{
			// "System.Net.HttpListener Exception : The I/O operation has been aborted
			// because of either a thread exit or an application request
			//   at System.Net.HttpListener.GetContext()
			//   at MonoTests.System.Net.HttpListenerTest.CloseWhileGet()

			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add ("http://127.0.0.1:" + NetworkHelpers.FindFreePort () + "/abortwhileget/");
			listener.Start ();
			RunMe rm = new RunMe (1000, new ThreadStart (listener.Abort), new object [0]);
			rm.Start ();
			HttpListenerContext ctx = listener.GetContext ();
		}

		class RunMe {
			Delegate d;
			int delay_ms;
			object [] args;
			public object Result;

			public RunMe (int delay_ms, Delegate d, object [] args)
			{
				this.delay_ms = delay_ms;
				this.d = d;
				this.args = args;
			}

			public void Start ()
			{
				Thread th = new Thread (new ThreadStart (Run));
				th.Start ();
			}

			void Run ()
			{
				Thread.Sleep (delay_ms);
				Result = d.DynamicInvoke (args);
			}
		}

		class CallMe {
			public ManualResetEvent Event = new ManualResetEvent (false);
			public bool Called;
			public HttpListenerContext Context;
			public Exception Error;

			public void Reset ()
			{
				Called = false;
				Context = null;
				Error = null;
				Event.Reset ();
			}

			public void Callback (IAsyncResult ares)
			{
				Called = true;
				if (ares == null) {
					Error = new ArgumentNullException ("ares");
					return;
				}
				
				try {
					HttpListener listener = (HttpListener) ares.AsyncState;
					Context = listener.EndGetContext (ares);
				} catch (Exception e) {
					Error = e;
				}
				Event.Set ();
			}

			public void Dispose ()
			{
				Event.Close ();
			}
		}

		[Test]
		public void ConnectionReuse ()
		{
			var uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";

			HttpListener listener = new HttpListener ();
			listener.Prefixes.Add (uri);
			listener.Start ();

			IPEndPoint expectedIpEndPoint = CreateListenerRequest (listener, uri);

			Assert.AreEqual (expectedIpEndPoint, CreateListenerRequest (listener, uri), "reuse1");
			Assert.AreEqual (expectedIpEndPoint, CreateListenerRequest (listener, uri), "reuse2");
		}

		public IPEndPoint CreateListenerRequest (HttpListener listener, string uri)
		{
			IPEndPoint ipEndPoint = null;
			var mre = new ManualResetEventSlim ();
			listener.BeginGetContext (result => {
				ipEndPoint = ListenerCallback (result);
				mre.Set ();
			}, listener);

			var request = (HttpWebRequest) WebRequest.Create (uri);
			request.Method = "POST";

			// We need to write something
			request.GetRequestStream ().Write (new byte [] {(byte)'a'}, 0, 1);
			request.GetRequestStream ().Dispose ();

			// Send request, socket is created or reused.
			var response = request.GetResponse ();

			// Close response so socket can be reused.
			response.Close ();

			mre.Wait ();

			return ipEndPoint;
		}

		public static IPEndPoint ListenerCallback (IAsyncResult result)
		{
			var listener = (HttpListener) result.AsyncState;
			var context = listener.EndGetContext (result);
			var clientEndPoint = context.Request.RemoteEndPoint;

			// Disposing InputStream should not avoid socket reuse
			context.Request.InputStream.Dispose ();

			// Close OutputStream to send response
			context.Response.OutputStream.Close ();

			return clientEndPoint;
		}
		
		[Test]
		public void HttpClientIsDisconnectedCheckForWriteException()
		{
			string uri = "http://localhost:" + NetworkHelpers.FindFreePort () + "/";

			AutoResetEvent exceptionOccuredEvent = new AutoResetEvent (false);
			HttpListener listener = new HttpListener {
				IgnoreWriteExceptions = false
			};
			listener.Prefixes.Add (uri);
			listener.Start ();
			listener.BeginGetContext (result =>
			{
				HttpListenerContext context = listener.EndGetContext (result);
				context.Response.SendChunked = true;
				context.Request.InputStream.Close ();
				
				var bytes = new byte [1024];
				using(Stream outputStream = context.Response.OutputStream) {
					try {
						while (true) 
							outputStream.Write (bytes, 0, bytes.Length);
					} catch {
						exceptionOccuredEvent.Set ();
					}
				}
			}, null);

			Task.Factory.StartNew (() =>
			{
				var webRequest = (HttpWebRequest)WebRequest.Create (uri);
				webRequest.Method = "POST";
				webRequest.KeepAlive = false;
				Stream requestStream = webRequest.GetRequestStream ();
				requestStream.WriteByte (1);
				requestStream.Close ();
				using (WebResponse response = webRequest.GetResponse ())
				using (Stream stream = response.GetResponseStream ()) {
					byte[] clientBytes = new byte [1024];
					Assert.IsNotNull (stream, "#01");
					stream.Read (clientBytes, 0, clientBytes.Length);
				}
			});

			Assert.IsTrue (exceptionOccuredEvent.WaitOne (15 * 1000), "#02");
		}
	}
}