Fix: Pass AllowedHandlebarsHelpers configuration to Handlebars.Net.Helpers (#1416)

Pass AllowedHandlebarsHelpers configuration to Handlebars.Net.Helpers so that optional handlebars helpers can be enabled.

Co-authored-by: Sam Latham <sam.latham@citrix.com>
This commit is contained in:
samlatham
2026-01-05 20:24:48 +00:00
committed by GitHub
parent 9cee6dde00
commit 0e60e3f3f9
2 changed files with 93 additions and 0 deletions

View File

@@ -39,6 +39,8 @@ internal static class WireMockHandlebarsHelpers
#endif #endif
o.CustomHelperPaths = paths; o.CustomHelperPaths = paths;
o.Categories = settings.HandlebarsSettings?.AllowedHandlebarsHelpers ?? HandlebarsSettings.DefaultAllowedHandlebarsHelpers;
o.CustomHelpers = new Dictionary<string, IHelpers>(); o.CustomHelpers = new Dictionary<string, IHelpers>();
if (settings.HandlebarsSettings?.AllowedCustomHandlebarsHelpers.HasFlag(CustomHandlebarsHelpers.File) == true) if (settings.HandlebarsSettings?.AllowedCustomHandlebarsHelpers.HasFlag(CustomHandlebarsHelpers.File) == true)
{ {

View File

@@ -0,0 +1,91 @@
// Copyright © WireMock.Net
using System;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using HandlebarsDotNet;
using HandlebarsDotNet.Helpers.Enums;
using Moq;
using NFluent;
using WireMock.Handlers;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests.Settings;
public class HandlebarsSettingsTests
{
private const string ClientIp = "::1";
private readonly WireMockServerSettings _settings;
private readonly Mock<IMapping> _mappingMock;
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
public HandlebarsSettingsTests()
{
_mappingMock = new Mock<IMapping>();
_fileSystemHandlerMock = new Mock<IFileSystemHandler>(MockBehavior.Strict);
_settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object
};
}
[Fact]
public async Task Response_HandlebarsHelpers_Environment_NotAllowed_By_Default()
{
// Arrange
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);
var responseBuilder = Response.Create()
.WithBody("Username: {{Environment.GetEnvironmentVariable \"USERNAME\"}}")
.WithTransformer();
// Act
Func<Task> action = () => responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings);
// Assert
action.Should().ThrowAsync<HandlebarsRuntimeException>();
}
[Fact]
public async Task Response_HandlebarsHelpers_Environment_Allowed_When_Configured()
{
// Arrange
var settingsWithEnv = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
HandlebarsSettings = new HandlebarsSettings
{
AllowedHandlebarsHelpers = HandlebarsSettings.DefaultAllowedHandlebarsHelpers
.Concat(new[] { Category.Environment })
.ToArray()
}
};
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);
var responseBuilder = Response.Create()
.WithBody("User: {{Environment.GetEnvironmentVariable \"USERNAME\"}}")
.WithTransformer();
// Act
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, settingsWithEnv).ConfigureAwait(false);
// Assert
Check.That(response.Message.BodyData.BodyAsString).Not.Contains("{{Environment.GetEnvironmentVariable");
Check.That(response.Message.BodyData.BodyAsString).StartsWith("User: ");
}
[Fact]
public void DefaultAllowedHandlebarsHelpers_Should_Not_Include_Environment()
{
// Assert
Check.That(HandlebarsSettings.DefaultAllowedHandlebarsHelpers).Not.Contains(Category.Environment);
}
}