mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-28 11:51:48 +01:00
Add IFileSystemHandler to support Azure for StaticMapping location (#180)
* wip * CustomStaticMappingFileHandler * Add unit-tests * Tests * IFileSystemHandler * version
This commit is contained in:
@@ -30,12 +30,13 @@ namespace WireMock.Server
|
||||
/// </summary>
|
||||
public partial class FluentMockServer
|
||||
{
|
||||
private static readonly string AdminMappingsFolder = Path.Combine("__admin", "mappings");
|
||||
private const string ContentTypeJson = "application/json";
|
||||
|
||||
private const string AdminMappings = "/__admin/mappings";
|
||||
private const string AdminRequests = "/__admin/requests";
|
||||
private const string AdminSettings = "/__admin/settings";
|
||||
private const string AdminScenarios = "/__admin/scenarios";
|
||||
|
||||
private readonly RegexMatcher _adminMappingsGuidPathMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, @"^\/__admin\/mappings\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
|
||||
private readonly RegexMatcher _adminRequestsGuidPathMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, @"^\/__admin\/requests\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
|
||||
|
||||
@@ -100,25 +101,39 @@ namespace WireMock.Server
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region StaticMappings
|
||||
#region StaticMappings
|
||||
/// <summary>
|
||||
/// Saves the static mappings.
|
||||
/// </summary>
|
||||
/// <param name="folder">The optional folder. If not defined, use {CurrentFolder}/__admin/mappings</param>
|
||||
[PublicAPI]
|
||||
public void SaveStaticMappings([CanBeNull] string folder = null)
|
||||
{
|
||||
foreach (var mapping in Mappings.Where(m => !m.IsAdminInterface))
|
||||
{
|
||||
SaveMappingToFile(mapping, folder);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the static mappings from a folder.
|
||||
/// </summary>
|
||||
/// <param name="folder">The optional folder. If not defined, use \__admin\mappings\</param>
|
||||
/// <param name="folder">The optional folder. If not defined, use {CurrentFolder}/__admin/mappings</param>
|
||||
[PublicAPI]
|
||||
public void ReadStaticMappings([CanBeNull] string folder = null)
|
||||
{
|
||||
if (folder == null)
|
||||
{
|
||||
folder = Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder);
|
||||
folder = _fileSystemHandler.GetMappingFolder();
|
||||
}
|
||||
|
||||
if (!Directory.Exists(folder))
|
||||
if (!_fileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
_logger.Info("The Static Mapping folder '{0}' does not exist, reading Static MappingFiles will be skipped.", folder);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string filename in Directory.EnumerateFiles(folder).OrderBy(f => f))
|
||||
foreach (string filename in _fileSystemHandler.EnumerateFiles(folder).OrderBy(f => f))
|
||||
{
|
||||
_logger.Info("Reading Static MappingFile : '{0}'", filename);
|
||||
|
||||
@@ -136,16 +151,16 @@ namespace WireMock.Server
|
||||
/// <summary>
|
||||
/// Watches the static mappings for changes.
|
||||
/// </summary>
|
||||
/// <param name="folder">The optional folder. If not defined, use \__admin\mappings\</param>
|
||||
/// <param name="folder">The optional folder. If not defined, use {CurrentFolder}/__admin/mappings</param>
|
||||
[PublicAPI]
|
||||
public void WatchStaticMappings([CanBeNull] string folder = null)
|
||||
{
|
||||
if (folder == null)
|
||||
{
|
||||
folder = Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder);
|
||||
folder = _fileSystemHandler.GetMappingFolder();
|
||||
}
|
||||
|
||||
if (!Directory.Exists(folder))
|
||||
if (!_fileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -192,7 +207,7 @@ namespace WireMock.Server
|
||||
|
||||
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
MappingModel mappingModel = JsonConvert.DeserializeObject<MappingModel>(FileHelper.ReadAllText(path));
|
||||
MappingModel mappingModel = JsonConvert.DeserializeObject<MappingModel>(_fileSystemHandler.ReadMappingFile(path));
|
||||
if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
|
||||
{
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path);
|
||||
@@ -345,29 +360,31 @@ namespace WireMock.Server
|
||||
#region Mappings
|
||||
private ResponseMessage MappingsSave(RequestMessage requestMessage)
|
||||
{
|
||||
foreach (var mapping in Mappings.Where(m => !m.IsAdminInterface))
|
||||
{
|
||||
SaveMappingToFile(mapping);
|
||||
}
|
||||
SaveStaticMappings();
|
||||
|
||||
return ResponseMessageBuilder.Create("Mappings saved to disk");
|
||||
}
|
||||
|
||||
private void SaveMappingToFile(Mapping mapping)
|
||||
private void SaveMappingToFile(Mapping mapping, string folder = null)
|
||||
{
|
||||
string folder = Path.Combine(Directory.GetCurrentDirectory(), AdminMappingsFolder);
|
||||
if (!Directory.Exists(folder))
|
||||
if (folder == null)
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
folder = _fileSystemHandler.GetMappingFolder();
|
||||
}
|
||||
|
||||
if (!_fileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
_fileSystemHandler.CreateFolder(folder);
|
||||
}
|
||||
|
||||
var model = MappingConverter.ToMappingModel(mapping);
|
||||
string filename = !string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString();
|
||||
string filename = (!string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString()) + ".json";
|
||||
|
||||
string filePath = Path.Combine(folder, filename + ".json");
|
||||
_logger.Info("Saving Mapping to file {0}", filePath);
|
||||
string path = Path.Combine(folder, filename);
|
||||
|
||||
File.WriteAllText(filePath, JsonConvert.SerializeObject(model, _settings));
|
||||
_logger.Info("Saving Mapping file {0}", filename);
|
||||
|
||||
_fileSystemHandler.WriteMappingFile(path, JsonConvert.SerializeObject(model, _settings));
|
||||
}
|
||||
|
||||
private static string SanitizeFileName(string name, char replaceChar = '_')
|
||||
|
||||
Reference in New Issue
Block a user