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:
Mindaugas Laganeckas
2023-12-14 08:28:21 +01:00
committed by GitHub
parent 68ad015dcf
commit 7b8e7bb684
7 changed files with 229 additions and 22 deletions

View 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";
}
}

View File

@@ -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";
}
}