| | | 1 | | #if !NETSTANDARD |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using WireMock.Validation; |
| | | 8 | | using Owin; |
| | | 9 | | using Microsoft.Owin.Hosting; |
| | | 10 | | using WireMock.Http; |
| | | 11 | | |
| | | 12 | | namespace WireMock.Owin |
| | | 13 | | { |
| | | 14 | | internal class OwinSelfHost : IOwinSelfHost |
| | | 15 | | { |
| | | 16 | | private readonly WireMockMiddlewareOptions _options; |
| | 47 | 17 | | private readonly CancellationTokenSource _cts = new CancellationTokenSource(); |
| | | 18 | | |
| | 47 | 19 | | public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes) |
| | 47 | 20 | | { |
| | 47 | 21 | | Check.NotNull(options, nameof(options)); |
| | 47 | 22 | | Check.NotNullOrEmpty(uriPrefixes, nameof(uriPrefixes)); |
| | | 23 | | |
| | 235 | 24 | | foreach (string uriPrefix in uriPrefixes) |
| | 47 | 25 | | { |
| | 47 | 26 | | Urls.Add(uriPrefix); |
| | | 27 | | |
| | 47 | 28 | | PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port); |
| | 47 | 29 | | Ports.Add(port); |
| | 47 | 30 | | } |
| | | 31 | | |
| | 47 | 32 | | _options = options; |
| | 47 | 33 | | } |
| | | 34 | | |
| | 108 | 35 | | public bool IsStarted { get; private set; } |
| | | 36 | | |
| | 141 | 37 | | public List<string> Urls { get; } = new List<string>(); |
| | | 38 | | |
| | 141 | 39 | | public List<int> Ports { get; } = new List<int>(); |
| | | 40 | | |
| | | 41 | | [PublicAPI] |
| | | 42 | | public Task StartAsync() |
| | 47 | 43 | | { |
| | 47 | 44 | | return Task.Run(() => |
| | 94 | 45 | | { |
| | 94 | 46 | | StartServers(); |
| | 57 | 47 | | }, _cts.Token); |
| | 47 | 48 | | } |
| | | 49 | | |
| | | 50 | | [PublicAPI] |
| | | 51 | | public Task StopAsync() |
| | 47 | 52 | | { |
| | 47 | 53 | | _cts.Cancel(); |
| | | 54 | | |
| | 47 | 55 | | return Task.FromResult(true); |
| | 47 | 56 | | } |
| | | 57 | | |
| | | 58 | | private void StartServers() |
| | 47 | 59 | | { |
| | | 60 | | #if NET46 |
| | | 61 | | Console.WriteLine("WireMock.Net server using .net 4.6.x or higher"); |
| | | 62 | | #else |
| | 47 | 63 | | Console.WriteLine("WireMock.Net server using .net 4.5.x or higher"); |
| | | 64 | | #endif |
| | | 65 | | |
| | 47 | 66 | | Action<IAppBuilder> startup = app => |
| | 94 | 67 | | { |
| | 94 | 68 | | app.Use<GlobalExceptionMiddleware>(_options); |
| | 94 | 69 | | _options.PreWireMockMiddlewareInit?.Invoke(app); |
| | 94 | 70 | | app.Use<WireMockMiddleware>(_options); |
| | 94 | 71 | | _options.PostWireMockMiddlewareInit?.Invoke(app); |
| | 94 | 72 | | }; |
| | | 73 | | |
| | 47 | 74 | | var servers = new List<IDisposable>(); |
| | 234 | 75 | | foreach (var url in Urls) |
| | 47 | 76 | | { |
| | 47 | 77 | | servers.Add(WebApp.Start(url, startup)); |
| | 46 | 78 | | } |
| | | 79 | | |
| | 46 | 80 | | IsStarted = true; |
| | | 81 | | |
| | 56 | 82 | | while (!_cts.IsCancellationRequested) |
| | 46 | 83 | | { |
| | 46 | 84 | | Thread.Sleep(30000); |
| | 10 | 85 | | } |
| | | 86 | | |
| | 10 | 87 | | IsStarted = false; |
| | | 88 | | |
| | 50 | 89 | | foreach (var server in servers) |
| | 10 | 90 | | { |
| | 10 | 91 | | server.Dispose(); |
| | 10 | 92 | | } |
| | 10 | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | #endif |