Summary

Class:WireMock.Http.TinyHttpServer
Assembly:WireMock.Net
File(s):C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Http\TinyHttpServer.cs
Covered lines:42
Uncovered lines:0
Coverable lines:42
Total lines:113
Line coverage:100%
Branch coverage:50%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)20100100
Start()10100100
Stop()2210066.67
<Start()4277.7866.67

File(s)

C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Http\TinyHttpServer.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Net;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using JetBrains.Annotations;
 7using WireMock.Validation;
 8
 9namespace WireMock.Http
 10{
 11    /// <summary>
 12    /// The tiny http server.
 13    /// </summary>
 14    public class TinyHttpServer
 15    {
 16        private readonly Action<HttpListenerContext, CancellationToken> _httpHandler;
 17
 18        private readonly HttpListener _listener;
 19
 20        private readonly CancellationTokenSource _cts;
 21
 22        /// <summary>
 23        /// Gets a value indicating whether this server is started.
 24        /// </summary>
 25        /// <value>
 26        /// <c>true</c> if this server is started; otherwise, <c>false</c>.
 27        /// </value>
 2828        public bool IsStarted { get; private set; }
 29
 30        /// <summary>
 31        /// Gets the url.
 32        /// </summary>
 33        /// <value>
 34        /// The urls.
 35        /// </value>
 36        [PublicAPI]
 5637        public List<Uri> Urls { get; } = new List<Uri>();
 38
 39        /// <summary>
 40        /// Gets the ports.
 41        /// </summary>
 42        /// <value>
 43        /// The ports.
 44        /// </value>
 45        [PublicAPI]
 7446        public List<int> Ports { get; } = new List<int>();
 47
 48        /// <summary>
 49        /// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
 50        /// </summary>
 51        /// <param name="uriPrefixes">The uriPrefixes.</param>
 52        /// <param name="httpHandler">The http handler.</param>
 2853        public TinyHttpServer([NotNull] Action<HttpListenerContext, CancellationToken> httpHandler, [NotNull] params str
 2854        {
 2855            Check.NotNull(httpHandler, nameof(httpHandler));
 2856            Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
 57
 2858            _cts = new CancellationTokenSource();
 59
 2860            _httpHandler = httpHandler;
 61
 62            // Create a listener.
 2863            _listener = new HttpListener();
 14064            foreach (string uriPrefix in uriPrefixes)
 2865            {
 2866                var uri = new Uri(uriPrefix);
 2867                Urls.Add(uri);
 2868                Ports.Add(uri.Port);
 69
 2870                _listener.Prefixes.Add(uriPrefix);
 2871            }
 2872        }
 73
 74        /// <summary>
 75        /// Start the server.
 76        /// </summary>
 77        [PublicAPI]
 78        public void Start()
 2879        {
 2880            _listener.Start();
 81
 2882            IsStarted = true;
 83
 2884            Task.Run(
 2885                async () =>
 5386                    {
 2887                        //using (_listener)
 5388                        {
 7689                             while (!_cts.Token.IsCancellationRequested)
 7690                            {
 7691                                HttpListenerContext context = await _listener.GetContextAsync();
 5192                                _httpHandler(context, _cts.Token);
 5193                            }
 2894
 2895                            _listener.Stop();
 2896                            IsStarted = false;
 2897                        }
 2898                    },
 2899                _cts.Token);
 28100        }
 101
 102        /// <summary>
 103        /// Stop the server.
 104        /// </summary>
 105        [PublicAPI]
 106        public void Stop()
 27107        {
 27108             _listener?.Stop();
 109
 27110            _cts.Cancel();
 27111        }
 112    }
 113}