mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 16:48:59 +01:00
Implement prefix for saved mapping file (#1040)
* Implement PrefixForSavedMappingFile * Add missing new line * Add missing new line * Fix warning * Fix typo * Change from readonly to const * Assign default value * Use nameof() * Change from readonly to const * Update tests * Update failing test * Rename settingsMock to settings * Create public const * Use const from ProxyAndRecordSettings --------- Co-authored-by: Mindaugas Laganeckas <mindaugas.laganeckas@nexigroup.com>
This commit is contained in:
committed by
GitHub
parent
68ad015dcf
commit
7b8e7bb684
@@ -69,6 +69,11 @@ public class ProxyAndRecordSettingsModel
|
||||
/// </summary>
|
||||
public bool AppendGuidToSavedMappingFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Set prefix for saved mapping file.
|
||||
/// </summary>
|
||||
public string PrefixForSavedMappingFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines the Replace Settings.
|
||||
/// </summary>
|
||||
|
||||
48
src/WireMock.Net/Serialization/MappingFileNameSanitizer.cs
Normal file
48
src/WireMock.Net/Serialization/MappingFileNameSanitizer.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// Creates sanitized file names for mappings
|
||||
/// </summary>
|
||||
public class MappingFileNameSanitizer
|
||||
{
|
||||
private const char ReplaceChar = '_';
|
||||
|
||||
private readonly WireMockServerSettings _settings;
|
||||
|
||||
public MappingFileNameSanitizer(WireMockServerSettings settings)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates sanitized file names for mappings
|
||||
/// </summary>
|
||||
public string BuildSanitizedFileName(IMapping mapping)
|
||||
{
|
||||
string name;
|
||||
if (!string.IsNullOrEmpty(mapping.Title))
|
||||
{
|
||||
// remove 'Proxy Mapping for ' and an extra space character after the HTTP request method
|
||||
name = mapping.Title.Replace(ProxyAndRecordSettings.DefaultPrefixForSavedMappingFile, "").Replace(' '.ToString(), string.Empty);
|
||||
if (_settings.ProxyAndRecordSettings?.AppendGuidToSavedMappingFile == true)
|
||||
{
|
||||
name += $"{ReplaceChar}{mapping.Guid}";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
name = mapping.Guid.ToString();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(_settings.ProxyAndRecordSettings?.PrefixForSavedMappingFile))
|
||||
{
|
||||
name = $"{_settings.ProxyAndRecordSettings.PrefixForSavedMappingFile}{ReplaceChar}{name}";
|
||||
}
|
||||
return $"{Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, ReplaceChar))}.json";
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,13 @@ internal class MappingToFileSaver
|
||||
{
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
private readonly MappingFileNameSanitizer _fileNameSanitizer;
|
||||
|
||||
public MappingToFileSaver(WireMockServerSettings settings, MappingConverter mappingConverter)
|
||||
{
|
||||
_settings = Guard.NotNull(settings);
|
||||
_mappingConverter = Guard.NotNull(mappingConverter);
|
||||
_fileNameSanitizer = new MappingFileNameSanitizer(settings);
|
||||
}
|
||||
|
||||
public void SaveMappingsToFile(IMapping[] mappings, string? folder = null)
|
||||
@@ -42,7 +44,7 @@ internal class MappingToFileSaver
|
||||
|
||||
var model = _mappingConverter.ToMappingModel(mapping);
|
||||
|
||||
var filename = BuildSanitizedFileName(mapping);
|
||||
var filename = _fileNameSanitizer.BuildSanitizedFileName(mapping);
|
||||
var path = Path.Combine(folder, filename);
|
||||
|
||||
Save(model, path);
|
||||
@@ -54,23 +56,4 @@ internal class MappingToFileSaver
|
||||
|
||||
_settings.FileSystemHandler.WriteMappingFile(path, JsonConvert.SerializeObject(value, JsonSerializationConstants.JsonSerializerSettingsDefault));
|
||||
}
|
||||
|
||||
private string BuildSanitizedFileName(IMapping mapping, char replaceChar = '_')
|
||||
{
|
||||
string name;
|
||||
if (!string.IsNullOrEmpty(mapping.Title))
|
||||
{
|
||||
name = mapping.Title!;
|
||||
if (_settings.ProxyAndRecordSettings?.AppendGuidToSavedMappingFile == true)
|
||||
{
|
||||
name += $"{replaceChar}{mapping.Guid}";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
name = mapping.Guid.ToString();
|
||||
}
|
||||
|
||||
return $"{Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, replaceChar))}.json";
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ namespace WireMock.Settings;
|
||||
/// </summary>
|
||||
public class ProxyAndRecordSettings : HttpClientSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Default prefix value for saved mapping file
|
||||
/// </summary>
|
||||
public const string DefaultPrefixForSavedMappingFile = "Proxy Mapping for ";
|
||||
|
||||
/// <summary>
|
||||
/// The URL to proxy.
|
||||
/// </summary>
|
||||
@@ -95,9 +100,14 @@ public class ProxyAndRecordSettings : HttpClientSettings
|
||||
/// </summary>
|
||||
public bool AppendGuidToSavedMappingFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Set prefix for saved mapping file.
|
||||
/// </summary>
|
||||
public string PrefixForSavedMappingFile { get; set; } = DefaultPrefixForSavedMappingFile;
|
||||
|
||||
/// <summary>
|
||||
/// Proxy all Api calls, irrespective of any condition
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool ProxyAll { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,7 @@ public static class WireMockServerSettingsParser
|
||||
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||
UseDefinedRequestMatchers = parser.GetBoolValue(nameof(ProxyAndRecordSettings.UseDefinedRequestMatchers)),
|
||||
AppendGuidToSavedMappingFile = parser.GetBoolValue(nameof(ProxyAndRecordSettings.AppendGuidToSavedMappingFile)),
|
||||
PrefixForSavedMappingFile = parser.GetStringValue(nameof(ProxyAndRecordSettings.PrefixForSavedMappingFile), null),
|
||||
Url = proxyUrl!,
|
||||
SaveMappingSettings = new ProxySaveMappingSettings
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user