Files
WireMock.Net-wiremock/test/WireMock.Net.Tests/Settings/WireMockServerSettingsExtensionsTests.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

83 lines
2.4 KiB
C#

// Copyright © WireMock.Net
using Moq;
using Scriban.Runtime;
using WireMock.Handlers;
using WireMock.Settings;
namespace WireMock.Net.Tests.Settings;
public class WireMockServerSettingsExtensionsTests
{
private readonly IFileSystemHandler _fileSystemHandler = Mock.Of<IFileSystemHandler>();
[Fact]
public void WithScribanRegistration_SetsCallbackWhichPassesTypedScriptObjectAndFileSystemHandler()
{
// Arrange
ScriptObject? receivedScriptObject = null;
IFileSystemHandler? receivedFileSystemHandler = null;
var settings = new WireMockServerSettings();
var scriptObject = new ScriptObject();
// Act
var result = settings.WithScribanRegistration((so, fs) =>
{
receivedScriptObject = so;
receivedFileSystemHandler = fs;
});
settings.ScribanRegistrationCallback!(scriptObject, _fileSystemHandler);
// Assert
result.Should().BeSameAs(settings);
receivedScriptObject.Should().BeSameAs(scriptObject);
receivedFileSystemHandler.Should().BeSameAs(_fileSystemHandler);
}
[Fact]
public void WithScribanRegistration_WithExistingCallback_InvokesBothInRegistrationOrder()
{
// Arrange
var calls = new List<string>();
var settings = new WireMockServerSettings
{
ScribanRegistrationCallback = (_, _) => calls.Add("existing")
};
// Act
settings
.WithScribanRegistration((_, _) => calls.Add("first"))
.WithScribanRegistration((_, _) => calls.Add("second"));
settings.ScribanRegistrationCallback!(new ScriptObject(), _fileSystemHandler);
// Assert
calls.Should().Equal("existing", "first", "second");
}
[Fact]
public void WithScribanRegistration_WithNullSettings_ThrowsArgumentNullException()
{
// Arrange
WireMockServerSettings settings = null!;
// Act
Action act = () => settings.WithScribanRegistration((_, _) => { });
// Assert
act.Should().Throw<ArgumentNullException>();
}
[Fact]
public void WithScribanRegistration_WithNullCallback_ThrowsArgumentNullException()
{
// Arrange
var settings = new WireMockServerSettings();
// Act
Action act = () => settings.WithScribanRegistration(null!);
// Assert
act.Should().Throw<ArgumentNullException>();
}
}