mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-31 06:03:03 +02:00
Handlebars Extension (#286)
* wip * HandlebarsRegistrationCallback * HandlebarsContextFactoryTests * 1.0.21.0 * fix sonar * LocalFileSystemHandler * readme * Fix System.IO.IOException
This commit is contained in:
@@ -164,10 +164,10 @@ namespace WireMock.Server
|
||||
{
|
||||
if (folder == null)
|
||||
{
|
||||
folder = _fileSystemHandler.GetMappingFolder();
|
||||
folder = _settings.FileSystemHandler.GetMappingFolder();
|
||||
}
|
||||
|
||||
if (!_fileSystemHandler.FolderExists(folder))
|
||||
if (!_settings.FileSystemHandler.FolderExists(folder))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -177,17 +177,23 @@ namespace WireMock.Server
|
||||
var watcher = new EnhancedFileSystemWatcher(folder, "*.json", EnhancedFileSystemWatcherTimeoutMs);
|
||||
watcher.Created += (sender, args) =>
|
||||
{
|
||||
_logger.Info("New MappingFile created : '{0}'", args.FullPath);
|
||||
ReadStaticMappingAndAddOrUpdate(args.FullPath);
|
||||
_logger.Info("MappingFile created : '{0}', reading file.", args.FullPath);
|
||||
if (!ReadStaticMappingAndAddOrUpdate(args.FullPath))
|
||||
{
|
||||
_logger.Error("Unable to read MappingFile '{0}'.", args.FullPath);
|
||||
}
|
||||
};
|
||||
watcher.Changed += (sender, args) =>
|
||||
{
|
||||
_logger.Info("New MappingFile updated : '{0}'", args.FullPath);
|
||||
ReadStaticMappingAndAddOrUpdate(args.FullPath);
|
||||
_logger.Info("MappingFile updated : '{0}', reading file.", args.FullPath);
|
||||
if (!ReadStaticMappingAndAddOrUpdate(args.FullPath))
|
||||
{
|
||||
_logger.Error("Unable to read MappingFile '{0}'.", args.FullPath);
|
||||
}
|
||||
};
|
||||
watcher.Deleted += (sender, args) =>
|
||||
{
|
||||
_logger.Info("New MappingFile deleted : '{0}'", args.FullPath);
|
||||
_logger.Info("MappingFile deleted : '{0}'", args.FullPath);
|
||||
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath);
|
||||
|
||||
if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
|
||||
@@ -208,24 +214,31 @@ namespace WireMock.Server
|
||||
/// </summary>
|
||||
/// <param name="path">The path.</param>
|
||||
[PublicAPI]
|
||||
public void ReadStaticMappingAndAddOrUpdate([NotNull] string path)
|
||||
public bool ReadStaticMappingAndAddOrUpdate([NotNull] string path)
|
||||
{
|
||||
Check.NotNull(path, nameof(path));
|
||||
|
||||
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
var mappingModels = DeserializeObjectToArray<MappingModel>(JsonConvert.DeserializeObject(_fileSystemHandler.ReadMappingFile(path)));
|
||||
foreach (var mappingModel in mappingModels)
|
||||
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_fileSystemHandler, path, out string value))
|
||||
{
|
||||
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
|
||||
var mappingModels = DeserializeObjectToArray<MappingModel>(JsonConvert.DeserializeObject(value));
|
||||
foreach (var mappingModel in mappingModels)
|
||||
{
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel, null, path);
|
||||
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
|
||||
{
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel, guidFromFilename, path);
|
||||
}
|
||||
else
|
||||
{
|
||||
DeserializeAndAddOrUpdateMapping(mappingModel, null, path);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -302,7 +315,7 @@ namespace WireMock.Server
|
||||
|
||||
var response = Response.Create(responseMessage);
|
||||
|
||||
return new Mapping(Guid.NewGuid(), string.Empty, null, _fileSystemHandler, request, response, 0, null, null, null);
|
||||
return new Mapping(Guid.NewGuid(), string.Empty, null, _settings, request, response, 0, null, null, null);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace WireMock.Server
|
||||
|
||||
private readonly IWireMockLogger _logger;
|
||||
private readonly IFileSystemHandler _fileSystemHandler;
|
||||
private readonly IFluentMockServerSettings _settings;
|
||||
private readonly IOwinSelfHost _httpServer;
|
||||
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
|
||||
@@ -92,7 +93,7 @@ namespace WireMock.Server
|
||||
/// <param name="settings">The FluentMockServerSettings.</param>
|
||||
/// <returns>The <see cref="FluentMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static FluentMockServer Start(IFluentMockServerSettings settings)
|
||||
public static FluentMockServer Start([NotNull] IFluentMockServerSettings settings)
|
||||
{
|
||||
Check.NotNull(settings, nameof(settings));
|
||||
|
||||
@@ -185,6 +186,7 @@ namespace WireMock.Server
|
||||
|
||||
private FluentMockServer(IFluentMockServerSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||
|
||||
_logger = settings.Logger;
|
||||
@@ -435,7 +437,7 @@ namespace WireMock.Server
|
||||
[PublicAPI]
|
||||
public IRespondWithAProvider Given(IRequestMatcher requestMatcher, bool saveToFile = false)
|
||||
{
|
||||
return new RespondWithAProvider(RegisterMapping, requestMatcher, _fileSystemHandler, saveToFile);
|
||||
return new RespondWithAProvider(RegisterMapping, requestMatcher, _settings, saveToFile);
|
||||
}
|
||||
|
||||
private void RegisterMapping(IMapping mapping, bool saveToFile)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.ResponseProviders;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Server
|
||||
{
|
||||
@@ -18,7 +18,7 @@ namespace WireMock.Server
|
||||
private string _scenario;
|
||||
private readonly RegistrationCallback _registrationCallback;
|
||||
private readonly IRequestMatcher _requestMatcher;
|
||||
private readonly IFileSystemHandler _fileSystemHandler;
|
||||
private readonly IFluentMockServerSettings _settings;
|
||||
private readonly bool _saveToFile;
|
||||
|
||||
public Guid Guid { get; private set; } = Guid.NewGuid();
|
||||
@@ -28,13 +28,13 @@ namespace WireMock.Server
|
||||
/// </summary>
|
||||
/// <param name="registrationCallback">The registration callback.</param>
|
||||
/// <param name="requestMatcher">The request matcher.</param>
|
||||
/// <param name="fileSystemHandler">The fileSystemHandler.</param>
|
||||
/// <param name="settings">The FluentMockServerSettings.</param>
|
||||
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
|
||||
public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestMatcher requestMatcher, IFileSystemHandler fileSystemHandler, bool saveToFile = false)
|
||||
public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestMatcher requestMatcher, IFluentMockServerSettings settings, bool saveToFile = false)
|
||||
{
|
||||
_registrationCallback = registrationCallback;
|
||||
_requestMatcher = requestMatcher;
|
||||
_fileSystemHandler = fileSystemHandler;
|
||||
_settings = settings;
|
||||
_saveToFile = saveToFile;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace WireMock.Server
|
||||
/// <param name="provider">The provider.</param>
|
||||
public void RespondWith(IResponseProvider provider)
|
||||
{
|
||||
_registrationCallback(new Mapping(Guid, _title, _path, _fileSystemHandler, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState), _saveToFile);
|
||||
_registrationCallback(new Mapping(Guid, _title, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState), _saveToFile);
|
||||
}
|
||||
|
||||
/// <see cref="IRespondWithAProvider.WithGuid(string)"/>
|
||||
|
||||
Reference in New Issue
Block a user