mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-11-14 21:04:27 +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\"}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
// 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>();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using Moq;
|
||||
using Scriban.Runtime;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Transformers.Scriban;
|
||||
using WireMock.Types;
|
||||
|
||||
@@ -17,23 +19,79 @@ namespace WireMock.Net.Tests.Transformers.Scriban
|
||||
public void Create_With_Scriban_TransformerType_Creates_ITransformerContext(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
var sut = new ScribanContextFactory(_fileSystemHandlerMock.Object, transformerType);
|
||||
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(_fileSystemHandlerMock.Object, TransformerType.Handlebars);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}␍
|
||||
@@ -0,0 +1,165 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using Moq;
|
||||
using Scriban.Runtime;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Models;
|
||||
using WireMock.Transformers;
|
||||
using WireMock.Transformers.Scriban;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Net.Tests.Transformers.Scriban;
|
||||
|
||||
public class ScribanContextTests
|
||||
{
|
||||
private readonly IFileSystemHandler _fileSystemHandler = Mock.Of<IFileSystemHandler>();
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban)]
|
||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public void ParseAndRender_WithCustomFunction_RendersFunctionResult(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
var customGlobals = new ScriptObject();
|
||||
customGlobals.Import("fake_name", new Func<string>(() => "Jane Doe"));
|
||||
var sut = new ScribanContext(_fileSystemHandler, transformerType, customGlobals);
|
||||
|
||||
// Act
|
||||
var result = sut.ParseAndRender("Hello {{ fake_name }} at {{ request.Path }}", CreateModel());
|
||||
|
||||
// Assert
|
||||
result.Should().Be("Hello Jane Doe at /foo/bar");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAndRender_Scriban_WithCustomFunctionWithArgument_RendersFunctionResult()
|
||||
{
|
||||
// Arrange
|
||||
var customGlobals = new ScriptObject();
|
||||
customGlobals.Import("greet", new Func<string, string>(name => $"Hello {name}"));
|
||||
var sut = new ScribanContext(_fileSystemHandler, TransformerType.Scriban, customGlobals);
|
||||
|
||||
// Act
|
||||
var result = sut.ParseAndRender("{{ greet request.Method }}", CreateModel());
|
||||
|
||||
// Assert
|
||||
result.Should().Be("Hello GET");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAndRender_ScribanDotLiquid_WithCustomFunctionAsFilter_RendersFunctionResult()
|
||||
{
|
||||
// Arrange
|
||||
var customGlobals = new ScriptObject();
|
||||
customGlobals.Import("greet", new Func<string, string>(name => $"Hello {name}"));
|
||||
var sut = new ScribanContext(_fileSystemHandler, TransformerType.ScribanDotLiquid, customGlobals);
|
||||
|
||||
// Act
|
||||
var result = sut.ParseAndRender("{{ request.Method | greet }}", CreateModel());
|
||||
|
||||
// Assert
|
||||
result.Should().Be("Hello GET");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban)]
|
||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public void ParseAndRender_WithCustomGlobalNamedLikeModelMember_ModelTakesPrecedence(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
var customGlobals = new ScriptObject();
|
||||
customGlobals.SetValue("request", "custom", false);
|
||||
customGlobals.SetValue("other", "custom", false);
|
||||
var sut = new ScribanContext(_fileSystemHandler, transformerType, customGlobals);
|
||||
|
||||
// Act
|
||||
var result = sut.ParseAndRender("{{ request.Path }} {{ other }}", CreateModel());
|
||||
|
||||
// Assert
|
||||
result.Should().Be("/foo/bar custom");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban)]
|
||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public void ParseAndRender_WithCustomGlobalNamedLikeBuiltin_CustomGlobalTakesPrecedence(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
var customGlobals = new ScriptObject();
|
||||
customGlobals.SetValue("date", "custom date", false);
|
||||
var sut = new ScribanContext(_fileSystemHandler, transformerType, customGlobals);
|
||||
|
||||
// Act
|
||||
var result = sut.ParseAndRender("{{ date }}", CreateModel());
|
||||
|
||||
// Assert
|
||||
result.Should().Be("custom date");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseAndRender_WithTemplateAssignment_DoesNotMutateCustomGlobals()
|
||||
{
|
||||
// Arrange
|
||||
var customGlobals = new ScriptObject();
|
||||
customGlobals.SetValue("x", "custom", false);
|
||||
var sut = new ScribanContext(_fileSystemHandler, TransformerType.Scriban, customGlobals);
|
||||
|
||||
// Act
|
||||
var first = sut.ParseAndRender("{{ x = 'assigned'; x }} {{ y = 1; y }}", CreateModel());
|
||||
var second = sut.ParseAndRender("{{ x }}", CreateModel());
|
||||
|
||||
// Assert
|
||||
first.Should().Be("assigned 1");
|
||||
second.Should().Be("custom");
|
||||
customGlobals.Keys.Should().BeEquivalentTo("x");
|
||||
customGlobals["x"].Should().Be("custom");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban, "{{ request.Url }} {{ request.Path }} {{ request.Method }}")]
|
||||
[InlineData(TransformerType.Scriban, "{{ request.PathSegments[1] }} {{ request.Headers.Accept }}")]
|
||||
[InlineData(TransformerType.Scriban, "{{ for s in request.PathSegments }}[{{ s }}]{{ end }}")]
|
||||
[InlineData(TransformerType.Scriban, "{{ request.Body | string.upcase }} {{ data.Id }}")]
|
||||
[InlineData(TransformerType.Scriban, "{{ x = 5; x * 2 }}")]
|
||||
[InlineData(TransformerType.ScribanDotLiquid, "{{ request.Url }} {{ request.Path }} {{ request.Method }}")]
|
||||
[InlineData(TransformerType.ScribanDotLiquid, "{% for s in request.PathSegments %}[{{ s }}]{% endfor %}")]
|
||||
[InlineData(TransformerType.ScribanDotLiquid, "{{ request.Body | upcase }} {{ data.Id }}")]
|
||||
[InlineData(TransformerType.ScribanDotLiquid, "{% assign x = 5 %}{{ x | times: 2 }}")]
|
||||
public void ParseAndRender_WithEmptyCustomGlobals_RendersSameAsWithoutCustomGlobals(TransformerType transformerType, string template)
|
||||
{
|
||||
// Arrange
|
||||
var model = CreateModel();
|
||||
var withoutCustomGlobals = new ScribanContext(_fileSystemHandler, transformerType);
|
||||
var withCustomGlobals = new ScribanContext(_fileSystemHandler, transformerType, new ScriptObject());
|
||||
|
||||
// Act
|
||||
var expected = withoutCustomGlobals.ParseAndRender(template, model);
|
||||
var result = withCustomGlobals.ParseAndRender(template, model);
|
||||
|
||||
// Assert
|
||||
expected.Should().NotBeNullOrWhiteSpace();
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
|
||||
private static TransformModel CreateModel()
|
||||
{
|
||||
var body = new BodyData
|
||||
{
|
||||
BodyAsString = "hello",
|
||||
DetectedBodyType = BodyType.String
|
||||
};
|
||||
var headers = new Dictionary<string, string[]>
|
||||
{
|
||||
{ "Accept", ["application/json"] }
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo/bar"), "GET", "::1", body, headers);
|
||||
|
||||
return new TransformModel
|
||||
{
|
||||
mapping = Mock.Of<IMapping>(),
|
||||
request = request,
|
||||
data = new { Id = 42 }
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -59,12 +59,13 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_AdminFalse_With_SaveMapping_Is_True_And_SaveMappingToFile_Is_False_Should_AddInternalMappingOnly()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
Url = target.Url!,
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = false,
|
||||
ExcludedHeaders = ["Connection"] // Needed for .NET 4.5.x and 4.6.x
|
||||
@@ -93,12 +94,13 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_AdminTrue_With_SaveMapping_Is_True_And_SaveMappingToFile_Is_False_Should_AddInternalMappingOnly()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
Url = target.Url!,
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = false,
|
||||
ExcludedHeaders = ["Connection"] // Needed for .NET 4.5.x and 4.6.x
|
||||
@@ -185,6 +187,7 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_With_SaveMapping_Is_False_And_SaveMappingToFile_Is_True_ShouldSaveMappingToFile()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||
fileSystemHandlerMock.Setup(f => f.GetMappingFolder()).Returns("m");
|
||||
|
||||
@@ -192,7 +195,7 @@ public class WireMockServerProxyTests
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
Url = target.Url!,
|
||||
SaveMapping = false,
|
||||
SaveMappingToFile = true
|
||||
},
|
||||
@@ -221,6 +224,7 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_With_SaveMappingForStatusCodePattern_Is_False_Should_Not_SaveMapping()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||
fileSystemHandlerMock.Setup(f => f.GetMappingFolder()).Returns("m");
|
||||
|
||||
@@ -228,7 +232,7 @@ public class WireMockServerProxyTests
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
Url = target.Url!,
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = true,
|
||||
SaveMappingForStatusCodePattern = "999" // Just make sure that we don't want this mapping
|
||||
@@ -258,6 +262,7 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_With_DoNotSaveMappingForHttpMethod_Should_Not_SaveMapping()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||
fileSystemHandlerMock.Setup(f => f.GetMappingFolder()).Returns("m");
|
||||
|
||||
@@ -265,7 +270,7 @@ public class WireMockServerProxyTests
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
Url = target.Url!,
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = true,
|
||||
SaveMappingSettings = new ProxySaveMappingSettings
|
||||
@@ -298,11 +303,12 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_Should_log_proxied_requests()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.google.com",
|
||||
Url = target.Url!,
|
||||
SaveMapping = true,
|
||||
SaveMappingToFile = false
|
||||
}
|
||||
@@ -328,12 +334,13 @@ public class WireMockServerProxyTests
|
||||
public async Task WireMockServer_Proxy_Should_proxy_responses()
|
||||
{
|
||||
// Assign
|
||||
using var target = StartProxyTargetServer();
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
string path = $"/prx_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create().WithPath(path))
|
||||
.RespondWith(Response.Create().WithProxy("http://www.google.com"));
|
||||
.RespondWith(Response.Create().WithProxy(target.Url!));
|
||||
|
||||
// Act
|
||||
var requestMessage = new HttpRequestMessage
|
||||
@@ -349,7 +356,7 @@ public class WireMockServerProxyTests
|
||||
// Assert
|
||||
server.Mappings.Should().HaveCount(1);
|
||||
server.LogEntries.Should().HaveCount(1);
|
||||
content.Should().Contain("google");
|
||||
content.Should().Be(ProxyTargetBody);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
@@ -947,6 +954,20 @@ public class WireMockServerProxyTests
|
||||
server.LogEntries.Should().HaveCount(1);
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
private const string ProxyTargetBody = "response from proxy target";
|
||||
|
||||
private static WireMockServer StartProxyTargetServer()
|
||||
{
|
||||
var target = WireMockServer.Start();
|
||||
target
|
||||
.Given(Request.Create().WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(HttpStatusCode.OK)
|
||||
.WithBody(ProxyTargetBody));
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Net.Http;
|
||||
using Moq;
|
||||
|
||||
using Scriban.Runtime;
|
||||
using WireMock.Authentication;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Owin;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
@@ -228,5 +233,33 @@ public class WireMockServerSettingsTests
|
||||
options.ActivityTracingOptions.Should().BeNull();
|
||||
}
|
||||
#endif
|
||||
|
||||
[Theory]
|
||||
[InlineData(TransformerType.Scriban)]
|
||||
[InlineData(TransformerType.ScribanDotLiquid)]
|
||||
public async Task WireMockServer_WireMockServerSettings_ScribanRegistrationCallback_IsUsedInResponseTemplate(TransformerType transformerType)
|
||||
{
|
||||
// Arrange
|
||||
var cancellationToken = TestContext.Current.CancellationToken;
|
||||
var path = $"/scriban_{Guid.NewGuid()}";
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
ScribanRegistrationCallback = (scriptObject, _) =>
|
||||
((ScriptObject)scriptObject).Import("fake_name", new Func<string>(() => "Jane Doe"))
|
||||
});
|
||||
|
||||
server
|
||||
.Given(Request.Create().WithPath(path).UsingGet())
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody("{{ fake_name }} {{ request.Path }}")
|
||||
.WithTransformer(transformerType)
|
||||
);
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().GetStringAsync(server.Url + path, cancellationToken);
|
||||
|
||||
// Assert
|
||||
response.Should().Be($"Jane Doe {path}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -640,6 +640,8 @@ public partial class WireMockServerTests(ITestOutputHelper testOutputHelper)
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_respond_to_ipv6_loopback()
|
||||
{
|
||||
Assert.SkipUnless(Socket.OSSupportsIPv6, "IPv6 is not supported on this machine.");
|
||||
|
||||
// Assign
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
|
||||
Reference in new issue
Block a user