mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-11-23 16:54:20 +01:00
- 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>
This commit is contained in:
1 parent
64734c4f92
commit
0132ec873a
13 files changed
+527
-22
No files matched your search
@@ -8,6 +8,8 @@ using WireMock.Util;
|
||||
using WireMock.Handlers;
|
||||
using Moq;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Scriban.Runtime;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilders;
|
||||
|
||||
@@ -65,5 +67,50 @@ public class ResponseWithScribanTests
|
||||
// Assert
|
||||
response.Message.BodyData.BodyAsString.Should().Be("test http://localhost/foo /foo POSt");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban)]
|
||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public async Task Response_ProvideResponse_WithScribanRegistrationCallback_UsesCustomFunctionInBodyAndHeaders(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
_settings.ScribanRegistrationCallback = (scriptObject, _) =>
|
||||
((ScriptObject)scriptObject).Import("fake_name", new Func<string>(() => "Jane Doe"));
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBody("{{ fake_name }} {{ request.Path }}")
|
||||
.WithHeader("x-name", "{{ fake_name }}")
|
||||
.WithTransformer(transformerType);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
response.Message.BodyData!.BodyAsString.Should().Be("Jane Doe /foo");
|
||||
response.Message.Headers!["x-name"].Should().ContainSingle().Which.Should().Be("Jane Doe");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban)]
|
||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public async Task Response_ProvideResponse_WithScribanRegistrationCallback_UsesCustomFunctionInBodyAsJson(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
_settings.WithScribanRegistration((scriptObject, _) => scriptObject.Import("fake_name", new Func<string>(() => "Jane Doe")));
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp);
|
||||
|
||||
var responseBuilder = Response.Create()
|
||||
.WithBodyAsJson(new { name = "{{ fake_name }}", path = "{{ request.Path }}" })
|
||||
.WithTransformer(transformerType);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, Mock.Of<HttpContext>(), request, _settings);
|
||||
|
||||
// Assert
|
||||
JsonConvert.SerializeObject(response.Message.BodyData!.BodyAsJson).Should().Be("{\"name\":\"Jane Doe\",\"path\":\"/foo\"}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user