PreWireMockMiddlewareInit / PostWireMockMiddlewareInit

This commit is contained in:
Stef Heyenrath
2017-10-17 20:47:19 +02:00
parent 07f03997c0
commit 37244d5c2f
10 changed files with 82 additions and 24 deletions

View File

@@ -46,7 +46,9 @@ namespace WireMock.Owin
_host = new WebHostBuilder()
.Configure(appBuilder =>
{
_options.PreWireMockMiddlewareInit?.Invoke(appBuilder);
appBuilder.UseMiddleware<WireMockMiddleware>(_options);
_options.PostWireMockMiddlewareInit?.Invoke(appBuilder);
})
.UseKestrel()
.UseUrls(_uriPrefixes)

View File

@@ -57,11 +57,13 @@ namespace WireMock.Owin
private void StartServers()
{
System.Console.WriteLine("WireMock.Net server using .net 4.5.x or .net 4.6.x");
Console.WriteLine("WireMock.Net server using .net 4.5.x or .net 4.6.x");
Action<IAppBuilder> startup = app =>
{
_options.PreWireMockMiddlewareInit?.Invoke(app);
app.Use<WireMockMiddleware>(_options);
_options.PostWireMockMiddlewareInit?.Invoke(app);
};
var servers = new List<IDisposable>();

View File

@@ -50,7 +50,7 @@ namespace WireMock.Owin
RequestMatchResult requestMatchResult = null;
try
{
foreach (var mapping in _options.Mappings.Where(m => m.Scenario != null))
foreach (var mapping in _options.Mappings.Where(m => m?.Scenario != null))
{
// Set start
if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)

View File

@@ -4,6 +4,11 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using WireMock.Logging;
using WireMock.Matchers;
#if !NETSTANDARD
using Owin;
#else
using Microsoft.AspNetCore.Builder;
#endif
namespace WireMock.Owin
{
@@ -24,5 +29,15 @@ namespace WireMock.Owin
public int? MaxRequestLogCount { get; set; }
public IDictionary<string, object> Scenarios { get; } = new ConcurrentDictionary<string, object>();
#if !NETSTANDARD
public Action<IAppBuilder> PreWireMockMiddlewareInit { get; set; }
public Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
#else
public Action<IApplicationBuilder> PreWireMockMiddlewareInit { get; set; }
public Action<IApplicationBuilder> PostWireMockMiddlewareInit { get; set; }
#endif
}
}

View File

@@ -1,5 +1,8 @@
namespace WireMock.ResponseBuilders
{
/// <summary>
/// Defines the BodyDestinationFormat
/// </summary>
public static class BodyDestinationFormat
{
/// <summary>

View File

@@ -162,6 +162,9 @@ namespace WireMock.Server
Urls = new[] { (settings.UseSSL == true ? "https" : "http") + "://localhost:" + port + "/" };
}
_options.PreWireMockMiddlewareInit = settings.PreWireMockMiddlewareInit;
_options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit;
#if NETSTANDARD
_httpServer = new AspNetCoreSelfHost(_options, Urls);
#else

View File

@@ -1,4 +1,7 @@
namespace WireMock.Settings
using System;
using JetBrains.Annotations;
namespace WireMock.Settings
{
/// <summary>
/// FluentMockServerSettings
@@ -8,76 +11,87 @@
/// <summary>
/// Gets or sets the port.
/// </summary>
/// <value>
/// The port.
/// </value>
[PublicAPI]
public int? Port { get; set; }
/// <summary>
/// Gets or sets the use SSL.
/// </summary>
/// <value>
/// The use SSL.
/// </value>
// ReSharper disable once InconsistentNaming
[PublicAPI]
public bool? UseSSL { get; set; }
/// <summary>
/// Gets or sets the start admin interface.
/// Gets or sets wether to start admin interface.
/// </summary>
/// <value>
/// The start admin interface.
/// </value>
[PublicAPI]
public bool? StartAdminInterface { get; set; }
/// <summary>
/// Gets or sets if the static mappings should be read at startup.
/// </summary>
/// <value>true/false</value>
[PublicAPI]
public bool? ReadStaticMappings { get; set; }
/// <summary>
/// Gets or sets if the server should record and save requests and responses.
/// </summary>
/// <value>true/false</value>
[PublicAPI]
public ProxyAndRecordSettings ProxyAndRecordSettings { get; set; }
/// <summary>
/// Gets or sets the urls.
/// </summary>
/// <value>
/// The urls.
/// </value>
[PublicAPI]
public string[] Urls { get; set; }
/// <summary>
/// StartTimeout
/// </summary>
[PublicAPI]
public int StartTimeout { get; set; } = 10000;
/// <summary>
/// Allow Partial Mapping (default set to false).
/// </summary>
[PublicAPI]
public bool? AllowPartialMapping { get; set; }
/// <summary>
/// The username needed for __admin access.
/// </summary>
[PublicAPI]
public string AdminUsername { get; set; }
/// <summary>
/// The password needed for __admin access.
/// </summary>
[PublicAPI]
public string AdminPassword { get; set; }
/// <summary>
/// The RequestLog expiration in hours (optional).
/// </summary>
[PublicAPI]
public int? RequestLogExpirationDuration { get; set; }
/// <summary>
/// The MaxRequestLog count (optional).
/// </summary>
[PublicAPI]
public int? MaxRequestLogCount { get; set; }
/// <summary>
/// Action which is called (with the IAppBuilder or IApplicationBuilder) before the internal WireMockMiddleware is initialized. [Optional]
/// </summary>
[PublicAPI]
public Action<object> PreWireMockMiddlewareInit { get; set; }
/// <summary>
/// Action which is called (with the IAppBuilder or IApplicationBuilder) after the internal WireMockMiddleware is initialized. [Optional]
/// </summary>
[PublicAPI]
public Action<object> PostWireMockMiddlewareInit { get; set; }
}
}