Summary

Class:WireMock.Owin.AspNetCoreSelfHost
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\AspNetCoreSelfHost.cs
Covered lines:81
Uncovered lines:5
Coverable lines:86
Total lines:148
Line coverage:94.1%
Branch coverage:75%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
StartAsync()0010
StartServers()000.6880
StopAsync()0010
.ctor(...)0010.75

File(s)

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

#LineLine coverage
 1#if USE_ASPNETCORE
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using JetBrains.Annotations;
 8using Microsoft.AspNetCore.Builder;
 9using Microsoft.AspNetCore.Hosting;
 10using WireMock.HttpsCertificate;
 11using WireMock.Logging;
 12using WireMock.Util;
 13using WireMock.Validation;
 14
 15namespace WireMock.Owin
 16{
 17    internal class AspNetCoreSelfHost : IOwinSelfHost
 18    {
 5519        private readonly CancellationTokenSource _cts = new CancellationTokenSource();
 20        private readonly WireMockMiddlewareOptions _options;
 21        private readonly string[] _urls;
 22        private readonly IWireMockLogger _logger;
 23        private Exception _runningException;
 24
 25        private IWebHost _host;
 26
 51027        public bool IsStarted { get; private set; }
 28
 11029        public List<string> Urls { get; } = new List<string>();
 30
 16531        public List<int> Ports { get; } = new List<int>();
 32
 39633        public Exception RunningException => _runningException;
 34
 5535        public AspNetCoreSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes)
 5536        {
 5537            Check.NotNull(options, nameof(options));
 5538            Check.NotNullOrEmpty(uriPrefixes, nameof(uriPrefixes));
 39
 5540            _logger = options.Logger ?? new WireMockConsoleLogger();
 41
 27542            foreach (string uriPrefix in uriPrefixes)
 5543            {
 5544                Urls.Add(uriPrefix);
 45
 5546                PortUtils.TryExtract(uriPrefix, out string protocol, out string host, out int port);
 5547                Ports.Add(port);
 5548            }
 49
 5550            _options = options;
 5551            _urls = uriPrefixes;
 5552        }
 53
 54        public Task StartAsync()
 5555        {
 5556            _host = new WebHostBuilder()
 5557                .Configure(appBuilder =>
 11058                {
 11059                    appBuilder.UseMiddleware<GlobalExceptionMiddleware>(_options);
 5560
 11061                    _options.PreWireMockMiddlewareInit?.Invoke(appBuilder);
 5562
 11063                    appBuilder.UseMiddleware<WireMockMiddleware>(_options);
 5564
 11065                    _options.PostWireMockMiddlewareInit?.Invoke(appBuilder);
 11066                })
 5567                .UseKestrel(options =>
 11068                {
 5569#if NETSTANDARD1_3
 5570                    if (_urls.Any(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
 5571                    {
 5572                        options.UseHttps(PublicCertificateHelper.GetX509Certificate2());
 5573                    }
 5574#else
 5575                    // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x
 38576                    foreach (string url in _urls.Where(u => u.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
 11077                    {
 11078                        PortUtils.TryExtract(url, out string protocol, out string host, out int port);
 11079                        options.Listen(System.Net.IPAddress.Any, port);
 11080                    }
 5581
 27582                    foreach (string url in _urls.Where(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase)
 5583                    {
 5584                        PortUtils.TryExtract(url, out string protocol, out string host, out int port);
 5585                        options.Listen(System.Net.IPAddress.Any, port, listenOptions =>
 5586                        {
 5587                            listenOptions.UseHttps(PublicCertificateHelper.GetX509Certificate2());
 5588                        });
 5589                    }
 5590#endif
 11091                })
 5592#if NETSTANDARD1_3
 5593                .UseUrls(_urls)
 5594#endif
 5595                .Build();
 96
 5597            return Task.Run(() =>
 11098            {
 11099                StartServers();
 57100            }, _cts.Token);
 55101        }
 102
 103        private void StartServers()
 55104        {
 105            try
 55106            {
 55107                var appLifetime = (IApplicationLifetime) _host.Services.GetService(typeof(IApplicationLifetime));
 110108                appLifetime.ApplicationStarted.Register(() => IsStarted = true);
 109
 110#if NETSTANDARD1_3
 111                _logger.Info("WireMock.Net server using netstandard1.3");
 112#elif NETSTANDARD2_0
 55113                _logger.Info("WireMock.Net server using netstandard2.0");
 114#elif NET46
 115                _logger.Info("WireMock.Net server using .net 4.6.1 or higher");
 116#endif
 117
 118#if NETSTANDARD1_3
 119                _host.Run(_cts.Token);
 120#else
 55121                _host.RunAsync(_cts.Token).Wait();
 122#endif
 2123            }
 0124            catch (Exception e)
 0125            {
 0126                _runningException = e;
 0127                _logger.Error(e.ToString());
 0128            }
 129            finally
 2130            {
 2131                IsStarted = false;
 2132            }
 2133        }
 134
 135        public Task StopAsync()
 2136        {
 2137            _cts.Cancel();
 138
 2139            IsStarted = false;
 140#if NETSTANDARD1_3
 141            return Task.FromResult(true);
 142#else
 2143            return _host.StopAsync();
 144#endif
 2145        }
 146    }
 147}
 148#endif