Add MappingBuilder to build mappings in code and export to Models or JSON (#869)

* MappingBuilder

* .

* ...

* sc

* t

* .
This commit is contained in:
Stef Heyenrath
2023-01-06 19:11:56 +01:00
committed by GitHub
parent 742f1d1f0a
commit 20eb37b0c8
10 changed files with 465 additions and 142 deletions

View File

@@ -38,6 +38,7 @@ public partial class WireMockServer : IWireMockServer
private readonly MappingConverter _mappingConverter;
private readonly MatcherMapper _matcherMapper;
private readonly MappingToFileSaver _mappingToFileSaver;
private readonly MappingBuilder _mappingBuilder;
/// <inheritdoc cref="IWireMockServer.IsStarted" />
[PublicAPI]
@@ -246,7 +247,7 @@ public partial class WireMockServer : IWireMockServer
[PublicAPI]
public static WireMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls)
{
Guard.NotNullOrEmpty(urls, nameof(urls));
Guard.NotNullOrEmpty(urls);
return new WireMockServer(new WireMockServerSettings
{
@@ -268,7 +269,7 @@ public partial class WireMockServer : IWireMockServer
/// <exception cref="TimeoutException">Service start timed out after {TimeSpan.FromMilliseconds(settings.StartTimeout)}</exception>
protected WireMockServer(WireMockServerSettings settings)
{
_settings = settings;
_settings = Guard.NotNull(settings);
// Set default values if not provided
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
@@ -305,28 +306,12 @@ public partial class WireMockServer : IWireMockServer
}
}
_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;
_options.DoNotSaveDynamicResponseInLogEntry = settings.DoNotSaveDynamicResponseInLogEntry;
_options.QueryParameterMultipleValueSupport = settings.QueryParameterMultipleValueSupport;
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;
}
WireMockMiddlewareOptionsHelper.InitFromSettings(settings, _options);
_matcherMapper = new MatcherMapper(_settings);
_mappingConverter = new MappingConverter(_matcherMapper);
_mappingToFileSaver = new MappingToFileSaver(_settings, _mappingConverter);
_mappingBuilder = new MappingBuilder(settings, _options, _mappingConverter, _mappingToFileSaver);
#if USE_ASPNETCORE
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
@@ -538,26 +523,7 @@ public partial class WireMockServer : IWireMockServer
[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, update the datetime and replace it.
if (_options.Mappings.ContainsKey(mapping.Guid))
{
mapping.UpdatedAt = DateTime.UtcNow;
_options.Mappings[mapping.Guid] = mapping;
}
else
{
_options.Mappings.TryAdd(mapping.Guid, mapping);
}
if (saveToFile)
{
_mappingToFileSaver.SaveMappingToFile(mapping);
}
return _mappingBuilder.Given(requestMatcher, saveToFile);
}
private void InitSettings(WireMockServerSettings settings)