|
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.Threading; |
| 6 | + |
7 | 7 | public class SimpleServer : IDisposable |
8 | 8 | { |
9 | | - private readonly HttpListener _listener; |
10 | | - private readonly Action<HttpListenerContext> _handler; |
11 | | - private Thread _processor; |
| 9 | + private readonly HttpListener listener; |
| 10 | + private readonly Action<HttpListenerContext> handler; |
| 11 | + private Thread processor; |
12 | 12 |
|
13 | | - public static SimpleServer Create(string url, Action<HttpListenerContext> handler, |
| 13 | + public static SimpleServer Create( |
| 14 | + string url, |
| 15 | + Action<HttpListenerContext> handler, |
14 | 16 | AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous) |
15 | 17 | { |
16 | | - var listener = new HttpListener |
17 | | - { |
18 | | - Prefixes = {url}, |
19 | | - AuthenticationSchemes = authenticationSchemes |
20 | | - }; |
| 18 | + var listener = new HttpListener { Prefixes = { url }, AuthenticationSchemes = authenticationSchemes }; |
21 | 19 | var server = new SimpleServer(listener, handler); |
22 | 20 |
|
23 | 21 | server.Start(); |
24 | | - |
25 | 22 | return server; |
26 | 23 | } |
27 | 24 |
|
28 | 25 | private SimpleServer(HttpListener listener, Action<HttpListenerContext> handler) |
29 | 26 | { |
30 | | - _listener = listener; |
31 | | - _handler = handler; |
| 27 | + this.listener = listener; |
| 28 | + this.handler = handler; |
32 | 29 | } |
33 | 30 |
|
34 | 31 | public void Start() |
35 | 32 | { |
36 | | - if (!_listener.IsListening) |
| 33 | + if (this.listener.IsListening) |
37 | 34 | { |
38 | | - _listener.Start(); |
| 35 | + return; |
| 36 | + } |
39 | 37 |
|
40 | | - _processor = new Thread(() => |
41 | | - { |
42 | | - var context = _listener.GetContext(); |
43 | | - _handler(context); |
44 | | - context.Response.Close(); |
45 | | - }) {Name = "WebServer"}; |
| 38 | + this.listener.Start(); |
46 | 39 |
|
47 | | - _processor.Start(); |
48 | | - } |
| 40 | + this.processor = new Thread(() => |
| 41 | + { |
| 42 | + var context = this.listener.GetContext(); |
| 43 | + this.handler(context); |
| 44 | + context.Response.Close(); |
| 45 | + }) { Name = "WebServer" }; |
| 46 | + |
| 47 | + this.processor.Start(); |
49 | 48 | } |
50 | 49 |
|
51 | 50 | public void Dispose() |
52 | 51 | { |
53 | | - _processor.Abort(); |
54 | | - _listener.Stop(); |
55 | | - _listener.Close(); |
| 52 | + this.processor.Abort(); |
| 53 | + this.listener.Stop(); |
| 54 | + this.listener.Close(); |
56 | 55 | } |
57 | 56 | } |
58 | 57 | } |
0 commit comments