|
1 |
| -using System; |
2 |
| -using System.Net; |
3 |
| -using System.Threading; |
4 |
| - |
5 |
| -namespace RestSharp.IntegrationTests.Helpers |
| 1 | +namespace RestSharp.IntegrationTests.Helpers |
6 | 2 | {
|
| 3 | + using System; |
| 4 | + using System.Net; |
| 5 | + using System.Security; |
| 6 | + using System.Threading; |
| 7 | + |
7 | 8 | public class SimpleServer : IDisposable
|
8 | 9 | {
|
9 |
| - private readonly HttpListener _listener; |
10 |
| - private readonly Action<HttpListenerContext> _handler; |
11 |
| - private Thread _processor; |
| 10 | + private readonly HttpListener listener; |
| 11 | + private readonly Action<HttpListenerContext> handler; |
| 12 | + private Thread thread; |
| 13 | + |
| 14 | + private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler) |
| 15 | + { |
| 16 | + this.listener = listener; |
| 17 | + this.handler = handler; |
| 18 | + } |
12 | 19 |
|
13 |
| - public static SimpleServer Create(string url, Action<HttpListenerContext> handler, |
| 20 | + public static SimpleServer Create( |
| 21 | + string url, |
| 22 | + Action<HttpListenerContext> handler, |
14 | 23 | AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous)
|
15 | 24 | {
|
16 |
| - var listener = new HttpListener |
17 |
| - { |
18 |
| - Prefixes = {url}, |
19 |
| - AuthenticationSchemes = authenticationSchemes |
20 |
| - }; |
| 25 | + var listener = new HttpListener { Prefixes = { url }, AuthenticationSchemes = authenticationSchemes }; |
21 | 26 | var server = new SimpleServer(listener, handler);
|
22 | 27 |
|
23 | 28 | server.Start();
|
24 |
| - |
25 | 29 | return server;
|
26 | 30 | }
|
27 | 31 |
|
28 |
| - private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler) |
29 |
| - { |
30 |
| - _listener = listener; |
31 |
| - _handler = handler; |
32 |
| - } |
33 |
| - |
34 | 32 | public void Start()
|
35 | 33 | {
|
36 |
| - if (!_listener.IsListening) |
| 34 | + if (this.listener.IsListening) |
37 | 35 | {
|
38 |
| - _listener.Start(); |
| 36 | + return; |
| 37 | + } |
39 | 38 |
|
40 |
| - _processor = new Thread(() => |
41 |
| - { |
42 |
| - var context = _listener.GetContext(); |
43 |
| - _handler(context); |
44 |
| - context.Response.Close(); |
45 |
| - }) {Name = "WebServer"}; |
| 39 | + this.listener.Start(); |
46 | 40 |
|
47 |
| - _processor.Start(); |
48 |
| - } |
| 41 | + this.thread = new Thread(() => |
| 42 | + { |
| 43 | + var context = this.listener.GetContext(); |
| 44 | + this.handler(context); |
| 45 | + context.Response.Close(); |
| 46 | + }) { Name = "WebServer" }; |
| 47 | + |
| 48 | + this.thread.Start(); |
49 | 49 | }
|
50 | 50 |
|
51 | 51 | public void Dispose()
|
52 | 52 | {
|
53 |
| - _processor.Abort(); |
54 |
| - _listener.Stop(); |
55 |
| - _listener.Close(); |
| 53 | + try |
| 54 | + { |
| 55 | + this.thread.Abort(); |
| 56 | + } |
| 57 | + catch (ThreadStateException threadStateException) |
| 58 | + { |
| 59 | + Console.WriteLine("Issue aborting thread - {0}.", threadStateException.Message); |
| 60 | + } |
| 61 | + catch (SecurityException securityException) |
| 62 | + { |
| 63 | + Console.WriteLine("Issue aborting thread - {0}.", securityException.Message); |
| 64 | + } |
| 65 | + |
| 66 | + if (this.listener.IsListening) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + this.listener.Stop(); |
| 71 | + } |
| 72 | + catch (ObjectDisposedException objectDisposedException) |
| 73 | + { |
| 74 | + Console.WriteLine("Issue stopping listener - {0}", objectDisposedException.Message); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + this.listener.Close(); |
56 | 79 | }
|
57 | 80 | }
|
58 | 81 | }
|
0 commit comments