-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MappingBuilder to build mappings in code and export to Models or …
…JSON (#869) * MappingBuilder * . * ... * sc * t * .
- Loading branch information
Showing
10 changed files
with
479 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using WireMock.Admin.Mappings; | ||
using WireMock.Matchers.Request; | ||
using WireMock.Server; | ||
|
||
namespace WireMock; | ||
|
||
/// <summary> | ||
/// IMappingBuilder | ||
/// </summary> | ||
public interface IMappingBuilder | ||
{ | ||
/// <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> | ||
IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false); | ||
|
||
/// <summary> | ||
/// Gets all the mappings as a list. | ||
/// </summary> | ||
/// <returns>A list from <see cref="MappingModel"/>s.</returns> | ||
MappingModel[] GetMappings(); | ||
|
||
/// <summary> | ||
/// Convert all mappings to JSON. | ||
/// </summary> | ||
/// <returns>JSON</returns> | ||
string ToJson(); | ||
|
||
/// <summary> | ||
/// Save all mappings as a single JSON to a file. | ||
/// </summary> | ||
/// <param name="path">The file to write to.</param> | ||
void SaveMappingsToFile(string path); | ||
|
||
/// <summary> | ||
/// Save all mappings as multiple JSON files (each file is 1 mapping). | ||
/// </summary> | ||
/// <param name="folder">The folder to write the files to.</param> | ||
void SaveMappingsToFolder(string folder); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Stef.Validation; | ||
using WireMock.Admin.Mappings; | ||
using WireMock.Matchers.Request; | ||
using WireMock.Owin; | ||
using WireMock.Serialization; | ||
using WireMock.Server; | ||
using WireMock.Settings; | ||
|
||
namespace WireMock; | ||
|
||
/// <summary> | ||
/// MappingBuilder | ||
/// </summary> | ||
public class MappingBuilder : IMappingBuilder | ||
{ | ||
private readonly WireMockServerSettings _settings; | ||
private readonly IWireMockMiddlewareOptions _options; | ||
|
||
private readonly MappingConverter _mappingConverter; | ||
private readonly MappingToFileSaver _mappingToFileSaver; | ||
|
||
/// <summary> | ||
/// Create a MappingBuilder | ||
/// </summary> | ||
/// <param name="settings">The optional <see cref="WireMockServerSettings"/>.</param> | ||
public MappingBuilder(WireMockServerSettings? settings = null) | ||
{ | ||
_settings = settings ?? new WireMockServerSettings(); | ||
_options = WireMockMiddlewareOptionsHelper.InitFromSettings(_settings); | ||
|
||
var matcherMapper = new MatcherMapper(_settings); | ||
_mappingConverter = new MappingConverter(matcherMapper); | ||
_mappingToFileSaver = new MappingToFileSaver(_settings, _mappingConverter); | ||
} | ||
|
||
internal MappingBuilder( | ||
WireMockServerSettings settings, | ||
IWireMockMiddlewareOptions options, | ||
MappingConverter mappingConverter, | ||
MappingToFileSaver mappingToFileSaver | ||
) | ||
{ | ||
_settings = Guard.NotNull(settings); | ||
_options = Guard.NotNull(options); | ||
_mappingConverter = Guard.NotNull(mappingConverter); | ||
_mappingToFileSaver = Guard.NotNull(mappingToFileSaver); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false) | ||
{ | ||
return new RespondWithAProvider(RegisterMapping, Guard.NotNull(requestMatcher), _settings, saveToFile); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public MappingModel[] GetMappings() | ||
{ | ||
return _options.Mappings.Values.ToArray() | ||
.Where(m => !m.IsAdminInterface) | ||
.Select(_mappingConverter.ToMappingModel) | ||
.ToArray(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string ToJson() | ||
{ | ||
return ToJson(GetMappings()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void SaveMappingsToFile(string path) | ||
{ | ||
_mappingToFileSaver.SaveMappingsToFile(GetNonAdminMappings(), path); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void SaveMappingsToFolder(string? folder) | ||
{ | ||
foreach (var mapping in GetNonAdminMappings().Where(m => !m.IsAdminInterface)) | ||
{ | ||
_mappingToFileSaver.SaveMappingToFile(mapping, folder); | ||
} | ||
} | ||
|
||
private IMapping[] GetNonAdminMappings() | ||
{ | ||
return _options.Mappings.Values.ToArray(); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
private static string ToJson(object value) | ||
{ | ||
return JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsDefault); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Stef.Validation; | ||
using WireMock.Settings; | ||
|
||
namespace WireMock.Owin; | ||
|
||
internal static class WireMockMiddlewareOptionsHelper | ||
{ | ||
public static IWireMockMiddlewareOptions InitFromSettings(WireMockServerSettings settings, IWireMockMiddlewareOptions? options = null) | ||
{ | ||
Guard.NotNull(settings); | ||
|
||
options ??= new WireMockMiddlewareOptions(); | ||
|
||
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; | ||
} | ||
|
||
return options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.