diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index 2c33ac692..41e1226b8 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27004.2010 +VisualStudioVersion = 15.0.27130.2020 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EF242EDF-7133-4277-9A0C-18744DE08707}" EndProject @@ -14,9 +14,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{F0C22C47-DF71-463C-9B04-B4E0F3B8708A}" - ProjectSection(SolutionItems) = preProject - examples\WireMock.Net.Console.Record.NETCoreApp\__admin\mappings\ab38efae-4e4d-4f20-8afe-635533ec2535.json = examples\WireMock.Net.Console.Record.NETCoreApp\__admin\mappings\ab38efae-4e4d-4f20-8afe-635533ec2535.json - EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{890A1DED-C229-4FA1-969E-AAC3BBFC05E5}" EndProject @@ -41,6 +38,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.StandAlone.Net EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Console.NET452", "examples\WireMock.Net.ConsoleApplication\WireMock.Net.Console.NET452.csproj", "{668F689E-57B4-422E-8846-C0FF643CA268}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.WebApplication", "examples\WireMock.Net.WebApplication\WireMock.Net.WebApplication.csproj", "{049539C1-7A66-4559-AD7A-B1C73B97CBB0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -83,6 +82,10 @@ Global {668F689E-57B4-422E-8846-C0FF643CA268}.Debug|Any CPU.Build.0 = Debug|Any CPU {668F689E-57B4-422E-8846-C0FF643CA268}.Release|Any CPU.ActiveCfg = Release|Any CPU {668F689E-57B4-422E-8846-C0FF643CA268}.Release|Any CPU.Build.0 = Release|Any CPU + {049539C1-7A66-4559-AD7A-B1C73B97CBB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {049539C1-7A66-4559-AD7A-B1C73B97CBB0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {049539C1-7A66-4559-AD7A-B1C73B97CBB0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {049539C1-7A66-4559-AD7A-B1C73B97CBB0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -97,6 +100,7 @@ Global {10E16614-61CA-48D8-8BDD-664C13913DED} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A} {668F689E-57B4-422E-8846-C0FF643CA999} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A} {668F689E-57B4-422E-8846-C0FF643CA268} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A} + {049539C1-7A66-4559-AD7A-B1C73B97CBB0} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BF428BCC-C837-433B-87D2-15C7014B73E9} diff --git a/examples/WireMock.Net.Client/WireMock.Net.Client.csproj b/examples/WireMock.Net.Client/WireMock.Net.Client.csproj index a46e519ca..c35fb9b34 100644 --- a/examples/WireMock.Net.Client/WireMock.Net.Client.csproj +++ b/examples/WireMock.Net.Client/WireMock.Net.Client.csproj @@ -9,7 +9,7 @@ - + diff --git a/examples/WireMock.Net.WebApplication/App.cs b/examples/WireMock.Net.WebApplication/App.cs new file mode 100644 index 000000000..fc63803fd --- /dev/null +++ b/examples/WireMock.Net.WebApplication/App.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Logging; + +namespace WireMock.Net.WebApplication +{ + public class App + { + private readonly IWireMockService _service; + private readonly ILogger _logger; + + public App(IWireMockService service, ILogger logger) + { + _service = service; + _logger = logger; + } + + public void Run() + { + _logger.LogInformation("WireMock.Net App running"); + _service.Run(); + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.WebApplication/IWireMockService.cs b/examples/WireMock.Net.WebApplication/IWireMockService.cs new file mode 100644 index 000000000..f99c23e47 --- /dev/null +++ b/examples/WireMock.Net.WebApplication/IWireMockService.cs @@ -0,0 +1,7 @@ +namespace WireMock.Net.WebApplication +{ + public interface IWireMockService + { + void Run(); + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.WebApplication/Program.cs b/examples/WireMock.Net.WebApplication/Program.cs new file mode 100644 index 000000000..582494f71 --- /dev/null +++ b/examples/WireMock.Net.WebApplication/Program.cs @@ -0,0 +1,57 @@ +using System; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using WireMock.Settings; + +namespace WireMock.Net.WebApplication +{ + public class Program + { + public static void Main(string[] args) + { + // Create service collection + var serviceCollection = new ServiceCollection(); + ConfigureServices(serviceCollection); + + // Create service provider + var serviceProvider = serviceCollection.BuildServiceProvider(); + + // Run app + serviceProvider.GetService().Run(); + } + + private static void ConfigureServices(IServiceCollection serviceCollection) + { + // Build configuration + var configuration = new ConfigurationBuilder() + .SetBasePath(AppContext.BaseDirectory) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddEnvironmentVariables() // <-- this is needed to to override settings via the Azure Portal App Settings + .Build(); + + // Add LoggerFactory + var factory = new LoggerFactory(); + serviceCollection.AddSingleton(factory + .AddConsole(configuration.GetSection("Logging")) + .AddDebug() + .AddAzureWebAppDiagnostics() + ); + + serviceCollection.AddSingleton(factory.CreateLogger("WireMock.Net Logger")); + + // Add access to generic IConfigurationRoot + serviceCollection.AddSingleton(configuration); + + // Add access to IFluentMockServerSettings + var settings = configuration.GetSection("FluentMockServerSettings").Get(); + serviceCollection.AddSingleton(settings); + + // Add services + serviceCollection.AddTransient(); + + // Add app + serviceCollection.AddTransient(); + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.WebApplication/Properties/launchSettings.json b/examples/WireMock.Net.WebApplication/Properties/launchSettings.json new file mode 100644 index 000000000..b056cf7a4 --- /dev/null +++ b/examples/WireMock.Net.WebApplication/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:56513/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchUrl": "__admin/settings", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "WireMock.Net.WebApplication": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "__admin/settings", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:56514/" + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.WebApplication/WireMock.Net.WebApplication.csproj b/examples/WireMock.Net.WebApplication/WireMock.Net.WebApplication.csproj new file mode 100644 index 000000000..2df47b457 --- /dev/null +++ b/examples/WireMock.Net.WebApplication/WireMock.Net.WebApplication.csproj @@ -0,0 +1,29 @@ + + + + netcoreapp2.0 + win10-x64 + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + + \ No newline at end of file diff --git a/examples/WireMock.Net.WebApplication/WireMockService.cs b/examples/WireMock.Net.WebApplication/WireMockService.cs new file mode 100644 index 000000000..389f0c7d3 --- /dev/null +++ b/examples/WireMock.Net.WebApplication/WireMockService.cs @@ -0,0 +1,37 @@ +using System.Threading; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using WireMock.Net.StandAlone; +using WireMock.Settings; + +namespace WireMock.Net.WebApplication +{ + public class WireMockService : IWireMockService + { + private static int sleepTime = 30000; + + private readonly ILogger _logger; + private readonly IFluentMockServerSettings _settings; + + public WireMockService(ILogger logger, IFluentMockServerSettings settings) + { + _logger = logger; + _settings = settings; + } + + public void Run() + { + _logger.LogInformation("WireMock.Net server starting"); + + StandAloneApp.Start(_settings); + + _logger.LogInformation($"WireMock.Net server settings {JsonConvert.SerializeObject(_settings)}"); + + while (true) + { + _logger.LogInformation("WireMock.Net server running"); + Thread.Sleep(sleepTime); + } + } + } +} \ No newline at end of file diff --git a/examples/WireMock.Net.WebApplication/appsettings.json b/examples/WireMock.Net.WebApplication/appsettings.json new file mode 100644 index 000000000..d7fee4133 --- /dev/null +++ b/examples/WireMock.Net.WebApplication/appsettings.json @@ -0,0 +1,20 @@ +{ + "Logging": { + "IncludeScopes": false, + "Debug": { + "LogLevel": { + "Default": "Debug" + } + }, + "Console": { + "LogLevel": { + "Default": "Debug" + } + } + }, + "FluentMockServerSettings": { + "AdminUsername": "a", + "AdminPassword": "a", + "StartAdminInterface": true + } +} \ No newline at end of file diff --git a/src/WireMock.Net.StandAlone/StandAloneApp.cs b/src/WireMock.Net.StandAlone/StandAloneApp.cs index d7f0238c5..2ce4b8e17 100644 --- a/src/WireMock.Net.StandAlone/StandAloneApp.cs +++ b/src/WireMock.Net.StandAlone/StandAloneApp.cs @@ -18,7 +18,7 @@ public static class StandAloneApp /// /// The FluentMockServerSettings [PublicAPI] - public static FluentMockServer Start([NotNull] FluentMockServerSettings settings) + public static FluentMockServer Start([NotNull] IFluentMockServerSettings settings) { Check.NotNull(settings, nameof(settings)); diff --git a/src/WireMock.Net/DynamicResponseProvider.cs b/src/WireMock.Net/DynamicResponseProvider.cs index acf4848ee..1b227bb35 100644 --- a/src/WireMock.Net/DynamicResponseProvider.cs +++ b/src/WireMock.Net/DynamicResponseProvider.cs @@ -42,10 +42,10 @@ public Task ProvideResponseAsync(RequestMessage requestMessage) internal class ProxyAsyncResponseProvider : IResponseProvider { - private readonly Func> _responseMessageFunc; - private readonly ProxyAndRecordSettings _settings; + private readonly Func> _responseMessageFunc; + private readonly IProxyAndRecordSettings _settings; - public ProxyAsyncResponseProvider([NotNull] Func> responseMessageFunc, [NotNull] ProxyAndRecordSettings settings) + public ProxyAsyncResponseProvider([NotNull] Func> responseMessageFunc, [NotNull] IProxyAndRecordSettings settings) { Check.NotNull(responseMessageFunc, nameof(responseMessageFunc)); Check.NotNull(settings, nameof(settings)); diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 9acef03bb..21072672e 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -132,13 +132,13 @@ private void InitAdmin() #region Proxy and Record private HttpClient _httpClientForProxy; - private void InitProxyAndRecord(ProxyAndRecordSettings settings) + private void InitProxyAndRecord(IProxyAndRecordSettings settings) { _httpClientForProxy = HttpClientHelper.CreateHttpClient(settings.X509Certificate2ThumbprintOrSubjectName); Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); } - private async Task ProxyAndRecordAsync(RequestMessage requestMessage, ProxyAndRecordSettings settings) + private async Task ProxyAndRecordAsync(RequestMessage requestMessage, IProxyAndRecordSettings settings) { var requestUri = new Uri(requestMessage.Url); var proxyUri = new Uri(settings.Url); diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 7eaa7c3a2..156d94cb0 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -25,12 +25,15 @@ public partial class FluentMockServer : IDisposable private readonly IOwinSelfHost _httpServer; private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions(); + /// + /// Gets a value indicating whether this server is started. + /// + [PublicAPI] + public bool IsStarted { get; } + /// /// Gets the ports. /// - /// - /// The ports. - /// [PublicAPI] public List Ports { get; } @@ -59,7 +62,7 @@ public partial class FluentMockServer : IDisposable /// The FluentMockServerSettings. /// The . [PublicAPI] - public static FluentMockServer Start(FluentMockServerSettings settings) + public static FluentMockServer Start(IFluentMockServerSettings settings) { Check.NotNull(settings, nameof(settings)); @@ -150,7 +153,7 @@ public static FluentMockServer StartWithAdminInterfaceAndReadStaticMappings(para }); } - private FluentMockServer(FluentMockServerSettings settings) + private FluentMockServer(IFluentMockServerSettings settings) { if (settings.Urls != null) { @@ -170,6 +173,8 @@ private FluentMockServer(FluentMockServerSettings settings) #else _httpServer = new OwinSelfHost(_options, Urls); #endif + IsStarted = _httpServer.IsStarted; + Ports = _httpServer.Ports; _httpServer.StartAsync(); diff --git a/src/WireMock.Net/Settings/FluentMockServerSettings.cs b/src/WireMock.Net/Settings/FluentMockServerSettings.cs index 67aae908e..d526eafb2 100644 --- a/src/WireMock.Net/Settings/FluentMockServerSettings.cs +++ b/src/WireMock.Net/Settings/FluentMockServerSettings.cs @@ -6,91 +6,62 @@ namespace WireMock.Settings /// /// FluentMockServerSettings /// - public class FluentMockServerSettings + public class FluentMockServerSettings : IFluentMockServerSettings { - /// - /// Gets or sets the port. - /// + /// [PublicAPI] public int? Port { get; set; } - /// - /// Gets or sets the use SSL. - /// - // ReSharper disable once InconsistentNaming + /// [PublicAPI] + // ReSharper disable once InconsistentNaming public bool? UseSSL { get; set; } - /// - /// Gets or sets wether to start admin interface. - /// + /// [PublicAPI] public bool? StartAdminInterface { get; set; } - /// - /// Gets or sets if the static mappings should be read at startup. - /// + /// [PublicAPI] public bool? ReadStaticMappings { get; set; } - /// - /// Gets or sets if the server should record and save requests and responses. - /// - /// true/false + /// [PublicAPI] - public ProxyAndRecordSettings ProxyAndRecordSettings { get; set; } + public IProxyAndRecordSettings ProxyAndRecordSettings { get; set; } - /// - /// Gets or sets the urls. - /// + /// [PublicAPI] public string[] Urls { get; set; } - /// - /// StartTimeout - /// + /// [PublicAPI] public int StartTimeout { get; set; } = 10000; - /// - /// Allow Partial Mapping (default set to false). - /// + /// [PublicAPI] public bool? AllowPartialMapping { get; set; } - /// - /// The username needed for __admin access. - /// + /// [PublicAPI] public string AdminUsername { get; set; } - /// - /// The password needed for __admin access. - /// + /// [PublicAPI] public string AdminPassword { get; set; } - /// - /// The RequestLog expiration in hours (optional). - /// + /// [PublicAPI] public int? RequestLogExpirationDuration { get; set; } - /// - /// The MaxRequestLog count (optional). - /// + /// [PublicAPI] public int? MaxRequestLogCount { get; set; } - /// - /// Action which is called (with the IAppBuilder or IApplicationBuilder) before the internal WireMockMiddleware is initialized. [Optional] - /// + /// [PublicAPI] public Action PreWireMockMiddlewareInit { get; set; } - /// - /// Action which is called (with the IAppBuilder or IApplicationBuilder) after the internal WireMockMiddleware is initialized. [Optional] - /// + /// [PublicAPI] public Action PostWireMockMiddlewareInit { get; set; } } diff --git a/src/WireMock.Net/Settings/IFluentMockServerSettings.cs b/src/WireMock.Net/Settings/IFluentMockServerSettings.cs new file mode 100644 index 000000000..92cebb7a2 --- /dev/null +++ b/src/WireMock.Net/Settings/IFluentMockServerSettings.cs @@ -0,0 +1,81 @@ +using System; + +namespace WireMock.Settings +{ + /// + /// IFluentMockServerSettings + /// + public interface IFluentMockServerSettings + { + /// + /// Gets or sets the port. + /// + int? Port { get; set; } + + /// + /// Gets or sets the use SSL. + /// + // ReSharper disable once InconsistentNaming + bool? UseSSL { get; set; } + + /// + /// Gets or sets wether to start admin interface. + /// + bool? StartAdminInterface { get; set; } + + /// + /// Gets or sets if the static mappings should be read at startup. + /// + bool? ReadStaticMappings { get; set; } + + /// + /// Gets or sets if the proxy and record settings. + /// + IProxyAndRecordSettings ProxyAndRecordSettings { get; set; } + + /// + /// Gets or sets the urls. + /// + string[] Urls { get; set; } + + /// + /// StartTimeout + /// + int StartTimeout { get; set; } + + /// + /// Allow Partial Mapping (default set to false). + /// + bool? AllowPartialMapping { get; set; } + + /// + /// The username needed for __admin access. + /// + string AdminUsername { get; set; } + + /// + /// The password needed for __admin access. + /// + string AdminPassword { get; set; } + + /// + /// The RequestLog expiration in hours (optional). + /// + int? RequestLogExpirationDuration { get; set; } + + /// + /// The MaxRequestLog count (optional). + /// + int? MaxRequestLogCount { get; set; } + + /// + /// Action which is called (with the IAppBuilder or IApplicationBuilder) before the internal WireMockMiddleware is initialized. [Optional] + /// + Action PreWireMockMiddlewareInit { get; set; } + + /// + /// Action which is called (with the IAppBuilder or IApplicationBuilder) after the internal WireMockMiddleware is initialized. [Optional] + /// + Action PostWireMockMiddlewareInit { get; set; } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs b/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs new file mode 100644 index 000000000..e5f0a4e07 --- /dev/null +++ b/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs @@ -0,0 +1,28 @@ +namespace WireMock.Settings +{ + /// + /// IRecordAndSaveSettings + /// + public interface IProxyAndRecordSettings + { + /// + /// The URL to proxy. + /// + string Url { get; set; } + + /// + /// Save the mapping for each request/response to the internal Mappings. + /// + bool SaveMapping { get; set; } + + /// + /// Save the mapping for each request/response to also file. (Note that SaveMapping must also be set to true.) + /// + bool SaveMappingToFile { get; set; } + + /// + /// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com"" + /// + string X509Certificate2ThumbprintOrSubjectName { get; set; } + } +} diff --git a/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs b/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs index 5787070ea..c1f4d10a0 100644 --- a/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs +++ b/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs @@ -1,28 +1,26 @@ -namespace WireMock.Settings +using JetBrains.Annotations; + +namespace WireMock.Settings { /// /// RecordAndSaveSettings /// - public class ProxyAndRecordSettings + public class ProxyAndRecordSettings : IProxyAndRecordSettings { - /// - /// The URL to proxy. - /// + /// + [PublicAPI] public string Url { get; set; } - /// - /// Save the mapping for each request/response to the internal Mappings. - /// + /// + [PublicAPI] public bool SaveMapping { get; set; } = true; - /// - /// Save the mapping for each request/response to also file. (Note that SaveMapping must also be set to true.) - /// + /// + [PublicAPI] public bool SaveMappingToFile { get; set; } = true; - /// - /// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com"" - /// + /// + [PublicAPI] public string X509Certificate2ThumbprintOrSubjectName { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index 9be9752f7..7c7d882ac 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -34,9 +34,9 @@ - - - + + + @@ -51,7 +51,7 @@ - + diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 5d9204a43..c29e54226 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -16,12 +16,12 @@ - + - +