mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 00:28:59 +01:00
Update the logic for ProxyAndRecord (#747)
* . * set * . * . * . * . * prio * appsettings * TinyMapperUtils * set * p * nullable * . * , * fs * . * . * --PreferProxyMapping
This commit is contained in:
@@ -25,7 +25,7 @@ namespace WireMock.Admin.Mappings
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The priority.
|
||||
/// The priority. (A low value means higher priority.)
|
||||
/// </summary>
|
||||
public int? Priority { get; set; }
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.Admin.Settings;
|
||||
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class ProxyAndRecordSettingsModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The clientCertificate thumbprint or subject name fragment to use.
|
||||
/// Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
||||
/// </summary>
|
||||
public string ClientX509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines the WebProxySettings.
|
||||
/// </summary>
|
||||
public WebProxySettingsModel WebProxySettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Proxy requests should follow redirection (30x).
|
||||
/// </summary>
|
||||
public bool? AllowAutoRedirect { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The URL to proxy.
|
||||
/// </summary>
|
||||
public string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Save the mapping for each request/response to the internal Mappings.
|
||||
/// </summary>
|
||||
public bool SaveMapping { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Save the mapping for each request/response also to a file. (Note that SaveMapping must also be set to true.)
|
||||
/// </summary>
|
||||
public bool SaveMappingToFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Only save request/response to the internal Mappings if the status code is included in this pattern. (Note that SaveMapping must also be set to true.)
|
||||
/// The pattern can contain a single value like "200", but also ranges like "2xx", "100,300,600" or "100-299,6xx" are supported.
|
||||
/// </summary>
|
||||
public string SaveMappingForStatusCodePattern { get; set; } = "*";
|
||||
|
||||
/// <summary>
|
||||
/// Defines a list from headers which will be excluded from the saved mappings.
|
||||
/// </summary>
|
||||
public string[] ExcludedHeaders { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines a list of cookies which will be excluded from the saved mappings.
|
||||
/// </summary>
|
||||
public string[] ExcludedCookies { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
|
||||
/// </summary>
|
||||
// public bool PreferProxyMapping { get; set; }
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.Admin.Settings
|
||||
{
|
||||
@@ -55,9 +55,29 @@ namespace WireMock.Admin.Settings
|
||||
/// </summary>
|
||||
public bool? SaveUnmatchedRequests { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if the static mappings should be read at startup.
|
||||
/// </summary>
|
||||
public bool? ReadStaticMappings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Watch the static mapping files + folder for changes when running.
|
||||
/// </summary>
|
||||
public bool? WatchStaticMappings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A value indicating whether subdirectories within the static mappings path should be monitored.
|
||||
/// </summary>
|
||||
public bool? WatchStaticMappingsInSubdirectories { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Policies to use when using CORS. By default CORS is disabled. [Optional]
|
||||
/// </summary>
|
||||
public string CorsPolicyOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The proxy and record settings.
|
||||
/// </summary>
|
||||
public ProxyAndRecordSettingsModel ProxyAndRecordSettings { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace WireMock.Admin.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// WebProxySettings
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class WebProxySettingsModel
|
||||
{
|
||||
/// <summary>
|
||||
/// A string instance that contains the address of the proxy server.
|
||||
/// </summary>
|
||||
public string Address { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The user name associated with the credentials.
|
||||
/// </summary>
|
||||
public string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The password for the user name associated with the credentials.
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -41,4 +41,11 @@
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<!--<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
|
||||
<PackageReference Include="Nullable" Version="1.2.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>-->
|
||||
|
||||
</Project>
|
||||
9
src/WireMock.Net/Constants/WireMockConstants.cs
Normal file
9
src/WireMock.Net/Constants/WireMockConstants.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace WireMock.Constants
|
||||
{
|
||||
internal static class WireMockConstants
|
||||
{
|
||||
public const int AdminPriority = int.MinValue;
|
||||
public const int MinPriority = -1_000_000;
|
||||
public const int ProxyPriority = -2_000_000;
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ namespace WireMock
|
||||
string Path { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the priority.
|
||||
/// Gets the priority. (A low value means higher priority.)
|
||||
/// </summary>
|
||||
int Priority { get; }
|
||||
|
||||
@@ -91,6 +91,14 @@ namespace WireMock
|
||||
/// </value>
|
||||
bool IsAdminInterface { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this mapping is a Proxy Mapping.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if this mapping is a Proxy Mapping; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
bool IsProxy { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this mapping to be logged.
|
||||
/// </summary>
|
||||
|
||||
@@ -52,6 +52,9 @@ namespace WireMock
|
||||
/// <inheritdoc />
|
||||
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsProxy => Provider is ProxyAsyncResponseProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool LogMapping => !(Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Settings;
|
||||
using Stef.Validation;
|
||||
@@ -13,13 +14,14 @@ namespace WireMock.Serialization
|
||||
|
||||
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
||||
{
|
||||
Guard.NotNull(settings, nameof(settings));
|
||||
Guard.NotNull(settings);
|
||||
Guard.NotNull(mappingConverter);
|
||||
|
||||
_settings = settings;
|
||||
_mappingConverter = mappingConverter;
|
||||
}
|
||||
|
||||
public void SaveMappingToFile(IMapping mapping, string folder = null)
|
||||
public void SaveMappingToFile(IMapping mapping, [CanBeNull] string folder = null)
|
||||
{
|
||||
if (folder == null)
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@ using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Stef.Validation;
|
||||
using WireMock.Constants;
|
||||
|
||||
namespace WireMock.Server
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -20,511 +20,512 @@ using WireMock.ResponseProviders;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Server
|
||||
namespace WireMock.Server;
|
||||
|
||||
/// <summary>
|
||||
/// The fluent mock server.
|
||||
/// </summary>
|
||||
public partial class WireMockServer : IWireMockServer
|
||||
{
|
||||
private const int ServerStartDelayInMs = 100;
|
||||
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly IOwinSelfHost _httpServer;
|
||||
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
private readonly MatcherMapper _matcherMapper;
|
||||
private readonly MappingToFileSaver _mappingToFileSaver;
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.IsStarted" />
|
||||
[PublicAPI]
|
||||
public bool IsStarted => _httpServer != null && _httpServer.IsStarted;
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public List<int> Ports { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public int Port => Ports?.FirstOrDefault() ?? default(int);
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public string[] Urls { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public string Url => Urls?.FirstOrDefault();
|
||||
|
||||
/// <summary>
|
||||
/// The fluent mock server.
|
||||
/// Gets the mappings.
|
||||
/// </summary>
|
||||
public partial class WireMockServer : IWireMockServer
|
||||
[PublicAPI]
|
||||
public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray();
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.MappingModels" />
|
||||
[PublicAPI]
|
||||
public IEnumerable<MappingModel> MappingModels => ToMappingModels();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the scenarios.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>(_options.Scenarios);
|
||||
|
||||
#region IDisposable Members
|
||||
/// <summary>
|
||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
private const int ServerStartDelayInMs = 100;
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly IOwinSelfHost _httpServer;
|
||||
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
private readonly MatcherMapper _matcherMapper;
|
||||
private readonly MappingToFileSaver _mappingToFileSaver;
|
||||
/// <summary>
|
||||
/// Releases unmanaged and - optionally - managed resources.
|
||||
/// </summary>
|
||||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
DisposeEnhancedFileSystemWatcher();
|
||||
_httpServer?.StopAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.IsStarted" />
|
||||
[PublicAPI]
|
||||
public bool IsStarted => _httpServer != null && _httpServer.IsStarted;
|
||||
#region Start/Stop
|
||||
/// <summary>
|
||||
/// Starts this WireMockServer with the specified settings.
|
||||
/// </summary>
|
||||
/// <param name="settings">The WireMockServerSettings.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start([NotNull] WireMockServerSettings settings)
|
||||
{
|
||||
Guard.NotNull(settings, nameof(settings));
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public List<int> Ports { get; }
|
||||
return new WireMockServer(settings);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public int Port => Ports?.FirstOrDefault() ?? default(int);
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public string[] Urls { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public string Url => Urls?.FirstOrDefault();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mappings.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray();
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.MappingModels" />
|
||||
[PublicAPI]
|
||||
public IEnumerable<MappingModel> MappingModels => ToMappingModels();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the scenarios.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>(_options.Scenarios);
|
||||
|
||||
#region IDisposable Members
|
||||
/// <summary>
|
||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
/// <summary>
|
||||
/// Start this WireMockServer.
|
||||
/// </summary>
|
||||
/// <param name="port">The port.</param>
|
||||
/// <param name="ssl">The SSL support.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start([CanBeNull] int? port = 0, bool ssl = false)
|
||||
{
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
Port = port,
|
||||
UseSSL = ssl
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer.
|
||||
/// </summary>
|
||||
/// <param name="urls">The urls to listen on.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start(params string[] urls)
|
||||
{
|
||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Urls = urls
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer with the admin interface.
|
||||
/// </summary>
|
||||
/// <param name="port">The port.</param>
|
||||
/// <param name="ssl">The SSL support.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer StartWithAdminInterface(int? port = 0, bool ssl = false)
|
||||
{
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Port = port,
|
||||
UseSSL = ssl,
|
||||
StartAdminInterface = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer with the admin interface.
|
||||
/// </summary>
|
||||
/// <param name="urls">The urls.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer StartWithAdminInterface(params string[] urls)
|
||||
{
|
||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Urls = urls,
|
||||
StartAdminInterface = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer with the admin interface and read static mappings.
|
||||
/// </summary>
|
||||
/// <param name="urls">The urls.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls)
|
||||
{
|
||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Urls = urls,
|
||||
StartAdminInterface = true,
|
||||
ReadStaticMappings = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WireMockServer"/> class.
|
||||
/// </summary>
|
||||
/// <param name="settings">The settings.</param>
|
||||
/// <exception cref="WireMockException">
|
||||
/// Service start failed with error: {_httpServer.RunningException.Message}
|
||||
/// or
|
||||
/// Service start failed with error: {startTask.Exception.Message}
|
||||
/// </exception>
|
||||
/// <exception cref="TimeoutException">Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}</exception>
|
||||
protected WireMockServer(WireMockServerSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
|
||||
// Set default values if not provided
|
||||
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
||||
|
||||
_settings.Logger.Info("By Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)");
|
||||
_settings.Logger.Debug("Server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
HostUrlOptions urlOptions;
|
||||
if (settings.Urls != null)
|
||||
{
|
||||
urlOptions = new HostUrlOptions
|
||||
{
|
||||
Urls = settings.Urls
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
urlOptions = new HostUrlOptions
|
||||
{
|
||||
UseSSL = settings.UseSSL == true,
|
||||
Port = settings.Port
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged and - optionally - managed resources.
|
||||
/// </summary>
|
||||
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
DisposeEnhancedFileSystemWatcher();
|
||||
_httpServer?.StopAsync();
|
||||
}
|
||||
#endregion
|
||||
_options.FileSystemHandler = _settings.FileSystemHandler;
|
||||
_options.PreWireMockMiddlewareInit = _settings.PreWireMockMiddlewareInit;
|
||||
_options.PostWireMockMiddlewareInit = _settings.PostWireMockMiddlewareInit;
|
||||
_options.Logger = _settings.Logger;
|
||||
_options.DisableJsonBodyParsing = _settings.DisableJsonBodyParsing;
|
||||
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;
|
||||
_options.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
|
||||
|
||||
#region Start/Stop
|
||||
/// <summary>
|
||||
/// Starts this WireMockServer with the specified settings.
|
||||
/// </summary>
|
||||
/// <param name="settings">The WireMockServerSettings.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start([NotNull] WireMockServerSettings settings)
|
||||
if (settings.CustomCertificateDefined)
|
||||
{
|
||||
Guard.NotNull(settings, nameof(settings));
|
||||
|
||||
return new WireMockServer(settings);
|
||||
_options.X509StoreName = settings.CertificateSettings.X509StoreName;
|
||||
_options.X509StoreLocation = settings.CertificateSettings.X509StoreLocation;
|
||||
_options.X509ThumbprintOrSubjectName = settings.CertificateSettings.X509StoreThumbprintOrSubjectName;
|
||||
_options.X509CertificateFilePath = settings.CertificateSettings.X509CertificateFilePath;
|
||||
_options.X509CertificatePassword = settings.CertificateSettings.X509CertificatePassword;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer.
|
||||
/// </summary>
|
||||
/// <param name="port">The port.</param>
|
||||
/// <param name="ssl">The SSL support.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start([CanBeNull] int? port = 0, bool ssl = false)
|
||||
{
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Port = port,
|
||||
UseSSL = ssl
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer.
|
||||
/// </summary>
|
||||
/// <param name="urls">The urls to listen on.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start(params string[] urls)
|
||||
{
|
||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Urls = urls
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer with the admin interface.
|
||||
/// </summary>
|
||||
/// <param name="port">The port.</param>
|
||||
/// <param name="ssl">The SSL support.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer StartWithAdminInterface(int? port = 0, bool ssl = false)
|
||||
{
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Port = port,
|
||||
UseSSL = ssl,
|
||||
StartAdminInterface = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer with the admin interface.
|
||||
/// </summary>
|
||||
/// <param name="urls">The urls.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer StartWithAdminInterface(params string[] urls)
|
||||
{
|
||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Urls = urls,
|
||||
StartAdminInterface = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this WireMockServer with the admin interface and read static mappings.
|
||||
/// </summary>
|
||||
/// <param name="urls">The urls.</param>
|
||||
/// <returns>The <see cref="WireMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static WireMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls)
|
||||
{
|
||||
Guard.NotNullOrEmpty(urls, nameof(urls));
|
||||
|
||||
return new WireMockServer(new WireMockServerSettings
|
||||
{
|
||||
Urls = urls,
|
||||
StartAdminInterface = true,
|
||||
ReadStaticMappings = true
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WireMockServer"/> class.
|
||||
/// </summary>
|
||||
/// <param name="settings">The settings.</param>
|
||||
/// <exception cref="WireMockException">
|
||||
/// Service start failed with error: {_httpServer.RunningException.Message}
|
||||
/// or
|
||||
/// Service start failed with error: {startTask.Exception.Message}
|
||||
/// </exception>
|
||||
/// <exception cref="TimeoutException">Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}</exception>
|
||||
protected WireMockServer(WireMockServerSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
|
||||
// Set default values if not provided
|
||||
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
||||
|
||||
_settings.Logger.Info("By Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)");
|
||||
_settings.Logger.Debug("Server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
HostUrlOptions urlOptions;
|
||||
if (settings.Urls != null)
|
||||
{
|
||||
urlOptions = new HostUrlOptions
|
||||
{
|
||||
Urls = settings.Urls
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
urlOptions = new HostUrlOptions
|
||||
{
|
||||
UseSSL = settings.UseSSL == true,
|
||||
Port = settings.Port
|
||||
};
|
||||
}
|
||||
|
||||
_options.FileSystemHandler = _settings.FileSystemHandler;
|
||||
_options.PreWireMockMiddlewareInit = _settings.PreWireMockMiddlewareInit;
|
||||
_options.PostWireMockMiddlewareInit = _settings.PostWireMockMiddlewareInit;
|
||||
_options.Logger = _settings.Logger;
|
||||
_options.DisableJsonBodyParsing = _settings.DisableJsonBodyParsing;
|
||||
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;
|
||||
_options.SaveUnmatchedRequests = settings.SaveUnmatchedRequests;
|
||||
|
||||
if (settings.CustomCertificateDefined)
|
||||
{
|
||||
_options.X509StoreName = settings.CertificateSettings.X509StoreName;
|
||||
_options.X509StoreLocation = settings.CertificateSettings.X509StoreLocation;
|
||||
_options.X509ThumbprintOrSubjectName = settings.CertificateSettings.X509StoreThumbprintOrSubjectName;
|
||||
_options.X509CertificateFilePath = settings.CertificateSettings.X509CertificateFilePath;
|
||||
_options.X509CertificatePassword = settings.CertificateSettings.X509CertificatePassword;
|
||||
}
|
||||
|
||||
_matcherMapper = new MatcherMapper(_settings);
|
||||
_mappingConverter = new MappingConverter(_matcherMapper);
|
||||
_mappingToFileSaver = new MappingToFileSaver(_settings, _mappingConverter);
|
||||
_matcherMapper = new MatcherMapper(_settings);
|
||||
_mappingConverter = new MappingConverter(_matcherMapper);
|
||||
_mappingToFileSaver = new MappingToFileSaver(_settings, _mappingConverter);
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
|
||||
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
||||
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
|
||||
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
||||
|
||||
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
||||
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
||||
#else
|
||||
_httpServer = new OwinSelfHost(_options, urlOptions);
|
||||
_httpServer = new OwinSelfHost(_options, urlOptions);
|
||||
#endif
|
||||
var startTask = _httpServer.StartAsync();
|
||||
var startTask = _httpServer.StartAsync();
|
||||
|
||||
using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
|
||||
using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
|
||||
{
|
||||
while (!_httpServer.IsStarted)
|
||||
{
|
||||
while (!_httpServer.IsStarted)
|
||||
// Throw exception if service start fails
|
||||
if (_httpServer.RunningException != null)
|
||||
{
|
||||
// Throw exception if service start fails
|
||||
if (_httpServer.RunningException != null)
|
||||
throw new WireMockException($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException);
|
||||
}
|
||||
|
||||
if (ctsStartTimeout.IsCancellationRequested)
|
||||
{
|
||||
// In case of an aggregate exception, throw the exception.
|
||||
if (startTask.Exception != null)
|
||||
{
|
||||
throw new WireMockException($"Service start failed with error: {_httpServer.RunningException.Message}", _httpServer.RunningException);
|
||||
throw new WireMockException($"Service start failed with error: {startTask.Exception.Message}", startTask.Exception);
|
||||
}
|
||||
|
||||
if (ctsStartTimeout.IsCancellationRequested)
|
||||
{
|
||||
// In case of an aggregate exception, throw the exception.
|
||||
if (startTask.Exception != null)
|
||||
{
|
||||
throw new WireMockException($"Service start failed with error: {startTask.Exception.Message}", startTask.Exception);
|
||||
}
|
||||
|
||||
// Else throw TimeoutException
|
||||
throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}");
|
||||
}
|
||||
|
||||
ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelayInMs);
|
||||
// Else throw TimeoutException
|
||||
throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}");
|
||||
}
|
||||
|
||||
Urls = _httpServer.Urls.ToArray();
|
||||
Ports = _httpServer.Ports;
|
||||
ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelayInMs);
|
||||
}
|
||||
|
||||
if (settings.AllowBodyForAllHttpMethods == true)
|
||||
{
|
||||
_options.AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods;
|
||||
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to True");
|
||||
}
|
||||
|
||||
if (settings.AllowOnlyDefinedHttpStatusCodeInResponse == true)
|
||||
{
|
||||
_options.AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse;
|
||||
_settings.Logger.Info("AllowOnlyDefinedHttpStatusCodeInResponse is set to True");
|
||||
}
|
||||
|
||||
if (settings.AllowPartialMapping == true)
|
||||
{
|
||||
AllowPartialMapping();
|
||||
}
|
||||
|
||||
if (settings.StartAdminInterface == true)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword))
|
||||
{
|
||||
SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(settings.AdminAzureADTenant) && !string.IsNullOrEmpty(settings.AdminAzureADAudience))
|
||||
{
|
||||
SetAzureADAuthentication(settings.AdminAzureADTenant, settings.AdminAzureADAudience);
|
||||
}
|
||||
|
||||
InitAdmin();
|
||||
}
|
||||
|
||||
if (settings.ReadStaticMappings == true)
|
||||
{
|
||||
ReadStaticMappings();
|
||||
}
|
||||
|
||||
if (settings.WatchStaticMappings == true)
|
||||
{
|
||||
WatchStaticMappings();
|
||||
}
|
||||
|
||||
if (settings.ProxyAndRecordSettings != null)
|
||||
{
|
||||
InitProxyAndRecord(settings);
|
||||
}
|
||||
|
||||
if (settings.RequestLogExpirationDuration != null)
|
||||
{
|
||||
SetRequestLogExpirationDuration(settings.RequestLogExpirationDuration);
|
||||
}
|
||||
|
||||
if (settings.MaxRequestLogCount != null)
|
||||
{
|
||||
SetMaxRequestLogCount(settings.MaxRequestLogCount);
|
||||
}
|
||||
Urls = _httpServer.Urls.ToArray();
|
||||
Ports = _httpServer.Ports;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.Stop" />
|
||||
[PublicAPI]
|
||||
public void Stop()
|
||||
{
|
||||
var result = _httpServer?.StopAsync();
|
||||
result?.Wait(); // wait for stop to actually happen
|
||||
}
|
||||
#endregion
|
||||
InitSettings(settings);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.AddCatchAllMapping" />
|
||||
[PublicAPI]
|
||||
public void AddCatchAllMapping()
|
||||
/// <inheritdoc cref="IWireMockServer.Stop" />
|
||||
[PublicAPI]
|
||||
public void Stop()
|
||||
{
|
||||
var result = _httpServer?.StopAsync();
|
||||
result?.Wait(); // wait for stop to actually happen
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.AddCatchAllMapping" />
|
||||
[PublicAPI]
|
||||
public void AddCatchAllMapping()
|
||||
{
|
||||
Given(Request.Create().WithPath("/*").UsingAnyMethod())
|
||||
.WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05"))
|
||||
.AtPriority(1000)
|
||||
.RespondWith(new DynamicResponseProvider(request => ResponseMessageBuilder.Create("No matching mapping found", 404)));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.Reset" />
|
||||
[PublicAPI]
|
||||
public void Reset()
|
||||
{
|
||||
ResetLogEntries();
|
||||
|
||||
ResetMappings();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.ResetMappings" />
|
||||
[PublicAPI]
|
||||
public void ResetMappings()
|
||||
{
|
||||
foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface))
|
||||
{
|
||||
Given(Request.Create().WithPath("/*").UsingAnyMethod())
|
||||
.WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05"))
|
||||
.AtPriority(1000)
|
||||
.RespondWith(new DynamicResponseProvider(request => ResponseMessageBuilder.Create("No matching mapping found", 404)));
|
||||
_options.Mappings.TryRemove(nonAdmin.Key, out _);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.DeleteMapping" />
|
||||
[PublicAPI]
|
||||
public bool DeleteMapping(Guid guid)
|
||||
{
|
||||
// Check a mapping exists with the same GUID, if so, remove it.
|
||||
if (_options.Mappings.ContainsKey(guid))
|
||||
{
|
||||
return _options.Mappings.TryRemove(guid, out _);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.Reset" />
|
||||
[PublicAPI]
|
||||
public void Reset()
|
||||
{
|
||||
ResetLogEntries();
|
||||
return false;
|
||||
}
|
||||
|
||||
ResetMappings();
|
||||
}
|
||||
private bool DeleteMapping(string path)
|
||||
{
|
||||
// Check a mapping exists with the same path, if so, remove it.
|
||||
var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, StringComparison.OrdinalIgnoreCase));
|
||||
return DeleteMapping(mapping.Key);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.ResetMappings" />
|
||||
[PublicAPI]
|
||||
public void ResetMappings()
|
||||
{
|
||||
foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface))
|
||||
{
|
||||
_options.Mappings.TryRemove(nonAdmin.Key, out _);
|
||||
}
|
||||
}
|
||||
/// <inheritdoc cref="IWireMockServer.AddGlobalProcessingDelay" />
|
||||
[PublicAPI]
|
||||
public void AddGlobalProcessingDelay(TimeSpan delay)
|
||||
{
|
||||
_options.RequestProcessingDelay = delay;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.DeleteMapping" />
|
||||
[PublicAPI]
|
||||
public bool DeleteMapping(Guid guid)
|
||||
{
|
||||
// Check a mapping exists with the same GUID, if so, remove it.
|
||||
if (_options.Mappings.ContainsKey(guid))
|
||||
{
|
||||
return _options.Mappings.TryRemove(guid, out _);
|
||||
}
|
||||
/// <inheritdoc cref="IWireMockServer.AllowPartialMapping" />
|
||||
[PublicAPI]
|
||||
public void AllowPartialMapping(bool allow = true)
|
||||
{
|
||||
_settings.Logger.Info("AllowPartialMapping is set to {0}", allow);
|
||||
_options.AllowPartialMapping = allow;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool DeleteMapping(string path)
|
||||
{
|
||||
// Check a mapping exists with the same path, if so, remove it.
|
||||
var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, StringComparison.OrdinalIgnoreCase));
|
||||
return DeleteMapping(mapping.Key);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.AddGlobalProcessingDelay" />
|
||||
[PublicAPI]
|
||||
public void AddGlobalProcessingDelay(TimeSpan delay)
|
||||
{
|
||||
_options.RequestProcessingDelay = delay;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.AllowPartialMapping" />
|
||||
[PublicAPI]
|
||||
public void AllowPartialMapping(bool allow = true)
|
||||
{
|
||||
_settings.Logger.Info("AllowPartialMapping is set to {0}", allow);
|
||||
_options.AllowPartialMapping = allow;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetAzureADAuthentication(string, string)" />
|
||||
[PublicAPI]
|
||||
public void SetAzureADAuthentication([NotNull] string tenant, [NotNull] string audience)
|
||||
{
|
||||
Guard.NotNull(tenant, nameof(tenant));
|
||||
Guard.NotNull(audience, nameof(audience));
|
||||
/// <inheritdoc cref="IWireMockServer.SetAzureADAuthentication(string, string)" />
|
||||
[PublicAPI]
|
||||
public void SetAzureADAuthentication([NotNull] string tenant, [NotNull] string audience)
|
||||
{
|
||||
Guard.NotNull(tenant, nameof(tenant));
|
||||
Guard.NotNull(audience, nameof(audience));
|
||||
|
||||
#if NETSTANDARD1_3
|
||||
throw new NotSupportedException("AzureADAuthentication is not supported for NETStandard 1.3");
|
||||
#else
|
||||
_options.AuthenticationMatcher = new AzureADAuthenticationMatcher(tenant, audience);
|
||||
_options.AuthenticationMatcher = new AzureADAuthenticationMatcher(tenant, audience);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetBasicAuthentication(string, string)" />
|
||||
[PublicAPI]
|
||||
public void SetBasicAuthentication([NotNull] string username, [NotNull] string password)
|
||||
{
|
||||
Guard.NotNull(username, nameof(username));
|
||||
Guard.NotNull(password, nameof(password));
|
||||
|
||||
_options.AuthenticationMatcher = new BasicAuthenticationMatcher(username, password);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.RemoveAuthentication" />
|
||||
[PublicAPI]
|
||||
public void RemoveAuthentication()
|
||||
{
|
||||
_options.AuthenticationMatcher = null;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetMaxRequestLogCount" />
|
||||
[PublicAPI]
|
||||
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
||||
{
|
||||
_options.MaxRequestLogCount = maxRequestLogCount;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetRequestLogExpirationDuration" />
|
||||
[PublicAPI]
|
||||
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
|
||||
{
|
||||
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.ResetScenarios" />
|
||||
[PublicAPI]
|
||||
public void ResetScenarios()
|
||||
{
|
||||
_options.Scenarios.Clear();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.WithMapping(MappingModel[])" />
|
||||
[PublicAPI]
|
||||
public IWireMockServer WithMapping(params MappingModel[] mappings)
|
||||
{
|
||||
foreach (var mapping in mappings)
|
||||
{
|
||||
ConvertMappingAndRegisterAsRespondProvider(mapping, mapping.Guid ?? Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetBasicAuthentication(string, string)" />
|
||||
[PublicAPI]
|
||||
public void SetBasicAuthentication([NotNull] string username, [NotNull] string password)
|
||||
{
|
||||
Guard.NotNull(username, nameof(username));
|
||||
Guard.NotNull(password, nameof(password));
|
||||
return this;
|
||||
}
|
||||
|
||||
_options.AuthenticationMatcher = new BasicAuthenticationMatcher(username, password);
|
||||
/// <inheritdoc cref="IWireMockServer.WithMapping(string)" />
|
||||
[PublicAPI]
|
||||
public IWireMockServer WithMapping(string mappings)
|
||||
{
|
||||
var mappingModels = DeserializeJsonToArray<MappingModel>(mappings);
|
||||
foreach (var mappingModel in mappingModels)
|
||||
{
|
||||
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.RemoveAuthentication" />
|
||||
[PublicAPI]
|
||||
public void RemoveAuthentication()
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The given.
|
||||
/// </summary>
|
||||
/// <param name="requestMatcher">The request matcher.</param>
|
||||
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
|
||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||
[PublicAPI]
|
||||
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
|
||||
{
|
||||
return new RespondWithAProvider(RegisterMapping, requestMatcher, _settings, saveToFile);
|
||||
}
|
||||
|
||||
private void RegisterMapping(IMapping mapping, bool saveToFile)
|
||||
{
|
||||
// Check a mapping exists with the same Guid, if so, replace it.
|
||||
if (_options.Mappings.ContainsKey(mapping.Guid))
|
||||
{
|
||||
_options.AuthenticationMatcher = null;
|
||||
_options.Mappings[mapping.Guid] = mapping;
|
||||
}
|
||||
else
|
||||
{
|
||||
_options.Mappings.TryAdd(mapping.Guid, mapping);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetMaxRequestLogCount" />
|
||||
[PublicAPI]
|
||||
public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount)
|
||||
if (saveToFile)
|
||||
{
|
||||
_options.MaxRequestLogCount = maxRequestLogCount;
|
||||
_mappingToFileSaver.SaveMappingToFile(mapping);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitSettings(WireMockServerSettings settings)
|
||||
{
|
||||
if (settings.AllowBodyForAllHttpMethods == true)
|
||||
{
|
||||
_options.AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods;
|
||||
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to True");
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetRequestLogExpirationDuration" />
|
||||
[PublicAPI]
|
||||
public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration)
|
||||
if (settings.AllowOnlyDefinedHttpStatusCodeInResponse == true)
|
||||
{
|
||||
_options.RequestLogExpirationDuration = requestLogExpirationDuration;
|
||||
_options.AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse;
|
||||
_settings.Logger.Info("AllowOnlyDefinedHttpStatusCodeInResponse is set to True");
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.ResetScenarios" />
|
||||
[PublicAPI]
|
||||
public void ResetScenarios()
|
||||
if (settings.AllowPartialMapping == true)
|
||||
{
|
||||
_options.Scenarios.Clear();
|
||||
AllowPartialMapping();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.WithMapping(MappingModel[])" />
|
||||
[PublicAPI]
|
||||
public IWireMockServer WithMapping(params MappingModel[] mappings)
|
||||
if (settings.StartAdminInterface == true)
|
||||
{
|
||||
foreach (var mapping in mappings)
|
||||
if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword))
|
||||
{
|
||||
ConvertMappingAndRegisterAsRespondProvider(mapping, mapping.Guid ?? Guid.NewGuid());
|
||||
SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword);
|
||||
}
|
||||
|
||||
return this;
|
||||
if (!string.IsNullOrEmpty(settings.AdminAzureADTenant) && !string.IsNullOrEmpty(settings.AdminAzureADAudience))
|
||||
{
|
||||
SetAzureADAuthentication(settings.AdminAzureADTenant, settings.AdminAzureADAudience);
|
||||
}
|
||||
|
||||
InitAdmin();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.WithMapping(string)" />
|
||||
[PublicAPI]
|
||||
public IWireMockServer WithMapping(string mappings)
|
||||
if (settings.ReadStaticMappings == true)
|
||||
{
|
||||
var mappingModels = DeserializeJsonToArray<MappingModel>(mappings);
|
||||
foreach (var mappingModel in mappingModels)
|
||||
{
|
||||
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
||||
}
|
||||
|
||||
return this;
|
||||
ReadStaticMappings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The given.
|
||||
/// </summary>
|
||||
/// <param name="requestMatcher">The request matcher.</param>
|
||||
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
|
||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||
[PublicAPI]
|
||||
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
|
||||
if (settings.WatchStaticMappings == true)
|
||||
{
|
||||
return new RespondWithAProvider(RegisterMapping, requestMatcher, _settings, saveToFile);
|
||||
WatchStaticMappings();
|
||||
}
|
||||
|
||||
private void RegisterMapping(IMapping mapping, bool saveToFile)
|
||||
{
|
||||
// Check a mapping exists with the same Guid, if so, replace it.
|
||||
if (_options.Mappings.ContainsKey(mapping.Guid))
|
||||
{
|
||||
_options.Mappings[mapping.Guid] = mapping;
|
||||
}
|
||||
else
|
||||
{
|
||||
_options.Mappings.TryAdd(mapping.Guid, mapping);
|
||||
}
|
||||
InitProxyAndRecord(settings);
|
||||
|
||||
if (saveToFile)
|
||||
{
|
||||
_mappingToFileSaver.SaveMappingToFile(mapping);
|
||||
}
|
||||
if (settings.RequestLogExpirationDuration != null)
|
||||
{
|
||||
SetRequestLogExpirationDuration(settings.RequestLogExpirationDuration);
|
||||
}
|
||||
|
||||
if (settings.MaxRequestLogCount != null)
|
||||
{
|
||||
SetMaxRequestLogCount(settings.MaxRequestLogCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,5 +43,11 @@ namespace WireMock.Settings
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public string[] ExcludedCookies { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
|
||||
/// </summary>
|
||||
//[PublicAPI]
|
||||
//public bool PreferProxyMapping { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Stef.Validation;
|
||||
using WireMock.Logging;
|
||||
@@ -35,34 +34,33 @@ namespace WireMock.Settings
|
||||
|
||||
settings = new WireMockServerSettings
|
||||
{
|
||||
StartAdminInterface = parser.GetBoolValue("StartAdminInterface", true),
|
||||
ReadStaticMappings = parser.GetBoolValue("ReadStaticMappings"),
|
||||
WatchStaticMappings = parser.GetBoolValue("WatchStaticMappings"),
|
||||
AllowPartialMapping = parser.GetBoolValue("AllowPartialMapping"),
|
||||
WatchStaticMappingsInSubdirectories = parser.GetBoolValue("WatchStaticMappingsInSubdirectories"),
|
||||
AdminUsername = parser.GetStringValue("AdminUsername"),
|
||||
AdminPassword = parser.GetStringValue("AdminPassword"),
|
||||
AdminAzureADTenant = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADTenant)),
|
||||
AdminAzureADAudience = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADAudience)),
|
||||
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
||||
AdminAzureADTenant = parser.GetStringValue(nameof(WireMockServerSettings.AdminAzureADTenant)),
|
||||
AdminPassword = parser.GetStringValue("AdminPassword"),
|
||||
AdminUsername = parser.GetStringValue("AdminUsername"),
|
||||
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"),
|
||||
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
||||
AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"),
|
||||
AllowPartialMapping = parser.GetBoolValue("AllowPartialMapping"),
|
||||
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"),
|
||||
HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously"),
|
||||
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
||||
ReadStaticMappings = parser.GetBoolValue("ReadStaticMappings"),
|
||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||
SaveUnmatchedRequests = parser.GetBoolValue(nameof(WireMockServerSettings.SaveUnmatchedRequests)),
|
||||
StartAdminInterface = parser.GetBoolValue("StartAdminInterface", true),
|
||||
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails"),
|
||||
UseRegexExtended = parser.GetBoolValue(nameof(WireMockServerSettings.UseRegexExtended), true),
|
||||
SaveUnmatchedRequests = parser.GetBoolValue(nameof(WireMockServerSettings.SaveUnmatchedRequests)),
|
||||
WatchStaticMappings = parser.GetBoolValue("WatchStaticMappings"),
|
||||
WatchStaticMappingsInSubdirectories = parser.GetBoolValue("WatchStaticMappingsInSubdirectories"),
|
||||
};
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
settings.CorsPolicyOptions = parser.GetValue(
|
||||
nameof(WireMockServerSettings.CorsPolicyOptions), values =>
|
||||
{
|
||||
var value = string.Join(string.Empty, values);
|
||||
return Enum.TryParse<CorsPolicyOptions>(value, true, out var corsPolicyOptions) ? corsPolicyOptions : CorsPolicyOptions.None;
|
||||
});
|
||||
settings.CorsPolicyOptions = parser.GetValue(nameof(WireMockServerSettings.CorsPolicyOptions), values =>
|
||||
{
|
||||
var value = string.Join(string.Empty, values);
|
||||
return Enum.TryParse<CorsPolicyOptions>(value, true, out var corsPolicyOptions) ? corsPolicyOptions : CorsPolicyOptions.None;
|
||||
});
|
||||
#endif
|
||||
|
||||
if (logger != null)
|
||||
@@ -75,9 +73,9 @@ namespace WireMock.Settings
|
||||
settings.Logger = new WireMockConsoleLogger();
|
||||
}
|
||||
|
||||
if (parser.Contains("Port"))
|
||||
if (parser.Contains(nameof(WireMockServerSettings.Port)))
|
||||
{
|
||||
settings.Port = parser.GetIntValue("Port");
|
||||
settings.Port = parser.GetIntValue(nameof(WireMockServerSettings.Port));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -89,14 +87,15 @@ namespace WireMock.Settings
|
||||
{
|
||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = proxyUrl,
|
||||
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
||||
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||
SaveMappingForStatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern"),
|
||||
AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect"),
|
||||
ClientX509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("ClientX509Certificate2ThumbprintOrSubjectName"),
|
||||
ExcludedHeaders = parser.GetValues("ExcludedHeaders"),
|
||||
ExcludedCookies = parser.GetValues("ExcludedCookies"),
|
||||
AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect")
|
||||
ExcludedHeaders = parser.GetValues("ExcludedHeaders"),
|
||||
// PreferProxyMapping = parser.GetBoolValue(nameof(ProxyAndRecordSettings.PreferProxyMapping)),
|
||||
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
||||
SaveMappingForStatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern"),
|
||||
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||
Url = proxyUrl
|
||||
};
|
||||
|
||||
string proxyAddress = parser.GetStringValue("WebProxyAddress");
|
||||
|
||||
30
src/WireMock.Net/Util/TinyMapperUtils.cs
Normal file
30
src/WireMock.Net/Util/TinyMapperUtils.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Nelibur.ObjectMapper;
|
||||
using WireMock.Admin.Settings;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Util
|
||||
{
|
||||
internal sealed class TinyMapperUtils
|
||||
{
|
||||
public static TinyMapperUtils Instance { get; } = new TinyMapperUtils();
|
||||
|
||||
private TinyMapperUtils()
|
||||
{
|
||||
TinyMapper.Bind<ProxyAndRecordSettings, ProxyAndRecordSettingsModel>();
|
||||
TinyMapper.Bind<WebProxySettings, WebProxySettingsModel>();
|
||||
|
||||
TinyMapper.Bind<ProxyAndRecordSettingsModel, ProxyAndRecordSettings>();
|
||||
TinyMapper.Bind<WebProxySettingsModel, WebProxySettings>();
|
||||
}
|
||||
|
||||
public ProxyAndRecordSettingsModel Map(ProxyAndRecordSettings instance)
|
||||
{
|
||||
return instance == null ? null : TinyMapper.Map<ProxyAndRecordSettingsModel>(instance);
|
||||
}
|
||||
|
||||
public ProxyAndRecordSettings Map(ProxyAndRecordSettingsModel model)
|
||||
{
|
||||
return model == null ? null : TinyMapper.Map<ProxyAndRecordSettings>(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Util\FileSystemWatcherExtensions.cs" />
|
||||
<Compile Remove="Util\FileSystemWatcherExtensions.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -64,7 +64,7 @@
|
||||
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.14" />
|
||||
<PackageReference Include="JmesPath.Net" Version="1.0.125" />
|
||||
<PackageReference Include="AnyOf" Version="0.3.0" />
|
||||
<!--<PackageReference Include="TinyMapper" Version="3.0.3" />-->
|
||||
<PackageReference Include="TinyMapper" Version="3.0.3" />
|
||||
<!--<PackageReference Include="Mapster" Version="7.2.0" />-->
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user