Summary

Class:WireMock.Owin.OwinSelfHost
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\OwinSelfHost.cs
Covered lines:51
Uncovered lines:0
Coverable lines:51
Total lines:96
Line coverage:100%
Branch coverage:66.6%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)20100100
StartAsync()10100100
StopAsync()10100100
StartServers()42100100

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\OwinSelfHost.cs

#LineLine coverage
 1#if !NETSTANDARD
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using JetBrains.Annotations;
 7using WireMock.Validation;
 8using Owin;
 9using Microsoft.Owin.Hosting;
 10using WireMock.Http;
 11
 12namespace WireMock.Owin
 13{
 14    internal class OwinSelfHost : IOwinSelfHost
 15    {
 16        private readonly WireMockMiddlewareOptions _options;
 4717        private readonly CancellationTokenSource _cts = new CancellationTokenSource();
 18
 4719        public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes)
 4720        {
 4721            Check.NotNull(options, nameof(options));
 4722            Check.NotNullOrEmpty(uriPrefixes, nameof(uriPrefixes));
 23
 23524            foreach (string uriPrefix in uriPrefixes)
 4725            {
 4726                Urls.Add(uriPrefix);
 27
 4728                PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
 4729                Ports.Add(port);
 4730            }
 31
 4732            _options = options;
 4733        }
 34
 10835        public bool IsStarted { get; private set; }
 36
 14137        public List<string> Urls { get; } = new List<string>();
 38
 14139        public List<int> Ports { get; } = new List<int>();
 40
 41        [PublicAPI]
 42        public Task StartAsync()
 4743        {
 4744            return Task.Run(() =>
 9445            {
 9446                StartServers();
 5747            }, _cts.Token);
 4748        }
 49
 50        [PublicAPI]
 51        public Task StopAsync()
 4752        {
 4753            _cts.Cancel();
 54
 4755            return Task.FromResult(true);
 4756        }
 57
 58        private void StartServers()
 4759        {
 60#if NET46
 61            Console.WriteLine("WireMock.Net server using .net 4.6.x or higher");
 62#else
 4763            Console.WriteLine("WireMock.Net server using .net 4.5.x or higher");
 64#endif
 65
 4766            Action<IAppBuilder> startup = app =>
 9467            {
 9468                app.Use<GlobalExceptionMiddleware>(_options);
 9469                 _options.PreWireMockMiddlewareInit?.Invoke(app);
 9470                app.Use<WireMockMiddleware>(_options);
 9471                 _options.PostWireMockMiddlewareInit?.Invoke(app);
 9472            };
 73
 4774            var servers = new List<IDisposable>();
 23475            foreach (var url in Urls)
 4776            {
 4777                servers.Add(WebApp.Start(url, startup));
 4678            }
 79
 4680            IsStarted = true;
 81
 5682             while (!_cts.IsCancellationRequested)
 4683            {
 4684                Thread.Sleep(30000);
 1085            }
 86
 1087            IsStarted = false;
 88
 5089            foreach (var server in servers)
 1090            {
 1091                server.Dispose();
 1092            }
 1093        }
 94    }
 95}
 96#endif