Files
WireMock.Net-wiremock/test/WireMock.Net.Tests/Transformers/Scriban/ScribanContextFactoryTests.cs
T
Mattias SundströmandMattias 0132ec873a Add ScribanRegistrationCallback to register custom Scriban functions (#1515) (#1516)
- Add WireMockServerSettings.ScribanRegistrationCallback
  (Action<object, IFileSystemHandler>?) so code-configured servers can add
  functions, objects or variables to Scriban and Scriban-DotLiquid templates.
- Pass the settings to ScribanContextFactory, which invokes the callback with
  a new ScriptObject per transformer context.
- Push the custom globals beneath the template model in ScribanContext so
  request, response, mapping and data take precedence; keep the original
  Template.Render call when no callback is configured.
- Add the strongly typed WithScribanRegistration extension method.
- Add a Scriban example to the .NET 8 console app.
- Make the proxy tests use a local target server instead of www.google.com,
  and skip the IPv6 loopback test when the OS has no IPv6 support.

Co-authored-by: Mattias <13809209+Quattordici@users.noreply.github.com>
2026-10-18 09:29:11 +02:00

97 lines
3.4 KiB
C#

// Copyright © WireMock.Net
using Moq;
using Scriban.Runtime;
using WireMock.Handlers;
using WireMock.Settings;
using WireMock.Transformers.Scriban;
using WireMock.Types;
namespace WireMock.Net.Tests.Transformers.Scriban
{
public class ScribanContextFactoryTests
{
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock = new Mock<IFileSystemHandler>();
[Theory]
[InlineData(TransformerType.Scriban)]
[InlineData(TransformerType.ScribanDotLiquid)]
public void Create_With_Scriban_TransformerType_Creates_ITransformerContext(TransformerType transformerType)
{
// Arrange
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object
};
var sut = new ScribanContextFactory(settings, transformerType);
// Act
var result = sut.Create();
// Assert
result.Should().NotBeNull();
result.FileSystemHandler.Should().BeSameAs(_fileSystemHandlerMock.Object);
}
[Fact]
public void Create_With_Invalid_TransformerType_Throws_Exception()
{
// Arrange
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object
};
// Act
Action action = () => new ScribanContextFactory(settings, TransformerType.Handlebars);
// Assert
action.Should().Throw<Exception>();
}
[Theory]
[InlineData(TransformerType.Scriban)]
[InlineData(TransformerType.ScribanDotLiquid)]
public void Create_WithScribanRegistrationCallback_InvokesCallbackOnceWithScriptObjectAndFileSystemHandler(TransformerType transformerType)
{
// Arrange
var calls = new List<(object ScriptObject, IFileSystemHandler FileSystemHandler)>();
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
ScribanRegistrationCallback = (scriptObject, fileSystemHandler) => calls.Add((scriptObject, fileSystemHandler))
};
var sut = new ScribanContextFactory(settings, transformerType);
// Act
var result = sut.Create();
// Assert
result.Should().NotBeNull();
calls.Should().ContainSingle();
calls[0].ScriptObject.Should().BeOfType<ScriptObject>();
calls[0].FileSystemHandler.Should().BeSameAs(_fileSystemHandlerMock.Object);
}
[Fact]
public void Create_CalledTwice_InvokesScribanRegistrationCallbackWithNewScriptObjectEachTime()
{
// Arrange
var scriptObjects = new List<object>();
var settings = new WireMockServerSettings
{
FileSystemHandler = _fileSystemHandlerMock.Object,
ScribanRegistrationCallback = (scriptObject, _) => scriptObjects.Add(scriptObject)
};
var sut = new ScribanContextFactory(settings, TransformerType.Scriban);
// Act
sut.Create();
sut.Create();
// Assert
scriptObjects.Should().HaveCount(2);
scriptObjects[0].Should().NotBeSameAs(scriptObjects[1]);
}
}
}